diff --git a/src/web/mdrender.ts b/src/web/mdrender.ts
index d414cf5f23..5348899132 100644
--- a/src/web/mdrender.ts
+++ b/src/web/mdrender.ts
@@ -34,6 +34,46 @@ export function makeMdRender(): MdRenderApi {
.replace(/"/g, """);
}
+ function inlineLinks(s: string, image: boolean): string {
+ const marker = image ? "![" : "[";
+ const parts: string[] = [];
+ let copied = 0;
+ let search = 0;
+ let urlEnd = 0;
+ while (search < s.length) {
+ const start = s.indexOf(marker, search);
+ if (start < 0) break;
+ const labelStart = start + marker.length;
+ const labelEnd = s.indexOf("]", labelStart);
+ if (labelEnd < 0) break;
+ // Every opening marker before this first ']' has the same closing delimiter.
+ // Skip that whole label on failure instead of retrying all of its suffixes.
+ search = labelEnd + 1;
+ if (s[labelEnd + 1] !== "(" || (!image && labelEnd === labelStart)) continue;
+ const urlStart = labelEnd + 2;
+ search = urlStart;
+ // Reuse the next URL terminator when nested starts share one long invalid URL.
+ // Both cursors only move forward, even for "[x]([x](... invalid".
+ if (urlEnd < urlStart) {
+ urlEnd = urlStart;
+ while (urlEnd < s.length && s[urlEnd] !== ")" && !/\s/.test(s[urlEnd])) urlEnd += 1;
+ }
+ if (urlEnd === s.length) break;
+ if (urlEnd === urlStart || s[urlEnd] !== ")") continue;
+ const label = s.slice(labelStart, labelEnd);
+ const url = s.slice(urlStart, urlEnd);
+ parts.push(s.slice(copied, start));
+ if (image) {
+ parts.push(/^(?:https?:\/\/|\/__bili\/blog-assets\/)/i.test(url) ? `` : "");
+ } else {
+ parts.push(/^(https?:\/\/|\/)/i.test(url) ? `${label}` : label);
+ }
+ copied = search = urlEnd + 1;
+ }
+ parts.push(s.slice(copied));
+ return parts.join("");
+ }
+
function inlineMd(s: string): string {
let out = escape(s);
out = out.replace(/`([^`]+)`/g, (_m, code: string) =>
@@ -43,18 +83,12 @@ export function makeMdRender(): MdRenderApi {
// panel's own bundled-asset route (/__bili/blog-assets/*, see
// web/blogfeed.ts). Quotes are already escaped by escape, so attribute
// injection is impossible.
- out = out.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, (_m, alt: string, url: string) => {
- if (/^(?:https?:\/\/|\/__bili\/blog-assets\/)/i.test(url)) return `
`;
- return "";
- });
+ out = inlineLinks(out, true);
out = out.replace(/\*\*([^*]+)\*\*/g, "$1");
out = out.replace(/(^|[^*])\*([^*\n]+)\*/g, "$1$2");
// [text](url) — only http/https/relative URLs survive; anything else
// degrades to plain bracketed text (no javascript: vectors).
- out = out.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_m, text: string, url: string) => {
- if (/^(https?:\/\/|\/)/i.test(url)) return `${text}`;
- return text;
- });
+ out = inlineLinks(out, false);
return out;
}
diff --git a/tests/mdrender-link-scaling.test.ts b/tests/mdrender-link-scaling.test.ts
new file mode 100644
index 0000000000..03b1837845
--- /dev/null
+++ b/tests/mdrender-link-scaling.test.ts
@@ -0,0 +1,51 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+import { runInNewContext } from "node:vm";
+import { build } from "esbuild";
+import { markdownToHtml } from "../src/web/markdown.ts";
+import { makeMdRender } from "../src/web/mdrender.ts";
+
+const malformed = [
+ ["unclosed link labels", "[".repeat(200_000)],
+ ["unclosed image labels", "![".repeat(100_000)],
+ ["nested link starts before an invalid URL", "[x](".repeat(30_000) + " invalid"],
+ ["nested URL starts sharing whitespace before a closing parenthesis", "[x](".repeat(200_000) + " )"],
+] as const;
+
+function boundedRender(render: (source: string) => string, source: string): string {
+ // Interrupt the synchronous old regex path without hanging the test runner. These inputs
+ // need only a short scan; the budget leaves ample room for a loaded CI machine.
+ return runInNewContext("render(source)", { render, source }, { timeout: 2_000 }) as string;
+}
+
+for (const [name, source] of malformed) {
+ test(`server Markdown finishes ${name} without rescanning every suffix`, () => {
+ assert.equal(boundedRender(markdownToHtml, source), `
${source}
`); + }); +} + +test("the minified detached browser renderer finishes the same malformed link inputs", async () => { + const built = await build({ entryPoints: ["src/web/mdrender.ts"], bundle: true, format: "cjs", minify: true, write: false, logLevel: "silent" }); + const module_ = { exports: {} as Record${source}
`); +}); + +const compatibility = [ + ["[outer [inner](https://example.test)", ''], + ["[bad](not [good](https://example.test))", '[bad](not good)
'], + ["[bad]([good with spaces](https://example.test))", '[bad](good with spaces)
'], + ["", '
[](/no-label) 
x)
'], + ["[x](bad url) [ok](/safe)", '[x](bad url) ok
'], + ["[x](javascript:alert(1))", "x)
"], +] as const; + +for (const [source, expected] of compatibility) { + test(`link scanning preserves existing delimiter and URL rules: ${source}`, () => { + assert.equal(markdownToHtml(source), expected); + }); +}