The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 14:30 UTC
fix(desktop): ask once per site before a preview page reads the clipboard (#26173)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 27, 2026 at 16:38 UTC
3ae672f82e17235972620f0085b5d527d3282cf2
2 files changed
+148
-30
apps/desktop/src/preview/BrowserSession.test.ts
+50
-6
@@ -168,12 +168,7 @@ describe("BrowserSession", () => {
168
return granted;
169
};
170
171
- for (const permission of [
172
- "clipboard-read",
173
- "clipboard-sanitized-write",
174
- "geolocation",
175
- "fullscreen",
176
- ]) {
171
+ for (const permission of ["clipboard-sanitized-write", "geolocation", "fullscreen"]) {
172
assert.isTrue(requestAllows(permission), `request handler should allow ${permission}`);
173
assert.isTrue(
174
checkHandler(null, permission) as boolean,
@@ -226,6 +221,55 @@ describe("BrowserSession", () => {
221
}).pipe(Effect.provide(layer)),
222
);
223
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
+ // Enter must not accept a prompt the page raised while the user was typing.
260
+ assert.strictEqual(showMessageBox.mock.calls[0]?.[0].defaultId, 1);
261
+
262
+ assert.isTrue(yield* readClipboard("https://allowed.test/other"));
263
+ assert.isTrue(checkClipboard("https://allowed.test"));
264
+
265
+ assert.isFalse(yield* readClipboard("https://blocked.test/"));
266
+ assert.isFalse(yield* readClipboard("https://blocked.test/again"));
267
+ assert.isFalse(checkClipboard("https://blocked.test"));
268
+ assert.isTrue(checkClipboard("https://allowed.test"));
269
+ assert.strictEqual(showMessageBox.mock.calls.length, 2);
270
+ }).pipe(Effect.provide(layer)),
271
+ );
272
+
273
it.effect("clears a partition whose session has not been opened yet", () =>
274
Effect.gen(function* () {
275
const browserSessions = yield* BrowserSession.BrowserSession;
apps/desktop/src/preview/BrowserSession.ts
+98
-24
@@ -20,7 +20,6 @@ export type BrowserSessionPartitionNamespace = "profile";
20
21
// Electron's clipboard API uses clipboard-sanitized-write and checks both permission handlers.
22
const ALLOWED_PREVIEW_PERMISSIONS: ReadonlySet<string> = new Set([
23
- "clipboard-read",
23
"clipboard-sanitized-write",
24
"geolocation",
25
// DesktopWindow confines HTML fullscreen to the guest's webview.
@@ -55,6 +54,19 @@ const externalProtocolPromptUrl = (rawUrl: string | undefined): string | null =>
54
55
const MAX_PROMPT_URL_LENGTH = 300;
56
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
+
70
export class BrowserSessionPartitionDerivationError extends Schema.TaggedError<BrowserSessionPartitionDerivationError>()(
71
"BrowserSessionPartitionDerivationError",
72
{
@@ -175,47 +187,70 @@ export const make = Effect.gen(function* BrowserSessionMake() {
187
const electronDialog = yield* ElectronDialog.ElectronDialog;
188
const runFork = Effect.runForkWith(yield* Effect.context<never>());
189
// 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 = (
193
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,
200
): void => {
185
- const url = externalProtocolPromptUrl(rawUrl);
186
- if (url === null || externalPromptOpen) {
187
- callback(false);
201
+ if (permissionPromptOpen) {
202
+ callback(undefined);
203
return;
204
}
190
- externalPromptOpen = true;
191
- const settle = (granted: boolean) =>
205
+ permissionPromptOpen = true;
206
+ const settle = (answer: boolean | undefined) =>
207
Effect.sync(() => {
193
- externalPromptOpen = false;
194
- callback(granted);
208
+ permissionPromptOpen = false;
209
+ callback(answer);
210
});
196
- const shownUrl =
197
- url.length > MAX_PROMPT_URL_LENGTH ? `${url.slice(0, MAX_PROMPT_URL_LENGTH)}…` : url;
211
runFork(
212
electronDialog
213
.showMessageBox(
214
{
215
type: "question",
203
- buttons: ["Open", "Cancel"],
204
- defaultId: 0,
216
+ buttons: [...prompt.buttons],
217
+ // Default to deny so an Enter typed into the page cannot accept a prompt it raised.
218
+ defaultId: 1,
219
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}`,
220
+ message: prompt.message,
221
+ ...(prompt.detail === undefined ? {} : { detail: prompt.detail }),
222
},
223
previewHostWindow(requester),
224
)
225
.pipe(
212
- Effect.map(({ response }) => response === 0),
213
- Effect.orElseSucceed(() => false),
226
+ Effect.map(({ response }): boolean | undefined => response === 0),
227
+ Effect.orElseSucceed(() => undefined),
228
Effect.flatMap(settle),
215
- Effect.onInterrupt(() => settle(false)),
229
+ Effect.onInterrupt(() => settle(undefined)),
230
),
231
);
232
};
233
+ /** Electron launches external protocols after granting openExternal; untrusted pages require confirmation. */
234
+ const confirmOpenExternal = (
235
+ requester: WebContents | null,
236
+ rawUrl: string | undefined,
237
+ callback: (granted: boolean) => void,
238
+ ): void => {
239
+ const url = externalProtocolPromptUrl(rawUrl);
240
+ if (url === null) {
241
+ callback(false);
242
+ return;
243
+ }
244
+ promptPermission(
245
+ requester,
246
+ {
247
+ buttons: ["Open", "Cancel"],
248
+ message: `Open this ${new URL(url).protocol.slice(0, -1)} link?`,
249
+ detail: `A page in the browser wants to open another application:\n\n${truncateForPrompt(url)}`,
250
+ },
251
+ (answer) => callback(answer === true),
252
+ );
253
+ };
254
const sessionsRef = yield* SynchronizedRef.make<ReadonlyMap<string, Session>>(new Map());
255
256
const getPartition = Effect.fn("BrowserSession.getPartition")(function* (
@@ -248,6 +283,35 @@ export const make = Effect.gen(function* BrowserSessionMake() {
283
return Effect.try({
284
try: () => {
285
const browserSession = session.fromPartition(partition);
286
+ // Like Chrome, clipboard reads ask once per origin; both answers last for the session.
287
+ const clipboardReadDecisions = new Map<string, boolean>();
288
+ const confirmClipboardRead = (
289
+ requester: WebContents,
290
+ rawUrl: string | undefined,
291
+ callback: (granted: boolean) => void,
292
+ ): void => {
293
+ const origin = permissionOrigin(rawUrl);
294
+ if (origin === null) {
295
+ callback(false);
296
+ return;
297
+ }
298
+ const remembered = clipboardReadDecisions.get(origin);
299
+ if (remembered !== undefined) {
300
+ callback(remembered);
301
+ return;
302
+ }
303
+ promptPermission(
304
+ requester,
305
+ {
306
+ buttons: ["Allow", "Block"],
307
+ message: `${truncateForPrompt(origin)} wants to see text and images copied to the clipboard`,
308
+ },
309
+ (answer) => {
310
+ if (answer !== undefined) clipboardReadDecisions.set(origin, answer);
311
+ callback(answer === true);
312
+ },
313
+ );
314
+ };
315
// Rewriting Electron's native User-Agent makes Turnstile fail integrity checks with error 600010.
316
browserSession.setPermissionRequestHandler(
317
(webContents, permission, callback, details) => {
@@ -259,11 +323,21 @@ export const make = Effect.gen(function* BrowserSessionMake() {
323
);
324
return;
325
}
326
+ if (permission === "clipboard-read") {
327
+ confirmClipboardRead(
328
+ webContents,
329
+ "requestingUrl" in details ? details.requestingUrl : undefined,
330
+ callback,
331
+ );
332
+ return;
333
+ }
334
callback(ALLOWED_PREVIEW_PERMISSIONS.has(permission));
335
},
336
);
265
- browserSession.setPermissionCheckHandler((_webContents, permission) =>
266
- ALLOWED_PREVIEW_PERMISSIONS.has(permission),
337
+ browserSession.setPermissionCheckHandler((_webContents, permission, requestingOrigin) =>
338
+ permission === "clipboard-read"
339
+ ? clipboardReadDecisions.get(permissionOrigin(requestingOrigin) ?? "") === true
340
+ : ALLOWED_PREVIEW_PERMISSIONS.has(permission),
341
);
342
const next = new Map(sessions);
343
next.set(partition, browserSession);