How to output HTML but prevent XSS attacks

前端 未结 2 401
忘了有多久
忘了有多久 2020-12-07 00:28

I wrote a php script to fetch the email content.

These contents are HTML format.

I\'d like to display the content, as below



        
2条回答
  •  生来不讨喜
    2020-12-07 00:59

    HTMLPurifer can do that:

    require_once '/path/to/HTMLPurifier.auto.php';
    
    $config = HTMLPurifier_Config::createDefault();
    $purifier = new HTMLPurifier($config);
    $clean_html = $purifier->purify($dirty_html);
    

    It takes dirty HTML (ie possibly containing Javascript) and removes any script.

    PHP doesn't have anything native or built in that can remove Javacript like HTMLPurifier. You could use DOMDocument but this would be a lengthy task because Javascript can execute in some attributes (onerror, onclick) and is not just limited to .

提交回复
热议问题