Skip to content

fix(admin): never render catalogue text as HTML, and guard the call sites - #230

Merged
shockalotti merged 1 commit into
mainfrom
fix/admin-i18n-guards
Nov 6, 2026
Merged

shockalotti merged 1 commit into
mainfrom
fix/admin-i18n-guards

Conversation

@shockalotti

Copy link
Copy Markdown
Contributor

First two items from the #217 review, the two that can bite quietly.

1. Catalogue text was rendered as HTML

{@html $t('layout.reportDialog.descriptionHtml')}

That contradicts the stated contract in catalog.ts ("never {@html} catalog content"), and the only guard was catalog.test.ts, which rejects <a and href= and nothing else. An <img src=x onerror=…> in a future locale file would have passed every test and executed in an operator's admin session. We accept community locale PRs, so this is a live surface.

The HTML key was also redundant: description already existed, plain, with the same sentence, and was unused. The dialog now uses it and descriptionHtml is gone from en.json and fr.json, so the contract is true rather than merely enforced.

2. Nothing checked the call sites

Roughly 1,500 $t/tr/trp/trp2 calls, and no test that the keys they pass exist. A typo renders the dotted key on screen and CI stays green.

frontend/scripts/check-i18n.mjs (pnpm check:i18n, run in CI after svelte-check) now enforces:

  • every literal key exists in en.json;
  • plural group paths count as keys, because trp appends .one/.other itself;
  • dynamic template/ternary keys are counted and skipped, not guessed at, and the count is printed so a reviewer can see what is uncovered;
  • no call site renders catalogue text with {@html}.

Why a script and not a test: vitest here runs in browser mode, so node:fs is unavailable, and the node:test file is excluded from CI. A CI step is the only place this check can actually run.

Verification

i18n guard: 1503 literal call site(s) checked against 1323 keys (+299 plural groups),
            11 dynamic call site(s) skipped (template/ternary keys).

The gate was proved to fail: a probe file with a missing $t key, a missing tr key and one {@html} call made it exit 1 and report all three, and removing the probe returned it to 0.

Also: svelte-check reports 0 errors and 0 warnings, and the visual suite passes 27/27.

Not in this PR

The remaining review items: the bundle cost (both catalogues in one 143 KB chunk), the admin's strict en/fr parity blocking partial German/Polish, pluralForm() limited to one/other before Polish, the hour12: localeTag() !== 'fr' special case, and three French corrections. Separate PR, so this one stays reviewable.

…ites

Two gaps the catalogue tests cannot see, both from the #217 review.

1. `{@html $t('layout.reportDialog.descriptionHtml')}` in +layout.svelte rendered
   catalogue content as markup, while catalog.ts claims the opposite and the guard in
   catalog.test.ts only rejected `<a` and `href=`. An `<img src=x onerror=...>` in a
   future locale file would have passed every test and executed in an operator's admin
   session. Community locale PRs are a real input here, so that is a live surface, not a
   theoretical one.

   The HTML key was also redundant: `description` already existed, plain and unused, with
   the same sentence. The dialog now uses it and `descriptionHtml` is deleted from en and
   fr, so the contract in catalog.ts is true again without needing a rule to enforce it.

2. Nothing checked the call sites against the catalogue. A typo rendered the dotted key on
   screen and CI stayed green.

frontend/scripts/check-i18n.mjs (pnpm check:i18n, run in CI) now enforces both: every
literal key passed to $t/tr/trp/trp2 exists in en.json; plural group paths count as keys
because trp appends `.one`/`.other` itself; dynamic template and ternary keys are counted
and skipped rather than guessed at; and no call site renders catalogue text with {@html}.

Verified: the guard passes on this tree (1503 call sites, 1323 keys, 299 plural groups, 11
dynamic skipped). A probe file containing a missing $t key, a missing tr key and one
{@html} call makes it exit 1 and report all three, so the gate fails when it should.
svelte-check is clean and the 27 visual tests pass.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one minor gap in the new guard, inline.

Reviewed changes

  • Catalogue text no longer rendered as HTML — +layout.svelte now uses the plain layout.reportDialog.description key, and descriptionHtml is deleted from en.json and fr.json. The only remaining {@html} in src/ is item.icon (lucide markup), and en/fr key parity still holds (catalog.test.ts, 22/22).
  • New check-i18n.mjs gate — walks src for literal $t/tr/trp/trp2 keys, checks them against en.json leaves plus plural group paths, and flags {@html} on a catalogue call. Verified locally: exits 0 at 1503 checked / 11 dynamic, and exits 1 with all three classes reported when probed.
  • CI wiring — pnpm check:i18n runs after svelte-check in the check job, which is triggered on pull_request, so it is a real pre-merge gate.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash | 𝕏

// checked.
const CALL = /(?:\$t\(|\b(?:tr|trp|trp2)\()\s*(['"])((?:(?!\1).)+)\1/g;
const ANY_CALL = /(?:\$t\(|\b(?:tr|trp|trp2)\()/g;
const RENDERED_AS_HTML = /\{@html[^}]*?(?:\$t\(|\b(?:tr|trp|trp2)\()/;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the scan is per line, a {@html whose expression wraps — {@html on one line, $t('…')} on the next — escapes this check entirely; I confirmed it with a probe file. The regex already crosses newlines via [^}]*?, so it is only the source.split('\n') above that hides it: matching against the whole source and deriving the line from the match index closes the gap.

Technical details
# `{@html}` guard misses wrapped expressions

## Affected sites
- `frontend/scripts/check-i18n.mjs:69-70` — `RENDERED_AS_HTML.test(line)` only ever sees one line of source.
- `frontend/scripts/check-i18n.mjs:59` — `RENDERED_AS_HTML` itself is fine for multi-line input; `[^}]*?` already spans newlines.

## Required outcome
- A `{@html` whose catalogue call sits on a following line is reported by `pnpm check:i18n`, anchored at the line of the `{@html`.
- Existing single-line detection and the `missing` / `dynamic` reporting stay unchanged.

## Suggested approach (optional)
- Leave the per-line loop as is, then run `RENDERED_AS_HTML` with the `/g` flag over the whole `source` and compute the line as `source.slice(0, match.index).split('\n').length`. Keep `[^}]*?` as the bound so code after the closing `}` cannot match.

## Verification
- Probe used: a `.svelte` file containing `<p>{@html\n  $t('layout.reportDialog.description')}</p>`; the current script exits 0 on it.

@shockalotti
shockalotti merged commit bdbc47b into main Nov 6, 2026
11 checks passed
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.

1 participant