Vue: v-html and bound URLs accept exactly what is safe #9
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/vue-url-html"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
template.rshad oneshape()classifier shared byvue-v-htmlandvue-unsafe-url, and itis a URL classifier. It is now split into
safe_htmlandsafe_url.vue-v-html'<b>hi</b>'was reported (it doesn't start like a URL), while'/' + props.c,`/${x}`andrender('/' + x)were accepted (they start like a safe URL).${}, a+of those, or a call whose namecontains
sanitize. Everything else is reported, including'<p>' + sanitize(x). HTMLsanitizers 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-urlWhat
packages/safe-ts-vue/src/url.tsreturns:safeHref(raw, fallback = "#")returnsrawifsafeUrlaccepts it, elsefallbackunchanged.safeUrl(raw)returnsResult<string, UnsafeUrl>, so anErr(a class instance) bound to:hrefrenders as[object Object].Changes:
safeUrl(x)is no longer accepted.unwrapOr(safeUrl(x), fallback)is accepted when thefallback is.
safeHref(x, fallback)is accepted only when the fallback is accepted itself(
safeHref(x, props.other)used to pass).${}is now checked like a string.`javascript:void(0)`used topass, while
'javascript:void(0)'was reported.+concatenation must start with written, non-empty, safe text. A call is nolonger accepted as the start of a concatenation:
safeHref("java") + "script:…"is ajavascript:URL.About the two template literals in the brief:
`javascript:${x}`and`${x}`on:hrefwere already reported onmain(checked with the CLI, and now in the fixture asregression 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.mdandvue-unsafe-url.md: new checked bad examples (concatenationand template in
v-html;`${url}`andsafeUrl(...)in:href), a good example ofconstant 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 bothrules). On the old
template.rs, 8 of these facts fail: 6 missed reports, andunwrapOr(safeUrl(link), '#')plus the plain HTML string reported when they shouldn't be.cargo nextest run: 94/95. The one failure,fixtures_match_annotationson thefeaturesfixture (
main.ts:55,createDecipheriv), also fails on unmodifiedmainand 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
mainwith the local Node v26.13.0 and is unrelated.Not covered / uncertain
v-bind="{ href: … }"(object form) isn't checked (documented).safeHref,safeUrl,unwrapOr,*sanitize*), not byimport, as before.
🤖 Generated with 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]>