From 051b9ced6a59636a8c8bf04dd80104f436ed837f Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Sun, 26 Jul 2026 22:28:46 +0800 Subject: [PATCH] fix(chat): restrict markdown link protocols Assistant messages come from a remote AstrBot instance and are untrusted, but the markdown renderer passed the href through verbatim, so `javascript:` and `data:` URLs reached an anchor. Inside this webview, script execution has access to the full Tauri IPC surface. Links are now resolved with `new URL()` and only http/https/mailto survive; anything else renders as the original plain text. Relative hrefs are rejected too rather than being presented as absolute links against the placeholder base. `rel` gains `noopener`. The helper lives in `src/lib/` rather than beside the component because a non-component export in a component module fails `react-refresh` under the repository's `--max-warnings 0` lint gate. Refs P1-15 / F-05. Co-Authored-By: Claude Opus 5 --- src/features/chat/MessageContent.test.tsx | 54 +++++++++++++++++++++++ src/features/chat/MessageContent.tsx | 7 ++- src/lib/safeLinkHref.ts | 24 ++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/features/chat/MessageContent.test.tsx create mode 100644 src/lib/safeLinkHref.ts diff --git a/src/features/chat/MessageContent.test.tsx b/src/features/chat/MessageContent.test.tsx new file mode 100644 index 0000000..0b82d13 --- /dev/null +++ b/src/features/chat/MessageContent.test.tsx @@ -0,0 +1,54 @@ +import { render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; + +import { MessageContent } from "./MessageContent"; + +vi.mock("@tauri-apps/api/core", () => ({ + invoke: vi.fn(async () => { + throw new Error("not available in tests"); + }), +})); + +describe("MessageContent link safety", () => { + it("renders javascript: links as plain text instead of anchors", () => { + const { container } = render( + , + ); + + expect(container.querySelector("a")).toBeNull(); + expect(screen.getByText("[x](javascript:alert(1))")).toBeInTheDocument(); + }); + + it("renders data: links as plain text instead of anchors", () => { + const { container } = render( + , + ); + + expect(container.querySelector("a")).toBeNull(); + }); + + it("keeps http and https links and marks them noopener", () => { + const { container } = render( + , + ); + + const anchor = container.querySelector("a"); + expect(anchor).not.toBeNull(); + expect(anchor).toHaveAttribute("href", "https://example.com/guide"); + expect(anchor?.getAttribute("rel")).toContain("noopener"); + }); + + it("does not turn relative hrefs into absolute placeholder links", () => { + const { container } = render( + , + ); + + expect(container.querySelector("a")).toBeNull(); + }); +}); diff --git a/src/features/chat/MessageContent.tsx b/src/features/chat/MessageContent.tsx index 316d4e9..559b07e 100644 --- a/src/features/chat/MessageContent.tsx +++ b/src/features/chat/MessageContent.tsx @@ -1,6 +1,7 @@ import { Fragment, useEffect, useState, type ReactNode } from "react"; import { invoke } from "@tauri-apps/api/core"; +import { safeLinkHref } from "../../lib/safeLinkHref"; import type { ChatSlot } from "./types"; type MessageContentProps = { @@ -65,8 +66,12 @@ function renderInline( } const link = token.match(/^\[([^\]]+)\]\(([^)]+)\)$/); if (link) { + const href = safeLinkHref(link[2]); + if (!href) { + return {token}; + } return ( - + {link[1]} ); diff --git a/src/lib/safeLinkHref.ts b/src/lib/safeLinkHref.ts new file mode 100644 index 0000000..b3efbf7 --- /dev/null +++ b/src/lib/safeLinkHref.ts @@ -0,0 +1,24 @@ +const SAFE_LINK_PROTOCOLS = new Set(["http:", "https:", "mailto:"]); + +/** Placeholder base used to resolve relative hrefs without a real origin. */ +const RESOLUTION_BASE = "https://astrbot.invalid/"; + +/** + * Assistant messages come from a remote AstrBot instance and are untrusted. + * Only well-known navigation protocols may reach an anchor href; anything else + * (`javascript:`, `data:`, `file:`, custom schemes) is rejected so the caller + * can render the original markdown as plain text. + */ +export function safeLinkHref(raw: string): string | null { + const candidate = raw.trim(); + if (!candidate) return null; + try { + const parsed = new URL(candidate, RESOLUTION_BASE); + if (!SAFE_LINK_PROTOCOLS.has(parsed.protocol)) return null; + // Relative inputs resolve against the placeholder base and must not be + // presented as if they pointed at a real destination. + return parsed.href.startsWith(RESOLUTION_BASE) ? null : parsed.href; + } catch { + return null; + } +}