The ultimate solution for PHP defense against XSS attacks

Source: Internet
Author: User

Recently tested XSS attack fix, found a relatively good article, share to everyone.

Update20151202:

Thank you for your attention and answer, at present I learned from various ways of defense methods, organized as follows:

PHP directly output HTML, you can use the following methods to filter:

1 1.htmlspecialchars函数
2
3 2.htmlentities函数
4
5 3.HTMLPurifier.auto.php插件
6
7 4.RemoveXss函数(百度可以查到)

PHP output to the JS code, or the development of the JSON API, you need to filter the front-end in JS:

1 1.尽量使用innerText(IE)和textContent(Firefox),也就是jQuery的text()来输出文本内容
2
3 2.必须要用innerHTML等等函数,则需要做类似php的htmlspecialchars的过滤(参照@eechen的答案)

Other general complementary defense methods

01 1.在输出html时,加上Content Security Policy的Http Header
02
03 (作用:可以防止页面被XSS攻击时,嵌入第三方的脚本文件等)
04 (缺陷:IE或低版本的浏览器可能不支持)
05
06 2.在设置Cookie时,加上HttpOnly参数
07
08 (作用:可以防止页面被XSS攻击时,Cookie信息被盗取,可兼容至IE6)
09 (缺陷:网站本身的JS代码也无法操作Cookie,而且作用有限,只能保证Cookie的安全)
10
11 3.在开发API时,检验请求的Referer参数
12
13 (作用:可以在一定程度上防止CSRF攻击)
14 (缺陷:IE或低版本的浏览器中,Referer参数可以被伪造)

4. Add an XSS filter function.

01 functionclean_xss(&$string$low= False)
02     {
03         if(! is_array $string))
04         {
05             $string= trim ( $string);
06             $stringstrip_tags $string);
07             $string= htmlspecialchars ( $string);
08             if($low)
09             {
10                 returnTrue;
11             }
[    ;            $string   str_replace  < Code class= "Brush Plain" (  array   ( ",   $string  
13             $no‘/%0[0-8bcef]/‘;
14             $string= preg_replace ( $no‘‘$string);
15             $no‘/%1[0-9a-f]/‘;
16             $string= preg_replace ( $no‘‘$string);
17             $no‘/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S‘;
18             $string= preg_replace ( $no‘‘$string);
19             returnTrue;
20         }
21         $keysarray_keys $string);
22         foreach$keys as $key)
23         {
24             clean_xss ( $string[$key] );
25         }
26     }

The ultimate solution for PHP defense against XSS attacks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.