mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: add a setting to allow url schemes other than http(s)
This commit is contained in:
@@ -19,7 +19,7 @@ export function cook(text) {
|
||||
}
|
||||
|
||||
export function sanitize(text) {
|
||||
return textSanitize(text, new WhiteLister(getOpts().features));
|
||||
return textSanitize(text, new WhiteLister(getOpts()));
|
||||
}
|
||||
|
||||
function emojiOptions() {
|
||||
|
||||
@@ -389,7 +389,7 @@ export function cook(raw, opts) {
|
||||
|
||||
preProcessors.forEach(p => raw = p(raw));
|
||||
|
||||
const whiteLister = new WhiteLister(opts.features);
|
||||
const whiteLister = new WhiteLister(opts);
|
||||
|
||||
const tree = parser.toHTMLTree(raw, 'Discourse');
|
||||
let result = opts.sanitizer(parser.renderJsonML(parseTree(tree, opts)), whiteLister);
|
||||
|
||||
@@ -48,6 +48,7 @@ export function buildOptions(state) {
|
||||
getCurrentUser,
|
||||
currentUser,
|
||||
mentionLookup: state.mentionLookup,
|
||||
allowedHrefSchemes: siteSettings.allowed_href_schemes ? siteSettings.allowed_href_schemes.split('|') : null
|
||||
};
|
||||
|
||||
_registerFns.forEach(fn => fn(siteSettings, options, state));
|
||||
@@ -71,6 +72,6 @@ export default class {
|
||||
}
|
||||
|
||||
sanitize(html) {
|
||||
return this.opts.sanitizer(html, new WhiteLister(this.opts.features));
|
||||
return this.opts.sanitizer(html, new WhiteLister(this.opts));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -42,7 +42,7 @@ export function escape(string) {
|
||||
return string.replace(BAD_CHARS, escapeChar);
|
||||
}
|
||||
|
||||
export function hrefAllowed(href) {
|
||||
export function hrefAllowed(href, extraHrefMatchers) {
|
||||
// escape single quotes
|
||||
href = href.replace(/'/g, "%27");
|
||||
|
||||
@@ -54,6 +54,12 @@ export function hrefAllowed(href) {
|
||||
if (/^#[\w\.\-]+/i.test(href)) { return href; }
|
||||
// mailtos
|
||||
if (/^mailto:[\w\.\-@]+/i.test(href)) { return href; }
|
||||
|
||||
if (extraHrefMatchers && extraHrefMatchers.length > 0) {
|
||||
for (let i=0; i<extraHrefMatchers.length; i++) {
|
||||
if (extraHrefMatchers[i].test(href)) { return href; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function sanitize(text, whiteLister) {
|
||||
@@ -62,7 +68,13 @@ export function sanitize(text, whiteLister) {
|
||||
// Allow things like <3 and <_<
|
||||
text = text.replace(/<([^A-Za-z\/\!]|$)/g, "<$1");
|
||||
|
||||
const whiteList = whiteLister.getWhiteList();
|
||||
const whiteList = whiteLister.getWhiteList(),
|
||||
allowedHrefSchemes = whiteLister.getAllowedHrefSchemes();
|
||||
let extraHrefMatchers = null;
|
||||
|
||||
if (allowedHrefSchemes && allowedHrefSchemes.length > 0) {
|
||||
extraHrefMatchers = [new RegExp('^(' + allowedHrefSchemes.join('|') + '):\/\/[\\w\\.\\-]+','i')];
|
||||
}
|
||||
|
||||
let result = xss(text, {
|
||||
whiteList: whiteList.tagList,
|
||||
@@ -75,8 +87,8 @@ export function sanitize(text, whiteLister) {
|
||||
const forAttr = forTag[name];
|
||||
if ((forAttr && (forAttr.indexOf('*') !== -1 || forAttr.indexOf(value) !== -1)) ||
|
||||
(name.indexOf('data-') === 0 && forTag['data-*']) ||
|
||||
((tag === 'a' && name === 'href') && hrefAllowed(value)) ||
|
||||
(tag === 'img' && name === 'src' && (/^data:image.*$/i.test(value) || hrefAllowed(value))) ||
|
||||
((tag === 'a' && name === 'href') && hrefAllowed(value, extraHrefMatchers)) ||
|
||||
(tag === 'img' && name === 'src' && (/^data:image.*$/i.test(value) || hrefAllowed(value, extraHrefMatchers))) ||
|
||||
(tag === 'iframe' && name === 'src' && _validIframes.some(i => i.test(value)))) {
|
||||
return attr(name, value);
|
||||
}
|
||||
|
||||
@@ -13,12 +13,13 @@ function concatUniq(src, elems) {
|
||||
}
|
||||
|
||||
export default class WhiteLister {
|
||||
constructor(features) {
|
||||
features.default = true;
|
||||
constructor(options) {
|
||||
options.features.default = true;
|
||||
|
||||
this._featureKeys = Object.keys(features).filter(f => features[f]);
|
||||
this._featureKeys = Object.keys(options.features).filter(f => options.features[f]);
|
||||
this._key = this._featureKeys.join(':');
|
||||
this._features = features;
|
||||
this._features = options.features;
|
||||
this._options = options||{};
|
||||
}
|
||||
|
||||
getCustom() {
|
||||
@@ -54,6 +55,10 @@ export default class WhiteLister {
|
||||
}
|
||||
return _whiteLists[this._key];
|
||||
}
|
||||
|
||||
getAllowedHrefSchemes() {
|
||||
return this._options.allowedHrefSchemes || [];
|
||||
}
|
||||
}
|
||||
|
||||
// Builds our object that represents whether something is sanitized for a particular feature.
|
||||
|
||||
Reference in New Issue
Block a user