Vue: v-html and bound URLs accept exactly what is safe #9

Merged
sionzee merged 1 commit from fix/vue-url-html into main 2026-11-03 21:34:46 +01:00
Owner

What

template.rs had one shape() classifier shared by vue-v-html and vue-unsafe-url, and it
is a URL classifier. It is now split into safe_html and safe_url.

vue-v-html

  • Before: '<b>hi</b>' was reported (it doesn't start like a URL), while '/' + props.c,
    `/${x}` and render('/' + x) were accepted (they start like a safe URL).
  • Now accepted: a string literal, a template without ${}, a + of those, or a call whose name
    contains sanitize. Everything else is reported, including '<p>' + sanitize(x). HTML
    sanitizers don't make their output safe to splice into attribute context, so the docs say to
    wrap the whole value in the sanitizer.

vue-unsafe-url

What packages/safe-ts-vue/src/url.ts returns: safeHref(raw, fallback = "#") returns raw if
safeUrl accepts it, else fallback unchanged. safeUrl(raw) returns
Result<string, UnsafeUrl>, so an Err (a class instance) bound to :href renders as
[object Object].

Changes:

  • safeUrl(x) is no longer accepted. unwrapOr(safeUrl(x), fallback) is accepted when the
    fallback is.
  • safeHref(x, fallback) is accepted only when the fallback is accepted itself
    (safeHref(x, props.other) used to pass).
  • A template without ${} is now checked like a string. `javascript:void(0)` used to
    pass, while 'javascript:void(0)' was reported.
  • A template or + concatenation must start with written, non-empty, safe text. A call is no
    longer accepted as the start of a concatenation: safeHref("java") + "script:…" is a
    javascript: URL.

About the two template literals in the brief: `javascript:${x}` and `${x}` on
:href were already reported on main (checked with the CLI, and now in the fixture as
regression cases). The template-literal hole I found was the constant template above. The object
form v-bind="{ href: x }" isn't checked at all; that is left out of this PR and listed under
"can't see" on the rule page.

Docs

docs/vue/rules/vue-v-html.md and vue-unsafe-url.md: new checked bad examples (concatenation
and template in v-html; `${url}` and safeUrl(...) in :href), a good example of
constant HTML, the "accepted values" text rewritten to match the code, and the object form
added to "can't see".

Tests

  • tests/fixtures/vue_sfc/Views.vue: 13 new lines (accepted and rejected cases for both
    rules). On the old template.rs, 8 of these facts fail: 6 missed reports, and
    unwrapOr(safeUrl(link), '#') plus the plain HTML string reported when they shouldn't be.
  • cargo nextest run: 94/95. The one failure, fixtures_match_annotations on the features
    fixture (main.ts:55, createDecipheriv), also fails on unmodified main and is unrelated.
  • npm run test:docs: passes (213 examples compile; wasm and VitePress build).
  • npm run test:vite: passes.
  • npm run test:std: 115/116. The one failure, every Node callable is mapped exactly once,
    also fails on main with the local Node v26.13.0 and is unrelated.

Not covered / uncertain

  • v-bind="{ href: … }" (object form) isn't checked (documented).
  • Callees are still matched by name (safeHref, safeUrl, unwrapOr, *sanitize*), not by
    import, as before.

🤖 Generated with Claude Code

## What `template.rs` had one `shape()` classifier shared by `vue-v-html` and `vue-unsafe-url`, and it is a URL classifier. It is now split into `safe_html` and `safe_url`. ### `vue-v-html` - Before: `'<b>hi</b>'` was reported (it doesn't start like a URL), while `'/' + props.c`, `` `/${x}` `` and `render('/' + x)` were accepted (they start like a safe URL). - Now accepted: a string literal, a template without `${}`, a `+` of those, or a call whose name contains `sanitize`. Everything else is reported, including `'<p>' + sanitize(x)`. HTML sanitizers don't make their output safe to splice into attribute context, so the docs say to wrap the whole value in the sanitizer. ### `vue-unsafe-url` What `packages/safe-ts-vue/src/url.ts` returns: `safeHref(raw, fallback = "#")` returns `raw` if `safeUrl` accepts it, else `fallback` **unchanged**. `safeUrl(raw)` returns `Result<string, UnsafeUrl>`, so an `Err` (a class instance) bound to `:href` renders as `[object Object]`. Changes: - `safeUrl(x)` is no longer accepted. `unwrapOr(safeUrl(x), fallback)` is accepted when the fallback is. - `safeHref(x, fallback)` is accepted only when the fallback is accepted itself (`safeHref(x, props.other)` used to pass). - A template without `${}` is now checked like a string. `` `javascript:void(0)` `` used to pass, while `'javascript:void(0)'` was reported. - A template or `+` concatenation must start with written, non-empty, safe text. A call is no longer accepted as the start of a concatenation: `safeHref("java") + "script:…"` is a `javascript:` URL. About the two template literals in the brief: `` `javascript:${x}` `` and `` `${x}` `` on `:href` were already reported on `main` (checked with the CLI, and now in the fixture as regression cases). The template-literal hole I found was the constant template above. The object form `v-bind="{ href: x }"` isn't checked at all; that is left out of this PR and listed under "can't see" on the rule page. ### Docs `docs/vue/rules/vue-v-html.md` and `vue-unsafe-url.md`: new checked bad examples (concatenation and template in `v-html`; `` `${url}` `` and `safeUrl(...)` in `:href`), a good example of constant HTML, the "accepted values" text rewritten to match the code, and the object form added to "can't see". ## Tests - `tests/fixtures/vue_sfc/Views.vue`: 13 new lines (accepted and rejected cases for both rules). On the old `template.rs`, 8 of these facts fail: 6 missed reports, and `unwrapOr(safeUrl(link), '#')` plus the plain HTML string reported when they shouldn't be. - `cargo nextest run`: 94/95. The one failure, `fixtures_match_annotations` on the `features` fixture (`main.ts:55`, `createDecipheriv`), also fails on unmodified `main` and is unrelated. - `npm run test:docs`: passes (213 examples compile; wasm and VitePress build). - `npm run test:vite`: passes. - `npm run test:std`: 115/116. The one failure, `every Node callable is mapped exactly once`, also fails on `main` with the local Node v26.13.0 and is unrelated. ## Not covered / uncertain - `v-bind="{ href: … }"` (object form) isn't checked (documented). - Callees are still matched by name (`safeHref`, `safeUrl`, `unwrapOr`, `*sanitize*`), not by import, as before. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
`vue-v-html` shared the URL rule's notion of a safe value, so it reported a
plain HTML string (`'<b>hi</b>'`, which doesn't start like a URL) and
accepted `'/' + props.c` or `render('/' + x)`, which carry whatever the value
holds. It now accepts only HTML written out in full (strings, templates
without `${}`, `+` of those) or a call to a sanitizer.

`vue-unsafe-url` accepted any template without `${}` (`` `javascript:void(0)` ``),
`safeUrl(x)`, which is a `Result` (an `Err` renders as "[object Object]"),
`safeHref(x, fallback)` with an unchecked fallback, and `safeHref(a) + b`.
Now a constant must have a safe start like a string, a template or
concatenation must start with written safe text, `safeHref`'s fallback must
be safe, and `unwrapOr(safeUrl(x), fallback)` is accepted with a safe fallback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch fix/vue-url-html 2026-11-03 21:34:46 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!9
No description provided.