fix(desktop): avoid destroyed window permission checks (#34300)

This commit is contained in:
Brendan Allan 2026-06-28 20:06:15 +08:00 committed by GitHub
commit 58ba99e505
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -354,16 +354,18 @@ function addDocumentPolicy(response: Response, file: string) {
} }
function allowRendererPermissions(win: BrowserWindow) { function allowRendererPermissions(win: BrowserWindow) {
const webContentsId = win.webContents.id
win.webContents.session.setPermissionRequestHandler((webContents, permission, callback, details) => { win.webContents.session.setPermissionRequestHandler((webContents, permission, callback, details) => {
callback( callback(
rendererPermissions.has(permission) && rendererPermissions.has(permission) &&
isTrustedRendererUrl(details.requestingUrl) && isTrustedRendererUrl(details.requestingUrl) &&
webContents.id === win.webContents.id, webContents.id === webContentsId,
) )
}) })
win.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => { win.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
if (!rendererPermissions.has(permission)) return false if (!rendererPermissions.has(permission)) return false
if (webContents && webContents.id !== win.webContents.id) return false if (webContents && webContents.id !== webContentsId) return false
return isTrustedRendererUrl(details.requestingUrl) || isTrustedRendererUrl(requestingOrigin) return isTrustedRendererUrl(details.requestingUrl) || isTrustedRendererUrl(requestingOrigin)
}) })
} }