Skip to content

Commit f71a3d1

Browse files
Brendonovichthdxr
authored andcommitted
feat(desktop): implement clipboard write permission handling (#25998)
1 parent 83c72ad commit f71a3d1

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

packages/desktop/src/main/windows.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const root = dirname(fileURLToPath(import.meta.url))
88
const rendererRoot = join(root, "../renderer")
99
const rendererProtocol = "oc"
1010
const rendererHost = "renderer"
11+
const clipboardWritePermission = "clipboard-sanitized-write"
1112

1213
protocol.registerSchemesAsPrivileged([
1314
{
@@ -107,6 +108,8 @@ export function createMainWindow() {
107108
},
108109
})
109110

111+
allowClipboardWrite(win)
112+
110113
win.webContents.session.webRequest.onBeforeSendHeaders((details, callback) => {
111114
const { requestHeaders } = details
112115
upsertKeyValue(requestHeaders, "Access-Control-Allow-Origin", ["*"])
@@ -157,6 +160,8 @@ export function createLoadingWindow() {
157160
},
158161
})
159162

163+
allowClipboardWrite(win)
164+
160165
loadWindow(win, "loading.html")
161166

162167
return win
@@ -191,6 +196,31 @@ function loadWindow(win: BrowserWindow, html: string) {
191196

192197
void win.loadURL(`${rendererProtocol}://${rendererHost}/${html}`)
193198
}
199+
200+
function allowClipboardWrite(win: BrowserWindow) {
201+
win.webContents.session.setPermissionRequestHandler((webContents, permission, callback, details) => {
202+
callback(
203+
permission === clipboardWritePermission &&
204+
isTrustedRendererUrl(details.requestingUrl) &&
205+
webContents.id === win.webContents.id,
206+
)
207+
})
208+
win.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => {
209+
if (permission !== clipboardWritePermission) return false
210+
if (webContents && webContents.id !== win.webContents.id) return false
211+
return isTrustedRendererUrl(details.requestingUrl) || isTrustedRendererUrl(requestingOrigin)
212+
})
213+
}
214+
215+
function isTrustedRendererUrl(value?: string) {
216+
if (!value || !URL.canParse(value)) return false
217+
const url = new URL(value)
218+
if (url.protocol === `${rendererProtocol}:` && url.host === rendererHost) return true
219+
const devUrl = process.env.ELECTRON_RENDERER_URL
220+
if (!devUrl || !URL.canParse(devUrl)) return false
221+
return url.origin === new URL(devUrl).origin
222+
}
223+
194224
function wireZoom(win: BrowserWindow) {
195225
win.webContents.setZoomFactor(1)
196226
win.webContents.on("zoom-changed", () => {

0 commit comments

Comments
 (0)