2
0
mirror of https://github.com/tenrok/vue-meta.git synced 2026-06-11 09:22:25 +03:00

implement sanitization

This commit is contained in:
Declan de Wet
2016-11-23 14:56:32 +02:00
parent cd6a1e5d58
commit ff6d7cc38a
5 changed files with 320 additions and 253 deletions
+35 -3
View File
@@ -1,4 +1,7 @@
import deepmerge from 'deepmerge'
import escapeHTML from 'lodash.escape'
import isPlainObject from 'lodash.isplainobject'
import isArray from './isArray'
import getComponentOption from './getComponentOption'
export default function _getMetaInfo (options = {}) {
@@ -23,11 +26,12 @@ export default function _getMetaInfo (options = {}) {
link: [],
style: [],
script: [],
noscript: []
noscript: [],
__dangerouslyDisableSanitizers: []
}
// collect & aggregate all metaInfo $options
const info = getComponentOption({
let info = getComponentOption({
component,
option: keyName,
deep: true,
@@ -73,6 +77,34 @@ export default function _getMetaInfo (options = {}) {
info.base = Object.keys(info.base).length ? [info.base] : []
}
return deepmerge(defaultInfo, info)
// sanitizes potentially dangerous characters
const escape = (info) => Object.keys(info).reduce((escaped, key) => {
const ref = info.__dangerouslyDisableSanitizers
const isDisabled = ref && ref.indexOf(key) > -1
const val = info[key]
if (!isDisabled) {
if (typeof val === 'string') {
escaped[key] = escapeHTML(val)
} else if (isPlainObject(val)) {
escaped[key] = escape(val)
} else if (isArray(val)) {
escaped[key] = val.map(escape)
} else {
escaped[key] = val
}
} else {
escaped[key] = val
}
return escaped
}, {})
// merge with defaults
info = deepmerge(defaultInfo, info)
// begin sanitization
info = escape(info)
return info
}
}