Skip to content

fix(crawler): carry the Content-Type charset to rules so core/charset sees header declarations #674 - #883

Merged
nc9 merged 1 commit into
mainfrom
fix/674-content-type-charset
Oct 17, 2026
Merged

nc9 merged 1 commit into
mainfrom
fix/674-content-type-charset

Conversation

@nc9

@nc9 nc9 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Closes #674
Closes #659

#659's only open part was the header-declared case, blocked on #674 per its 2026-10-12 release check. Its placement advice, 1024-byte check and conflict warning shipped in pub#676 (core-charset.test.ts 23/23 on main), and this PR's e2e shows the header-only page passing in a real crawl.

What changed

  • ResponseHeaders (core-contracts) gains an optional contentTypeCharset. contentType stays the lowercased media type, so nothing comparing it changes.
  • contentTypeCharset() (crawler fetcher.ts) reads the charset parameter from the raw header: split on ; outside quoted strings, quoted value unescaped, only an RFC 9110 token of at most 40 characters counts.
  • The crawler sets it on every fetched page record. Both fetch paths (plain HTTP and cloud render, which uses Playwright's raw response headers) keep the raw Content-Type in result.headers.
  • An incremental crawl that fetches an unchanged body (hash_match reuse) refreshes the charset from the response just fetched, so old records pick it up and a charset-only change is not lost.
  • buildHeadersMap rejoins them as text/html; charset=<label> for rules. A record without the key reads as before: no charset.

Real CLI audit (before / after)

Local server: / sends text/html; charset=utf-8 with no meta, /conflict sends text/html; charset=iso-8859-1 with <meta charset="utf-8">. squirrel audit http://localhost:<port>/ -m 5 --http --offline --disable-discovery-probes --rule-include core/charset -f json, fresh HOME and project dir each run:

  • origin/main: fail No charset declaration found on /, nothing on /conflict
  • this branch: / passes, warn Conflicting charset declarations: header iso-8859-1, page utf-8 on /conflict

Cloud

The checkpoint stores headers as a record of unknown values, so cloud audits get this when the gitlink moves. The API's responseHeadersSchema strips unknown keys from published reports; follow-up squirrelscan/repo#2947 (does not affect rule results).

Acceptance criteria

  • Header-only UTF-8 passes core/charset in a real CLI audit
  • Header iso-8859-1 vs meta utf-8 reported in a real audit
  • Charset reaches rules through page headers for every fetcher (HTTP, cloud render, incremental reuse)
  • Records without the field load and read as today (build-headers-map.test.ts)
  • A test runs the real fetcher, store and rule path: charset-header-674-e2e.test.ts (local Bun server -> createCrawler -> SQLite -> v1 and v2 rule engines), plus an incremental hash-match case
  • Skill: no change needed (the skill links to the rule page).

Tests

  • packages/audit-engine/tests/charset-header-674-e2e.test.ts (fails on origin/main's crawler; the incremental case fails without the reuse refresh)
  • packages/crawler/tests/content-type-charset-674.test.ts
  • packages/audit-engine/tests/build-headers-map.test.ts

Typecheck (crawler, audit-engine, core-contracts), oxlint on changed files, golden suite (209/210; the one failure was a 180 s timeout in rule-cache-replay-golden under load, and that file passes alone, 11/11 in 15 s), codex review (2 rounds; P2 hash-match reuse and P3 quoted parameters fixed).

@nc9
nc9 force-pushed the fix/674-content-type-charset branch from 7d13281 to dfc56ad Compare October 17, 2026 04:35
@nc9
nc9 marked this pull request as ready for review October 17, 2026 04:35
@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Reviewed the diff. The change is small and well tested: the header charset is stored beside the media type, stored as JSON so there's no schema migration, and it's re-joined in buildHeadersMap. I found no blocking problems.

  • (low) docs/rules/core/charset.mdx and the CHANGELOG say the charset arrives "plain HTTP or cloud browser rendering", but the e2e test only covers the plain Bun.serve fetch path. I only checked the crawler.ts call site, which reads result.headers.contentType. I did not check that the browser-rendering fetch path fills in result.headers.contentType with the raw header, so please confirm that or add a test.
  • (low) packages/crawler/src/fetcher.ts contentTypeCharset: a charset longer than 40 characters, or one that isn't a valid token, returns null. core/charset then reports "No charset declaration found" even though the header declared something. This is an edge case and probably acceptable, but the comment could say so.
  • (nit) In the same function, the escape handling in the splitter keeps the backslash and the next character, and the later regex strips them. This is correct but done in two places, so a short comment would help.
  • (nit) charset-header-674-e2e.test.ts starts a new crawler and server for each engine in audit(), and the separate "store keeps…" test runs audit again. It's fine, but you could share one run per engine to speed up the suite.

Test coverage is good: there are unit tests for the parser, buildHeadersMap tests, v1 and v2 e2e tests, and an incremental-reuse test.

@nc9

nc9 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Review triage (dfc56ad):

  • (low) cloud browser rendering: confirmed. Both fetch paths in packages/crawler/src/fetcher.ts build result.headers with extractResponseHeaders, which keeps the raw content-type value. On the cloud render path that is extractResponseHeaders(headersFromRecord(response.headers)), and the render service passes on the main response's headers as sent. Only result.contentType is normalised to the media type, so the charset survives on both paths.
  • (low) labels over 40 characters or not a token: intended. They read as no charset, which is the behaviour before this change. No browser label is that long.
  • (nits) escape comment and shared e2e runs: leaving as is.

The branch needs a rebase over the v0.0.117 release cut; that is next in my queue.

… sees header declarations #674

New optional ResponseHeaders.contentTypeCharset, parsed quote-aware from the
raw header and stored beside the media type; buildHeadersMap rejoins them.
An incremental hash-match reuse refreshes it from the fetched response. Old
records without the key read as before.

Signed-off-by: Nik Cubrilovic <[email protected]>
@nc9
nc9 force-pushed the fix/674-content-type-charset branch from dfc56ad to d64473e Compare October 17, 2026 09:13
@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Review of #883 (pass the Content-Type charset through to core/charset).

I read the diff and traced both fetch paths: plain HTTP and cloud render. Both build headers with extractResponseHeaders, so the raw header reaches the crawler. I did not run the tests.

  • (nit) packages/crawler/src/fetcher.ts contentTypeCharset: the hand-rolled quote-aware splitter is correct and well tested. If you want less code, a single regex could cover the common cases. The explicit RFC 9110 handling is fine as written, so this is optional.
  • (nit) packages/audit-engine/src/adapter.ts buildHeadersMap: the charset is re-joined as ; charset=X. Any other rule that reads content-type for the media type should split on ;. I did not audit them all. A quick grep for strict equality comparisons against content-type would settle it.
  • (low) The CHANGELOG and docs say a page stored by an older version is read without the header charset until it is refetched. That limitation is documented, and the incremental hash-match path correctly refreshes the charset. No change needed.

Test coverage is good: unit tests for the parser, buildHeadersMap tests, and v1 and v2 end-to-end tests that include the incremental-refetch case. No security or performance concerns. The charset label is capped at 40 token characters and is not executed.

No blocking issues found.

@nc9
nc9 merged commit 0bcdd93 into main Oct 17, 2026
15 checks passed
@nc9
nc9 deleted the fix/674-content-type-charset branch October 17, 2026 09:43
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