设置安全的header防止xss跨站攻击

作者:老刘 发布时间:March 20, 2017 分类:学习笔记,资源下载 阅读次数:673

1 修改 php.ini 开启cookie httponly

session.cookie_httponly = 1

2 为webserver开启安全的header[本站为aphache]
如果不使用SSL,请在CSP添加 http:


<IfModule headers_module>
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-Xss-Protection "1; mode=block"
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy  "no-referrer-when-downgrade"

#CSP HSTS HPKP单独设置每个域名
Header always edit Set-Cookie ^(.*)$ "$1;HttpOnly;Secure"
Header always set Content-Security-Policy "default-src https: data: blob: 'unsafe-inline' 'unsafe-eval'"
Header always set Strict-Transport-Security "max-age=31536000"
Header always set Public-Key-Pins "pin-sha256=\"amMeV6gb9QNx0Zf7FtJ19Wa/t2B7KpCF/1n2Js3UuSU=\";pin-sha256=\"YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg=\"; max-age=2592000;includeSubdomains"
</IfModule>

3 nginx设置安全的header

add_header  X-Frame-Options "SAMEORIGIN" always;
add_header  X-Xss-Protection "1; mode=block" always;
add_header  X-Content-Type-Options "nosniff" always;
add_header  Referrer-Policy  "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src https: data: blob: 'unsafe-inline' 'unsafe-eval'" always;

add_header Strict-Transport-Security "max-age=31536000; " always; 


个人备用设置
Header always set Content-Security-Policy "default-src 'self'; script-src 'self'  https://ajax.googleapis.com https://www.google-analytics.com  https://apps.bdimg.com https://lib.sinaapp.com  https://cdnjs.cloudflare.com; img-src 'self'  data: https://www.google-analytics.com; style-src 'self' 'unsafe-inline'  https://fonts.googleapis.com https://cdnjs.cloudflare.com; font-src 'self' https://fonts.gstatic.com https://cdnjs.cloudflare.com; form-action 'self' "

具体参考 https://scotthelme.co.uk/hardening-your-http-response-headers/
https://content-security-policy.com/

标签: 安全, https, 跨站, xss

前一篇:网站安全检测 安全扫描网站

后一篇:生成HTTP公钥固定,HPKP防止中间人攻击

添加新评论 »