Skip to content

fix(rules): read sitemap and page dates as periods, the same on every machine #772 - #780

Merged
nc9 merged 4 commits into
mainfrom
fix/partial-dates-772
Oct 15, 2026
Merged

nc9 merged 4 commits into
mainfrom
fix/partial-dates-772

Conversation

@nc9

@nc9 nc9 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #772.

What was wrong

#771 taught the sitemap lastmod rules that 2024 and 2024-09 name a whole year or month. Five related misreadings were left:

  1. Cloaking probe recency. The probe read a lastmod with Date.parse, so 2026 was 1 January and a page stamped with the current year was never "recent" (287 days against the default 14), while 2026-10 was.
  2. Drift's page-side date. crawl/sitemap-lastmod-drift still read the page's own date as an instant: a schema dateModified: "2024" warned 253 days ahead of a lastmod of 2024-09-10.
  3. Local-time fallback. Values outside the W3C forms (2024-9, 2024-09-1, 2024/09, and a datetime with no zone) fell through to new Date, which reads them at the host's local time. The result depended on the machine.
  4. Churn's day. crawl/sitemap-lastmod-churn counted the UTC day of an offset datetime: 2024-09-10T23:30:00-05:00 counted as the 11th, a day the sitemap never wrote.
  5. Day vs month. A date-only lastmod was its first instant, so against a page date of 2025-01-08T12:00Z, 2024-12-31 warned 8 days behind while 2024-12 passed at 7.

The fix

The period helper moves from the rules package to @squirrelscan/utils/date-period, so the engine's cloaking probe can share it. It now has two functions:

  • datePeriod, used for every date these checks read: a sitemap lastmod, a schema dateModified, a visible date.
    • A W3C Datetime is read exactly:
      • a date-only value is the whole day;
      • a datetime is an instant, and day keeps the calendar day it was written on, whatever its offset;
      • a datetime with no zone, or with a space for the T, is read in UTC.
    • A value in another format is read as written:
      • a comment in parentheses is ignored;
      • a zone at the end (-0500, GMT-05:00, +2, GMT, EST, a trailing Z) is split off;
      • the zoneless remainder is read through Date's local fields and then as UTC, minus the zone's offset when there is one. No path trusts Date's own zone handling, so the result does not depend on the machine.
    • YYYY/MM and September 2024 are months; a value with no time is a day.
    • Null for:
      • an ISO-shaped value the W3C forms reject (2024-9, 2024-09-31, +002024-09-10), which Date reads sometimes in UTC and sometimes at local time;
      • a value that does not write a whole date (Tuesday 2024), whose missing fields Date would invent.
  • w3cDatePeriod: the W3C forms only, for a future check that reports non-W3C lastmods.
  • periodGapMs: 0 when two periods overlap, else the time between their nearer ends.

Callers:

  • Drift reads both dates with datePeriod and measures the gap between the two periods.
  • Churn counts period.day; a bare year or month is still "coarse".
  • The cloaking probe's parseLastmod(value, now) returns the end of the lastmod's period, capped at now; selection uses the injected now. Recent pages are ranked by the period's start, so a page dated to the day outranks one stamped only with the year.

One consequence of item 5 is that a date-only lastmod is measured from the end of its day. An existing drift test (1 March lastmod, page dated 9 March) moved from 8 days to 7, so it now uses 10 March to stay over the threshold.

Tests

  • packages/utils/tests/date-period.test.ts (new). Every case is checked under TZ = UTC, America/New_York, Asia/Tokyo and Pacific/Kiritimati. The zone is restored by assignment, because Bun keeps the last zone once TZ is deleted, and a first test asserts that each switch takes effect. Cases:
    • day periods and invalid dates;
    • offset datetimes keeping their written day;
    • zone-less datetimes;
    • zoned values in other formats, comments included;
    • zone-less values in other formats;
    • month forms;
    • the null cases;
    • periodGapMs.
  • packages/rules/tests/sitemap-lastmod-partial-dates-758.test.ts:
    • a page dated 2024 agrees with a lastmod inside it, and is measured from the end of its year;
    • 2024-12-31 and 2024-12 agree;
    • 2024/09/10 and Tue, 10 Sep 2024 lastmods agree with a page dated the 10th, in every zone;
    • ISO-shaped non-W3C lastmods are not compared;
    • churn reports 2024-09-10 for 2024-09-10T23:30:00-05:00, and for 30 2024/09/10 lastmods in every zone.
  • packages/audit-engine/tests/cloaking-probe.test.ts:
    • parseLastmod for a day, an RFC date, and periods capped at now;
    • the current year and month are recent; an earlier month is recent only when its end is within recent_days; a future year is not;
    • precise dates outrank year stamps.

Also run: rules sitemap-lastmod-drift, sitemap-lastmod-churn and engine streaming-rules-canonical-golden, all passing. golden-baseline timed out locally under a load average of about 15. That test checks determinism, not a stored snapshot, and the synthetic fixture has no lastmods. tsgo passes in utils, rules and audit-engine, detect-secrets passes on the changed files, and tangly check --strict passes on the docs.

Docs

  • rules/crawl/sitemap-lastmod-drift:
    • date-only values and page dates are periods, with an example of the day count;
    • zone-less datetimes are read in UTC;
    • other formats are read as written, in their zone or UTC, and ISO-shaped non-W3C values are left out;
    • the item label is now quoted as its template.
  • rules/crawl/sitemap-lastmod-churn: a lastmod counts on the day it was written, whatever its offset or format.
  • rules/integrity/cloaking: how a coarse lastmod counts as recent.

CHANGELOG entry under Fixed.

Agent skill

The squirrelscan agent skill needs no change.

Review follow-up (4038220, rebased on v0.0.113 main)

  • ReDoS: the parenthesised-comment pattern rescanned the rest of a value from every unclosed (, which took 2.9 s on page-controlled text. Comments are now stripped in one pass, and datePeriod reads nothing over 200 characters. A test times 8 hostile shapes at 50k, 100k and 200k characters.
  • Zones: a zone that isn't at the end (Sep 10 2024 EST 12:00, 12:00 Sep 10 2024 +0200), or a second zone, made Date's reading depend on the machine. These are now refused.
  • Refused values:
    • an offset no zone has (+99, -0560);
    • a value with no four-digit year (Sep 10 12:00: a time's numbers no longer count);
    • a day Date rolled over (2024/09/31, Feb 30, 2024);
    • a word that only starts like a month (Junk 2024).
  • Churn: crawl/sitemap-lastmod-churn counts the lastmods it can't read and skips with N sitemap lastmod value(s) are not dates squirrel can read the same way on every machine; M name a day, too few to judge lastmod churn instead of Only 0 sitemap URL(s) carry a lastmod.

Comment thread packages/utils/src/date-period.ts Fixed
Comment thread packages/utils/src/date-period.ts Fixed
@nc9
nc9 force-pushed the fix/partial-dates-772 branch from 1dcca98 to 4038220 Compare October 15, 2026 01:23
@nc9
nc9 marked this pull request as ready for review October 15, 2026 01:32
@claude

claude Bot commented Oct 15, 2026

Copy link
Copy Markdown

Review of #780 (shared datePeriod in utils; drift, churn and cloaking-probe moved onto it).

The refactor is sound. The DatePeriod model is applied consistently, and periodGapMs replaces the hand-rolled gap logic cleanly. The input-length cap, the single-pass comment stripping and the "no lookbehind" note show care about ReDoS. There are tests for the new module and for each rule. I found nothing that should block merge.

  • (medium) packages/utils/src/date-period.ts (writtenForm): the "same on every machine" claim doesn't hold for zoneless non-ISO values that land in a local DST gap. Examples are 2024/03/10 02:30 in a US zone, or 2024/03/31 01:30 in Europe. new Date(value) shifts the hour, and the local fields are then re-read as UTC, so the result differs by host. Fix: parse the numeric fields from the string rather than round-tripping through local Date getters. Otherwise, add a test run under several TZ values (e.g. America/New_York, Europe/London, UTC) and document the limitation.
  • (low) writtenForm still depends on the host's Date parser for non-W3C formats (V8 and JSC differ on quirks). The heuristic checks (month/day must be among the written numbers, a four-digit year must be present) cut most of that risk. A table-driven test with a TZ matrix would lock the behaviour in.
  • (low) packages/audit-engine/src/cloaking-probe.ts: selectSuspiciousPaths calls datePeriod(c.lastmod) a second time, using !, after parseLastmod has already parsed it. Carrying the period through once would avoid the double parse and the non-null assertion. Parsing cost is small, so this is minor.
  • (low) parseLastmod is exported and its signature changed (the optional now argument is added). Check there are no other callers that relied on instant semantics. Those callers are mainly tests.
  • (nit) w3cDatePeriod is still exported from utils. If it has no non-test callers now, consider dropping it or noting its intended consumer (e.g. sitemap-valid).
  • (nit) The comment in sitemap-lastmod-churn.ts says (#780), but the CHANGELOG entry references Partial and offset dates elsewhere: cloaking-probe recency, page-side dates in lastmod drift, local-time fallback, churn's UTC day #772. Make sure the issue/PR numbers are what you intend.
  • (nit) The CHANGELOG entry is a single very long paragraph. Splitting it into a few bullets would be easier to read.

Test coverage looks good for the new behaviour. Adding a DST-gap case would cover the medium finding.

nc9 added 4 commits October 15, 2026 12:51
… machine #772

Move the period helper to @squirrelscan/utils/date-period and widen it: a
date-only value is its whole day and keeps the day it was written on; a
datetime with no zone is read in UTC; a lastmod outside the W3C format is
read only when it names its zone. looseDatePeriod reads a page's own date
the same way, with zone-less text read as written in UTC. Drift measures
the gap between the page's period and the lastmod's, churn counts the day
as written, and the cloaking probe measures recency from the end of the
period, capped at now.

Signed-off-by: Nik Cubrilovic <[email protected]>
Review follow-up. Bun keeps the last zone when TZ is deleted, so the tests
mostly ran in one zone and leaked it: restore by assignment and assert the
switch. datePeriod replaces looseDatePeriod for lastmods too: a zone is
found after stripping comments (AM/PM and short offsets allowed) and the
zoneless remainder is read as written, so no fallback trusts Date's own
zone handling; ISO-shaped values the W3C forms reject, and values that do
not write a whole date, are null; YYYY/MM and "September 2024" are months.
w3cDatePeriod is now W3C-only. Cloaking ranks recent pages by period
start.

Signed-off-by: Nik Cubrilovic <[email protected]>
…eads per machine #772

Delta review. The parenthesised-comment pattern rescanned the rest of a
value from every unclosed "(" (2.9 s on page-controlled text): strip
comments in one pass and read nothing over 200 characters. A zone that is
not at the end, or a second zone, made Date's reading depend on the
machine: refuse it. Refuse an offset no zone has, a value with no
four-digit year (a time's numbers no longer count), a day Date rolled over
(2024/09/31), and a word that only starts like a month (Junk 2024). Churn
counts the lastmods it cannot read and says so.

Signed-off-by: Nik Cubrilovic <[email protected]>
…he zone patterns #772

Delta review. Date still applied a zone wrapped in punctuation ("EST",
EST;, ,+0200) or an offset glued to a time mid-value (12:00+0200 Sep 10
2024), so 324 of 870 generated variants read differently per machine:
match a zone token by its neighbours not being letters or digits, and an
offset glued to a time. The lookbehinds ran in JSC's slow interpreter,
quadratic even under the 200-char cap: anchor the zone patterns without
them. Churn's "Only N" now counts unreadable lastmods, news sitemaps'
excluded; its docs give the unreadable skip its own bullet.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/partial-dates-772 branch from 56ad20c to fd59ffb Compare October 15, 2026 01:51
@claude

claude Bot commented Oct 15, 2026

Copy link
Copy Markdown

Reviewed the diff (utils/date-period, cloaking probe, drift/churn callers). I found no blocking problems.

  • (low) packages/utils/src/date-period.ts (writtenForm): the fallback reads new Date(value) through local getters and then re-reads those fields as UTC. On a host whose zone has a DST gap, a non-W3C zoneless value that falls inside the gap (for example 2024/03/10 02:30 in a US zone) is shifted by an hour. That breaks the "same on every machine" claim for that narrow case. A test run under TZ=America/New_York with such a value would confirm it. A hand-rolled parse of the numeric fields would remove the dependence.
  • (low) packages/audit-engine/src/cloaking-probe.ts (selectSuspiciousPaths): datePeriod(c.lastmod)! is parsed a second time just to get rankMs. Returning the period from one helper would avoid the repeat parse and the non-null assertion.
  • (nit) parseLastmod now has an optional now parameter defaulting to Date.now(), and it is exported from audit-engine/src/index.ts. Existing external callers keep working. A comment noting the default is only for convenience would help, since the selection path always passes opts.now.
  • (nit) The ZONE_TOKEN, NUMERIC_ZONE and NAMED_ZONE regexes are dense. A short table of accepted and rejected examples in the tests, or a comment next to each regex, would make later edits safer. The 200-character cap on input is good.

Tests cover the new helper, the probe and the drift/churn rules. I did not run the suite here.

@nc9
nc9 merged commit d0b993b into main Oct 15, 2026
15 checks passed
@nc9
nc9 deleted the fix/partial-dates-772 branch October 15, 2026 03:51
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.

Partial and offset dates elsewhere: cloaking-probe recency, page-side dates in lastmod drift, local-time fallback, churn's UTC day

2 participants