Skip to content

fix(sitemaps): report sitemaps that are not well-formed XML and lastmod values outside W3C Datetime (#760, #781) - #892

Merged
nc9 merged 1 commit into
mainfrom
fix/sitemap-wellformed-lastmod
Oct 16, 2026
Merged

nc9 merged 1 commit into
mainfrom
fix/sitemap-wellformed-lastmod

Conversation

@nc9

@nc9 nc9 commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Two sitemap findings crawl/sitemap-valid missed.

Closes #760, closes #781.

#760: sitemaps that are not well-formed XML (packages/crawler/src/sitemaps.ts)

fast-xml-parser reads past a mismatched closing tag, an element left open and a raw &, so crawl/sitemap-valid passed a file a strict consumer rejects. Once a <urlset> or <sitemapindex> root parses, parseSitemap now also runs the same package's XMLValidator. On failure it records, first among the sitemap's errors:

XML is not well formed: <reason> (line <L>, column <C>)

  • The URLs the parser can read are still read.
  • Positions are in the body as served, including the position inside the validator's own "opened in line … col …" text.
  • Whitespace before <?xml is tolerated. It is outside the spec, but common, and the walk already reads past it.
  • A file with an internal DTD subset (<!DOCTYPE urlset [ … ]>) is not checked. The validator misreads a quoted > in the subset and rejects long entity names, so a valid file would fail. Such files are rare in sitemaps, and the parser refuses declared entities anyway. The subset is found by a linear scan, so a prolog comment cannot hide it and a body of repeated <!DOCTYPE costs its length.
  • In decodeSitemapBody, a body whose only errors are well-formedness errors still counts as a sitemap. Without that, a page-labelled body that opens with another element would have lost its URLs.
  • A raw & in an attribute (an xhtml:link href) is not caught. The validator does not check attribute escapes, and the docs say "element text".
  • Timing is linear in the body: 12.5 / 25 / 50 MB take 168 / 326 / 672 ms for a sitemap-shaped body, and adversarial shapes (deep open tags, many attributes) also double per doubling. A test pins the ratio, and another pins repeated DOCTYPE openers.

Tests are in packages/crawler/tests/sitemap-well-formed-760.test.ts:

  • mismatched closing tag, unclosed element, raw &;
  • positions past leading whitespace and CRLF;
  • the DOCTYPE cases;
  • the page-labelled body;
  • well-formed files that must get no error: CDATA, comments, xhtml:link and image:image, escapes, a BOM, leading whitespace, an index;
  • linearity.

In the #687 test, the raw < cases now also expect the well-formedness error first.

#781: lastmod values outside W3C Datetime (packages/rules/src/crawl/sitemap-valid.ts)

Two new warning checks, each with a sample of URLs and lastmod "<value>" labels (up to 50) and the exact count in total:

  • sitemap-lastmod-format: N sitemap lastmod value(s) are not in W3C Datetime format. Examples: 2024/09/10, 2024-9, 2024-09-31, Tue, 10 Sep 2024 12:00:00 GMT.
  • sitemap-lastmod-zone: N sitemap lastmod datetime(s) have no time zone, for 2024-09-10T12:00:00. It reads as a date, just not in a known zone.

W3C values are not reported: 2024, 2024-09, 2024-09-10, …T12:00Z, …T12:00:00.123+02:00. There is no pass check: one would add a passed check to every audit's score for this rule. So scores for sites without lastmod problems are unchanged, and the codex review measured how a pass check would dilute a failing rule's score. The tests are in packages/rules/tests/sitemap-lastmod-format-781.test.ts.

Docs (docs/rules/crawl/sitemap-valid.mdx): the new error and both checks, with their messages as templates, and how to fix them. CHANGELOG: one Fixed entry and one Added entry.

Checks

  • tsgo --noEmit: packages/crawler and packages/rules are clean.
  • bun test:
    • all 12 packages/crawler/tests/sitemap* files plus the new one: 108 pass;
    • packages/rules sitemap-lastmod-format-781, sitemap-valid and sitemap-caps: 21 pass.
  • bun docs/scripts/check-links.ts and tangly check --strict: pass.
  • Engine golden suite once at this sha: 209 pass, 0 fail.
  • Codex review, three rounds:
    • DOCTYPE false positives: now skipped.
    • The notASitemap interaction: fixed.
    • The attribute & claim: narrowed in the docs.
    • Nested position: fixed.
    • The pass check's score dilution: the check was removed.
    • The prolog-comment scan: fixed.
    • The last round was clean.

Docs: in this PR. Agent skill: no change needed.

@nc9
nc9 marked this pull request as ready for review October 16, 2026 19:39
@claude

claude Bot commented Oct 16, 2026

Copy link
Copy Markdown

Reviewed the diff (well-formedness check in sitemaps.ts, lastmod format checks in sitemap-valid.ts, docs, changelog, tests). I did not run the test suite. I found no blocking problems.

  • (medium) packages/crawler/src/sitemaps.ts (parseSitemap): a sitemap with a raw & in a loc used to pass sitemap-syntax and now fails it. That is correct and the changelog says so, but it will turn previously green audits red for many hand-built sitemaps even though the URLs are still read. Worth confirming that fail severity is intended.
  • (low) packages/rules/src/crawl/sitemap-valid.ts: the two new lastmod checks are warn, so they lower the score on sites whose generator writes one bad format on every URL. The "no pass check" comment explains the choice. A docs note that these warnings can be numerous would help.
  • (low) wellFormednessError: trimStart() also strips a leading BOM. The BOM counts as one column in the reported position, so a BOM on line 1 shifts the column by one versus an editor. Cosmetic.
  • (low) hasInternalSubset also matches a DOCTYPE with [ inside a comment or CDATA, which skips the check for that file. It fails open, so it is acceptable, and the docs already say such files are not checked.
  • (low) tests/sitemap-well-formed-760.test.ts: the "stays linear" test compares wall-clock timings (large < small*8 + 50). The margin is generous, but it can still flake on a busy CI runner.
  • (nit) lastmodProblem trims the value before testing, so a lastmod with surrounding spaces passes. This is lenient, while the docs describe the format strictly.
  • (nit) decodeSitemapBody: "readable" is vacuously true when errors is empty. That is correct, but a short comment would help since it replaces the old errors.length === 0 short-circuit.

Test coverage is good. It covers the mismatched tag, the unclosed element, the raw &, position offsets, DOCTYPE skipping, CDATA and well-formed regressions, the lastmod formats, the no-zone case and sample truncation.

@nc9

nc9 commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

Review triage, no code change:

  • (medium) a raw & in <loc> now fails sitemap-syntax: intended. It is crawl/sitemap-valid passes a sitemap with mismatched or unclosed tags: the parser never checks well-formedness #760's first acceptance criterion, and a raw & makes the file invalid XML that a strict consumer rejects. The URLs are still read, the error names the problem and where it is, and the docs say to escape it as &amp;.
  • (low) the lastmod warnings on every URL: each is one check per rule, not one per URL. It carries the exact count and a 50-item sample, so a generator that writes one bad format costs one warning row.
  • (low) the BOM column: cosmetic, as noted. Lines are exact.
  • (low) a [ inside a commented-out DOCTYPE skips the check: fails open, and the docs say such files are not checked.
  • (low) the timing test: it compares 1 MB against 4 MB, linear is about 4x, and the bound is 8x + 50 ms. Only a stalled runner could flake it.
  • (nit) trimmed lastmod: the parser trims every field (trimValues: true), so the rule never sees the spaces either way.
  • (nit) the readable comment: the line above it says a well-formedness error does not make a parsed sitemap a page, and the empty case is the old short-circuit.

@nc9
nc9 force-pushed the fix/sitemap-wellformed-lastmod branch from 2094a52 to d953b29 Compare October 16, 2026 19:49
@claude

claude Bot commented Oct 16, 2026

Copy link
Copy Markdown

Review of #892 (sitemap well-formedness and lastmod format checks). I read the diff and confirmed that sampleUrlItems, w3cDatePeriod and its ./date-period export, and the fast-xml-parser dependency all exist. I did not run the test suite.

Overall the change looks sound. The parser still reads the URLs, the new error is additive, and decodeSitemapBody handles it correctly. The DOCTYPE scan is linear, and the tests cover the edge cases.

  • (low) packages/crawler/tests/sitemap-well-formed-760.test.ts: the linear-time test compares wall-clock timings (large < small*8+50). It can flake on a busy shared CI runner, which the CHANGELOG already mentions. Consider a larger margin, or a smaller or opt-in perf test.
  • (low) packages/crawler/src/sitemaps.ts parseSitemap: XMLValidator.validate runs as a second full pass over bodies of up to 50 MB, on top of parser.parse. This is linear and acceptable, but it roughly doubles the parse cost for large sitemaps. It is worth a quick benchmark on a near-limit file.
  • (low) packages/rules/src/crawl/sitemap-valid.ts: the new sitemap-lastmod-format and sitemap-lastmod-zone checks are warnings, so existing sites with slash-separated or zoneless lastmods will see score changes. The CHANGELOG covers the feature but doesn't say this explicitly. Consider mentioning it.
  • (nit) sitemap-valid.ts: lastmodProblem trims the value before testing, so 2024-09-10 passes even though the protocol doesn't allow padding. That is a lenient choice and is fine, but a comment would help.
  • (nit) sitemaps.ts: readable in decodeSitemapBody reads more clearly as onlyWellFormednessErrors.

No blocking issues found.

…od values outside W3C Datetime (#760, #781)

parseSitemap runs fast-xml-parser's XMLValidator once a sitemap root parses and records 'XML is not well formed: <reason> (line L, column C)' for a mismatched closing tag, an element left open or a raw & in element text; the URLs it can read are kept, a page-labelled body with only that error stays a sitemap, leading whitespace is tolerated and a file with an internal DTD subset is not checked (the validator misreads those). crawl/sitemap-valid adds sitemap-lastmod-format (not W3C Datetime) and sitemap-lastmod-zone (a datetime with no zone) warnings with sampled URLs and values; no pass check, so scores without lastmod problems are unchanged.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/sitemap-wellformed-lastmod branch from d953b29 to 84e980b Compare October 16, 2026 20:50
@claude

claude Bot commented Oct 16, 2026

Copy link
Copy Markdown

Review of #892 (sitemap well-formedness and lastmod format checks). I read the diff and confirmed that @squirrelscan/utils/date-period exports w3cDatePeriod. I did not run the tests.

  • (low) packages/crawler/tests/sitemap-well-formed-760.test.ts: the linear-time test compares wall-clock timings (large < small*8 + 50) on a 4 MB body, so it may flake on a loaded CI runner. Consider a wider margin or a larger iteration count. The test is also useful as written.
  • (low) packages/crawler/src/sitemaps.ts, hasInternalSubset: it skips the whole check whenever a <!DOCTYPE ... [ appears anywhere in the body, including inside a comment or CDATA section after the prolog. A malformed file could avoid the check this way. This only produces a false negative, so the impact is small. A scan limited to the prolog would be tighter.
  • (nit) sitemaps.ts, decodeSitemapBody: readable relies on matching an error string prefix. A structured error kind would be sturdier than startsWith.
  • (nit) sitemap-valid.ts: the lastmod checks only cover <url> entries. Lastmods on <sitemap> entries in an index are not checked. Worth a note in the docs, or a follow-up.

I found no correctness, security or performance problems that should block the merge. Docs, changelog and tests cover the new behaviour.

@nc9
nc9 merged commit 4bf7d3c into main Oct 16, 2026
15 checks passed
@nc9
nc9 deleted the fix/sitemap-wellformed-lastmod branch October 16, 2026 21:04
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