Strict HTML Validation and Filtering in PHP

前端 未结 4 804
刺人心
刺人心 2020-12-08 03:04

I\'m looking for best practices for performing strict (whitelist) validation/filtering of user-submitted HTML.

Main purpose is to filter out XSS and similar nasties

4条回答
  •  情歌与酒
    2020-12-08 03:41

    I've tested all exploits I know on HTML Purifier and it did very well. It filters not only HTML, but also CSS and URLs.

    Once you narrow elements and attributes to innocent ones, the pitfalls are in attribute content – javascript: pseudo-URLs (IE allows tab characters in protocol name - java script: still works) and CSS properties that trigger JS.

    Parsing of URLs may be tricky, e.g. these are valid: http://spoof.com:xxx@evil.com or //evil.com. Internationalized domains (IDN) can be written in two ways – Unicode and punycode.

    Go with HTML Purifier – it has most of these worked out. If you just want to fix broken HTML, then use HTML Tidy (it's available as PHP extension).

提交回复
热议问题