Skip to content

Commit

Permalink
fix: problem with backslash and space at the beginning of attribute v…
Browse files Browse the repository at this point in the history
…alue
  • Loading branch information
lumburr committed Mar 12, 2022
1 parent 621b4c2 commit 5f0eaf8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
48 changes: 29 additions & 19 deletions dist/xss.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ var REGEXP_QUOTE_2 = /"/g;
var REGEXP_ATTR_VALUE_1 = /&#([a-zA-Z0-9]*);?/gim;
var REGEXP_ATTR_VALUE_COLON = /:?/gim;
var REGEXP_ATTR_VALUE_NEWLINE = /&newline;?/gim;
var REGEXP_DEFAULT_ON_TAG_ATTR_3 = /\/\*|\*\//gm;
// 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;
/((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;
Expand Down Expand Up @@ -485,8 +485,8 @@ function filterXSS(html, options) {
exports = module.exports = filterXSS;
exports.filterXSS = filterXSS;
exports.FilterXSS = FilterXSS;
for (var i in DEFAULT) exports[i] = DEFAULT[i];
for (var i in parser) exports[i] = parser[i];
for (let i in DEFAULT) exports[i] = DEFAULT[i];
for (let i in parser) exports[i] = parser[i];

// using `xss` on the browser, output `filterXSS` to the globals
if (typeof window !== "undefined") {
Expand Down Expand Up @@ -521,11 +521,12 @@ var _ = require("./util");
* @return {String}
*/
function getTagName(html) {
var i = _.spaceIndex(html);
let i = _.spaceIndex(html);
let tagName;
if (i === -1) {
var tagName = html.slice(1, -1);
tagName = html.slice(1, -1);
} else {
var tagName = html.slice(1, i + 1);
tagName = html.slice(1, i + 1);
}
tagName = _.trim(tagName).toLowerCase();
if (tagName.slice(0, 1) === "/") tagName = tagName.slice(1);
Expand Down Expand Up @@ -620,7 +621,7 @@ function parseTag(html, onTag, escapeHtml) {
return rethtml;
}

var REGEXP_ILLEGAL_ATTR_NAME = /[^a-zA-Z0-9_:\.\-]/gim;
var REGEXP_ILLEGAL_ATTR_NAME = /[^a-zA-Z0-9\\_:.-]/gim;

/**
* parse input attributes and returns processed attributes
Expand All @@ -633,6 +634,7 @@ function parseAttr(html, onAttr) {
"use strict";

var lastPos = 0;
var lastMarkPos = 0;
var retAttrs = [];
var tmpName = false;
var len = html.length;
Expand All @@ -652,19 +654,18 @@ function parseAttr(html, onAttr) {
if (tmpName === false && c === "=") {
tmpName = html.slice(lastPos, i);
lastPos = i + 1;
lastMarkPos = html.charAt(lastPos) === '"' || html.charAt(lastPos) === "'" ? lastPos : findNextQuotationMark(html, i + 1);
continue;
}
if (tmpName !== false) {
if (
i === lastPos &&
(c === '"' || c === "'") &&
html.charAt(i - 1) === "="
i === lastMarkPos
) {
j = html.indexOf(c, i + 1);
if (j === -1) {
break;
} else {
v = _.trim(html.slice(lastPos + 1, j));
v = _.trim(html.slice(lastMarkPos + 1, j));
addAttr(tmpName, v);
tmpName = false;
i = j;
Expand Down Expand Up @@ -723,6 +724,15 @@ function findNextEqual(str, i) {
}
}

function findNextQuotationMark(str, i) {
for (; i < str.length; i++) {
var c = str[i];
if (c === " ") continue;
if (c === "'" || c === '"') return i;
return -1;
}
}

function findBeforeEqual(str, i) {
for (; i > 0; i--) {
var c = str[i];
Expand Down Expand Up @@ -927,7 +937,7 @@ FilterXSS.prototype.process = function (html) {
// if enable stripIgnoreTagBody
var stripIgnoreTagBody = false;
if (options.stripIgnoreTagBody) {
var stripIgnoreTagBody = DEFAULT.StripTagBody(
stripIgnoreTagBody = DEFAULT.StripTagBody(
options.stripIgnoreTagBody,
onIgnoreTag
);
Expand All @@ -941,7 +951,7 @@ FilterXSS.prototype.process = function (html) {
sourcePosition: sourcePosition,
position: position,
isClosing: isClosing,
isWhite: whiteList.hasOwnProperty(tag),
isWhite: Object.prototype.hasOwnProperty.call(whiteList, tag),
};

// call `onTag()`
Expand Down Expand Up @@ -971,21 +981,21 @@ FilterXSS.prototype.process = function (html) {
}
} else {
// call `onIgnoreTagAttr()`
var ret = onIgnoreTagAttr(tag, name, value, isWhiteAttr);
ret = onIgnoreTagAttr(tag, name, value, isWhiteAttr);
if (!isNull(ret)) return ret;
return;
}
});

// build new tag html
var html = "<" + tag;
html = "<" + tag;
if (attrsHtml) html += " " + attrsHtml;
if (attrs.closing) html += " /";
html += ">";
return html;
} else {
// call `onIgnoreTag()`
var ret = onIgnoreTag(tag, html, info);
ret = onIgnoreTag(tag, html, info);
if (!isNull(ret)) return ret;
return escapeHtml(html);
}
Expand Down
Loading

0 comments on commit 5f0eaf8

Please sign in to comment.