Skip to content

Commit 84572f8

Browse files
authored
Only approve web API permission requests for permissions that FreeTube needs (#5022)
1 parent 43a7fbd commit 84572f8

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/main/index.js

+39
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,32 @@ function runApp() {
286286
})
287287
}
288288

289+
// Electron defaults to approving all permission checks and permission requests.
290+
// FreeTube only needs a few permissions, so we reject requests for other permissions
291+
// and reject all requests on non-FreeTube URLs.
292+
//
293+
// FreeTube needs the following permissions:
294+
// - "fullscreen": So that the video player can enter full screen
295+
// - "clipboard-sanitized-write": To allow the user to copy video URLs and error messages
296+
297+
session.defaultSession.setPermissionCheckHandler((webContents, permission, requestingOrigin) => {
298+
if (!isFreeTubeUrl(requestingOrigin)) {
299+
return false
300+
}
301+
302+
return permission === 'fullscreen' || permission === 'clipboard-sanitized-write'
303+
})
304+
305+
session.defaultSession.setPermissionRequestHandler((webContents, permission, callback) => {
306+
if (!isFreeTubeUrl(webContents.getURL())) {
307+
// eslint-disable-next-line n/no-callback-literal
308+
callback(false)
309+
return
310+
}
311+
312+
callback(permission === 'fullscreen' || permission === 'clipboard-sanitized-write')
313+
})
314+
289315
let docArray
290316
try {
291317
docArray = await baseHandlers.settings._findAppReadyRelatedSettings()
@@ -547,6 +573,19 @@ function runApp() {
547573
}
548574
}
549575

576+
/**
577+
* @param {string} urlString
578+
*/
579+
function isFreeTubeUrl(urlString) {
580+
const { protocol, host, pathname } = new URL(urlString)
581+
582+
if (process.env.NODE_ENV === 'development') {
583+
return protocol === 'http:' && host === 'localhost:9080' && (pathname === '/' || pathname === '/index.html')
584+
} else {
585+
return protocol === 'app:' && host === 'bundle' && pathname === '/index.html'
586+
}
587+
}
588+
550589
async function installDevTools() {
551590
try {
552591
/* eslint-disable */

0 commit comments

Comments
 (0)