Skip to content

Only approve web API permission requests for permissions that FreeTube needs #5022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,32 @@ function runApp() {
})
}

// Electron defaults to approving all permission checks and permission requests.
// FreeTube only needs a few permissions, so we reject requests for other permissions
// and reject all requests on non-FreeTube URLs.
//
// FreeTube needs the following permissions:
// - "fullscreen": So that the video player can enter full screen
// - "clipboard-sanitized-write": To allow the user to copy video URLs and error messages

session.defaultSession.setPermissionCheckHandler((webContents, permission, requestingOrigin) => {
if (!isFreeTubeUrl(requestingOrigin)) {
return false
}

return permission === 'fullscreen' || permission === 'clipboard-sanitized-write'
})

session.defaultSession.setPermissionRequestHandler((webContents, permission, callback) => {
if (!isFreeTubeUrl(webContents.getURL())) {
// eslint-disable-next-line n/no-callback-literal
callback(false)
return
}

callback(permission === 'fullscreen' || permission === 'clipboard-sanitized-write')
})

let docArray
try {
docArray = await baseHandlers.settings._findAppReadyRelatedSettings()
Expand Down Expand Up @@ -547,6 +573,19 @@ function runApp() {
}
}

/**
* @param {string} urlString
*/
function isFreeTubeUrl(urlString) {
const { protocol, host, pathname } = new URL(urlString)

if (process.env.NODE_ENV === 'development') {
return protocol === 'http:' && host === 'localhost:9080' && (pathname === '/' || pathname === '/index.html')
} else {
return protocol === 'app:' && host === 'bundle' && pathname === '/index.html'
}
}

async function installDevTools() {
try {
/* eslint-disable */
Expand Down