Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(windows): block untrusted file navigation (#1776)

Henry Su45c20b809c29
diff --git a/src/helpers/navigationGuard.js b/src/helpers/navigationGuard.js
new file mode 100644
index 000000000..83176deb0
--- /dev/null
+++ b/src/helpers/navigationGuard.js
@@ -0,0 +1,26 @@
+// will-navigate also fires for renderer-initiated location.reload() with the
+// window's own URL (appUrl), so that URL must stay allowed.
+function isAllowedAppNavigation(url, appUrl) {
+ if (url.startsWith("devtools://")) return true;
+ if (!appUrl) return false;
+
+ try {
+ const candidate = new URL(url);
+ const app = new URL(appUrl);
+ // file:// URLs all share the opaque "null" origin, so protocol and
+ // pathname carry the comparison there.
+ return (
+ candidate.origin === app.origin &&
+ candidate.protocol === app.protocol &&
+ candidate.pathname === app.pathname
+ );
+ } catch {
+ return false;
+ }
+}
+
+function isExternalBrowserUrl(url) {
+ return url.startsWith("http://") || url.startsWith("https://");
+}
+
+module.exports = { isAllowedAppNavigation, isExternalBrowserUrl };
diff --git a/src/helpers/windowManager.js b/src/helpers/windowManager.js
index 17bdcba5f..9d479e1fc 100644
--- a/src/helpers/windowManager.js
+++ b/src/helpers/windowManager.js
@@ -6,6 +6,8 @@ const DragManager = require("./dragManager");
const MainWindowPlacementCoordinator = require("./mainWindowPlacementCoordinator");
const MenuManager = require("./menuManager");
const DevServerManager = require("./devServerManager");
+const { isAllowedAppNavigation, isExternalBrowserUrl } = require("./navigationGuard");
+const { pathToFileURL } = require("url");
const dockManager = require("./dockManager");
const { i18nMain } = require("./i18nMain");
const { NotificationDismissTimer, getNotificationTimeoutMs } = require("./notificationTimer");
@@ -1089,19 +1091,21 @@ class WindowManager {
this._onboardingWindowState = null;
this.controlPanelWindow.webContents.on("will-navigate", (event, url) => {
- const appUrl = DevServerManager.getAppUrl(true);
- const controlPanelUrl = appUrl.startsWith("http") ? appUrl : `file://${appUrl}`;
+ // getAppUrl() is null in packaged builds; exactly one of the two is set.
+ const appUrl =
+ DevServerManager.getAppUrl(true) ??
+ pathToFileURL(DevServerManager.getAppFilePath(true).path).href;
- if (
- url.startsWith(controlPanelUrl) ||
- url.startsWith("file://") ||
- url.startsWith("devtools://")
- ) {
+ if (isAllowedAppNavigation(url, appUrl)) {
return;
}
event.preventDefault();
- this.openExternalUrl(url);
+ if (isExternalBrowserUrl(url)) {
+ this.openExternalUrl(url);
+ } else {
+ debugLogger.debug("Blocked untrusted navigation", { url }, "window");
+ }
});
this.controlPanelWindow.webContents.setWindowOpenHandler(({ url }) => {
diff --git a/test/helpers/windowManagerNavigation.test.js b/test/helpers/windowManagerNavigation.test.js
new file mode 100644
index 000000000..503b68993
--- /dev/null
+++ b/test/helpers/windowManagerNavigation.test.js
@@ -0,0 +1,60 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+const {
+ isAllowedAppNavigation,
+ isExternalBrowserUrl,
+} = require("../../src/helpers/navigationGuard.js");
+
+const PACKAGED_APP_URL =
+ "file:///Applications/OpenWhispr.app/Contents/Resources/app.asar/src/dist/index.html";
+
+test("packaged app: the window's own reload is allowed regardless of query or hash", () => {
+ // location.reload() fires will-navigate with the window's own URL —
+ // sign-out, restart-onboarding, account deletion, and the ErrorBoundary
+ // reload all break if this is blocked.
+ assert.equal(isAllowedAppNavigation(`${PACKAGED_APP_URL}?panel=true`, PACKAGED_APP_URL), true);
+ assert.equal(isAllowedAppNavigation(PACKAGED_APP_URL, PACKAGED_APP_URL), true);
+ assert.equal(
+ isAllowedAppNavigation(`${PACKAGED_APP_URL}?panel=true#settings`, PACKAGED_APP_URL),
+ true
+ );
+});
+
+test("packaged app: Windows-style file URLs compare on the encoded pathname", () => {
+ const winAppUrl = "file:///C:/Program%20Files/OpenWhispr/resources/app.asar/src/dist/index.html";
+ assert.equal(isAllowedAppNavigation(`${winAppUrl}?panel=true`, winAppUrl), true);
+});
+
+test("packaged app: foreign file URLs are blocked", () => {
+ assert.equal(isAllowedAppNavigation("file:///etc/passwd", PACKAGED_APP_URL), false);
+ assert.equal(isAllowedAppNavigation("file:///Users/test/secret.txt", PACKAGED_APP_URL), false);
+});
+
+test("packaged app: remote and unparseable URLs are blocked", () => {
+ assert.equal(isAllowedAppNavigation("https://example.com/", PACKAGED_APP_URL), false);
+ assert.equal(isAllowedAppNavigation("http://localhost:5183/", PACKAGED_APP_URL), false);
+ assert.equal(isAllowedAppNavigation("not a url", PACKAGED_APP_URL), false);
+});
+
+test("a null appUrl blocks everything except devtools without throwing", () => {
+ assert.equal(isAllowedAppNavigation("file:///etc/passwd", null), false);
+ assert.equal(isAllowedAppNavigation("https://example.com/", undefined), false);
+ assert.equal(isAllowedAppNavigation("devtools://devtools/bundled/inspector.html", null), true);
+});
+
+test("navigation guard keeps the development app route and devtools available", () => {
+ const appUrl = "http://localhost:5183/?panel=true";
+ assert.equal(isAllowedAppNavigation("http://localhost:5183/", appUrl), true);
+ assert.equal(isAllowedAppNavigation("http://localhost:5183/?panel=true", appUrl), true);
+ assert.equal(isAllowedAppNavigation("http://localhost:5183/other", appUrl), false);
+ assert.equal(isAllowedAppNavigation("devtools://devtools/bundled/inspector.html", null), true);
+});
+
+test("only http(s) targets are handed to the external browser", () => {
+ assert.equal(isExternalBrowserUrl("https://example.com/docs"), true);
+ assert.equal(isExternalBrowserUrl("http://example.com/"), true);
+ assert.equal(isExternalBrowserUrl("file:///etc/passwd"), false);
+ assert.equal(isExternalBrowserUrl("about:blank"), false);
+ assert.equal(isExternalBrowserUrl("devtools://devtools/bundled/inspector.html"), false);
+});