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 <noreply@anthropic.com>
这个提交存在于:
父节点
78f03e378e
当前提交
051b9ced6a
共有 3 个文件被更改,包括 84 次插入 和 1 次删除
|
|
@ -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(
|
||||
<MessageContent content="[x](javascript:alert(1))" slot="companion" />,
|
||||
);
|
||||
|
||||
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(
|
||||
<MessageContent
|
||||
content="[x](data:text/html,<script>alert(1)</script>)"
|
||||
slot="companion"
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(container.querySelector("a")).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps http and https links and marks them noopener", () => {
|
||||
const { container } = render(
|
||||
<MessageContent
|
||||
content="[docs](https://example.com/guide)"
|
||||
slot="companion"
|
||||
/>,
|
||||
);
|
||||
|
||||
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(
|
||||
<MessageContent content="[x](/local/path)" slot="companion" />,
|
||||
);
|
||||
|
||||
expect(container.querySelector("a")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
|
@ -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 <Fragment key={key}>{token}</Fragment>;
|
||||
}
|
||||
return (
|
||||
<a key={key} href={link[2]} target="_blank" rel="noreferrer">
|
||||
<a key={key} href={href} target="_blank" rel="noopener noreferrer">
|
||||
{link[1]}
|
||||
</a>
|
||||
);
|
||||
|
|
|
|||
24
src/lib/safeLinkHref.ts
普通文件
24
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;
|
||||
}
|
||||
}
|
||||
正在加载…
添加表格
在新工单中引用