diff --git a/src/main/browser/browser-manager-guest-lifecycle.test.ts b/src/main/browser/browser-manager-guest-lifecycle.test.ts index e13801282f5e..3948322730ed 100644 --- a/src/main/browser/browser-manager-guest-lifecycle.test.ts +++ b/src/main/browser/browser-manager-guest-lifecycle.test.ts @@ -240,6 +240,7 @@ describe('browserManager', () => { const guest = { isDestroyed: vi.fn(() => false), getType: vi.fn(() => 'webview'), + getURL: vi.fn(() => 'https://example.com/'), setBackgroundThrottling: guestSetBackgroundThrottlingMock, setWindowOpenHandler: guestSetWindowOpenHandlerMock, on: guestOnMock, @@ -260,6 +261,32 @@ describe('browserManager', () => { expect(preventDefault).toHaveBeenCalledTimes(1) }) + it.each([ + ['https://example.com/', true], + ['file:///docs/plan.html', false] + ])('from %s, a file: link navigation is blocked=%s', (currentUrl, blocked) => { + const guest = { + isDestroyed: vi.fn(() => false), + getType: vi.fn(() => 'webview'), + getURL: vi.fn(() => currentUrl), + setBackgroundThrottling: guestSetBackgroundThrottlingMock, + setWindowOpenHandler: guestSetWindowOpenHandlerMock, + on: guestOnMock, + off: guestOffMock, + openDevTools: guestOpenDevToolsMock + } + webContentsFromIdMock.mockReturnValue(guest) + + browserManager.attachGuestPolicies(guest as never) + + const willNavigateHandler = guestOnMock.mock.calls.find( + ([event]) => event === 'will-navigate' + )?.[1] as ((event: { preventDefault: () => void }, url: string) => void) | undefined + const preventDefault = vi.fn() + willNavigateHandler?.({ preventDefault }, 'file:///docs/index.html') + expect(preventDefault).toHaveBeenCalledTimes(blocked ? 1 : 0) + }) + it('unregisterAll clears tracked guests and context-menu listeners', () => { const guest = { id: 101, diff --git a/src/main/browser/browser-manager-guest-navigation-policy.ts b/src/main/browser/browser-manager-guest-navigation-policy.ts index 5b30b4a34b1c..0ef9fac89672 100644 --- a/src/main/browser/browser-manager-guest-navigation-policy.ts +++ b/src/main/browser/browser-manager-guest-navigation-policy.ts @@ -13,7 +13,7 @@ export abstract class BrowserManagerGuestNavigationPolicy extends BrowserManager if (url.startsWith('blob:https://') || url.startsWith('blob:http://')) { return true } - // Why: initial file:// attach is allowed for user-opened previews, but block later file:// redirects so remote pages can't probe the FS. + // Why: initial file:// attach is allowed for user-opened previews, but block web → file:// navs and redirects so remote pages can't probe the FS. if (url.startsWith('file:')) { event.preventDefault() return false @@ -26,6 +26,15 @@ export abstract class BrowserManagerGuestNavigationPolicy extends BrowserManager return true } + const willNavigateHandler = (event: Electron.Event, url: string): void => { + // Why: a local page's own links (e.g. relative ) stay file:// → file://, as in Chrome; + // web pages still can't navigate into the filesystem. + if (url.startsWith('file:') && guest.getURL().startsWith('file:')) { + return + } + navigationGuard(event, url) + } + const willRedirectHandler = ( event: Electron.Event, url: string, @@ -127,7 +136,7 @@ export abstract class BrowserManagerGuestNavigationPolicy extends BrowserManager this.notifyBrowserGuestStateChanged(guest.id) } - guest.on('will-navigate', navigationGuard) + guest.on('will-navigate', willNavigateHandler) guest.on('will-redirect', willRedirectHandler) guest.on('did-start-navigation', didStartNavigationHandler) guest.on('did-navigate', didNavigateHandler) @@ -151,7 +160,7 @@ export abstract class BrowserManagerGuestNavigationPolicy extends BrowserManager // guest may already be destroyed } if (!guest.isDestroyed()) { - guest.off('will-navigate', navigationGuard) + guest.off('will-navigate', willNavigateHandler) guest.off('will-redirect', willRedirectHandler) guest.off('did-start-navigation', didStartNavigationHandler) guest.off('did-navigate', didNavigateHandler)