Skip to content

fix(report): escape site-controlled text in the Markdown report (squirrelscan/repo#3522) - #1186

Merged
nc9 merged 4 commits into
mainfrom
fix/3522-markdown-escape
Oct 27, 2026
Merged

nc9 merged 4 commits into
mainfrom
fix/3522-markdown-escape

Conversation

@nc9

@nc9 nc9 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the main-branch half of squirrelscan/repo#3522: text the audited site controls reached the Markdown report (squirrel report --format markdown, squirrel audit -f markdown) unescaped. A page URL such as https://example.com/a](https://evil.example/x)![i](https://evil.example/p.png) (new URL().pathname keeps ] ( ) ! [) closed the page-list link early and added a link and a remote image to the report a reader opens.

One shared helper

packages/report/src/output/markdown-escape.ts:

  • escapeMarkdownText: link labels and plain values.
    • Backslash-escapes \ [ ] ( ) ! + "" + < > & |`.
    • Collapses line breaks to a space.
    • Escapes a leading block marker (# - + * _ ~ =, 1.), so a label after - cannot open a heading, rule, fence or nested list.
    • *, _ and ~ are otherwise left alone: they only emphasise, and URL paths full of underscores are read raw by agents.
  • escapeMarkdownCell: the same escapes for a table cell, with line breaks as <br>, as before. It replaces the old local escapeMarkdownTableCell.
  • escapeMarkdownDestination: link destinations.
    • Percent-encodes \ ( ) < > [ ] + "" + `, whitespace and control characters.
    • Backslash-escapes an entity-shaped &.
    • Leaves ! (inert in a destination, and %21 is not the same URL under RFC 3986) and plain query & separators.
  • markdownLink(label, url): links only an http(s) URL or a root-relative path. A javascript:, data: or //host URL in a stored report stays plain text.
  • markdownCodeSpan: fences past the longest backtick run inside the value.
  • escapeHtmlText: values inside raw HTML (<summary>, alt).

Sinks fixed

  • markdown.ts:
    • a check's page list (label and destination);
    • item rows: URL items, plain labels, from: source pages;
    • the check message and check name in the table, in <details><summary>, and in the "not listed" line;
    • the partial-audit reason;
    • the **URL:** line;
    • the site profile cells;
    • the technology name, logo alt and version.
  • entities.ts: entityCell (the Entities section and the entity diff) now escapes brackets, !, < and backticks as well as the pipe. The conflicting property goes in a safe code span.
  • coverage.ts: the scan line prints a stored report's generator version only when it is version-shaped (0.0.137, 0.0.137-beta.1 still print).

Not in this PR

Tests

packages/report/tests/markdown-crawled-text-3522.test.ts, 17 tests.

  • It renders each sink with the hostile URL or a payload carrying a link, an image and raw HTML, parses the output with Bun's built-in CommonMark renderer (Bun.markdown.html), and asserts no href or src points at the attacker host.
  • A guard test shows the old page-list line does inject under that renderer.
  • The new sink tests failed before the fix.
  • Test hosts are example.com and evil.example only.

Ran 28 report test files and 4 CLI test files that render Markdown, entities or the scan line: 0 fail. tsgo --noEmit is clean in packages/report and apps/cli.

Independent review (Sonnet, high effort), three rounds:

  • Round 1 found the missed sinks above, fixed in the second commit.
  • Round 2 found one MED: a line break inside raw HTML (<summary>, the logo alt) could end the HTML block. Fixed in the third commit: escapeHtmlText now flattens line breaks. The entity diff's property cell also became a pipe-safe code span (entityCodeCell).
  • Round 3: APPROVE.

Fixed after review (CodeQL js/incomplete-sanitization): entityCodeCell showed a pipe as \|, which a value already holding a backslash defeated (a\|b still split the cell). It now shows each | as ¦ (U+00A6) inside the code span, with no backslash inserted, and a test asserts the row keeps its two cells.

The commits after the first also widened the CHANGELOG entry to cover the added sinks.

CHANGELOG.md: Security entry under [Unreleased].

Docs: none needed (output escaping only; no flag or format changes). Skill: no change needed.

@nc9
nc9 marked this pull request as ready for review October 27, 2026 04:59
@claude

claude Bot commented Oct 27, 2026

Copy link
Copy Markdown

Review of the diff only. I could not run the tests: dependencies are not installed in my sandbox.

The approach is sound. A shared escape module, a real CommonMark parser in the tests, a test that checks the exploit works on unescaped input, and the markdownLink http(s)/root-relative allowlist are all good.

  • (medium) markdown-escape.ts markdownCodeSpan: Math.max(0, ...Array.from(flat.matchAll(/+/g), ...)) spreads one argument per backtick run. A site-controlled value with a very large number of separate runs (for example ``" ".repeat(200000)``) can throw RangeError: Maximum call stack size exceeded. The entity paths clip to 90 characters, but `t.version` in `markdown.ts` is passed in unclamped. A crash in report rendering is worse than the injection being fixed. Use a loop that tracks the max, and add a test with a large input.
  • (medium) escapeMarkdownCell is now applied to every check message and name. It escapes ( ) ! & [ ] < > and backticks, so ordinary messages such as Title too long (72 chars) or text containing & will read as \(72 chars\) in the raw Markdown. This output is meant to be read raw by coding agents, and the PR itself says that matters for underscores. Please confirm that existing snapshot and golden tests still pass, and consider escaping only what can open a link, image, HTML or code span ([ ] < > and backtick, plus ! before [) in cells. Also add a CHANGELOG note that raw message text changes.
  • (low) escapeMarkdownDestination: /&(?=#?[0-9A-Za-z]+;)/g has an unbounded quantifier over site text, which AGENTS.md says to bound. It runs in linear time, because each & scans only its own alphanumeric run, but add {1,32} to match the repo convention.
  • (low) The <details><summary><strong> lines and the "not listed" line escape check.name with different helpers (escapeHtmlText and escapeMarkdownText). That works, but the **${escapeMarkdownText(name)}:** line leaves * unescaped, so a name containing ** can break the bold span. It is cosmetic. A test for the not-listed line is missing; the summary and table paths are covered.
  • (low) escapeMarkdownCell does not collapse other line separators (U+2028, U+2029, \v, \f). Some renderers treat these as line ends, so a cell could break across lines. Normalize them to <br> or a space.
  • (low) markdown.ts still defines escapeMarkdownInline, which is separate from the new module. If it is now unused, remove it. If it is still used, check that it is not a second, weaker escaper.
  • (nit) In coverage.ts the new isVersionLike helper sits between the doc comment for scanScopeLine and the function it documents, so the doc comment is now attached to the wrong function. Move the helper above that comment.
  • (nit) The # Injected heading test and the label tests assert exact HTML strings that depend on how Bun.markdown renders. If Bun is bumped, they may need updating.

Comment thread packages/report/src/entities.ts Fixed
@nc9
nc9 force-pushed the fix/3522-markdown-escape branch from a9b44e9 to 0079749 Compare October 27, 2026 05:35
@claude

claude Bot commented Oct 27, 2026

Copy link
Copy Markdown

Review of #1186 (Markdown escaping for crawled text). I read the diff only and did not run the tests.

The shared helper is a clear improvement. It is easier to reason about than the old per-sink escapes. The escape set, the destination encoding, the javascript: and //host guard in markdownLink, and the backtick fencing in markdownCodeSpan all look right.

  • (medium) packages/report/src/coverage.ts (~L102): the new isVersionLike and its JSDoc were inserted between the existing JSDoc for scanScopeLine and the function itself. The "UNCLAMPED wording" doc block now sits above isVersionLike, and scanScopeLine has no doc. Move the helper above that JSDoc.
  • (low) CHANGELOG.md: ### Security follows the previous bullet with no blank line, so some Markdown parsers will fold it into that list item. Check that the heading ordering matches the repo's Added, Changed, Fixed, Security, Docs convention. The entry is also long. Consider trimming it to the user-visible effect.
  • (low) markdown.ts, **URL:** line: escapeMarkdownText backslash-escapes (, ), & and ! in a plain URL. Agents reading the raw Markdown will see https://example.com/a?x=1\&y=2, which cuts against your stated goal of keeping URLs readable. Consider a lighter URL-specific escape for display-only URLs.
  • (low) Bare http(s)://… text in escaped values, such as messages and the URL line, still becomes a clickable GFM autolink in renderers that support it. This is a link only, with no image or hijacked label, so it is probably acceptable. Say so explicitly in the helper docs.
  • (low) AGENTS.md asks that a URL the website chose be canonicalised with new URL(v).href before printing, which handles bidi and control characters. markdownLink only percent-encodes ASCII controls and specials in the destination, and the label (getPathname(page)) can still carry bidi or format characters. Consider canonicalising with canonicalizeReportUrl as well.
  • (low) escapeMarkdownCell does not escape the <br> it emits. That is fine because < in the input is escaped first. A one-line comment would help the next reader.
  • (nit) The table rows still interpolate rule.id and categoryName raw. These are internal values, so this is fine today. A comment would stop someone later assuming they are escaped.
  • (nit) escapeMarkdownText escapes a leading - or _ even when no block would open, for example -foo. That is harmless over-escaping.

Tests: the new markdown-crawled-text-3522.test.ts covers the main injection shapes. Please confirm that it fails against the old code, per AGENTS.md's "prove a test bites", and that it covers a stored report whose generatorVersion is not version-shaped. Run build:css:check and the report suite to make sure the entityCell change did not shift existing snapshots.

No critical or high findings.

@claude

claude Bot commented Oct 27, 2026

Copy link
Copy Markdown

Solid fix: one shared escaping module, and the tests parse the output with a real CommonMark renderer and include a guard proving the repro injects when left raw. I found no blocking problems. Notes below; I did not run the tests, only read the diff.

  • (low) packages/report/src/coverage.ts:~102: isVersionLike is inserted between the JSDoc block for scanScopeLine and the function itself. The doc comment now attaches to isVersionLike. Move the helper above that comment.
  • (low) CHANGELOG.md: the ### Security heading has no blank line before it, so it is glued to the last ### Docs bullet. Some Markdown renderers and the release-notes extractor may fold it into that bullet. The entry is also one very long bullet; consider trimming it.
  • (low) packages/report/src/output/markdown.ts (**U... / "not listed" line and escapeMarkdownCell): escapeMarkdownCell escapes & ! ( ) [ ] and the like in every check message, not just site-controlled text. Plain messages such as "A & B" or "(3 pages)" now read as A \& B in raw Markdown, which agents read raw. This is probably acceptable. Worth confirming that existing markdown snapshot or golden tests still pass.
  • (nit) escapeMarkdownCell does not escape a leading block marker, unlike escapeMarkdownText. That is fine inside a table cell, but the difference deserves a one-line comment.
  • (nit) escapeMarkdownInline (older helper) and the new escapeMarkdownText now coexist in markdown.ts with different rules. Consider consolidating in a follow-up. Other sinks such as branding text, seedRedirect and notCheckedLines still use the old one.
  • (nit) markdownLink leaves a root-relative /... URL linkable, while isLinkable rejects //host. A /\evil.example path is also treated as relative by browsers and gets percent-encoded to %5C here, so it is safe. A test case for it would pin that down.
  • (nit) Per AGENTS.md, run bun run build:css:check, format:check and lint in packages/report. Also run the secret scan, since the test file contains many URL-like fixtures.

nc9 added 4 commits October 27, 2026 17:23
…ts, item rows, partial reason, URL line, tech version (squirrelscan/repo#3522)

One shared helper module (output/markdown-escape.ts) escapes link labels and plain values, percent-encodes link destinations, links only http(s) and root-relative URLs, and fences code spans past any backtick run. A stored report's generator version prints on the scan line only when it is version-shaped.

Signed-off-by: Nik Cubrilovic <[email protected]>
…ile, tech name and alt; leading block markers; entity-shaped & in destinations (squirrelscan/repo#3522 review)

Signed-off-by: Nik Cubrilovic <[email protected]>
…'s property cell is a pipe-safe code span (squirrelscan/repo#3522 review round 2)

Signed-off-by: Nik Cubrilovic <[email protected]>
…ookalike instead of backslash-escaping it (CodeQL js/incomplete-sanitization; squirrelscan/repo#3522)

A backslash before a pipe inside a code span does not survive a value that already holds a backslash: 'a\|b' still split its GFM table cell. Each | is now shown as U+00A6, so no backslash is inserted at all.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/3522-markdown-escape branch from b3b9643 to c5790af Compare October 27, 2026 06:23
@claude

claude Bot commented Oct 27, 2026

Copy link
Copy Markdown

Review of the Markdown escaping change. I read the diff but did not run the tests or the linters.

The approach is sound. The escaping sits in one module, markdown-escape.ts. The tests render through a real CommonMark parser (Bun.markdown) and assert on links and images, which is the right check. I found nothing that blocks the merge.

  • (low) packages/report/src/output/markdown-escape.ts escapeMarkdownText escapes &, (, ) and ! everywhere. The **URL:** line and every plain item label now show \& and \( in the raw .md. Agents read that raw text, and a query string like ?a=1&b=2 becomes ?a=1\&b=2. Rendered Markdown looks the same. Consider escaping & only when it would start an entity, as escapeMarkdownDestination already does, or confirm the raw readability cost is acceptable.
  • (low) escapeMarkdownCell has a different escape set from escapeMarkdownText. It does not guard a leading block marker, which is fine in a cell. The markdown.ts JSDoc says the two are escaped alike, though, so check that wording is accurate.
  • (low) packages/report/src/output/markdown.ts still has several escapeMarkdownInline calls and a second escaper alongside the new module. Both are valid, but two parallel escapers will drift. A follow-up could fold them into markdown-escape.ts.
  • (low) The Entity conflicts row in entities.ts uses markdownCodeSpan(clipCell(...)), which does not replace |. That is fine in a list item. If the same call is ever reused in a table cell, it will split the cell, so keep the use of entityCodeCell there.
  • (nit) packages/report/src/coverage.ts: isVersionLike was inserted between the JSDoc block for scanScopeLine and the function itself. The doc comment now attaches to the helper. Move the helper above the comment.
  • (nit) CHANGELOG.md: add a blank line before ### Security. It follows a bullet directly.
  • (nit) The CHANGELOG.md bullet is very long. Consider splitting the sink list out of it.
  • (nit) isVersionLike has a {0,64} bound and no ReDoS risk. It silently drops anything longer or odd, which matches the stated intent.

Test coverage is good. It covers the repro, the sinks, the line-break and HTML-block cases, and the helper unit tests. The suite should still be run once with the revert-the-fix check from AGENTS.md, which the the repro URL really does inject when left raw test partly covers. bun run scripts/test-suites.ts --check should confirm the new test file is wired into a suite.

Docs: none. Skill: none.

@nc9
nc9 merged commit 40f810a into main Oct 27, 2026
15 checks passed
@nc9
nc9 deleted the fix/3522-markdown-escape branch October 27, 2026 06:38
nc9 added a commit that referenced this pull request Oct 27, 2026
…eses, !, bare &) (#1203)

The escaping from #1186 also wrote backslashes into plain sentences (recommended: 30-60 became \(recommended: 30-60\)). Every link, image and reference definition needs an unescaped [, and [ ] stay escaped, so ( ) ! cannot form anything. Still escaped: backslash, [ ], backtick, <, |, entity-shaped &, leading block markers. Hostile-input tests unchanged and green. Refs repo#3522. Docs: none. Skill: no change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants