2016-06-14 14:31:51 -04:00
|
|
|
import xss from 'pretty-text/xss';
|
|
|
|
|
|
|
|
function attr(name, value) {
|
2016-07-20 13:30:36 -04:00
|
|
|
if (value) {
|
|
|
|
return `${name}="${xss.escapeAttrValue(value)}"`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const ESCAPE_REPLACEMENTS = {
|
|
|
|
"&": "&",
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
'"': """,
|
|
|
|
"'": "'",
|
|
|
|
'`': '`'
|
|
|
|
};
|
|
|
|
const BAD_CHARS = /[&<>"'`]/g;
|
|
|
|
const POSSIBLE_CHARS = /[&<>"'`]/;
|
|
|
|
|
|
|
|
function escapeChar(chr) {
|
|
|
|
return ESCAPE_REPLACEMENTS[chr];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function escape(string) {
|
|
|
|
// don't escape SafeStrings, since they're already safe
|
|
|
|
if (string === null) {
|
|
|
|
return "";
|
|
|
|
} else if (!string) {
|
|
|
|
return string + '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force a string conversion as this will be done by the append regardless and
|
|
|
|
// the regex test will do this transparently behind the scenes, causing issues if
|
|
|
|
// an object's to string has escaped characters in it.
|
|
|
|
string = "" + string;
|
|
|
|
|
|
|
|
if (!POSSIBLE_CHARS.test(string)) { return string; }
|
|
|
|
return string.replace(BAD_CHARS, escapeChar);
|
|
|
|
}
|
|
|
|
|
2016-10-21 11:39:48 -04:00
|
|
|
export function hrefAllowed(href, extraHrefMatchers) {
|
2016-06-14 14:31:51 -04:00
|
|
|
// escape single quotes
|
|
|
|
href = href.replace(/'/g, "%27");
|
|
|
|
|
|
|
|
// absolute urls
|
|
|
|
if (/^(https?:)?\/\/[\w\.\-]+/i.test(href)) { return href; }
|
|
|
|
// relative urls
|
|
|
|
if (/^\/[\w\.\-]+/i.test(href)) { return href; }
|
|
|
|
// anchors
|
|
|
|
if (/^#[\w\.\-]+/i.test(href)) { return href; }
|
|
|
|
// mailtos
|
|
|
|
if (/^mailto:[\w\.\-@]+/i.test(href)) { return href; }
|
2016-10-21 11:39:48 -04:00
|
|
|
|
|
|
|
if (extraHrefMatchers && extraHrefMatchers.length > 0) {
|
|
|
|
for (let i=0; i<extraHrefMatchers.length; i++) {
|
|
|
|
if (extraHrefMatchers[i].test(href)) { return href; }
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function sanitize(text, whiteLister) {
|
|
|
|
if (!text) return "";
|
|
|
|
|
|
|
|
// Allow things like <3 and <_<
|
|
|
|
text = text.replace(/<([^A-Za-z\/\!]|$)/g, "<$1");
|
|
|
|
|
2016-10-21 11:39:48 -04:00
|
|
|
const whiteList = whiteLister.getWhiteList(),
|
2017-09-01 10:15:34 -04:00
|
|
|
allowedHrefSchemes = whiteLister.getAllowedHrefSchemes(),
|
|
|
|
allowedIframes = whiteLister.getAllowedIframes();
|
2016-10-21 11:39:48 -04:00
|
|
|
let extraHrefMatchers = null;
|
|
|
|
|
|
|
|
if (allowedHrefSchemes && allowedHrefSchemes.length > 0) {
|
|
|
|
extraHrefMatchers = [new RegExp('^(' + allowedHrefSchemes.join('|') + '):\/\/[\\w\\.\\-]+','i')];
|
|
|
|
}
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
let result = xss(text, {
|
|
|
|
whiteList: whiteList.tagList,
|
|
|
|
stripIgnoreTag: true,
|
|
|
|
stripIgnoreTagBody: ['script', 'table'],
|
2016-07-04 14:15:51 -04:00
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
onIgnoreTagAttr(tag, name, value) {
|
|
|
|
const forTag = whiteList.attrList[tag];
|
|
|
|
if (forTag) {
|
|
|
|
const forAttr = forTag[name];
|
2017-09-01 10:15:34 -04:00
|
|
|
if (
|
|
|
|
(forAttr && (forAttr.indexOf('*') !== -1 || forAttr.indexOf(value) !== -1)) ||
|
2016-06-14 14:31:51 -04:00
|
|
|
(name.indexOf('data-') === 0 && forTag['data-*']) ||
|
2016-10-21 11:39:48 -04:00
|
|
|
((tag === 'a' && name === 'href') && hrefAllowed(value, extraHrefMatchers)) ||
|
|
|
|
(tag === 'img' && name === 'src' && (/^data:image.*$/i.test(value) || hrefAllowed(value, extraHrefMatchers))) ||
|
2017-09-01 10:15:34 -04:00
|
|
|
(tag === 'iframe' && name === 'src' && allowedIframes.some(i => { return value.toLowerCase().indexOf((i || '').toLowerCase()) === 0;}))
|
|
|
|
) {
|
2016-06-14 14:31:51 -04:00
|
|
|
return attr(name, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag === 'iframe' && name === 'src') {
|
|
|
|
return "-STRIP-";
|
|
|
|
}
|
|
|
|
|
2017-10-16 11:53:47 -04:00
|
|
|
// Heading ids must begin with `heading--`
|
|
|
|
if (
|
|
|
|
['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].indexOf(tag) !== -1 &&
|
|
|
|
value.match(/^heading\-\-[a-zA-Z0-9\-\_]+$/)
|
|
|
|
) {
|
|
|
|
return attr(name, value);
|
|
|
|
}
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
const custom = whiteLister.getCustom();
|
|
|
|
for (let i=0; i<custom.length; i++) {
|
|
|
|
const fn = custom[i];
|
|
|
|
if (fn(tag, name, value)) {
|
|
|
|
return attr(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return result.replace(/\[removed\]/g, '')
|
|
|
|
.replace(/\<iframe[^>]+\-STRIP\-[^>]*>[^<]*<\/iframe>/g, '')
|
|
|
|
.replace(/&(?![#\w]+;)/g, '&')
|
|
|
|
.replace(/'/g, "'")
|
|
|
|
.replace(/ \/>/g, '>');
|
|
|
|
};
|