validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

More details about regexp patterns in xss.js

Open discretepackets opened this issue 13 years ago • 1 comments

Hi!

I was looking at the regular expression patterns in xss.js and am wondering about the patterns at L160-161

var event_handlers = ['on\\w*', 'style', 'formaction'];

...

attribs = attribs.concat(str.match(new RegExp("("+event_handlers.join('|')+")\\s*=\\s*(\\x22|\\x27)([^\\2]*?)(\\2)", 'ig')));
attribs = attribs.concat(str.match(new RegExp("("+event_handlers.join('|')+")\\s*=\\s*([^\\s>]*)", 'ig')));

It appears that the first pattern attempts to match strings of the form beginning with style = ' or style=" etc but I'm not sure what the \2 pattern is supposed to match?

It will be helpful if some explanations are included in the code with the reg exp.

Thanks!!

discretepackets avatar Jun 17 '13 20:06 discretepackets

The \2 pattern references the second matching group. It's useful in situations like /style=('|")([^\1]+?)\1/ where you want to match an attribute like style="..." or style='...' but not style="...'.

I don't have time to document the patterns but you could go through the CodeIgniter xss code from which this is ported from to get more information. If you're just after general info on regular expressions, regularexpressions.info is a great resource.

chriso avatar Jun 19 '13 03:06 chriso