Skip to content
Module 8 of 8Architecture
Estimated time: 20 min

Defense-in-Depth Web Security Stack

Layered security: Helmet headers, Subresource Integrity (SRI), and Trusted Types.

Core Concept & Mental Model

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.

Threat Vector & Impact

Relying solely on one defense (e.g. only HTML escaping) fails when developers introduce template engines, direct DOM manipulation, or third-party CDN compromises.

Defensive Architecture & Mitigation

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 Visualizer
Defense-in-Depth Layered Switchboard
OWASP Top 10
Active Browser Defense Layers:
HTTP Strict Transport Security (HSTS)ACTIVE
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Protects against: SSL-stripping & unencrypted plaintext MITM eavesdropping.
MIME-Type Sniffing ProtectionACTIVE
X-Content-Type-Options: nosniff
Protects against: Malicious scripts disguised as harmless images or text files.
Subresource Integrity (SRI)ACTIVE
integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
Protects against: Compromised third-party CDNs injecting modified JS into users.
Referrer PolicyACTIVE
Referrer-Policy: strict-origin-when-cross-origin
Protects against: Leaking sensitive internal URL tokens and user parameters to external sites.
Permissions PolicyOFF
Permissions-Policy: camera=(), microphone=(), geolocation=()
Protects against: Unauthorized hardware sensor access by embedded widgets or third-party ads.
Simulate Penetration Vector Against Active Stack:

Code Implementation & Patching

Vulnerable Code (Exploitable)
<!-- 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 Patched Code (Hardened)
<!-- Secure Subresource Integrity (SRI) include -->
<script 
  src="https://cdn.example.com/analytics.js"
  integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
  crossorigin="anonymous">
</script>
Security Analysis & Core Architectural Rule

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.

Launch Practice Arena