Does this set of regular expressions FULLY protect against cross site scripting?

前端 未结 11 817
旧巷少年郎
旧巷少年郎 2020-12-13 11:35

What\'s an example of something dangerous that would not be caught by the code below?

EDIT: After some of the comments I added another line, commented below. See V

11条回答
  •  佛祖请我去吃肉
    2020-12-13 12:20

    Another vote for whitelisting. But it looks like you're going about this the wrong way. The way I do it, is to parse the HTML into a tag tree. If the tag you're parsing is in the whitelist, give it a tree node, and parse on. Same goes for its attributes.

    Dropped attributes are just dropped. Everything else is HTML-escaped literal content.

    And the bonus of this route is because you're effectively regenerating all the markup, it's all completely valid markup! (I hate it when people leave comments and they screw up the validation/design.)

    Re "I can't whitelist" (para): Blacklisting is a maintenance-heavy approach. You'll have to keep an eye on new exploits and make sure your covered. It's a miserable existence. Just do it right once and you'll never need to touch it again.

提交回复
热议问题