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;
+ }
+}