Vue Safe HTML Directive

3.0.1 · active · verified Sun Apr 19

Vue Safe HTML is a Vue.js directive, currently at version 3.0.1, designed to dynamically render HTML content after programmatically stripping unwanted tags. It supports both Vue 2 and Vue 3, is TypeScript-ready, and has zero external dependencies, making it a lightweight solution for basic HTML sanitization. The library differentiates itself by offering explicit customization of allowed HTML tags and attributes, either globally during plugin installation or locally via directive modifiers. While it provides tag-stripping functionality, it explicitly states that it is not a comprehensive XSS (Cross-Site Scripting) protection mechanism and should not be relied upon for full security against malicious inputs. Releases appear to be event-driven, addressing bug fixes and compatibility, rather than following a strict time-based cadence. It also supports Nuxt SSR environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates installation of the `vue-safe-html` plugin globally, passing custom `allowedTags` and `allowedAttributes`, and then using the `v-safe-html` directive in a Vue component to render sanitized HTML, including a local override.

import Vue from 'vue';
import VueSafeHTML from 'vue-safe-html';

// Optionally configure global allowed tags or attributes
Vue.use(VueSafeHTML, {
  allowedTags: ['p', 'strong', 'em', 'a'],
  allowedAttributes: ['href', 'title']
});

new Vue({
  el: '#app',
  data: {
    unsafeHtml: '<script>alert("XSS attempt!")</script><b>Hello</b> <a href="malicious.com" onclick="doEvil()">World</a> <p>This is safe.</p>'
  },
  template: `
    <div id="app">
      <h3>Content with default sanitization:</h3>
      <div v-safe-html="unsafeHtml"></div>
      <h3>Content with local override (only allowing 'p'):</h3>
      <div v-safe-html.p="unsafeHtml"></div>
    </div>
  `
});

view raw JSON →