</>
⌕
Senior #csp#security-headers#xss#hsts

Content Security Policy (CSP) là gì? Các security header quan trọng khác?

⚡ Trả lời nhanh

CSP là header cho trình duyệt biết nguồn nào được phép tải script, style, ảnh..., nhờ đó giảm thiệt hại của XSS (script chèn vào bị chặn nếu không khớp policy). Nên dùng nonce hoặc hash thay vì 'unsafe-inline', và thử bằng Report-Only trước khi enforce.

Content-Security-Policy:
  default-src 'self';
  script-src 'self' 'nonce-r4nd0m';
  img-src 'self' https: data:;
  object-src 'none'; base-uri 'self'; frame-ancestors 'none'
<script nonce="r4nd0m">/* inline script được phép */</script>
HeaderCông dụng
Strict-Transport-SecurityÉp HTTPS
X-Content-Type-Options: nosniffChặn đoán MIME type
Referrer-PolicyGiới hạn thông tin referrer
Permissions-PolicyTắt API như camera, geolocation
frame-ancestors (CSP)Chống clickjacking (thay X-Frame-Options)
  • Nonce phải sinh ngẫu nhiên mới cho mỗi response; hash dùng cho script inline cố định.
  • strict-dynamic cho phép script tin cậy tự nạp thêm script mà không liệt kê domain.
  • Triển khai dần: Content-Security-Policy-Report-Only kèm report-to, quan sát vi phạm rồi mới enforce.
🃏 Ôn bằng flashcard