Fix a few edge cases for XSS
This commit is contained in:
parent
271a23cdad
commit
1c3273978b
15
lib/xss.js
15
lib/xss.js
|
|
@ -59,7 +59,7 @@ TagParser.prototype.parse = function () {
|
||||||
|
|
||||||
// Attributes
|
// Attributes
|
||||||
var attrs = {};
|
var attrs = {};
|
||||||
while (this.text[this.i] !== ">") {
|
while (this.i < this.text.length && this.text[this.i] !== ">") {
|
||||||
var key = this.readLiteralOrString(/[^\s=>]/);
|
var key = this.readLiteralOrString(/[^\s=>]/);
|
||||||
this.skipWhitespace();
|
this.skipWhitespace();
|
||||||
if (this.text[this.i] !== "=") {
|
if (this.text[this.i] !== "=") {
|
||||||
|
|
@ -77,7 +77,10 @@ TagParser.prototype.parse = function () {
|
||||||
}
|
}
|
||||||
this.skipWhitespace();
|
this.skipWhitespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.i < this.text.length) {
|
||||||
this.i++;
|
this.i++;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
tagName: tname,
|
tagName: tname,
|
||||||
|
|
@ -153,9 +156,17 @@ function sanitizeHTML(str) {
|
||||||
for (var k in t.attributes) {
|
for (var k in t.attributes) {
|
||||||
if (k.match(badAttrs)) {
|
if (k.match(badAttrs)) {
|
||||||
delete t.attributes[k];
|
delete t.attributes[k];
|
||||||
} else if (t.attributes[k].match(badAttrValues)) {
|
} else {
|
||||||
|
if (t.attributes[k].match(badAttrValues)) {
|
||||||
t.attributes[k] = t.attributes[k].replace(badAttrValues, "");
|
t.attributes[k] = t.attributes[k].replace(badAttrValues, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var k2 = k.replace(/[^\w]/g, "");
|
||||||
|
if (k2 !== k) {
|
||||||
|
t.attributes[k2] = t.attributes[k];
|
||||||
|
delete t.attributes[k];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var fmt = "<" + t.tagName;
|
var fmt = "<" + t.tagName;
|
||||||
for (var k in t.attributes) {
|
for (var k in t.attributes) {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,11 @@ function basicTest() {
|
||||||
|
|
||||||
assert(sanitize("<a href='javascript:alert(document.cookie)'>") ===
|
assert(sanitize("<a href='javascript:alert(document.cookie)'>") ===
|
||||||
"<a href=\":()\">");
|
"<a href=\":()\">");
|
||||||
|
|
||||||
|
assert(sanitize("<a ") === "<a>");
|
||||||
|
|
||||||
|
assert(sanitize("<img src=\"<a href=\"javascript:void(0)\">>") ===
|
||||||
|
"<img src=\"<a href=\" javascriptvoid0=\"\">>");
|
||||||
}
|
}
|
||||||
|
|
||||||
basicTest();
|
basicTest();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue