Skip to content

fix(crawler): read a sitemap whose root is prefixed with the sitemap namespace #759 - #792

Merged
nc9 merged 2 commits into
mainfrom
fix/prefixed-sitemap-root-759
Oct 15, 2026
Merged

nc9 merged 2 commits into
mainfrom
fix/prefixed-sitemap-root-759

Conversation

@nc9

@nc9 nc9 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Closes #759.

What was wrong

parseSitemap looked for a root element named exactly urlset or sitemapindex. A sitemap that binds the sitemap namespace to a prefix, such as <ns0:urlset xmlns:ns0="http://www.sitemaps.org/schemas/sitemap/0.9"><ns0:url><ns0:loc>…, was reported by crawl/sitemap-valid as Unknown sitemap format, and none of its URLs were read. Python's ElementTree writes this form by default when a namespace is registered without an empty prefix, and it is valid XML in the sitemap namespace. The body sniffing in decodeSitemapBody already accepted the prefixed root, so the file was treated as a sitemap and then failed to parse as one.

The fix

When the parsed root is <prefix>:urlset or <prefix>:sitemapindex, and that element binds xmlns:<prefix> to a sitemap namespace, the prefix is taken off every element name in the tree before the existing reader runs.

  • The accepted namespaces are the protocol's 0.9 namespace over http or https, and Google's older 0.84 and 0.9 namespaces, which old generators still write.
  • Attributes and elements in other prefixes, such as news:, are left alone. So xmlns:news still marks a news sitemap.
  • A root whose prefix is bound to any other namespace, or to none, is left as it is and stays Unknown sitemap format.

The unprefixed form is unchanged.

Tests

packages/crawler/tests/sitemap-prefixed-root-759.test.ts:

  • a prefixed urlset reads the same as its unprefixed copy (URLs, lastmod, changefreq, priority, entity decoding);
  • a prefixed sitemap index lists its children;
  • a prefixed news sitemap is still news;
  • three foreign-namespace or unbound prefixed roots stay Unknown sitemap format;
  • the fetched-body path (decodeSitemapBody) reads a prefixed sitemap.

Four of the five fail on main. Also passing: sitemap-xml-escapes, sitemap-field-text-687, sitemap-news-namespace, sitemap-gzip, sitemap-entity-expansion, sitemap-guessed-html, sitemap-gate, and rules sitemap-valid. tsgo passes in crawler, tangly check --strict passes, and detect-secrets is clean.

Docs and skill

rules/crawl/sitemap-valid now says when a prefixed root counts. There's a CHANGELOG entry under Fixed. The squirrelscan agent skill needs no change.

@claude

claude Bot commented Oct 15, 2026

Copy link
Copy Markdown

Reviewed the diff. I didn't run the tests; I read the code and the new test file.

The fix looks correct and well scoped. It only strips prefixes when the root's own xmlns:<prefix> is bound to a known sitemap namespace. An unprefixed root takes the old path unchanged. The __proto__ guard and the repeated-element handling are covered by tests.

  • (low) packages/crawler/src/sitemaps.ts withoutElementPrefix: it recurses over the whole parsed tree with no depth guard. A pathologically deep document could overflow the stack. The try around it should turn that into a parse error rather than a crash, but it is worth confirming that the parser's own limits make this unreachable.
  • (low) withoutElementPrefix: it strips the prefix from every nested element, so a descendant that rebinds the same prefix to a different namespace (<ns0:foo xmlns:ns0="other">) would be treated as a sitemap element. This is very unlikely in practice. A one-line comment noting the assumption would be enough.
  • (nit) isNewsSitemap: the new !SITEMAP_NAMESPACES.has(...) check also changes behaviour for an unprefixed root that declares xmlns:news as a sitemap namespace. That seems right, but the comment only mentions the prefixed-root case.
  • (nit) Test coverage is good. A test for a prefixed index containing both prefixed and unprefixed children, and one for the #text and attribute pass-through, would round it out.

I found no blocking issues.

nc9 added 2 commits October 15, 2026 14:52
…namespace #759

A urlset or sitemapindex written as <ns0:urlset xmlns:ns0="...sitemap/0.9">,
as Python's ElementTree writes one, read as "Unknown sitemap format" and lost
every URL. When the root's prefix is bound to a sitemap namespace, strip that
prefix from the element names before reading; a prefix bound to any other
namespace stays an unknown format.

Signed-off-by: Nik Cubrilovic <[email protected]>
… second root #759

Review follow-up. fast-xml-parser refuses <__proto__> but not
<ns0:__proto__>, so stripping the prefix assigned __proto__ on the copy:
refuse it the same way. An element written both prefixed and not now stays
a repeat (flagged as one) instead of the last one winning. An unprefixed
root is read as before when a prefixed one sits beside it, and a root
prefixed news: bound to the sitemap namespace is not a news sitemap.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/prefixed-sitemap-root-759 branch from 33a1ae0 to 75ac5e1 Compare October 15, 2026 03:53
@claude

claude Bot commented Oct 15, 2026

Copy link
Copy Markdown

Reviewed the diff for sitemaps.ts, the tests, the docs and the changelog. I didn't run the test suite.

I found no blocking problems. The fix is narrow and well scoped, and the tests cover the edge cases.

  • (low) sitemaps.ts withoutElementPrefix: this recurses over the whole parsed tree and copies it, so memory roughly doubles for very large prefixed sitemaps, such as 50k URLs. That is acceptable. If the parser has no depth limit, deeply nested input could in theory hit the recursion limit, though the existing parse would likely fail first. Consider confirming the parser's nesting bound.
  • (low) isNewsSitemap: the new check treats xmlns:news as news only when it is not bound to a sitemap namespace. This is correct for the news:urlset case. A comment saying why the check exists would help, because it reads oddly next to the previous namespace-presence logic.
  • (nit) In the .flat() merge of <ns0:loc> and <loc>, [out[name], stripped].flat() flattens one level only. That is fine for the current cases, but a short comment would make the intent clear.
  • (nit) Test coverage is good. You could add one case for a prefixed root that has attributes on child elements, to show that @_ keys survive stripping.

@nc9
nc9 merged commit ea7acb0 into main Oct 15, 2026
15 checks passed
@nc9
nc9 deleted the fix/prefixed-sitemap-root-759 branch October 15, 2026 04:12
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.

crawl/sitemap-valid: a sitemap with a namespace-prefixed root (ns0:urlset) is reported as Unknown sitemap format

1 participant