|
| 1 | +// Copyright (c) Jupyter Development Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import { app, shell, WebContents } from 'electron'; |
| 5 | +import log from 'electron-log'; |
| 6 | + |
| 7 | +// http/https for ordinary links, mailto for contact links |
| 8 | +const EXTERNAL_SCHEMES = ['https:', 'http:', 'mailto:']; |
| 9 | + |
| 10 | +const guarded = new WeakSet<WebContents>(); |
| 11 | + |
| 12 | +/** |
| 13 | + * Declare that this webContents carries its own navigation policy, so the |
| 14 | + * application-wide guard leaves its navigations alone. The lab view decides per |
| 15 | + * origin, and the server connection window has to follow a login wherever it |
| 16 | + * goes. |
| 17 | + */ |
| 18 | +export function markGuarded(contents: WebContents): void { |
| 19 | + guarded.add(contents); |
| 20 | +} |
| 21 | + |
| 22 | +export function openUrlInSystemBrowser(url: string): void { |
| 23 | + try { |
| 24 | + const { protocol, href } = new URL(url); |
| 25 | + if (EXTERNAL_SCHEMES.includes(protocol)) { |
| 26 | + shell.openExternal(href); |
| 27 | + } |
| 28 | + } catch { |
| 29 | + // unparseable target, nothing safe to open |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Pin a view that renders a bundled document. Those views are built from a |
| 35 | + * data: URL and are never meant to navigate: keeping them on their own document |
| 36 | + * means a link in content they render, the news feed on the welcome page for |
| 37 | + * instance, cannot replace app chrome with a page from the network. |
| 38 | + */ |
| 39 | +export function guardAppOwnedView(contents: WebContents): void { |
| 40 | + markGuarded(contents); |
| 41 | + |
| 42 | + const sendToBrowser = (event: Electron.Event, url: string) => { |
| 43 | + event.preventDefault(); |
| 44 | + openUrlInSystemBrowser(url); |
| 45 | + }; |
| 46 | + |
| 47 | + contents.on('will-navigate', sendToBrowser); |
| 48 | + contents.on('will-redirect', sendToBrowser); |
| 49 | + contents.setWindowOpenHandler(({ url }) => { |
| 50 | + openUrlInSystemBrowser(url); |
| 51 | + return { action: 'deny' }; |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Deny navigation for any webContents nobody claimed. Views are added over |
| 57 | + * time and the safe default is that a new one cannot be navigated away from its |
| 58 | + * document until someone decides what its policy should be. The check runs when |
| 59 | + * a navigation happens rather than when the webContents is created, because |
| 60 | + * creation fires before the owner has had a chance to claim it. |
| 61 | + */ |
| 62 | +export function installGlobalNavigationGuard(): void { |
| 63 | + app.on('web-contents-created', (_event, contents) => { |
| 64 | + contents.on('will-attach-webview', event => { |
| 65 | + event.preventDefault(); |
| 66 | + }); |
| 67 | + |
| 68 | + const denyUnclaimed = (event: Electron.Event, url: string) => { |
| 69 | + if (guarded.has(contents)) { |
| 70 | + return; |
| 71 | + } |
| 72 | + event.preventDefault(); |
| 73 | + log.warn(`Blocked navigation to ${url} in an unguarded view`); |
| 74 | + }; |
| 75 | + |
| 76 | + contents.on('will-navigate', denyUnclaimed); |
| 77 | + contents.on('will-redirect', denyUnclaimed); |
| 78 | + |
| 79 | + // an owner that sets its own handler replaces this one, which is what |
| 80 | + // claiming a view looks like for window creation |
| 81 | + contents.setWindowOpenHandler(({ url }) => { |
| 82 | + log.warn(`Blocked window opening ${url} from an unguarded view`); |
| 83 | + return { action: 'deny' }; |
| 84 | + }); |
| 85 | + }); |
| 86 | +} |
0 commit comments