Files
js-xss/lib/default.js

431 lines
9.8 KiB
JavaScript
Raw Normal View History

2014-02-13 11:18:03 +08:00
/**
2017-12-21 14:19:10 +08:00
* default settings
2014-02-13 11:18:03 +08:00
*
2017-12-21 14:19:10 +08:00
* @author Zongmin Lei<leizongmin@gmail.com>
2014-02-13 11:18:03 +08:00
*/
2017-12-21 14:22:34 +08:00
var FilterCSS = require("cssfilter").FilterCSS;
var getDefaultCSSWhiteList = require("cssfilter").getDefaultWhiteList;
var _ = require("./util");
2014-02-13 11:18:03 +08:00
2017-12-21 14:22:34 +08:00
function getDefaultWhiteList() {
2015-12-23 12:33:46 +08:00
return {
2017-12-21 14:22:34 +08:00
a: ["target", "href", "title"],
abbr: ["title"],
2015-12-23 12:33:46 +08:00
address: [],
2017-12-21 14:22:34 +08:00
area: ["shape", "coords", "href", "alt"],
2015-12-23 12:33:46 +08:00
article: [],
2017-12-21 14:22:34 +08:00
aside: [],
audio: ["autoplay", "controls", "loop", "preload", "src"],
b: [],
bdi: ["dir"],
bdo: ["dir"],
big: [],
blockquote: ["cite"],
br: [],
2015-12-23 12:33:46 +08:00
caption: [],
center: [],
2017-12-21 14:22:34 +08:00
cite: [],
code: [],
col: ["align", "valign", "span", "width"],
colgroup: ["align", "valign", "span", "width"],
dd: [],
del: ["datetime"],
details: ["open"],
div: [],
dl: [],
dt: [],
em: [],
figcaption: [],
figure: [],
2017-12-21 14:22:34 +08:00
font: ["color", "size", "face"],
2015-12-23 12:33:46 +08:00
footer: [],
2017-12-21 14:22:34 +08:00
h1: [],
h2: [],
h3: [],
h4: [],
h5: [],
h6: [],
2015-12-23 12:33:46 +08:00
header: [],
2017-12-21 14:22:34 +08:00
hr: [],
i: [],
img: ["src", "alt", "title", "width", "height"],
ins: ["datetime"],
li: [],
mark: [],
nav: [],
ol: [],
p: [],
pre: [],
s: [],
section: [],
small: [],
span: [],
sub: [],
summary: [],
2017-12-21 14:22:34 +08:00
sup: [],
2015-12-23 12:33:46 +08:00
strong: [],
2017-12-21 14:22:34 +08:00
table: ["width", "border", "align", "valign"],
tbody: ["align", "valign"],
td: ["width", "rowspan", "colspan", "align", "valign"],
tfoot: ["align", "valign"],
th: ["width", "rowspan", "colspan", "align", "valign"],
thead: ["align", "valign"],
tr: ["rowspan", "align", "valign"],
tt: [],
u: [],
ul: [],
video: [
"autoplay",
"controls",
"loop",
"preload",
"src",
"height",
"width",
],
2015-12-23 12:33:46 +08:00
};
}
2014-02-13 11:18:03 +08:00
var defaultCSSFilter = new FilterCSS();
2014-02-13 11:18:03 +08:00
/**
2017-12-21 14:19:10 +08:00
* default onTag function
2014-02-13 11:18:03 +08:00
*
* @param {String} tag
* @param {String} html
* @param {Object} options
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function onTag(tag, html, options) {
2014-02-13 15:55:36 +08:00
// do nothing
2014-02-13 11:18:03 +08:00
}
/**
2017-12-21 14:19:10 +08:00
* default onIgnoreTag function
2014-02-13 11:18:03 +08:00
*
* @param {String} tag
* @param {String} html
* @param {Object} options
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function onIgnoreTag(tag, html, options) {
2014-02-13 15:55:36 +08:00
// do nothing
2014-02-13 11:18:03 +08:00
}
/**
2017-12-21 14:19:10 +08:00
* default onTagAttr function
2014-02-13 11:18:03 +08:00
*
* @param {String} tag
* @param {String} name
* @param {String} value
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function onTagAttr(tag, name, value) {
2014-02-13 15:55:36 +08:00
// do nothing
2014-02-13 11:18:03 +08:00
}
/**
2017-12-21 14:19:10 +08:00
* default onIgnoreTagAttr function
2014-02-13 11:18:03 +08:00
*
* @param {String} tag
* @param {String} name
* @param {String} value
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function onIgnoreTagAttr(tag, name, value) {
2014-02-13 15:55:36 +08:00
// do nothing
2014-02-13 11:18:03 +08:00
}
/**
2017-12-21 14:19:10 +08:00
* default escapeHtml function
2014-02-13 11:18:03 +08:00
*
* @param {String} html
*/
2017-12-21 14:22:34 +08:00
function escapeHtml(html) {
return html.replace(REGEXP_LT, "&lt;").replace(REGEXP_GT, "&gt;");
2014-02-13 11:18:03 +08:00
}
/**
2017-12-21 14:19:10 +08:00
* default safeAttrValue function
2014-02-13 11:18:03 +08:00
*
* @param {String} tag
* @param {String} name
* @param {String} value
* @param {Object} cssFilter
2014-02-13 11:18:03 +08:00
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function safeAttrValue(tag, name, value, cssFilter) {
2017-12-21 14:19:10 +08:00
// unescape attribute value firstly
2014-02-13 11:18:03 +08:00
value = friendlyAttrValue(value);
2017-12-21 14:22:34 +08:00
if (name === "href" || name === "src") {
2017-12-21 14:19:10 +08:00
// filter `href` and `src` attribute
// only allow the value that starts with `http://` | `https://` | `mailto:` | `/` | `#`
2015-03-27 16:09:45 +11:00
value = _.trim(value);
2017-12-21 14:22:34 +08:00
if (value === "#") return "#";
if (
!(
value.substr(0, 7) === "http://" ||
value.substr(0, 8) === "https://" ||
value.substr(0, 7) === "mailto:" ||
value.substr(0, 4) === "tel:" ||
value.substr(0, 11) === "data:image/" ||
value.substr(0, 6) === "ftp://" ||
value.substr(0, 2) === "./" ||
2020-02-22 21:59:36 +01:00
value.substr(0, 3) === "../" ||
2017-12-21 14:22:34 +08:00
value[0] === "#" ||
value[0] === "/"
)
) {
return "";
2014-02-13 11:18:03 +08:00
}
2017-12-21 14:22:34 +08:00
} else if (name === "background") {
2017-12-21 14:19:10 +08:00
// filter `background` attribute (maybe no use)
// `javascript:`
2014-02-20 10:44:08 +08:00
REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0;
if (REGEXP_DEFAULT_ON_TAG_ATTR_4.test(value)) {
2017-12-21 14:22:34 +08:00
return "";
2014-02-20 10:44:08 +08:00
}
2017-12-21 14:22:34 +08:00
} else if (name === "style") {
2017-12-21 14:19:10 +08:00
// `expression()`
2014-02-20 10:44:08 +08:00
REGEXP_DEFAULT_ON_TAG_ATTR_7.lastIndex = 0;
if (REGEXP_DEFAULT_ON_TAG_ATTR_7.test(value)) {
2017-12-21 14:22:34 +08:00
return "";
2014-02-13 11:18:03 +08:00
}
2017-12-21 14:19:10 +08:00
// `url()`
2014-02-20 10:44:08 +08:00
REGEXP_DEFAULT_ON_TAG_ATTR_8.lastIndex = 0;
if (REGEXP_DEFAULT_ON_TAG_ATTR_8.test(value)) {
REGEXP_DEFAULT_ON_TAG_ATTR_4.lastIndex = 0;
if (REGEXP_DEFAULT_ON_TAG_ATTR_4.test(value)) {
2017-12-21 14:22:34 +08:00
return "";
2014-02-20 10:44:08 +08:00
}
}
if (cssFilter !== false) {
cssFilter = cssFilter || defaultCSSFilter;
value = cssFilter.process(value);
}
2014-02-13 11:18:03 +08:00
}
2017-12-21 14:19:10 +08:00
// escape `<>"` before returns
2014-02-13 11:18:03 +08:00
value = escapeAttrValue(value);
return value;
}
2017-12-21 14:19:10 +08:00
// RegExp list
2014-02-13 11:18:03 +08:00
var REGEXP_LT = /</g;
var REGEXP_GT = />/g;
var REGEXP_QUOTE = /"/g;
var REGEXP_QUOTE_2 = /&quot;/g;
2017-12-21 14:22:34 +08:00
var REGEXP_ATTR_VALUE_1 = /&#([a-zA-Z0-9]*);?/gim;
var REGEXP_ATTR_VALUE_COLON = /&colon;?/gim;
var REGEXP_ATTR_VALUE_NEWLINE = /&newline;?/gim;
var REGEXP_DEFAULT_ON_TAG_ATTR_3 = /\/\*|\*\//gm;
var REGEXP_DEFAULT_ON_TAG_ATTR_4 = /((j\s*a\s*v\s*a|v\s*b|l\s*i\s*v\s*e)\s*s\s*c\s*r\s*i\s*p\s*t\s*|m\s*o\s*c\s*h\s*a)\:/gi;
var REGEXP_DEFAULT_ON_TAG_ATTR_5 = /^[\s"'`]*(d\s*a\s*t\s*a\s*)\:/gi;
var REGEXP_DEFAULT_ON_TAG_ATTR_6 = /^[\s"'`]*(d\s*a\s*t\s*a\s*)\:\s*image\//gi;
var REGEXP_DEFAULT_ON_TAG_ATTR_7 = /e\s*x\s*p\s*r\s*e\s*s\s*s\s*i\s*o\s*n\s*\(.*/gi;
var REGEXP_DEFAULT_ON_TAG_ATTR_8 = /u\s*r\s*l\s*\(.*/gi;
2014-02-13 11:18:03 +08:00
/**
* escape double quote
2014-02-13 11:18:03 +08:00
*
* @param {String} str
* @return {String} str
*/
2017-12-21 14:22:34 +08:00
function escapeQuote(str) {
return str.replace(REGEXP_QUOTE, "&quot;");
2014-02-13 11:18:03 +08:00
}
/**
2017-12-21 14:19:10 +08:00
* unescape double quote
2014-02-13 11:18:03 +08:00
*
* @param {String} str
* @return {String} str
*/
2017-12-21 14:22:34 +08:00
function unescapeQuote(str) {
2014-02-13 11:18:03 +08:00
return str.replace(REGEXP_QUOTE_2, '"');
}
/**
2017-12-21 14:19:10 +08:00
* escape html entities
2014-02-13 11:18:03 +08:00
*
* @param {String} str
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function escapeHtmlEntities(str) {
return str.replace(REGEXP_ATTR_VALUE_1, function replaceUnicode(str, code) {
return code[0] === "x" || code[0] === "X"
? String.fromCharCode(parseInt(code.substr(1), 16))
: String.fromCharCode(parseInt(code, 10));
2014-02-13 11:18:03 +08:00
});
}
/**
2017-12-21 14:19:10 +08:00
* escape html5 new danger entities
2014-02-13 11:18:03 +08:00
*
* @param {String} str
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function escapeDangerHtml5Entities(str) {
return str
.replace(REGEXP_ATTR_VALUE_COLON, ":")
.replace(REGEXP_ATTR_VALUE_NEWLINE, " ");
2014-02-13 11:18:03 +08:00
}
/**
2017-12-21 14:19:10 +08:00
* clear nonprintable characters
2014-02-13 11:18:03 +08:00
*
* @param {String} str
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function clearNonPrintableCharacter(str) {
var str2 = "";
2014-02-13 11:18:03 +08:00
for (var i = 0, len = str.length; i < len; i++) {
2017-12-21 14:22:34 +08:00
str2 += str.charCodeAt(i) < 32 ? " " : str.charAt(i);
2014-02-13 11:18:03 +08:00
}
2015-03-27 16:09:45 +11:00
return _.trim(str2);
2014-02-13 11:18:03 +08:00
}
/**
2017-12-21 14:19:10 +08:00
* get friendly attribute value
2014-02-13 11:18:03 +08:00
*
* @param {String} str
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function friendlyAttrValue(str) {
2017-12-21 14:19:10 +08:00
str = unescapeQuote(str);
str = escapeHtmlEntities(str);
str = escapeDangerHtml5Entities(str);
str = clearNonPrintableCharacter(str);
2014-02-13 11:18:03 +08:00
return str;
}
/**
2017-12-21 14:19:10 +08:00
* unescape attribute value
2014-02-13 11:18:03 +08:00
*
* @param {String} str
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function escapeAttrValue(str) {
2014-02-13 11:18:03 +08:00
str = escapeQuote(str);
str = escapeHtml(str);
return str;
}
2014-02-13 16:27:49 +08:00
/**
2017-12-21 14:19:10 +08:00
* `onIgnoreTag` function for removing all the tags that are not in whitelist
2014-02-13 16:27:49 +08:00
*/
2017-12-21 14:22:34 +08:00
function onIgnoreTagStripAll() {
return "";
2014-02-13 16:27:49 +08:00
}
2014-02-13 17:55:43 +08:00
/**
2017-12-21 14:19:10 +08:00
* remove tag body
* specify a `tags` list, if the tag is not in the `tags` list then process by the specify function (optional)
2014-02-13 17:55:43 +08:00
*
2017-12-21 14:19:10 +08:00
* @param {array} tags
* @param {function} next
2014-02-13 17:55:43 +08:00
*/
2017-12-21 14:22:34 +08:00
function StripTagBody(tags, next) {
if (typeof next !== "function") {
next = function () {};
2014-02-13 17:55:43 +08:00
}
2014-02-13 18:18:43 +08:00
var isRemoveAllTag = !Array.isArray(tags);
2017-12-21 14:22:34 +08:00
function isRemoveTag(tag) {
2014-02-13 18:18:43 +08:00
if (isRemoveAllTag) return true;
2017-12-21 14:22:34 +08:00
return _.indexOf(tags, tag) !== -1;
2014-02-13 18:18:43 +08:00
}
2017-12-21 14:19:10 +08:00
var removeList = [];
var posStart = false;
2014-02-13 17:55:43 +08:00
return {
onIgnoreTag: function (tag, html, options) {
2014-02-13 18:18:43 +08:00
if (isRemoveTag(tag)) {
2014-02-13 17:55:43 +08:00
if (options.isClosing) {
2017-12-21 14:22:34 +08:00
var ret = "[/removed]";
2014-02-13 17:55:43 +08:00
var end = options.position + ret.length;
2017-12-21 14:22:34 +08:00
removeList.push([
posStart !== false ? posStart : options.position,
end,
2017-12-21 14:22:34 +08:00
]);
2014-02-13 17:55:43 +08:00
posStart = false;
return ret;
} else {
if (!posStart) {
posStart = options.position;
}
2017-12-21 14:22:34 +08:00
return "[removed]";
2014-02-13 17:55:43 +08:00
}
} else {
return next(tag, html, options);
}
},
remove: function (html) {
2017-12-21 14:22:34 +08:00
var rethtml = "";
2014-02-13 17:55:43 +08:00
var lastPos = 0;
_.forEach(removeList, function (pos) {
2014-02-13 17:55:43 +08:00
rethtml += html.slice(lastPos, pos[0]);
lastPos = pos[1];
});
rethtml += html.slice(lastPos);
return rethtml;
},
2014-02-13 17:55:43 +08:00
};
}
/**
2017-12-21 14:19:10 +08:00
* remove html comments
*
* @param {String} html
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function stripCommentTag(html) {
return html.replace(STRIP_COMMENT_TAG_REGEXP, "");
}
2014-09-11 19:14:27 +08:00
var STRIP_COMMENT_TAG_REGEXP = /<!--[\s\S]*?-->/g;
2014-02-13 17:55:43 +08:00
2015-01-20 13:06:54 +08:00
/**
2017-12-21 14:19:10 +08:00
* remove invisible characters
2015-01-20 13:06:54 +08:00
*
* @param {String} html
* @return {String}
*/
2017-12-21 14:22:34 +08:00
function stripBlankChar(html) {
var chars = html.split("");
chars = chars.filter(function (char) {
var c = char.charCodeAt(0);
if (c === 127) return false;
if (c <= 31) {
if (c === 10 || c === 13) return true;
return false;
}
return true;
});
2017-12-21 14:22:34 +08:00
return chars.join("");
2015-01-20 13:06:54 +08:00
}
2015-12-23 12:33:46 +08:00
exports.whiteList = getDefaultWhiteList();
exports.getDefaultWhiteList = getDefaultWhiteList;
2014-02-13 11:18:03 +08:00
exports.onTag = onTag;
exports.onIgnoreTag = onIgnoreTag;
exports.onTagAttr = onTagAttr;
exports.onIgnoreTagAttr = onIgnoreTagAttr;
exports.safeAttrValue = safeAttrValue;
2014-02-13 14:58:36 +08:00
exports.escapeHtml = escapeHtml;
2014-02-13 11:18:03 +08:00
exports.escapeQuote = escapeQuote;
exports.unescapeQuote = unescapeQuote;
exports.escapeHtmlEntities = escapeHtmlEntities;
exports.escapeDangerHtml5Entities = escapeDangerHtml5Entities;
exports.clearNonPrintableCharacter = clearNonPrintableCharacter;
exports.friendlyAttrValue = friendlyAttrValue;
exports.escapeAttrValue = escapeAttrValue;
2014-02-13 16:27:49 +08:00
exports.onIgnoreTagStripAll = onIgnoreTagStripAll;
2014-02-13 17:56:18 +08:00
exports.StripTagBody = StripTagBody;
exports.stripCommentTag = stripCommentTag;
2015-01-20 13:06:54 +08:00
exports.stripBlankChar = stripBlankChar;
exports.cssFilter = defaultCSSFilter;
exports.getDefaultCSSWhiteList = getDefaultCSSWhiteList;