@@ -160,6 +160,8 @@ interface TestCapturedPreviewImage {
160160const makeTestPreviewWebContents = (
161161 capturePage : ( ) => Promise < TestCapturedPreviewImage > ,
162162 id = 42 ,
163+ loadURL = vi . fn ( async ( ) => undefined ) ,
164+ setWindowOpenHandler = vi . fn ( ) ,
163165) =>
164166 ( {
165167 id,
@@ -170,12 +172,13 @@ const makeTestPreviewWebContents = (
170172 isLoading : ( ) => false ,
171173 getZoomFactor : ( ) => 1 ,
172174 setZoomFactor : vi . fn ( ) ,
175+ loadURL,
173176 on : vi . fn ( ) ,
174177 off : vi . fn ( ) ,
175178 ipc : { on : vi . fn ( ) , off : vi . fn ( ) } ,
176179 send : webviewSend ,
177180 navigationHistory : { canGoBack : ( ) => false , canGoForward : ( ) => false } ,
178- setWindowOpenHandler : vi . fn ( ) ,
181+ setWindowOpenHandler,
179182 debugger : {
180183 isAttached : ( ) => false ,
181184 attach : vi . fn ( ) ,
@@ -230,6 +233,53 @@ describe("PreviewManager", () => {
230233 webviewSend . mockClear ( ) ;
231234 } ) ;
232235
236+ effectIt . effect ( "validates preview popup URLs before loading them" , ( ) =>
237+ withManager ( ( manager ) =>
238+ Effect . gen ( function * ( ) {
239+ const loadURL = vi . fn ( async ( ) => undefined ) ;
240+ const setWindowOpenHandler = vi . fn ( ) ;
241+ const webContents = makeTestPreviewWebContents (
242+ async ( ) => ( {
243+ toJPEG : ( ) => Buffer . from ( "popup-url-validation" ) ,
244+ getSize : ( ) => ( { width : 1 , height : 1 } ) ,
245+ } ) ,
246+ 42 ,
247+ loadURL ,
248+ setWindowOpenHandler ,
249+ ) ;
250+ fromId . mockReturnValue ( webContents ) ;
251+
252+ yield * manager . createTab ( "tab_popup_url_validation" ) ;
253+ yield * manager . registerWebview ( "tab_popup_url_validation" , 42 ) ;
254+
255+ expect ( setWindowOpenHandler ) . toHaveBeenCalledOnce ( ) ;
256+ const handler = setWindowOpenHandler . mock . calls [ 0 ] ?. [ 0 ] as
257+ | ( ( details : { readonly url : string } ) => { readonly action : string } )
258+ | undefined ;
259+ expect ( handler ) . toBeDefined ( ) ;
260+ if ( ! handler ) return ;
261+
262+ for ( const url of [
263+ "" ,
264+ "data:text/html,<script>document.body.innerText='blocked'</script>" ,
265+ "file:///tmp/preview.html" ,
266+ "javascript:document.body.innerText='blocked'" ,
267+ "http://[::1" ,
268+ ] ) {
269+ expect ( handler ( { url } ) ) . toEqual ( { action : "deny" } ) ;
270+ }
271+ expect ( loadURL ) . not . toHaveBeenCalled ( ) ;
272+
273+ expect ( handler ( { url : "http://localhost:3200" } ) ) . toEqual ( { action : "deny" } ) ;
274+ expect ( handler ( { url : "https://example.com/path" } ) ) . toEqual ( { action : "deny" } ) ;
275+ yield * Effect . yieldNow ;
276+
277+ expect ( loadURL ) . toHaveBeenNthCalledWith ( 1 , "http://localhost:3200/" ) ;
278+ expect ( loadURL ) . toHaveBeenNthCalledWith ( 2 , "https://example.com/path" ) ;
279+ } ) ,
280+ ) ,
281+ ) ;
282+
233283 effectIt . effect ( "reports an unregistered webview as temporarily unavailable" , ( ) =>
234284 withManager ( ( manager ) =>
235285 Effect . gen ( function * ( ) {
0 commit comments