Teknologi, AI, internet, dan keamanan digitalKunjungi Nazuaf.com ↗
NNazuaf.
Keamanan Siber

Content Security Policy (CSP): How to Protect Your Website from XSS Attacks

Learn how Content Security Policy (CSP) works, essential directives, header examples, and implementation steps to help reduce XSS risks on your website.

Redaksi Nazuaf··4 menit
Content Security Policy (CSP): How to Protect Your Website from XSS Attacks

Websites can display dynamic content, load third-party scripts, and communicate with various services. This flexibility is useful, but it also creates opportunities for attackers to inject malicious scripts. One defense layer that can help reduce this risk is Content Security Policy (CSP).

What Is Content Security Policy?

Content Security Policy is a security policy sent by the server to the browser via an HTTP Content-Security-Policy header. This policy specifies which sources are allowed to load scripts, stylesheets, images, fonts, frames, and other resource types. With proper rules, the browser can reject unauthorized sources.

CSP is not a replacement for input validation or secure output encoding. Its function is to add a defense layer, particularly to help limit the impact of cross-site scripting (XSS) attacks.

How Does CSP Work?

When a browser receives a page, it also reads the CSP from the server. Each time the page attempts to load a resource or execute a script, the browser compares that action against the defined rules. If the action violates the policy, the browser can block it and log the violation in the developer console.

For example, the default-src 'self' directive makes the website’s own origin the default rule for several resource types. More specific directives, such as img-src or script-src, can regulate particular resource types separately.

CSP Header Examples

Content-Security-Policy: default-src 'self'; img-src 'self' https:; object-src 'none'; base-uri 'self'; frame-ancestors 'self'

This example restricts default sources to the same origin, allows images from the same origin and HTTPS connections, blocks objects such as legacy plugins, limits base URL elements, and only permits the page to be displayed in frames by the same origin. This is merely a starting point; an appropriate policy must follow the actual needs of the website.

Important CSP Directives

  • default-src: the default rule for resource types without more specific directives.
  • script-src: specifies which script sources may execute.
  • style-src: controls stylesheet sources.
  • img-src: restricts image sources.
  • connect-src: governs connection destinations for APIs such as Fetch, XHR, and WebSocket.
  • object-src 'none': disables loading of objects such as Flash.
  • frame-ancestors: limits which sites may embed the page via frames.
  • upgrade-insecure-requests: requests that the browser upgrade HTTP URLs to HTTPS when possible.

How to Implement CSP Safely

  1. Inventory your resources. Document the scripts, fonts, images, APIs, and third-party services your website actually uses.
  2. Start in report-only mode. Use Content-Security-Policy-Report-Only to monitor violations without immediately blocking resources. Check reports and the browser console.
  3. Build rules gradually. Avoid adding 'unsafe-inline' or 'unsafe-eval' without strong justification. For inline scripts, consider random nonces per response or matching hashes.
  4. Test critical pages. Check forms, logins, editors, analytics, media players, and other features to ensure the policy does not break required functionality.
  5. Enable enforcement. Once reports show the rules are correct, switch to the standard Content-Security-Policy header and continue monitoring violations.

Mistakes to Avoid

A common mistake is immediately deploying a very strict policy without testing the website, or conversely using wildcards and overly broad sources that weaken protection. Do not copy policies from other websites blindly: each site’s resources and architecture differ.

Also remember that CSP does not automatically fix XSS vulnerabilities. Applications must still validate data, encode output according to context, avoid dangerous DOM patterns, and update dependencies.

How to Check Your CSP

Open Developer Tools in your browser, select the Network tab, then reload the page and check the response headers for the main document. CSP can also be verified through the console to see whether the browser reports policy violations. If using WordPress, ensure header changes are made through appropriate hosting, server, or security solution configurations, and test first in a safe environment.

Conclusion

Content Security Policy helps websites limit which resources can be loaded and which scripts the browser can execute. Gradual implementation, report monitoring, and specific rules make CSP more effective without breaking site functionality. Use CSP as part of a layered security strategy, not as the sole protection against XSS. CSP also complements connection protection; see the guide on HTTPS and TLS to understand transport security.

Terus jelajahi Nazuaf

Baca topik lain atau kunjungi situs utama untuk melihat publikasi Nazuaf.

Artikel kategori ini →

Komentar (0)

Belum ada komentar. Jadilah yang pertama berkomentar.

Komentar langsung tampil setelah dikirim. Mohon berkomentar dengan sopan.