From 6d5b6f7ae4005159eb2d270b65f5901340db14ff Mon Sep 17 00:00:00 2001 From: Shiva Tej <51899602+SomaRe@users.noreply.github.com> Date: Tue, 20 Oct 2026 14:41:56 -0500 Subject: [PATCH] fix(desktop): keep clipboard permissions for every app window Permission handlers live on the shared session, so registering one per window replaced the previous handler and only the newest window passed the webContents id check. Clipboard writes from older windows rejected with NotAllowedError. Keep the live webContents ids in one set and register the handlers once per session. --- packages/desktop/src/main/windows/security.ts | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/desktop/src/main/windows/security.ts b/packages/desktop/src/main/windows/security.ts index 100ff0e3e1f4..e994370e0685 100644 --- a/packages/desktop/src/main/windows/security.ts +++ b/packages/desktop/src/main/windows/security.ts @@ -5,23 +5,39 @@ import { isRendererUrl } from "./scheme" const rendererPermissions = new Set(["clipboard-sanitized-write", "notifications"]) +// Permission handlers live on the session, which every app window shares. Registering them per +// window replaced the previous handler, so only the newest window kept clipboard access. +const rendererWebContents = new Set() + +const wiredSessions = new WeakSet() + export function allowRendererPermissions(win: BrowserWindow) { const webContentsId = win.webContents.id - win.webContents.session.setPermissionRequestHandler((webContents, permission, callback, details) => { + rendererWebContents.add(webContentsId) + win.once("closed", () => rendererWebContents.delete(webContentsId)) + + const session = win.webContents.session + + if (wiredSessions.has(session)) return + wiredSessions.add(session) + + session.setPermissionRequestHandler((webContents, permission, callback, details) => { callback( - rendererPermissions.has(permission) && isRendererUrl(details.requestingUrl) && webContents.id === webContentsId, + rendererPermissions.has(permission) && + rendererWebContents.has(webContents.id) && + isRendererUrl(details.requestingUrl), ) }) - win.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => { + session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => { if (!rendererPermissions.has(permission)) return false - if (webContents && webContents.id !== webContentsId) return false + if (webContents && !rendererWebContents.has(webContents.id)) return false return isRendererUrl(details.requestingUrl) || isRendererUrl(requestingOrigin) }) } -export function wireNavigationPolicy(win: BrowserWindow, openExternalURL: (url: string) => unknown) { +export function wireNavigationPolicy(win: BrowserWindow, openExternalURL: (url: string) => void) { win.webContents.setWindowOpenHandler(({ url }) => { if (!isRendererUrl(url)) openExternalURL(url)