@@ -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
5655const 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+
5870export 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