top of page

Defending Against XSS Using CSP (script-src)

  • Writer: Shounak Itraj
    Shounak Itraj
  • 4 days ago
  • 1 min read

Content Security Policy (CSP) is one of the most powerful defences against Cross-Site Scripting (XSS) attacks. The script-src directive controls which scripts are permitted to execute in the browser, effectively blocking injected malicious scripts even if an attacker manages to inject content into your page. Key script-src approaches: nonce-based CSP: A cryptographically random nonce is generated per request and embedded in both the HTTP header and script tags. Only scripts with matching nonces execute. This is the recommended approach for dynamic applications. hash-based CSP: A SHA hash of allowed inline scripts is included in the header. Suitable for static inline scripts that don't change. strict-dynamic: Allows scripts loaded by trusted scripts to also execute, simplifying CSP deployment in complex SPAs. Avoid unsafe-inline and unsafe-eval at all costs — these negate most XSS protections. A well-configured script-src policy significantly reduces your XSS attack surface and is a critical layer in any defence-in-depth web security strategy.

Recent Posts

See All
Port Scanner using Go Programming

Building a port scanner in Go is an excellent way to learn both network programming and Go's powerful concurrency model. Port scanners are fundamental tools in security reconnaissance — understanding

 
 
 
Content Security Policy

Content Security Policy (CSP) is a browser security mechanism that helps prevent cross-site scripting (XSS) and data injection attacks. It works by allowing web developers to specify which content sou

 
 
 

Comments


bottom of page