Skip to content

fix(crawler): read gzip sitemaps within 50 MB, a guessed path that answers HTML is no sitemap (#662, #670) - #686

Merged
nc9 merged 4 commits into
mainfrom
fix/sitemaps-662-670
Oct 12, 2026
Merged

nc9 merged 4 commits into
mainfrom
fix/sitemaps-662-670

Conversation

@nc9

@nc9 nc9 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #662
Closes #670

What changed

All in packages/crawler/src/sitemaps.ts (the sitemap walk); crawl/sitemap-valid and crawl/sitemap-exists are unchanged and report what the walk stores.

#662, gzip sitemaps. fetchSitemap read every body with response.text(), so a .xml.gz child served as application/gzip reached the XML parser as compressed bytes. Now:

  • The body is read as bytes, metered and cancelled at the sitemap protocol limit (SITEMAP_MAX_BYTES = 52,428,800).
  • Content-Encoding is undone by the walk itself, within the same limit. The fetch passes Bun's decompress: false and sends Accept-Encoding: gzip. This was needed for the bound to be real: Bun's own decoding inflates a whole response before a reader can stop it. Measured with a 1 MB Content-Encoding: gzip bomb (1 GiB of spaces), server and client in separate processes: response.text() (old path) peaked +1.39 GB, a 50 MB-capped reader on Bun-decoded fetch peaked +1.79 GB, the new path +66 MB. gzip, x-gzip, deflate (zlib or raw) and br are decoded; anything else, or a stacked coding, is Unsupported Content-Encoding: <coding>. On a runtime other than Bun (where fetch ignores decompress: false and decodes), the header is not re-applied, and a body that opens with markup under a coding that does not decode is read as already decoded.
  • A body that still starts with 1f 8b is a gzip file and is decompressed once, maxOutputLength bounded, then parsed.
  • Errors: Sitemap exceeds the 50 MB uncompressed size limit, Corrupt gzip body: <zlib reason> (Corrupt br body / Corrupt deflate body for those codings). An uncompressed sitemap over 50 MB is now refused the same way (search engines reject it too).

#670, catch-all servers. decodeSitemapBody classifies the body's opening with a linear scan (whitespace, XML declaration, PIs, comments, a non-HTML doctype with its internal subset): an HTML doctype or <html> root, or a body that is not XML at all, or a non-sitemap root served as text/html when the parse found nothing, is notASitemap. A body that opens with <urlset>/<sitemapindex> is never ruled out, however broken. After the walk, discoverSitemaps drops a notASitemap result unless the site claimed that URL (a robots.txt Sitemap: line or a child entry of a parsed index); a dropped one lands in failed with source common, which no rule reports. A claimed one stays stored with Not a sitemap: the URL returned an HTML page.

Behaviour change worth knowing: on a catch-all site with no sitemap, crawl/sitemap-exists used to PASS ("8 sitemap(s) found") because the eight HTML pages were stored as sitemaps; it now reports No XML sitemap found.

Acceptance evidence

BEFORE = squirrel 0.0.108 binary, AFTER = CLI from this branch (bun run apps/cli/src/cli.ts audit <fixture> --http --refresh -m 2 -f json --rule-include crawl/sitemap-valid --rule-include crawl/sitemap-exists), local fixtures, stored sitemaps rows read from the project DB:

Fixture BEFORE AFTER
#662 repro: index -> child.xml.gz (application/gzip, no CE) child Unknown sitemap format, 0 URLs, sitemap-syntax fail child 1 URL, no errors, 0 failed checks (same as the plain control)
plain child.xml control 1 URL, pass 1 URL, pass
corrupt gzip child Unknown sitemap format Corrupt gzip body: invalid stored block lengths
gzip child inflating to 60 MiB Parse error: readTagExp ... <binary garbage> Sitemap exceeds the 50 MB uncompressed size limit
Content-Encoding: gzip sitemap 1 URL, pass 1 URL, pass
#670 catch-all, no Sitemap line 8 guessed paths stored, 8 error(s) in 8 sitemap(s); sitemap-exists passed 0 stored; sitemap-exists No XML sitemap found
catch-all, robots.txt declares /site-map.xml (HTML) 9 errors (declared + 8 guessed) 1 error: /site-map.xml Not a sitemap: the URL returned an HTML page
catch-all, real urlset at guessed /sitemap_index.xml real one OK + 7 Unknown sitemap format only the real one, 2 URLs, no errors

Tests (new): packages/crawler/tests/sitemap-gzip.test.ts (gzip child vs plain child against a real origin, CE gzip, CE + gzip file, corrupt and truncated gzip, gzip bomb, endless body cut at the limit, decompressed once, br/deflate/raw deflate, unsupported/stacked/constructor coding, decompress: false + Accept-Encoding: gzip on every redirect hop) and packages/crawler/tests/sitemap-guessed-html.test.ts (catch-all, declared-but-HTML in robots.txt and at a common location, real guessed sitemap, guessed path that an index names, XML non-sitemap still reported, classifier cases incl. doctype/comment/PI openings, gzipped HTML, broken sitemap served as text/html, linear time on 5,000 comments).

Local gates: tsgo --noEmit in packages/crawler and packages/audit-engine clean. Tests, explicit files: the two new files plus crawler sitemap-entity-expansion, sitemap-xml-escapes, sitemap-news-namespace, sitemap-gate, preamble-budget, refusals, redirect-header-security, fetch-body-deadline, discovery-probes, sitemaps-column-collision, sitemap-news-column, slow-origin-crawl, full-crawl-harness; audit-engine robots-sitemap-scheme-less, truncated-sitemap-discovery, refused-fetches-e2e; cli crawler/sitemap-cap: all pass. scripts/test-suites.ts --check ok. Docs: [email protected] check --strict passes (426 pages). Three codex review rounds; findings fixed (regex backtracking replaced by the scanner, gzipped HTML, root vs extraction-error, long comments, doctype/comments in the doctype, the Bun eager-decode memory hole, inherited-property coding lookup, doc wording).

Docs: docs/rules/crawl/sitemap-valid.mdx (the error labels as templates, gzip handling, guessed locations), docs/rules/crawl/sitemap-exists.mdx (a guessed location answering with a page does not count). CHANGELOG under Unreleased / Fixed.

Agent skill (squirrelscan/skills): no change needed; the skill does not describe sitemap errors.

Follow-ups (not fixed here)

  • chore(cli): remove the dead legacy sitemap walk in apps/cli/src/crawl #688: apps/cli/src/crawl/sitemaps.ts is a dead legacy copy of the walk (only tests import it) that still reads response.text().
  • crawl/sitemap-valid: a bare-year <lastmod> fails the whole sitemap with a parse error #687: parseSitemap turns a bare-year <lastmod>2024</lastmod> (valid W3C datetime) into Parse error: ... trim is not a function for the whole file: fast-xml-parser hands numbers through.
  • The same Bun eager-decoding hole exists for every other capped read of an untrusted response (page fetcher, root probes using readBodyCapped): a Content-Encoding bomb is inflated in full before the cap applies. Tracked in the private monorepo (hosted-crawl impact).
  • Plain-text sitemaps (one URL per line, allowed by the protocol) are still not parsed: declared ones report Unknown sitemap format, guessed ones now count as no sitemap.

…swers HTML is no sitemap #662 #670

#662: a gzip child sitemap (.xml.gz served as application/gzip, no
Content-Encoding) reached the XML parser as compressed bytes and was
reported as "Unknown sitemap format". The walk now reads the body as
bytes, undoes Content-Encoding itself (fetched with Bun's
decompress: false, Accept-Encoding: gzip) and decompresses a remaining
gzip file once. Every stage is bounded by the 50 MB protocol limit:
Bun's own decoding inflated a 1 MB Content-Encoding bomb to ~1.4 GB,
now ~66 MB. Distinct errors for oversize, corrupt gzip/br/deflate and
unsupported codings.

#670: on a catch-all server every guessed common location was stored
as a sitemap failing with "Unknown sitemap format". A guessed location
that answers with an HTML page or a non-XML body is now no sitemap; one
named in robots.txt or a sitemap index is still reported, labelled
"Not a sitemap: the URL returned an HTML page". Classification is a
linear scan of the opening (comments, PIs, doctype), and a body that
opens with a sitemap root is never ruled out.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 marked this pull request as ready for review October 12, 2026 01:14
@claude

claude Bot commented Oct 12, 2026

Copy link
Copy Markdown

Reviewed packages/crawler/src/sitemaps.ts and the new tests. I read the diff only and did not run the tests. The approach looks sound: bounded byte reads, bounded inflate, and a linear scan for the body opening. I found no blocking issues.

  • (low) sitemaps.ts discoverSitemaps: the "claimed" check compares result.data.url against raw robotsSitemaps and childSitemaps strings. If a claimed URL differs from the fetched one only by normalisation (trailing slash, fragment, case of host, or a redirect), an HTML page at a claimed location would be treated as guessed and silently downgraded to a failure. Normalising both sides before comparing would close that gap. A test for a claimed URL that redirects would also help.
  • (low) inflateEither: any non-RangeError from inflateSync falls through to raw inflate. If both fail, the reported error is the raw-inflate message, which can be misleading for a corrupt zlib body. Consider reporting the first error.
  • (low) FETCH_RETURNS_RAW_BODY = "Bun" in globalThis is fixed at module load, and decompress: false is cast via as RequestInit. If Bun ever changes or renames that option, decoding silently stops with no failure. A small test that asserts the Bun behaviour, which the PR may already have in its decompress: false hop test, would guard it.
  • (nit) bodyOpening calls text.slice(...) for every skipped construct, and new TextDecoder().decode runs on the whole 50 MB body even when only the opening is needed. Both are fine at current limits.
  • (nit) brotliDecompressSync respects maxOutputLength, but a brotli bomb test next to the gzip bomb test would confirm that on Bun.

Security and performance: the bounds (wire, post-Content-Encoding, gzip file) and the single-coding restriction address the decompression-bomb risk well. Test coverage is thorough.

…late bombs and a redirected claim #662 #670

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9

nc9 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Triage of the review, fixed in 568b59b:

  • Claimed-URL matching: no gap by construction. The walk dedupes on the raw URL string and records each result under the string it REQUESTED (result.data.url is never the post-redirect URL), and every claimed string (resolved robots.txt line, raw index child) is fetched under exactly that string. A child spelled differently from a guessed location is a separate fetch under its own string, so the claimed copy is kept. Added a test for the redirect case: a robots.txt sitemap that 301s to the HTML homepage is still stored with Not a sitemap: the URL returned an HTML page.
  • inflateEither: fixed. When both zlib and raw inflate fail, the zlib reason is reported; the test checks the exact message and that the two reasons differ.
  • decompress: false / FETCH_RETURNS_RAW_BODY: guarded already. The "every request, redirect hops included, turns the runtime's decoding off" test pins the option and Accept-Encoding: gzip on both hops, and the real-origin "Content-Encoding: gzip still parses" test fails if Bun ever stops honouring the option (the decoded XML would then be gunzipped again and reported corrupt).
  • Brotli bomb: added, together with a deflate bomb; both return Sitemap exceeds the 50 MB uncompressed size limit on Bun.
  • The two nits (per-construct slice, decoding the whole body): agreed they are fine at these limits; the whole body is decoded because the parser needs it.

@claude

claude Bot commented Oct 12, 2026

Copy link
Copy Markdown

Review of #686 (gzip sitemaps, catch-all HTML sitemaps). I read the diff, mainly packages/crawler/src/sitemaps.ts. I did not run the tests, and I did not read the rest of the test files past the start of sitemap-guessed-html.test.ts.

The design holds up. Reading raw bytes with decompress: false and then inflating with maxOutputLength is the right way to bound memory. The linear scanner is a good replacement for the backtracking-prone regex. The tests cover the hard cases (bomb, endless body, stacked and unsupported codings, redirect hops).

  • (low) sitemaps.ts FETCH_RETURNS_RAW_BODY = "Bun" in globalThis: runtime sniffing by global name is brittle. If a Bun version ignores decompress: false on some path (such as a redirect or a proxy), the body is already decoded while the Content-Encoding header is still re-applied. That gives a spurious Corrupt gzip body. The tests assert the option is passed on every hop, but nothing checks that the behaviour holds. Consider falling back when the inflate fails and the bytes already look like XML.
  • (low) sitemaps.ts claimed.has(result.data.url): the match is an exact string comparison. A robots.txt entry is normalised via resolvedUrl at line 813. An index child entry in childSitemaps may differ in trailing slash, case or fragment from the URL the walk fetched. If it does, a claimed sitemap that answers with HTML is silently dropped into failed with source common, so no rule reports it. Consider normalising both sides.
  • (low) decodeSitemapBody fallback via isHtmlContentType: a genuinely broken sitemap served as text/html that opens with <?xml plus a stray element is marked notASitemap and, if guessed, silently dropped. That is probably acceptable, but it is worth a line in the docs.
  • (nit) as RequestInit cast for decompress: a small typed local (RequestInit & { decompress?: boolean }) would keep the cast out of the call site.
  • (nit) The PR notes that apps/cli/src/crawl/sitemaps.ts is a dead legacy copy. Consider a tracking issue for removing it, and for the same eager-decode memory hole in the page fetcher and readBodyCapped.
  • (nit) Bare-year <lastmod> crashing the whole parse is already listed as a follow-up. A tracking issue would keep it from getting lost.

No critical or high findings.

@nc9

nc9 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Second review, fixed in 48c1b42:

  • FETCH_RETURNS_RAW_BODY brittleness: added the fallback. When the declared coding does not decode and the bytes open with markup (after a BOM and whitespace), some hop already decoded them and they are read as they are; a too-large result is never retried. Test covers gzip, br, deflate and zstd headers on plain XML. Behaviour is now pinned too: a real-origin test serves a corrupt Content-Encoding: gzip body and expects Corrupt gzip body: .... With Bun's own decoding that fetch throws ZlibError (checked: {} threw, { decompress: false } returned the 18 raw bytes), so the test fails if Bun stops honouring the option.
  • Claimed-URL normalisation: no change, same reasoning as my previous comment. An index child is fetched under its own raw string, and seen dedupes on raw strings, so a child spelled differently from the guessed location is its own result and stays claimed. Normalising would only matter if the walk deduped on normalised URLs, which it does not.
  • text/html broken sitemap with a non-sitemap root: documented in docs/rules/crawl/sitemap-valid.mdx ("A response labelled text/html that does not parse counts as a page unless it opens with a <urlset> or <sitemapindex> root").
  • Typed local for the request init: done, the as RequestInit cast is gone.
  • Tracking issues: crawl/sitemap-valid: a bare-year <lastmod> fails the whole sitemap with a parse error #687 (bare-year lastmod), chore(cli): remove the dead legacy sitemap walk in apps/cli/src/crawl #688 (dead CLI copy of the walk). The eager-decode hole in the page fetcher and readBodyCapped probes is tracked in the private monorepo, since its impact is on hosted crawls.

@claude

claude Bot commented Oct 12, 2026

Copy link
Copy Markdown

Review of #686 (gzip sitemap decoding, bounded reads, guessed-HTML handling). I read the diff but did not run the tests.

I found nothing blocking. The design is sound: bytes on the wire, each decoded layer and a gzip file inside are all capped at 50 MB, and the error messages are documented and tested.

  • (low) packages/crawler/src/sitemaps.ts, FETCH_RETURNS_RAW_BODY = "Bun" in globalThis: on a non-Bun runtime decompress: false is ignored, so the runtime inflates the whole body before readBodyWithin can stop it. The gzip-bomb protection only holds under Bun. The comment says Bun runs the crawl, so this is acceptable. Consider a note in the docs or an assertion if a Node path is ever supported.
  • (low) inflateWithin uses the sync zlib APIs with a 50 MB output cap. A hostile sitemap can block the event loop for a noticeable time (about 50 MB of inflate or brotli work per fetch). Async gunzip with maxOutputLength would avoid that, though it is probably fine for a CLI.
  • (nit) The behaviour change is documented, but an uncompressed sitemap over 50 MB is now an error where it used to parse. Make sure the CHANGELOG wording is enough for users who hit it.
  • (nit) Accept-Encoding: gzip is sent now. Confirm there is a test that user-supplied headers override it, as the CHANGELOG claims.

@nc9

nc9 commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Third review: no code change, triage below.

  • Non-Bun runtimes: agreed, the bound holds where decompress: false is honoured, which is every place the crawl runs today (the compiled CLI and the Bun-based cloud containers). The code comment on FETCH_RETURNS_RAW_BODY says so; there is no Node path to assert against yet.
  • Sync inflate: bounded at 50 MB of output per sitemap, a few hundred ms at worst, on the same thread that already parses that XML synchronously. Moving to async zlib would not change the bound and is not needed for this fix.
  • Uncompressed sitemaps over 50 MB: the CHANGELOG says it in so many words ("an uncompressed sitemap over 50 MB, which search engines also reject, is now reported the same way instead of parsed"), and the docs list the error with its trigger.
  • Custom headers overriding Accept-Encoding: by construction, the same as the existing User-Agent/Accept defaults: ...originScopedHeaders is spread after the defaults in the request init, and decompress: false stays on regardless, so whatever coding a user asks for is still decoded within the limit (gzip, deflate and br are decoded; others are reported as unsupported).

@claude

claude Bot commented Oct 12, 2026

Copy link
Copy Markdown

Reviewed the sitemaps.ts changes, the docs and the test list. The design is sound: the bounded byte read, the single-coding limit, decompress: false, the linear opening scanner and the claimed-URL gate all hold up. I did not run the tests. I found nothing that should block merge.

  • (low) packages/crawler/src/sitemaps.ts (discoverSitemaps, the claimed set): claimed.has(result.data.url) is an exact string match against raw robots Sitemap: values and raw childSitemaps entries. If the walk normalizes a URL differently (trailing slash, fragment, scheme-less robots entries, a relative <loc>), a declared sitemap that returns HTML would be silently dropped to failed with source common. Normalize both sides, or add a test with a non-canonical declared URL.
  • (low) inflateWithin: the sync gunzipSync/brotliDecompressSync calls block the event loop for up to 50 MB of output. This is acceptable for a CLI but matters for the hosted crawler running many concurrent sitemap fetches. Consider the async zlib API, or note the trade-off.
  • (low) decodeSitemapBody: when a declared Content-Encoding fails to inflate and the body opens with <, it is read as already decoded. This is a reasonable fallback, but it could hide a genuinely mislabelled or corrupt response. A debug log for that path would help diagnose reports.
  • (nit) The code comment on init says a 1 MB bomb cost 1.9 GB, while the PR description and CHANGELOG say 1.39 GB / 1.79 GB. Make the figures consistent.
  • (nit) FETCH_RETURNS_RAW_BODY is evaluated once at module load. That is fine, but it makes the non-Bun path hard to unit test. The tests could cover it through decodeSitemapBody with a null encoding, which seems to be what is done.

Test coverage is thorough: gzip bomb, endless body, corrupt and truncated gzip, br/deflate, redirect headers, catch-all and declared cases, and linear time on 5,000 comments. The CHANGELOG and docs are updated. The PR already flags the apps/cli/src/crawl/sitemaps.ts duplicate (#688) as a follow-up, which is the right call.

@nc9
nc9 merged commit e1eea32 into main Oct 12, 2026
15 checks passed
@nc9
nc9 deleted the fix/sitemaps-662-670 branch October 12, 2026 04:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant