Skip to content

Commit 4fd9f98

Browse files
fix(desktop): ask once per site before a preview page reads the clipboard
Replace the flat clipboard-read deny with a Chrome-style Allow/Block prompt per origin, remembered for the browser session. The check handler reports the remembered state so navigator.permissions.query matches. The prompt reuses the openExternal dialog guard and length cap. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 20086ad commit 4fd9f98

2 files changed

Lines changed: 145 additions & 25 deletions

File tree

‎apps/desktop/src/preview/BrowserSession.test.ts‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ describe("BrowserSession", () => {
178178

179179
// `clipboard-write` is not a real Electron permission — the async write API
180180
// uses `clipboard-sanitized-write` — so the stale name must not be granted,
181-
// and reads and unrelated permissions stay denied.
182-
for (const permission of ["clipboard-read", "clipboard-write", "notifications", "midi"]) {
181+
// and unrelated permissions stay denied.
182+
for (const permission of ["clipboard-write", "notifications", "midi"]) {
183183
assert.isFalse(requestAllows(permission), `request handler should deny ${permission}`);
184184
assert.isFalse(
185185
checkHandler(null, permission) as boolean,
@@ -221,6 +221,53 @@ describe("BrowserSession", () => {
221221
}).pipe(Effect.provide(layer)),
222222
);
223223

224+
it.effect("asks once per origin before a page reads the clipboard", () =>
225+
Effect.gen(function* () {
226+
const browserSessions = yield* BrowserSession.BrowserSession;
227+
const partition = yield* browserSessions.getPartition("scope-a");
228+
yield* browserSessions.getSession("scope-a");
229+
const browserSession = sessions.get(partition);
230+
assert.isDefined(browserSession);
231+
const requestHandler = browserSession.setPermissionRequestHandler.mock.calls[0]?.[0];
232+
const checkHandler = browserSession.setPermissionCheckHandler.mock.calls[0]?.[0];
233+
assert.isFunction(requestHandler);
234+
assert.isFunction(checkHandler);
235+
236+
const readClipboard = (requestingUrl: string) =>
237+
Effect.callback<boolean>((resume) => {
238+
requestHandler(
239+
null,
240+
"clipboard-read",
241+
(granted: boolean) => resume(Effect.succeed(granted)),
242+
{ requestingUrl, isMainFrame: true },
243+
);
244+
});
245+
const checkClipboard = (origin: string): boolean =>
246+
checkHandler(null, "clipboard-read", origin, {}) as boolean;
247+
248+
showMessageBox.mockReset();
249+
showMessageBox.mockReturnValueOnce(0).mockReturnValueOnce(1);
250+
251+
assert.isFalse(checkClipboard("https://allowed.test"));
252+
assert.isTrue(yield* readClipboard("https://allowed.test/page"));
253+
assert.strictEqual(showMessageBox.mock.calls.length, 1);
254+
assert.strictEqual(
255+
showMessageBox.mock.calls[0]?.[0].message,
256+
"https://allowed.test wants to see text and images copied to the clipboard",
257+
);
258+
assert.deepStrictEqual(showMessageBox.mock.calls[0]?.[0].buttons, ["Allow", "Block"]);
259+
260+
assert.isTrue(yield* readClipboard("https://allowed.test/other"));
261+
assert.isTrue(checkClipboard("https://allowed.test"));
262+
263+
assert.isFalse(yield* readClipboard("https://blocked.test/"));
264+
assert.isFalse(yield* readClipboard("https://blocked.test/again"));
265+
assert.isFalse(checkClipboard("https://blocked.test"));
266+
assert.isTrue(checkClipboard("https://allowed.test"));
267+
assert.strictEqual(showMessageBox.mock.calls.length, 2);
268+
}).pipe(Effect.provide(layer)),
269+
);
270+
224271
it.effect("clears a partition whose session has not been opened yet", () =>
225272
Effect.gen(function* () {
226273
const browserSessions = yield* BrowserSession.BrowserSession;

‎apps/desktop/src/preview/BrowserSession.ts‎

Lines changed: 96 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const ALLOWED_PREVIEW_PERMISSIONS: ReadonlySet<string> = new Set([
2424
"geolocation",
2525
// DesktopWindow confines HTML fullscreen to the guest's webview.
2626
"fullscreen",
27-
// Deny clipboard-read: any page could read the system clipboard silently; native paste needs no grant.
2827
// Deny local-fonts to untrusted previews: it exposes installed-font fingerprints and file bytes.
2928
// Deny notifications: pages could impersonate T3 Code; app notifications use the default session.
3029
]);
@@ -55,6 +54,19 @@ const externalProtocolPromptUrl = (rawUrl: string | undefined): string | null =>
5554

5655
const MAX_PROMPT_URL_LENGTH = 300;
5756

57+
const truncateForPrompt = (text: string): string =>
58+
text.length > MAX_PROMPT_URL_LENGTH ? `${text.slice(0, MAX_PROMPT_URL_LENGTH)}…` : text;
59+
60+
const permissionOrigin = (rawUrl: string | undefined): string | null => {
61+
if (!rawUrl) return null;
62+
try {
63+
const { origin } = new URL(rawUrl);
64+
return origin === "null" ? null : origin;
65+
} catch {
66+
return null;
67+
}
68+
};
69+
5870
export class BrowserSessionPartitionDerivationError extends Schema.TaggedError<BrowserSessionPartitionDerivationError>()(
5971
"BrowserSessionPartitionDerivationError",
6072
{
@@ -175,47 +187,69 @@ export const make = Effect.gen(function* BrowserSessionMake() {
175187
const electronDialog = yield* ElectronDialog.ElectronDialog;
176188
const runFork = Effect.runForkWith(yield* Effect.context<never>());
177189
// One prompt at a time, so a page cannot stack dialogs by looping a deep link.
178-
let externalPromptOpen = false;
179-
/** Electron launches external protocols after granting openExternal; untrusted pages require confirmation. */
180-
const confirmOpenExternal = (
190+
let permissionPromptOpen = false;
191+
/** Settles with `undefined` when the user gave no answer: another prompt was open or the dialog failed. */
192+
const promptPermission = (
181193
requester: WebContents | null,
182-
rawUrl: string | undefined,
183-
callback: (granted: boolean) => void,
194+
prompt: {
195+
readonly buttons: readonly [allow: string, deny: string];
196+
readonly message: string;
197+
readonly detail?: string;
198+
},
199+
callback: (answer: boolean | undefined) => void,
184200
): void => {
185-
const url = externalProtocolPromptUrl(rawUrl);
186-
if (url === null || externalPromptOpen) {
187-
callback(false);
201+
if (permissionPromptOpen) {
202+
callback(undefined);
188203
return;
189204
}
190-
externalPromptOpen = true;
191-
const settle = (granted: boolean) =>
205+
permissionPromptOpen = true;
206+
const settle = (answer: boolean | undefined) =>
192207
Effect.sync(() => {
193-
externalPromptOpen = false;
194-
callback(granted);
208+
permissionPromptOpen = false;
209+
callback(answer);
195210
});
196-
const shownUrl =
197-
url.length > MAX_PROMPT_URL_LENGTH ? `${url.slice(0, MAX_PROMPT_URL_LENGTH)}…` : url;
198211
runFork(
199212
electronDialog
200213
.showMessageBox(
201214
{
202215
type: "question",
203-
buttons: ["Open", "Cancel"],
216+
buttons: [...prompt.buttons],
204217
defaultId: 0,
205218
cancelId: 1,
206-
message: `Open this ${new URL(url).protocol.slice(0, -1)} link?`,
207-
detail: `A page in the browser wants to open another application:\n\n${shownUrl}`,
219+
message: prompt.message,
220+
...(prompt.detail === undefined ? {} : { detail: prompt.detail }),
208221
},
209222
previewHostWindow(requester),
210223
)
211224
.pipe(
212-
Effect.map(({ response }) => response === 0),
213-
Effect.orElseSucceed(() => false),
225+
Effect.map(({ response }): boolean | undefined => response === 0),
226+
Effect.orElseSucceed(() => undefined),
214227
Effect.flatMap(settle),
215-
Effect.onInterrupt(() => settle(false)),
228+
Effect.onInterrupt(() => settle(undefined)),
216229
),
217230
);
218231
};
232+
/** Electron launches external protocols after granting openExternal; untrusted pages require confirmation. */
233+
const confirmOpenExternal = (
234+
requester: WebContents | null,
235+
rawUrl: string | undefined,
236+
callback: (granted: boolean) => void,
237+
): void => {
238+
const url = externalProtocolPromptUrl(rawUrl);
239+
if (url === null) {
240+
callback(false);
241+
return;
242+
}
243+
promptPermission(
244+
requester,
245+
{
246+
buttons: ["Open", "Cancel"],
247+
message: `Open this ${new URL(url).protocol.slice(0, -1)} link?`,
248+
detail: `A page in the browser wants to open another application:\n\n${truncateForPrompt(url)}`,
249+
},
250+
(answer) => callback(answer === true),
251+
);
252+
};
219253
const sessionsRef = yield* SynchronizedRef.make<ReadonlyMap<string, Session>>(new Map());
220254

221255
const getPartition = Effect.fn("BrowserSession.getPartition")(function* (
@@ -248,6 +282,35 @@ export const make = Effect.gen(function* BrowserSessionMake() {
248282
return Effect.try({
249283
try: () => {
250284
const browserSession = session.fromPartition(partition);
285+
// Like Chrome, clipboard reads ask once per origin; both answers last for the session.
286+
const clipboardReadDecisions = new Map<string, boolean>();
287+
const confirmClipboardRead = (
288+
requester: WebContents,
289+
rawUrl: string | undefined,
290+
callback: (granted: boolean) => void,
291+
): void => {
292+
const origin = permissionOrigin(rawUrl);
293+
if (origin === null) {
294+
callback(false);
295+
return;
296+
}
297+
const remembered = clipboardReadDecisions.get(origin);
298+
if (remembered !== undefined) {
299+
callback(remembered);
300+
return;
301+
}
302+
promptPermission(
303+
requester,
304+
{
305+
buttons: ["Allow", "Block"],
306+
message: `${truncateForPrompt(origin)} wants to see text and images copied to the clipboard`,
307+
},
308+
(answer) => {
309+
if (answer !== undefined) clipboardReadDecisions.set(origin, answer);
310+
callback(answer === true);
311+
},
312+
);
313+
};
251314
// Rewriting Electron's native User-Agent makes Turnstile fail integrity checks with error 600010.
252315
browserSession.setPermissionRequestHandler(
253316
(webContents, permission, callback, details) => {
@@ -259,11 +322,21 @@ export const make = Effect.gen(function* BrowserSessionMake() {
259322
);
260323
return;
261324
}
325+
if (permission === "clipboard-read") {
326+
confirmClipboardRead(
327+
webContents,
328+
"requestingUrl" in details ? details.requestingUrl : undefined,
329+
callback,
330+
);
331+
return;
332+
}
262333
callback(ALLOWED_PREVIEW_PERMISSIONS.has(permission));
263334
},
264335
);
265-
browserSession.setPermissionCheckHandler((_webContents, permission) =>
266-
ALLOWED_PREVIEW_PERMISSIONS.has(permission),
336+
browserSession.setPermissionCheckHandler((_webContents, permission, requestingOrigin) =>
337+
permission === "clipboard-read"
338+
? clipboardReadDecisions.get(permissionOrigin(requestingOrigin) ?? "") === true
339+
: ALLOWED_PREVIEW_PERMISSIONS.has(permission),
267340
);
268341
const next = new Map(sessions);
269342
next.set(partition, browserSession);

0 commit comments

Comments
 (0)