Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active September 17, 2024 14:50
Show Gist options
  • Save daliborgogic/60fd819f214be5033243b0f8c69d2566 to your computer and use it in GitHub Desktop.
Save daliborgogic/60fd819f214be5033243b0f8c69d2566 to your computer and use it in GitHub Desktop.
import DOMPurify from 'dompurify'
export default defineNuxtPlugin(async (nuxtApp) => {
let JSDOM
if (import.meta.server) {
const { JSDOM: dom } = await import('jsdom')
JSDOM = dom
}
nuxtApp.vueApp.directive('sanitize', {
mounted: (el, binding) => {
el.innerHTML = DOMPurify.sanitize(binding.value)
},
updated: (el, binding) => {
el.innerHTML = DOMPurify.sanitize(binding.value)
},
getSSRProps(binding) {
return {
innerHTML: DOMPurify(new JSDOM('').window).sanitize(binding.value)
}
}
})
})
@daliborgogic
Copy link
Author

daliborgogic commented Sep 14, 2024

const html= `<p>Some potentially <strong onClick=\"alert('Hack!')\">dangerous</strong> HTML content</p>`
<div v-sanitize="html" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment