mirror of https://github.com/calzoneman/sync.git
Comment xss.js
This commit is contained in:
parent
2e8f31ae2b
commit
d06ca9a99f
80
lib/xss.js
80
lib/xss.js
|
@ -1,15 +1,42 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
Copyright (c) 2013 Calvin Montgomery
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
WARNING
|
||||
|
||||
This file contains an XSS prevention module I wrote myself. It has not
|
||||
been verified by any external agency, and due to the nature of XSS I cannot
|
||||
guarantee that it will filter correctly. Feel free to send me bug reports
|
||||
and I will do my best to fix them, but use at your own risk.
|
||||
|
||||
*/
|
||||
|
||||
/* Prototype for a basic XML tag parser */
|
||||
function TagParser(text) {
|
||||
this.text = text;
|
||||
this.i = 0;
|
||||
this.tag = this.parse();
|
||||
}
|
||||
|
||||
/* Moves the position marker past any whitespace characters */
|
||||
TagParser.prototype.skipWhitespace = function () {
|
||||
while (this.i < this.text.length && this.text[this.i].match(/\s/)) {
|
||||
this.i++;
|
||||
}
|
||||
};
|
||||
|
||||
/* Reads a literal value matching the given regexp. Defaults
|
||||
to /[^\s>]/; i.e. any string not containing whitespace or
|
||||
the end of tag character '>'
|
||||
*/
|
||||
TagParser.prototype.readLiteral = function (regexp) {
|
||||
if (regexp === void 0) {
|
||||
regexp = /[^\s>]/;
|
||||
|
@ -22,6 +49,9 @@ TagParser.prototype.readLiteral = function (regexp) {
|
|||
return str;
|
||||
};
|
||||
|
||||
/* If the character at the current position is a quote, read
|
||||
a string. Otherwise, read a literal
|
||||
*/
|
||||
TagParser.prototype.readLiteralOrString = function (regexp) {
|
||||
if (this.text[this.i].match(/["']/)) {
|
||||
return this.readString();
|
||||
|
@ -29,6 +59,11 @@ TagParser.prototype.readLiteralOrString = function (regexp) {
|
|||
return this.readLiteral(regexp);
|
||||
};
|
||||
|
||||
/* Read a string delimited by the character at the current
|
||||
position. For XML tags this means strings enclosed in
|
||||
" or '. Treats \" as a literal '"' symbol and not a
|
||||
delimiter.
|
||||
*/
|
||||
TagParser.prototype.readString = function () {
|
||||
var delim = this.text[this.i++];
|
||||
|
||||
|
@ -46,8 +81,17 @@ TagParser.prototype.readString = function () {
|
|||
return str;
|
||||
};
|
||||
|
||||
/* Attempts to parse a tagname and attributes from an
|
||||
XML tag.
|
||||
NOTE: Does not actually parse a DOM node, only parses
|
||||
the tag between '<' and '>' because that's all I need
|
||||
to do XSS filtering, I don't care what's between a tag
|
||||
and its end tag (if it's another tag I handle that
|
||||
separately)
|
||||
*/
|
||||
TagParser.prototype.parse = function () {
|
||||
this.i = this.text.indexOf("<");
|
||||
// Not a tag
|
||||
if (this.i === -1) {
|
||||
return null;
|
||||
}
|
||||
|
@ -55,13 +99,19 @@ TagParser.prototype.parse = function () {
|
|||
this.i++;
|
||||
this.skipWhitespace();
|
||||
|
||||
// First non-whitespace string after the opening '<' is the tag name
|
||||
var tname = this.readLiteral();
|
||||
|
||||
// Attributes
|
||||
var attrs = {};
|
||||
// Continue parsing attributes until the end of string is reached or
|
||||
// the end of tag is reached
|
||||
while (this.i < this.text.length && this.text[this.i] !== ">") {
|
||||
// Read any string not containing equals, possibly delimited by
|
||||
// " or '
|
||||
var key = this.readLiteralOrString(/[^\s=>]/);
|
||||
this.skipWhitespace();
|
||||
// It's possible for tags to have attributes with no value, where
|
||||
// the equals sign is not necessary
|
||||
if (this.text[this.i] !== "=") {
|
||||
if (key.trim().length > 0) {
|
||||
attrs[key] = "";
|
||||
|
@ -78,6 +128,7 @@ TagParser.prototype.parse = function () {
|
|||
this.skipWhitespace();
|
||||
}
|
||||
|
||||
// If end-of-string was not reached, consume the ending '>'
|
||||
if (this.i < this.text.length) {
|
||||
this.i++;
|
||||
}
|
||||
|
@ -85,7 +136,7 @@ TagParser.prototype.parse = function () {
|
|||
return {
|
||||
tagName: tname,
|
||||
attributes: attrs,
|
||||
text: this.text.substring(0, this.i)
|
||||
text: this.text.substring(0, this.i) // Original text (for replacement)
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -125,11 +176,18 @@ const badTags = new RegExp([
|
|||
"xss"
|
||||
].join("|"), "i");
|
||||
|
||||
/* Nasty attributes. Anything starting with "on" is probably a javascript
|
||||
callback, and I hope you see why formaction is a bad idea.
|
||||
*/
|
||||
const badAttrs = new RegExp([
|
||||
"\\bon\\S*",
|
||||
"\\bformaction"
|
||||
].join("|"), "i");
|
||||
|
||||
/* These are things commonly used in the values of HTML attributes of
|
||||
XSS injections. Go ahead and strip them, they don't have any other
|
||||
use besides javascript
|
||||
*/
|
||||
const badAttrValues = new RegExp([
|
||||
"alert",
|
||||
"document.cookie",
|
||||
|
@ -142,25 +200,32 @@ const badAttrValues = new RegExp([
|
|||
function sanitizeHTML(str) {
|
||||
var i = str.indexOf("<");
|
||||
if (i === -1) {
|
||||
// No HTML tags
|
||||
// No HTML tags in the string
|
||||
return str;
|
||||
}
|
||||
|
||||
// Loop across all tag delimiters '<' in string, parse each one,
|
||||
// and replace the results with sanitized tags
|
||||
while (i !== -1) {
|
||||
var t = new TagParser(str.substring(i)).tag;
|
||||
if (t.tagName.replace("/", "").match(badTags)) {
|
||||
// Note: Important that I replace the tag with a nonempty value,
|
||||
// otherwise <scr<script>ipt> would possibly defeat the filter.
|
||||
str = str.replace(t.text, "[tag removed]");
|
||||
i = str.indexOf("<", i+1);
|
||||
continue;
|
||||
}
|
||||
for (var k in t.attributes) {
|
||||
// If it's an evil attribute, just nuke it entirely
|
||||
if (k.match(badAttrs)) {
|
||||
delete t.attributes[k];
|
||||
} else {
|
||||
if (t.attributes[k].match(badAttrValues)) {
|
||||
// As above, replacing with a nonempty string is important.
|
||||
t.attributes[k] = t.attributes[k].replace(badAttrValues, "[removed]");
|
||||
}
|
||||
|
||||
// Keys should not contain non-word characters.
|
||||
var k2 = k.replace(/[^\w]/g, "");
|
||||
if (k2 !== k) {
|
||||
t.attributes[k2] = t.attributes[k];
|
||||
|
@ -168,9 +233,15 @@ function sanitizeHTML(str) {
|
|||
}
|
||||
}
|
||||
}
|
||||
// Build the sanitized tag
|
||||
var fmt = "<" + t.tagName;
|
||||
for (var k in t.attributes) {
|
||||
fmt += " " + k + '="' + t.attributes[k] + '"';
|
||||
if (k.trim().length > 0) {
|
||||
fmt += " " + k;
|
||||
if (t.attributes[k].trim().length > 0) {
|
||||
fmt += '="' + t.attributes[k] + '"';
|
||||
}
|
||||
}
|
||||
}
|
||||
str = str.replace(t.text, fmt + ">");
|
||||
i = str.indexOf("<", i + fmt.length + 1);
|
||||
|
@ -179,6 +250,7 @@ function sanitizeHTML(str) {
|
|||
return str;
|
||||
}
|
||||
|
||||
/* WIP: Sanitize a string where HTML is prohibited */
|
||||
function sanitizeText(str) {
|
||||
str = str.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
|
|
Loading…
Reference in New Issue