Skip to content

fix(viewer): escape ts and superseded_by cells; badge() escapes at the helper (#11) - #33

Merged
abogartz merged 1 commit into
mainfrom
fix/viewer-cell-escaping
Oct 22, 2026
Merged

abogartz merged 1 commit into
mainfrom
fix/viewer-cell-escaping

Conversation

@abogartz

Copy link
Copy Markdown
Owner

Closes #11 (stored-XSS defense-in-depth in the viewer, from the 2026-10-21 security review).

Problem

The row template renders store data via innerHTML. Every field passed through esc() except two cells:

  • the ts cell: ${m.ts.split("T")[0]}<br>${(m.ts.split("T")[1] || "").slice(0,8)}
  • the superseded badge: badge(... \superseded → ${m.superseded_by.slice(0,8)}`)—badge()` interpolated its text raw.

ts/superseded_by are normally code-written ISO strings, but the reader must not trust the store: a hostile file on disk (or the pre-#9 import route) planting <img src=x onerror=…> would execute in the viewer's own origin, inheriting its same-origin write access to the whole store.

Fix

  • New <script id="viewer-helpers"> block in page.html holding the pure, DOM-free render helpers: esc, badge (now escapes both its text and its class argument), and a new tsCell(ts) that escapes the date and time segments. Inline scripts share global scope, so the main script uses them unchanged.
  • The two raw cells now go through helpers: ${tsCell(m.ts)} and badge("status-superseded", "superseded → " + m.superseded_by.slice(0,8)).
  • badge("type"/"scope", …) call sites pass raw values (badge escapes once — no double-escaping).
  • All other sinks were audited: toast() and askModal() use textContent, option lists and data-attributes already used esc().

Tests

test/viewer.test.ts gains a describe block that evals the shipped helpers block (extracted from page.html, not a copy) and asserts:

  • badge() neutralizes <img src=x onerror="alert(1)"> to the exact escaped string; class argument escaped too;
  • tsCell() escapes both segments — including the slice-before-escape order ("2026-01-01T<img…" → "2026-01-01<br>&lt;img src") — and renders a normal ISO ts unchanged;
  • source tripwires: the page no longer contains ${m.ts. or ${m.superseded_by interpolation, uses ${tsCell(m.ts)}, and the helpers block precedes the main script.

Gate: 256 pass / 4 skip / 0 fail, tsc clean, biome clean (advisory warnings unchanged).

Compatibility

Visual output for well-formed rows is byte-identical; only hostile values change (they render as inert text now). No server/API change.

…e helper

Issue #11 (stored-XSS defense-in-depth). The row template renders via
innerHTML and every field passed esc() except the ts cell and the
superseded badge (badge() interpolated its text raw). A hostile store
value could execute in the viewer's own origin, inheriting its
same-origin write access to the whole store.

- extract the pure render helpers (esc, badge, new tsCell) into a
  DOM-free <script id="viewer-helpers"> block so tests can eval the
  SHIPPED code; badge now escapes text and class; tsCell escapes both
  segments (slice-then-escape order preserved).
- row template: ts cell -> tsCell(m.ts); superseded badge -> badge();
  type/scope call sites pass raw (badge escapes once, no double-escape).
- audited remaining sinks: toast/askModal use textContent; option lists
  and data-attributes already escaped.
- viewer.test.ts evals the helpers block from page.html and asserts
  escaping of hostile values plus source tripwires (no ${m.ts.} or
  ${m.superseded_by} interpolation, helpers precede main script).
@abogartz
abogartz merged commit d9c8564 into main Oct 22, 2026
2 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.6.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@abogartz
abogartz deleted the fix/viewer-cell-escaping branch November 19, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

viewer: escape ts and superseded_by cells in page.html (stored-XSS defense-in-depth)

1 participant