π OpenHands: AI-Driven Development
fix(desktop): parse window-open URLs instead of prefix-matching localhost (#16961)
ιεΏθ°¦7ec4ed03afc1
diff --git a/electron/lib/window-url-policy.mjs b/electron/lib/window-url-policy.mjsnew file mode 100644index 0000000000..27e7d26f18--- /dev/null+++ b/electron/lib/window-url-policy.mjs@@ -0,0 +1,55 @@+/**+ * URL classification for window.open() and popup navigation in the Electron+ * shell.+ *+ * The renderer renders untrusted links (chat / agent markdown) with+ * `target="_blank"`, which routes through `setWindowOpenHandler` in+ * main.mjs. Classifying those URLs by string prefix+ * (`url.startsWith("http://localhost")`) lets attacker-controlled hosts+ * through as "app URLs" β `http://localhost.evil.com` satisfies the prefix,+ * and so does `http://[email protected]` (userinfo). Either one would open+ * remote content in a chromeless native window that visually belongs to the+ * app. Schemes outside a small allowlist must also never be handed to+ * `shell.openExternal`, which forwards them to OS protocol handlers.+ * Both classifications therefore go through URL parsing, not prefixes.+ */++const LOOPBACK_HOSTNAMES = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);++// Schemes `shell.openExternal` may be given. Kept in sync with the `href`+// protocols the renderer's markdown sanitizer accepts+// (`MARKDOWN_SANITIZE_SCHEMA` in+// src/components/features/markdown/markdown-renderer.tsx) β a link the+// renderer is willing to render but this policy denies becomes a click that+// silently does nothing. `mailto:` and `tel:` reach the mail/dialer handler;+// `file:`, `smb:` and custom scheme handlers stay denied.+const EXTERNAL_PROTOCOLS = new Set(["http:", "https:", "mailto:", "tel:"]);++/**+ * True for http(s) URLs whose host is the loopback app server β the only+ * URLs that may open inside an Electron window.+ */+export function isLoopbackAppUrl(rawUrl) {+ try {+ const url = new URL(rawUrl);+ return (+ (url.protocol === "http:" || url.protocol === "https:") &&+ LOOPBACK_HOSTNAMES.has(url.hostname)+ );+ } catch {+ return false;+ }+}++/**+ * True for URLs that may be handed to the OS via `shell.openExternal` β the+ * allowlisted schemes above. Everything else (file:, smb:, custom scheme+ * handlers, unparsable strings) must be denied, not delegated to the OS.+ */+export function isExternalBrowsableUrl(rawUrl) {+ try {+ return EXTERNAL_PROTOCOLS.has(new URL(rawUrl).protocol);+ } catch {+ return false;+ }+}diff --git a/electron/lib/window-url-policy.test.mjs b/electron/lib/window-url-policy.test.mjsnew file mode 100644index 0000000000..6e25974784--- /dev/null+++ b/electron/lib/window-url-policy.test.mjs@@ -0,0 +1,62 @@+import { describe, expect, it } from "vitest";++import {+ isExternalBrowsableUrl,+ isLoopbackAppUrl,+} from "./window-url-policy.mjs";++describe("isLoopbackAppUrl", () => {+ it.each([+ "http://localhost:8000/",+ "http://localhost:8000/some/app/path?query=1",+ "http://127.0.0.1:8000/",+ "http://[::1]:8000/",+ "https://localhost:8000/",+ ])("allows the loopback app server: %s", (url) => {+ expect(isLoopbackAppUrl(url)).toBe(true);+ });++ it.each([+ // Prefix look-alikes an attacker controls.+ "http://localhost.evil.com/",+ "http://127.0.0.1.evil.com/",+ "http://[email protected]/",+ "http://[email protected]/",+ // Remote hosts and non-http schemes.+ "https://openhands.dev/",+ "http://192.168.1.10:8000/",+ "file:///etc/passwd",+ "javascript:alert(1)",+ "about:blank",+ "smb://nas/share",+ "not a url",+ "",+ ])("rejects everything else: %s", (url) => {+ expect(isLoopbackAppUrl(url)).toBe(false);+ });+});++describe("isExternalBrowsableUrl", () => {+ it.each([+ "https://github.com/openhands",+ "http://example.com/docs",+ // Allowed `href` protocols in the renderer's markdown sanitizer; denying+ // them here would make such links click into nothing.+ "mailto:[email protected]",+ "tel:+15551234",+ ])("allows browsable schemes: %s", (url) => {+ expect(isExternalBrowsableUrl(url)).toBe(true);+ });++ it.each([+ "file:///etc/passwd",+ "file://\\\\nas\\share",+ "smb://nas/share",+ "javascript:alert(1)",+ "about:blank",+ "not a url",+ "",+ ])("denies everything the OS must not handle: %s", (url) => {+ expect(isExternalBrowsableUrl(url)).toBe(false);+ });+});diff --git a/electron/main.mjs b/electron/main.mjsindex 6806479836..3445cae1f6 100644--- a/electron/main.mjs+++ b/electron/main.mjs@@ -47,6 +47,8 @@ import { dirname, join } from "node:path";import { fileURLToPath, pathToFileURL } from "node:url";import { spawnSync } from "node:child_process";+import { isExternalBrowsableUrl, isLoopbackAppUrl } from "./lib/window-url-policy.mjs";+const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);@@ -391,15 +393,20 @@ function createMainWindow() {overrideBrowserWindowOptions: { width: 800, height: 700 },};}- // All other external URLs open directly in the system browser.- if (- !url.startsWith("http://localhost") &&- !url.startsWith("http://127.0.0.1")- ) {+ // All other URLs open directly in the system browser. The loopback test+ // goes through URL parsing: prefix matching would also accept+ // attacker-controlled hosts like http://localhost.evil.com (or+ // http://[email protected]) and render them in a chromeless native+ // window. Schemes outside the openExternal allowlist are denied+ // outright β shell.openExternal would forward them to OS protocol+ // handlers.+ if (isLoopbackAppUrl(url)) {+ return { action: "allow" };+ }+ if (isExternalBrowsableUrl(url)) {shell.openExternal(url);- return { action: "deny" };}- return { action: "allow" };+ return { action: "deny" };});// When the renderer opens a popup (the about:blank above), watch for its@@ -408,13 +415,11 @@ function createMainWindow() {// now-unneeded Electron popup.mainWin.webContents.on("did-create-window", (popupWin) => {popupWin.webContents.on("will-navigate", (_event, url) => {- if (- url !== "about:blank" &&- !url.startsWith("http://localhost") &&- !url.startsWith("http://127.0.0.1")- ) {+ if (url !== "about:blank" && !isLoopbackAppUrl(url)) {_event.preventDefault();- shell.openExternal(url);+ if (isExternalBrowsableUrl(url)) {+ shell.openExternal(url);+ }popupWin.close();}});