fix(security): harden URL and HTML sanitization

This commit is contained in:
RhenCloud committed 2026-10-24 16:41:29 +08:00
1 parent 978c53eeb3
commit c07741bf54
6 files changed
+9 -6

No files matched your search

+1 -1
View File
@@ -7,7 +7,7 @@ import { renderNeutralMessage } from "./render";
const markdown = new MarkdownIt({ html: false, linkify: false, breaks: false, typographer: false }); const markdown = new MarkdownIt({ html: false, linkify: false, breaks: false, typographer: false });
function markdownToText(value: string): string { function markdownToText(value: string): string {
return markdown.renderInline(value).replace(/<[^>]*>/gu, ""); return markdown.renderInline(value).replace(/<[^>]*>/gu, "").replace(/</g, "");
} }
function smallAvatar(url: string): string { function smallAvatar(url: string): string {
+1 -1
View File
@@ -74,7 +74,7 @@ function capHtml(text: string, max: number): string {
const weight = entity ? 1 : step; const weight = entity ? 1 : step;
// One character is kept for the ellipsis. // One character is kept for the ellipsis.
if (shown + weight > max - 1) { if (shown + weight > max - 1) {
const rest = text.slice(i).replace(/<[^>]*>/g, ""); const rest = text.slice(i).replace(/<[^>]*>/g, "").replace(/</g, "");
// What is left fits after all (no ellipsis needed). // What is left fits after all (no ellipsis needed).
if (shown + rest.replace(/&(?:#\d+|#x[0-9a-f]+|[a-z]+);/gi, "x").length <= max) return text; if (shown + rest.replace(/&(?:#\d+|#x[0-9a-f]+|[a-z]+);/gi, "x").length <= max) return text;
const closers = [...stack] const closers = [...stack]
+3
View File
@@ -218,6 +218,9 @@ function stripHtml(input: string): string {
.replace(/&gt;/gi, ">") .replace(/&gt;/gi, ">")
.replace(/&quot;/gi, '"') .replace(/&quot;/gi, '"')
.replace(/&apos;/gi, "'") .replace(/&apos;/gi, "'")
// Entity decoding can reveal a tag prefix after the first pass. Remove
// angle brackets again so encoded `<script` cannot survive sanitization.
.replace(/</g, "")
.replace(/&#x([0-9a-f]+);/gi, (m, hex: string) => codePoint(parseInt(hex, 16), m)) .replace(/&#x([0-9a-f]+);/gi, (m, hex: string) => codePoint(parseInt(hex, 16), m))
.replace(/&#(\d+);/g, (m, dec: string) => codePoint(Number(dec), m)) .replace(/&#(\d+);/g, (m, dec: string) => codePoint(Number(dec), m))
.replace(/&amp;/gi, "&"); .replace(/&amp;/gi, "&");
+2 -2
View File
@@ -151,7 +151,7 @@ describe("discord interactions", () => {
const url = String(input); const url = String(input);
if (url.includes("/callback")) { if (url.includes("/callback")) {
order.push(`callback:${(JSON.parse(String(init?.body)) as { type: number }).type}`); order.push(`callback:${(JSON.parse(String(init?.body)) as { type: number }).type}`);
} else if (url.includes("api.github.com")) { } else if (new URL(url).hostname === "api.github.com") {
order.push("github"); order.push("github");
return new Response( return new Response(
JSON.stringify({ html_url: "https://github.com/o/r/issues/7#c", user: { login: "a" } }), JSON.stringify({ html_url: "https://github.com/o/r/issues/7#c", user: { login: "a" } }),
@@ -203,7 +203,7 @@ describe("discord interactions", () => {
components: [{ components: [{ custom_id: "body", value: "hello" }] }], components: [{ components: [{ custom_id: "body", value: "hello" }] }],
}, },
}); });
expect(calls.some((u) => u.includes("api.github.com"))).toBe(false); expect(calls.some((u) => new URL(u).hostname === "api.github.com")).toBe(false);
}); });
it("refuses to save an edit over changes made on GitHub after the modal opened", async () => { it("refuses to save an edit over changes made on GitHub after the modal opened", async () => {
+1 -1
View File
@@ -657,7 +657,7 @@ describe("dispatchEvent fallback routing", () => {
const sent: string[] = []; const sent: string[] = [];
const bodies: string[] = []; const bodies: string[] = [];
mockFetch((url, init) => { mockFetch((url, init) => {
if (url.includes("api.github.com") && url.includes("/jobs")) { if (new URL(url).hostname === "api.github.com" && url.includes("/jobs")) {
return Response.json({ return Response.json({
jobs: [ jobs: [
{ {
+1 -1
View File
@@ -330,7 +330,7 @@ describe("telegram-commands refusals and whitespace", () => {
let replies = 0; let replies = 0;
let github = 0; let github = 0;
mockFetch((url) => { mockFetch((url) => {
if (url.includes("api.github.com")) github++; if (new URL(url).hostname === "api.github.com") github++;
replies++; replies++;
return new Response(JSON.stringify({ ok: false, description: "Bad Gateway" }), { return new Response(JSON.stringify({ ok: false, description: "Bad Gateway" }), {
status: 502, status: 502,