Design a CSP (Content Security Policy) Enforcer

Medium45 min
1 / 30
System design answer

Design a CSP (Content Security Policy) Enforcer

Medium45 min30 sections
understanding•10 min read

Problem Statement: A Policy Plane for Browser Code Execution

Frames the CSP enforcer as a four-plane security platform: authoring, distribution, enforcement, and feedback.

Problem statement

Design a Content Security Policy (CSP) enforcer for a large web platform. The system must let security and product teams define, test, roll out, and operate browser-enforced policies that restrict which origins may serve scripts, styles, images, fonts, frames, and connections; generate per-response nonces so legitimate inline scripts keep working while unauthorized inline code is blocked; run in report-only mode before enforcement; and ingest, deduplicate, analyze, and act on violation reports at pageview scale.

CSP is a browser feature, not a server feature. The browser is the actual enforcement point: the server's job is to emit the correct header on every HTML response and to operate the feedback loop. That split is the heart of the design. The header is tiny and cheap; the platform around it - policy authoring, safe rollout, nonce lifecycle, violation analytics, rollback - is the engineering work.

Why the problem is distinctive

A naive answer says 'add a header that bans inline scripts.' Google's 2016 study, CSP Is Dead, Long Live CSP by Weichselbaum and colleagues, measured the deployed web and found roughly 94.7% of observed policies were ineffective against XSS because allowlists contained wildcard origins, JSONP-style endpoints, or other script-injectable sources. The lesson is architectural: an allowlist of hosts is not a security boundary by itself. Effective policies are nonce-based or hash-based, use strict-dynamic propagation, and are operated with measurement. So this question is really about building the machine that produces and maintains effective policies across hundreds of properties without breaking production.

The four planes

  1. Policy authoring plane: policy-as-code, compilation, validation, simulation against real page inventories, approval workflow.
  2. Policy distribution plane: signed immutable versions, edge and app caches, propagation SLO, instant rollback.
  3. Enforcement plane: per-response header emission plus nonce generation, at the edge, in app middleware, or both, chosen per route.
  4. Feedback plane: violation ingestion, deduplication, aggregation, dashboards, alerting, and the report-only to enforcing promotion pipeline.

A strong answer keeps these planes separate. A bad publish in the authoring plane must be stoppable before it reaches enforcement. A flood in the feedback plane must never degrade enforcement. Enforcement must keep working with the entire control plane offline, using the last known good cached policy and a local nonce source.

Public baseline versus design assumptions

Public crawl data, such as HTTP Archive's annual Web Almanac analyses, shows CSP present on only a minority of crawled responses, far behind headers like X-Content-Type-Options, which proves the operational gap this system closes. Cloudflare's published security-header analyses reach the same conclusion: adoption is low precisely because operation is hard. All scale numbers used for capacity planning in this answer are explicitly stated design assumptions for a fictional mature platform, labeled wherever they appear.

Key Highlights

  • •The browser enforces; the platform authors, distributes, measures, and rolls back. Header emission is the easy 5%.
  • •Host-allowlist CSP is mostly ineffective; Google's 2016 measurement found about 94.7% of deployed policies failed to block XSS.
  • •Four planes: authoring, distribution, enforcement, feedback - each degrades independently.
  • •Enforcement stays correct with the control plane offline via last-known-good cache and local nonce generation.
  • •Every uncited scale figure in this answer is an explicit design assumption, not a company metric.
Lead With the Enforcement Split
Say in the first two minutes: the browser enforces CSP, the platform only authors and serves the header. This instantly reframes the question from 'add a header' to 'operate a policy machine' and matches how Google and GitHub actually talk about CSP.
Do Not Stop at 'Block Inline Scripts'
A design that bans inline scripts without nonce or hash migration, backward-compat scanning, and report-only rollout will break real applications. Google's measurement shows most allowlist-only policies are ineffective anyway.

Section Rescue Kit

Buzzwords to use:

Strict CSPReport-Only Mode

Safe statements:

  • "I will separate policy authoring from header emission, because they fail differently and scale differently."
  • "Before choosing where enforcement runs, let me define what must keep working when the control plane is down."
Design a CSP (Content Security Policy) Enforcer - System Design | WinJob | WinJob