Skip to content

Security and HTML Sanitization in Vue 3 ​

@erag/text-editor-vue runs a browser-side allowlist sanitizer, built on DOMParser and TreeWalker, over content before it is inserted into the editor or shown in preview. It strips common unsafe markup, but it is not a complete XSS boundary, so HTML submitted from a Vue form bound with v-model must be sanitized again on the server.


Built-in Sanitization Mechanics ​

The internal sanitizer inspects HTML strings before inserting, previewing, or exporting content:

  • ❌ Script Stripping: Removes <script> tags, inline javascript: URIs, data-URIs with executable content, and inline event handlers (such as onload=, onerror=, onclick=).
  • ❌ Unsafe Iframe and Style Blocking: Blocks unauthorized iframe protocols and dangerous style declarations (e.g. expression(), behavior, url() injections).
  • 🔒 Mention Markup Normalization: Validates inserted @ mentions and strips unexpected or tampered attributes, keeping only allowlisted data-erag-mention-* properties and contenteditable="false".
  • 🔒 Merge-Tag Normalization: Preserves only valid .erag-merge-tag tokens with the expected marker, value, and non-editable state; malformed candidates become plain text.
  • 🧹 Allowlisted Formatting: Keeps configured tags and attributes while filtering style declarations to the package's safe property set.

Defense-in-Depth and Server-Side Sanitization ​

IMPORTANT

Client-side sanitization is defense-in-depth and cannot replace server-side validation.

Because callers can bypass browser code and submit HTTP requests directly, always sanitize untrusted HTML on the server with a maintained, context-appropriate sanitizer. Step 9 of the Laravel and Inertia guide covers this for Laravel.

Server-Side Allowlist Recommendation ​

When configuring your server sanitizer, ensure the following attributes are allowlisted for mentions and merge tags:

  • Mention Tags: <span class="erag-mention" data-erag-mention="true" data-erag-mention-id="..." data-erag-mention-label="..." data-erag-mention-value="..." contenteditable="false">
  • Merge Tag Tokens: <span class="erag-merge-tag" data-erag-merge-tag="true" data-erag-merge-tag-value="..." data-erag-merge-tag-name="..." contenteditable="false"> (data-erag-merge-tag-name is optional)

Rendering saved HTML with v-html ​

When you show stored content outside the editor, sanitize it with the exported sanitizeHtml utility and the editor's default allowlist before binding it to v-html:

vue
<script setup lang="ts">
import { computed } from 'vue';
import { defaultEditorConfig, sanitizeHtml } from '@erag/text-editor-vue';

const props = defineProps<{ html: string }>();

const safeHtml = computed(() =>
    sanitizeHtml(props.html, {
        allowedTags: defaultEditorConfig.allowedTags,
        allowedAttributes: defaultEditorConfig.allowedAttributes,
        allowRelativeUrls: defaultEditorConfig.relativeUrls,
    }),
);
</script>

<template>
    <div v-html="safeHtml" />
</template>

sanitizeHtml returns an empty string when DOM APIs are not available, such as during server-side rendering, so keep server-side sanitization in place. See media and embeds for iframe rules.


Content Security Policy (CSP) ​

For maximum protection against XSS vulnerabilities, enforce a strict Content Security Policy (CSP) header in your web application:

http
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:;