Skip to content

test(core): keep HTML policy fixtures in sync - #47

Merged
allisonmahmood merged 2 commits into
mainfrom
test/html-policy-fixtures
Aug 27, 2026
Merged

allisonmahmood merged 2 commits into
mainfrom
test/html-policy-fixtures

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Aug 27, 2026 •

Copy link
Copy Markdown
Owner

The HTML policy's hand-written cases left individual blocked tags and protocols unverified, so the policy could change without a representative page. Server tests and local seed data also had no shared set of realistic examples.

This introduces one real-shaped accept/reject corpus in packages/core. The core tables require a fixture for every exported blocklist entry, while the server upload tests and seed:dev command consume the same corpus.

Closes #40

Verification: pnpm test (454 tests), affected-package typechecks, pnpm lint, and a local seed:dev smoke run.

Implemented with OpenAI Codex GPT-5.6-sol in the Oh My Pi harness.

@github-actions github-actions Bot added area:server Touches the server area area:ci CI, shared testing infrastructure, development scripts and tooling area:core Touches the core area area:docs Touches the docs area size:m 100-500 changed lines labels Aug 27, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Two-axis review of origin/main...HEAD (2a17e63), plus the extra axes asked for on this pass (issue #40 intent, implementation method, YAGNI). Six independent reviewers; findings are not merged across axes.

Standards

No hard breach of AGENTS.md/CLAUDE.md (review notes stay off-tree), eslint/prettier, core files: ["dist"], tsconfig (rootDir/include src only — fixtures stay out of dist), or it.each usage.

Convention, not a written rule — judgement: apps/server/src/html-fixtures.test.ts imports ../../../packages/core/fixtures/corpus.mjs instead of @patchy/core. Production code uses workspace names; packages/core/package.json exports only ".". Cross-tree relative imports already exist for test helpers (apps/server/src/app.postgres.test.ts → ../../../test/postgres.js). Gap vs postgres: this reaches a package-internal path, not a root test/ module, so it bypasses the export map.

Hand-written packages/core/fixtures/corpus.d.mts has no repo precedent; corpus.mjs is untyped JS (kind untyped). Sidecar types are only for TS importers (seed-dev.mjs gets none). Not a hard rule.

Exporting BLOCKED_TAGS / BLOCKED_PROTOCOLS from html-policy.ts leaks through existing export * on @patchy/core. Kernel public surface grows for tests only (docs/agents/domain.md: core changes ripple both ways). Tests already import ./html-policy.js; production callers do not need the lists.

Baseline smells (all judgement)

Divergent change — packages/core/src/html-policy.ts: policy enforcement plus a test catalog. Set.has became .some() so it.each can iterate; URL_ATTRS stays a Set.

export const BLOCKED_TAGS = [ ... ] as const;
if (BLOCKED_TAGS.some((blockedTag) => blockedTag === tagName)) {

Speculative generality — those exports (and the fixtureKinds TypeError in corpus.mjs) exist for tests/seed, not production.

Feature envy / mysterious name — html-policy.test.ts rebuilds corpus filenames from policy data (blocked-tag-${tagName}.html, protocol.slice(0, -1) to drop the colon).

Duplicated code — same file then it.each(rejectFixtures) asserting ok === false after the tag/protocol cases already rejected those files.


Spec

Against issue #40: table-test the HTML policy with shared accept/reject fixtures.

Do: ``it.each over the exported blocklists so every entry has a case. · `fixtures/accept/` and `fixtures/reject/` real-shaped pages in `packages/core`, shared by the server tests and the dev seed script.

Done when: Adding a blocklist entry without a fixture fails, and the server and seed use the same fixture corpus.

(a) Missing / partial

  • “it.each over the exported blocklists so every entry has a case.” Tags and protocols are table-tested. Other policy rules (srcdoc, on*, meta refresh, unsafe CSS) are not exported lists, so they only ride the generic rejects $filename table (ok === false). Those entries are not one-by-one cases.

  • “the port carries the HTML policy faithfully; table tests and shared fixtures are what make ‘faithfully’ checkable.” Accept/reject corpus exists, but reject coverage for non-blocklist rules does not pin an error string. Protocol rows all expect the same 'Blocked unsafe URL in "href" attribute.' and never assert the iterated protocol appears in the HTML, so a misnamed/miswritten protocol fixture still passes.

  • “Done when: … the server and seed use the same fixture corpus.” Both call readFixtureCorpus from packages/core/fixtures. Seed only uploads accept/. Sharing the reader matches the problem statement; the full accept+reject corpus is not what seed uses.

(b) Not asked for

  • Public package API: BLOCKED_TAGS / BLOCKED_PROTOCOLS were private on main. Exporting them (plus Set → array / .some) is how it.each is fed; export * from "./html-policy.js" also leaks them from @patchy/core. Spec asked for exported lists for tests, not a public-API change.

  • Dropped handwritten coverage: main’s java\nscript: whitespace-smuggling case is gone. Spec asked to replace hand-written cases with table tests over blocklists, not to drop the normalization check.

  • Extra: docs/DEVELOPMENT.md pnpm seed:dev note; prettier-only wraps in validateHtml.

(c) Looks done, implementation wrong

  • “Adding a blocklist entry without a fixture fails.” True for tags (blocked-tag-${tagName}.html). Protocols use blocked-protocol-${protocol.slice(0,-1)}.html. That only matches names that end in :; a new protocol without a trailing colon looks up a truncated filename.

  • “fixtures/reject/ real-shaped pages” / faithfully checkable: iframe-srcdoc.html contains <iframe>, so it still rejects via BLOCKED_TAGS if srcdoc checking is deleted. inline-event-handler.html, meta-refresh.html, and unsafe-inline-css.html are isolated; srcdoc is not.

  • Whitespace-stripping ([\u0000-\u0020]) is still in the policy; no fixture exercises it after the old test was removed.


Implementation method

The ticket asked to it.each exported blocklists. The implementation followed that literally and warped the policy API to make the table work. Shared corpus + one reject file per tag/protocol is the sound part. Filename magic, public lists, and Set → .some() are accidental complexity.

Filename + exported constants. it.each(BLOCKED_TAGS) + filename === `blocked-tag-${tagName}.html` (and protocol.slice(0, -1) for javascript: → blocked-protocol-javascript.html) does fail if you add a tag/protocol and skip the file. It also fails if you rename a file. That is a hidden contract, not a table. The lists were not public on main; export * now ships them from @patchy/core. Tests should not dictate the package surface.

Better method. Keep Sets private. Drive coverage from fixtures (or a tiny manifest: file → { tag } | { protocol } | { check }), then assert every list entry is cited. If you still want it.each, it.each([...BLOCKED_TAGS]) on a non-exported Set, or a lists module not re-exported from index.ts. Invert is better than encoding policy in filenames.

Array + .some() in walk(). On main this was BLOCKED_TAGS.has(tagName). Eight tags / three protocols: no hot-path win, only it.each. URL_ATTRS stayed a Set. Keep Set.has in the walker.

“Real-shaped.” Accept pages are small static sites. Reject pages are the same skeleton plus one pill. Fine as cases; they are not representative published HTML. Protocol tables only assert href, not src / action / formaction / poster / xlink:href. Lost: the whitespace-split java\nscript: case that actually exercised replace(/[\u0000-\u0020]+/g, ""). Split combo script+iframe+onclick is a fair trade for single-cause files.

Server / seed. Upload 422 is only validateHtml, so status 201/422 is a real policy gate — but the body is never checked, so errors can vanish and CI stays green. Seed accept-only + PATCHY_API_TOKEN ?? "dev-token" is the right shape for a local seeder, not a test.

Fixture import. Relative paths skip package.json exports (only ".", files: ["dist"]). A ./fixtures subpath is cleaner than leaking packages/core layout. Do not put fixtures on the runtime . export.

Coverage claim. “Add a blocklist entry without a fixture fails” is true only for the two newly exported arrays. URL_ATTRS, on*, srcdoc, meta refresh, and unsafe CSS are unguarded; extra reject files only prove some error.


YAGNI

Issue 40 wanted a shared on-disk corpus, it.each over the blocklists so a missing file fails, and seed + server on the same files. That is the whole job. Almost everything else is ceremony.

Essential: fixtures/accept|reject/*.html, a tiny directory reader, core it.each(BLOCKED_*) that asserts a named file exists, seed uploading accept, server hitting the same files.

Speculative: runtime kind check, handwritten .d.mts, exporting policy lists and rewriting production lookup, dual overlapping tables, fixture!, protocol.slice(0, -1) as a filename API.

  1. Set → array. The const tuple is a fine source of truth. .some((t) => t === tagName) is not. That is production made dumber for Vitest. Use .includes(tagName) (or a Set derived from the tuple). it.each([...tags]) would have avoided touching policy at all. Must-fix: the .some loop. Taste: tuple vs Set — tuple is fine.

  2. Loader. corpus.mjs + hand-typed .d.mts + untyped kind is two files of Node-package theater for 15 lines. Simplest: keep the .mjs beside the HTML (tsc rootDir is src; HTML would not ship from src). Must-fix: the ../../../packages/core/fixtures/corpus.mjs cross-package relative import. Taste: .d.mts.

  3. Reject HTML. Accept pages earn their keep (seed). The 15 reject files are one shell and a different crime — which is what “real-shaped file is the contract” is. Do not generate them. Do not merge them. Fine.

  4. Dual tables. Blocklist it.each is the done-when. Corpus it.each re-runs those plus srcdoc/css/onclick as ok: false only. Keep both, but the blocklist cases should not find + slice(0,-1) + !. Name files from the constants; don’t invent a transform.

  5. Server test. it.each two kinds, for inside, one app per kind: pragmatic (avoid 17 bootstraps) and the filename is the assertion message. Flattening would report better. Taste.

  6. Wince. .some(===) instead of .includes. protocol.slice(0, -1). expect(x).toBeDefined(); x!. Public export * of test-only constants. Python-shaped untyped function readFixtureCorpus(kind).

Do not add more fixtures, a generator, or a fixtures package. Fix lookup, package export, filename coupling; leave the HTML.


Fixture uniqueness (adversarial)

Ranked regressions that stay green today:

  1. Delete the srcdoc check. iframe-srcdoc.html is <iframe srcdoc=…>. BLOCKED_TAGS still rejects. Corpus .ok === false / server 422 stay green.

  2. Drop expression( / behavior: from the CSS regex. Fixture is only url(javascript:…). style is not in URL_ATTRS, so the whole CSS check is unique; those two arms are not.

  3. Empty-doc and maxBytes. No fixture, no unit test (also true on main — pre-existing hole, not introduced here).

  4. Whitespace-stripped smuggling. All protocol fixtures are clean javascript: / vbscript: / file: on href. Stripper + src/action/formaction/poster/xlink:href can vanish; protocol it.each still looks for "href".

  5. Wrong-reason reject. Only tags/protocols assert error strings. A fixture can fail for a new reason and stay green.

  6. Accept over-block. Proven: <style> blocks, img data:, tables, https <a>. Ban every inline style="" (keep <style> tags) — accept stays green.

Intact: adding a BLOCKED_TAGS / BLOCKED_PROTOCOLS entry without blocked-tag-* / blocked-protocol-* still fails.


Module seams

  • apps/server reaching packages/core/fixtures/corpus.mjs by relative path bypasses @patchy/*. The similar ../../../ hops in this repo go to root test/postgres.js, which is not another package’s guts. @patchy/core/fixtures as a subpath export is the seam: HTML stays outside src, tsc still only compiles src. It fights files: ["dist"] only if core is packed; the package is private: true.
  • File-level export of BLOCKED_* for intra-package tests is fine. Starring them from index.ts is a kernel API expansion. Server/CLI only import validateHtml.
  • Root seed:dev talking HTTP is fine. It should use the same fixtures subpath, not ../packages/core/fixtures.

Summary: Standards: 0 hard violations, 4 smells — worst is production Set.has → .some() plus a cross-package relative import. Spec: 3 partial, 2 scope, 2 wrong — worst is “faithfully checkable” failing for srcdoc (and the dropped java\nscript: case). Method / YAGNI agree: keep the corpus, stop encoding policy in filenames and in the published @patchy/core surface.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

I checked each finding against the branch. I’m going to address the parts that weaken the intended contract:

  • Move the corpus reader to the root test-support seam, matching the existing Postgres helper, so server tests no longer import another package’s internals.
  • Keep the blocklists exportable from html-policy.ts for it.each, but stop exposing them from the @patchy/core package root. I’ll also restore a static membership lookup so test iteration does not make the production walker linear.
  • Replace filename derivation and protocol.slice(0, -1) with an explicit fixture manifest, remove the non-null assertion, and pin each reject fixture to its expected policy error. The blocklist tables and corpus table will stay: one enforces entry→fixture completeness; the other enforces exact corpus outcomes.
  • Restore the whitespace-smuggling coverage in the JavaScript protocol fixture, make the srcdoc fixture reject for srcdoc alone, and assert that server 422 responses still carry validation errors.

I’m not going to make the seed upload reject fixtures: accept-only is intentional for useful local seed data; it still reads the shared corpus through the same loader. I’m also not expanding this ticket into matrices for every URL attribute, CSS-regex arm, empty input, or max-size behavior. Those are either pre-existing coverage gaps or broader policy tests, not the blocklist/fixture-sync contract in #40. I’ll keep the .d.mts sidecar because the shared .mjs loader otherwise has no typed contract for TypeScript test consumers. The runtime kind guard can go; all callers use fixed accepted kinds.

@github-actions github-actions Bot added size:l 501-1,000 changed lines and removed size:m 100-500 changed lines labels Aug 27, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Addressed the accepted findings in 8a04260:

  • corpus loading now uses the root test/ seam;
  • blocklists no longer leak through @patchy/core, and tag checks use a static lookup;
  • explicit manifests replace filename transforms and pin every reject fixture to exact errors;
  • JavaScript protocol coverage again exercises embedded whitespace;
  • srcdoc now has an isolated fixture;
  • server reject cases require non-empty validation errors.

The intentionally deferred coverage remains as described above. Local verification is green (pnpm test: 454 tests; typechecks; lint; seed smoke), and all seven checks on the latest commit passed.

@allisonmahmood
allisonmahmood merged commit df10eff into main Aug 27, 2026
7 checks passed
@allisonmahmood
allisonmahmood deleted the test/html-policy-fixtures branch August 27, 2026 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ci CI, shared testing infrastructure, development scripts and tooling area:core Touches the core area area:docs Touches the docs area area:server Touches the server area size:l 501-1,000 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test(core): table-test the HTML policy with shared accept/reject fixtures

1 participant