QA
Beginner LevelWeb Security & Cyber Defense

What is Content Security Policy (CSP) and how do script-src directive nonces restrict script execution?

❓ Interview Prompt: Explain CSP HTTP header, `default-src`, `script-src`, nonces (`'nonce-rAnd0m'`), hashes, and mitigating XSS script execution.

Content Security Policy (CSP) is an HTTP response header that restricts which domain origins and resources (scripts, styles, images, iframes) the browser is allowed to load and execute:

Key Directives:

•
'default-src 'self'': Restricts resource loading to same domain by default.
•
'script-src 'self' 'nonce-rAnd0m234'': Disables inline '<script>' tags and 'eval()' execution unless the inline script tag includes a matching single-use cryptographic nonce ('<script nonce="rAnd0m234">').

Code Implementation Example

http
HTTP/1.1 200 OK
Content-Type: text/html
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-2726c7f26c'; object-src 'none'; base-uri 'self';
💡HTTP Content-Security-Policy header enforcing strict nonce script restrictions.
🎯 Interview Key Takeaway

CSP HTTP headers restrict executable script sources and disable dangerous inline scripts to neutralize XSS attacks.

More Web Security & Cyber Defense Interview Questions

Beginner LevelSecurity & Web Security

Explain OWASP Top 10 vulnerabilities (XSS, CSRF, SQL Injection) and their defenses.

Beginner LevelSecurity & Web Security

What are the 3 types of XSS (Stored, Reflected, DOM-based) and how do they differ?

Beginner LevelSecurity & Web Security

How do Parameterized Queries (Prepared Statements) prevent SQL Injection attacks?