Defense-in-Depth Web Security Stack
Layered security: Helmet headers, Subresource Integrity (SRI), and Trusted Types.
Security in depth acknowledges that any single layer can fail. A resilient web architecture combines server-side input validation, strict browser security headers, client-side cryptographic hashing, and runtime isolation.
Relying solely on one defense (e.g. only HTML escaping) fails when developers introduce template engines, direct DOM manipulation, or third-party CDN compromises.
Apply the complete security header suite: Strict-Transport-Security (HSTS), X-Content-Type-Options, Referrer-Policy, Permissions-Policy, and Subresource Integrity (SRI) for external scripts.
Technical Deep Dive
The Essential Security Headers Suite
- Strict-Transport-Security (HSTS)Forces browsers to communicate exclusively over HTTPS for max-age seconds (includeSubDomains; preload).
- X-Content-Type-Options: nosniffPrevents the browser from MIME-sniffing a response away from the declared Content-Type.
- Referrer-Policy: strict-origin-when-cross-originProtects user privacy by stripping sensitive URL path/query parameters on cross-origin requests.
- Permissions-PolicyDisables unused browser hardware APIs like geolocation, camera, and microphone.
Subresource Integrity (SRI)
When loading third-party scripts from CDNs, specify cryptographic hashes (`integrity="sha384-..."`). If the CDN is compromised and the script is altered, the browser aborts execution.
Interactive Mental Model Simulation
Live VisualizerCode Implementation & Patching
<!-- Vulnerable third-party script include without integrity check --> <script src="https://cdn.example.com/analytics.js"></script> <!-- If cdn.example.com is hacked, attacker injects malicious keylogger into your users! -->
<!-- Secure Subresource Integrity (SRI) include --> <script src="https://cdn.example.com/analytics.js" integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC" crossorigin="anonymous"> </script>
The browser computes the SHA-384 hash of the downloaded script. If it does not match the integrity attribute bit-for-bit, execution is blocked immediately.
Test Your Knowledge on Defense-in-Depth Web Security Stack
Challenge yourself with realistic scenario questions in the Web Security Arena.