Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/browser/browser-manager-guest-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
15 changes: 12 additions & 3 deletions src/main/browser/browser-manager-guest-navigation-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <a href>) 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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Loading