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>
24 行
989 B
TypeScript
24 行
989 B
TypeScript
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;
|
|
}
|
|
}
|