Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 23 additions & 2 deletions src/main/security-policy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export function isTrustedAppUrl(rawUrl: string): boolean {
function isHarnessUrl(rawUrl: string): boolean {
try {
const url = new URL(rawUrl)
if (url.protocol === 'file:') return true
return (
url.protocol === 'http:' &&
(url.hostname === '127.0.0.1' || url.hostname === 'localhost')
Expand All @@ -10,3 +9,25 @@ export function isTrustedAppUrl(rawUrl: string): boolean {
return false
}
}

export function isTrustedAppUrl(rawUrl: string): boolean {
try {
if (new URL(rawUrl).protocol === 'file:') return true
} catch {
return false
}
return isHarnessUrl(rawUrl)
}

export function canGrantWindowPermission(
permission: string,
requestingUrl: string | undefined,
isMainFrame: boolean
): boolean {
return (
permission === 'clipboard-sanitized-write' &&
isMainFrame &&
requestingUrl !== undefined &&
isHarnessUrl(requestingUrl)
)
}
20 changes: 16 additions & 4 deletions src/main/security.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { shell, type BrowserWindow } from 'electron'
import { isTrustedAppUrl } from './security-policy'
import { canGrantWindowPermission, isTrustedAppUrl } from './security-policy'

export function secureWindow(window: BrowserWindow): void {
window.webContents.setWindowOpenHandler(({ url }) => {
Expand All @@ -15,7 +15,19 @@ export function secureWindow(window: BrowserWindow): void {
})

window.webContents.on('will-attach-webview', (event) => event.preventDefault())
window.webContents.session.setPermissionRequestHandler((_webContents, _permission, callback) => {
callback(false)
})
window.webContents.session.setPermissionCheckHandler(
(_webContents, permission, requestingOrigin, details) =>
canGrantWindowPermission(
permission,
details.requestingUrl ?? requestingOrigin,
details.isMainFrame
)
)
window.webContents.session.setPermissionRequestHandler(
(_webContents, permission, callback, details) => {
callback(
canGrantWindowPermission(permission, details.requestingUrl, details.isMainFrame)
)
}
)
}
39 changes: 38 additions & 1 deletion test/runtime.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
import { buildHarnessArguments, buildNodeArguments } from '../src/main/runtime/harness-runtime'
import { isTrustedAppUrl } from '../src/main/security-policy'
import { canGrantWindowPermission, isTrustedAppUrl } from '../src/main/security-policy'
import { shouldLoadHarnessUrl } from '../src/main/window-navigation'

describe('Harness launch contract', () => {
Expand Down Expand Up @@ -36,6 +36,43 @@ describe('navigation trust boundary', () => {
expect(isTrustedAppUrl('http://example.com')).toBe(false)
expect(isTrustedAppUrl('javascript:alert(1)')).toBe(false)
})

it('only grants clipboard writes from the trusted main frame', () => {
expect(
canGrantWindowPermission(
'clipboard-sanitized-write',
'http://127.0.0.1:43127/session',
true
)
).toBe(true)
expect(
canGrantWindowPermission(
'clipboard-sanitized-write',
'http://localhost:43127/session',
true
)
).toBe(true)
expect(
canGrantWindowPermission('clipboard-read', 'http://127.0.0.1:43127/session', true)
).toBe(false)
expect(
canGrantWindowPermission(
'clipboard-sanitized-write',
'http://127.0.0.1:43127/session',
false
)
).toBe(false)
expect(
canGrantWindowPermission(
'clipboard-sanitized-write',
'https://example.com/session',
true
)
).toBe(false)
expect(
canGrantWindowPermission('clipboard-sanitized-write', 'file:///tmp/app.html', true)
).toBe(false)
})
})

describe('Harness window activation', () => {
Expand Down