composables
How to use the useHeadSafe composable.
The useHeadSafe
composable is a wrapper around the useHead composable that restricts the input to only allow safe values.
There is a whitelist of allowed tags and attributes. If you try to use a tag or attribute that isn't on the whitelist, it will be ignored.
The whitelist is very restrictive, as there are many vectors for XSS attacks. If you need to use a tag or attribute that isn't on the whitelist, you can use the useHead composable instead, just make sure you sanitise the input.
The whitelist is as follows:
const WhitelistAttributes = {
htmlAttrs: ['id', 'class', 'lang', 'dir'],
bodyAttrs: ['id', 'class'],
meta: ['id', 'name', 'property', 'charset', 'content'],
noscript: ['id', 'textContent'],
script: ['id', 'type', 'textContent'],
link: ['id', 'color', 'crossorigin', 'fetchpriority', 'href', 'hreflang', 'imagesrcset', 'imagesizes', 'integrity', 'media', 'referrerpolicy', 'rel', 'sizes', 'type'],
}
<link rel="stylesheet" ...>
, <style>
, they are a vector for XSS attacks and clickjacking.<script type="application/json">
, use textContent: myObject
.data-*
attributes are allowed.['stylesheet', 'canonical', 'modulepreload', 'prerender', 'preload', 'prefetch']
.Using head data from an untrusted data source.
const thirdPartyMeta = loadMeta()
useHeadSafe(thirdPartyMeta)