mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 01:46:39 +00:00
fix(browser): reject a file URL with a hostname (#14792)
* fix(browser): reject a file URL with a hostname A file URL names another host when its hostname is not empty. The navigation hook blocks that URL. An empty host stays allowed, including file://localhost, because the URL standard ignores that host. * test(browser): cover a blocked file-host redirect The file-host regression emits will-redirect for a remote file URL and a local file URL. Dropping the redirect check leaves preventDefault unset and fails the test.
This commit is contained in:
1 parent
0de6029fb6
commit
777832eff5
2 files changed
+80
-1
No files matched your search
@@ -175,7 +175,9 @@ const isAllowedNavigation = (value) => {
|
||||
if (typeof value !== "string") return true;
|
||||
try {
|
||||
const parsed = new URL(value);
|
||||
if (["file:", "data:", "about:"].includes(parsed.protocol)) return true;
|
||||
// A file URL with a hostname names another host, not a local path.
|
||||
if (parsed.protocol === "file:") return parsed.hostname === "";
|
||||
if (["data:", "about:"].includes(parsed.protocol)) return true;
|
||||
return (
|
||||
["http:", "https:"].includes(parsed.protocol) &&
|
||||
["127.0.0.1", "localhost", "[::1]", "::1"].includes(parsed.hostname)
|
||||
|
||||
@@ -3259,6 +3259,83 @@ it("preserves fatal process termination after recording uncaught exceptions", as
|
||||
expect(result.stderr).toContain("fatal-hook-secret");
|
||||
});
|
||||
|
||||
it("rejects a file URL whose hostname is not empty", async () => {
|
||||
const root = await createTestTempDirectory("rea-electron-file-host-");
|
||||
await writeElectronFixture(root);
|
||||
const script = join(root, "navigate.cjs");
|
||||
await writeFile(
|
||||
script,
|
||||
`
|
||||
(async () => {
|
||||
const electron = require("electron");
|
||||
const window = new electron.BrowserWindow({ webPreferences: {} });
|
||||
const patched = window.webContents.loadURL;
|
||||
const reached = [];
|
||||
window.webContents.loadURL = function observe(url) {
|
||||
return Promise.resolve(patched.call(this, url)).then((value) => {
|
||||
reached.push(url);
|
||||
return value;
|
||||
});
|
||||
};
|
||||
const outcome = async (url) => {
|
||||
try {
|
||||
await window.webContents.loadURL(url);
|
||||
return "allowed";
|
||||
} catch (cause) {
|
||||
return cause instanceof Error ? cause.message : "unknown";
|
||||
}
|
||||
};
|
||||
const remote = await outcome("file://attacker.example/share/a.html");
|
||||
const localhost = await outcome("file://localhost/tmp/a.html");
|
||||
const local = await outcome("file:///tmp/local.html");
|
||||
const loopback = await outcome("http://127.0.0.1/");
|
||||
const data = await outcome("data:text/html,hi");
|
||||
const prevented = {
|
||||
navigateRemote: false,
|
||||
navigateLocal: false,
|
||||
redirectRemote: false,
|
||||
redirectLocal: false,
|
||||
};
|
||||
const emitNavigation = (slot, eventName, url) => {
|
||||
window.webContents.emit(eventName, {
|
||||
preventDefault() { prevented[slot] = true; },
|
||||
}, url);
|
||||
};
|
||||
emitNavigation("navigateRemote", "will-navigate", "file://attacker.example/share/a.html");
|
||||
emitNavigation("navigateLocal", "will-navigate", "file:///tmp/local.html");
|
||||
emitNavigation("redirectRemote", "will-redirect", "file://attacker.example/share/a.html");
|
||||
emitNavigation("redirectLocal", "will-redirect", "file:///tmp/local.html");
|
||||
process.stdout.write(JSON.stringify({ remote, localhost, local, loopback, data, reached, prevented }) + "\\n");
|
||||
})().catch((cause) => {
|
||||
console.error(cause);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
`,
|
||||
);
|
||||
const result = await runNode(script, root);
|
||||
expect(result.code, result.stderr).toBe(0);
|
||||
// The URL standard ignores a localhost file host, so this URL is a local path.
|
||||
expect(JSON.parse(result.stdout)).toEqual({
|
||||
remote: "External navigation is blocked",
|
||||
localhost: "allowed",
|
||||
local: "allowed",
|
||||
loopback: "allowed",
|
||||
data: "allowed",
|
||||
reached: [
|
||||
"file://localhost/tmp/a.html",
|
||||
"file:///tmp/local.html",
|
||||
"http://127.0.0.1/",
|
||||
"data:text/html,hi",
|
||||
],
|
||||
prevented: {
|
||||
navigateRemote: true,
|
||||
navigateLocal: false,
|
||||
redirectRemote: true,
|
||||
redirectLocal: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
"standard-prefix",
|
||||
"sandbox-prefix",
|
||||
|
||||
Reference in new issue
Block a user