fix(utils): prevent sitemap metadata leak across url without loc#3795
Merged
Conversation
A <url> with no <loc> is dropped, but SitemapXmlParser only reset its url buffer when loc was present, so its lastmod/priority/changefreq bled into the next emitted url. Reset the buffer on every </url>, and only set lastmod when the date is valid so junk values no longer emit Invalid Date.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
SitemapXmlParserinpackages/utils/src/internals/sitemap.tscould leak per-url metadata from a malformed<url>block into the next one, and emittedInvalid Datefor unparseable<lastmod>values. This fixes both.Why
Metadata leak. A
<url>with no<loc>is correctly dropped, butonCloseTagonly reset thethis.urlbuffer whenlocwas present. So a loc-less<url>that carriedlastmod/priority/changefreqleft those values in the buffer, and they bled onto the next emitted url:Invalid lastmod.
this.url.lastmod = new Date(text)was set unconditionally, so junk like<lastmod>not-a-date</lastmod>produced anInvalid Date(its.getTime()isNaNand consumers calling.toISOString()throw). Nowlastmodis only set when the parsed date is valid, mirroring howchangefreqis already enum-guarded.Well-formed sitemaps are unaffected; the buffer was already cleared for valid urls.
Testing
Added one offline regression test to
packages/utils/test/sitemap.test.tsusing a{ type: 'raw' }urlset (no network): a url with an invalidlastmod, a loc-less url carryinglastmod/priority, then a bare url. It asserts only the two loc'd urls are emitted, the invalidlastmodis dropped, and nolastmod/priorityleaks onto the following url. Fails onmaster, passes with the fix. Fullsitemapsuite: 30/30.yarn lintandtsc -p packages/utils --noEmitclean.Note
This mirrors the same metadata-leak fix I opened against the sibling crawlee-python repo (apify/crawlee-python#1992); the TS parser additionally needed the
lastmodvalidity guard. Happy to file a tracking issue if you'd prefer one.