opyebAPI Bench

CSP for API frontends: a policy that does not break

Security · 7 min read

A Content-Security-Policy is the strongest browser control against injected script, and the fastest way to break a working application. The difference between the two outcomes is usually the order in which the directives are written and whether the policy is deployed in report-only mode first.

Start with default-src

Every directive you do not specify falls back to default-src, and if that is absent, anything is allowed. Setting default-src 'self' first gives you a deny-by-default baseline; every subsequent directive narrows it further. Once the baseline exists, missing directives are restrictive rather than permissive, which is the safe direction to be wrong in.

DirectivePurposeTypical value
default-srcFallback for everything'self'
script-srcJavaScript sources'self' or a nonce
style-srcStylesheets'self' 'unsafe-inline'
img-srcImages'self' data:
connect-srcfetch, XHR, WebSocket'self' plus your API hosts
frame-ancestorsWho may embed you'none'

The nonce pattern

For a server-rendered frontend, the cleanest script policy is a per-response nonce: generate a random value, add script-src 'nonce-…' and put the same value on each script tag. Inline event handlers and eval stop working, which is the point. Static sites without a server can use a build-time hash instead, or keep scripts in external files and rely on 'self'.

Gotcha: when a nonce or hash is present, browsers ignore 'unsafe-inline' in the same directive. Adding both looks permissive but silently disables the inline allowance.

connect-src is the API boundary

For an application that calls an API, connect-src is the directive that matters most: it limits which origins the page can send requests to, which turns a script injection into a much less useful attack. List your API host explicitly and avoid wildcard subdomains. If the frontend uses WebSocket or Server-Sent Events, those origins belong in the same directive.

Deploying without an outage

Send the policy in Content-Security-Policy-Report-Only first, collect violations for a week, and fix the legitimate ones. Then enforce. Keep frame-ancestors 'none' and base-uri 'none' from the start, because they rarely break anything and close two real attack vectors. Add object-src 'none' unless you genuinely embed plugins.

Finally, remember what CSP does not do. It does not sanitise content, it does not stop server-side injection, and it does not replace escaping. It is a second line of defence that makes a successful injection much harder to exploit, and like any second line it should be tested rather than trusted.

Keeping the policy maintainable

A policy that nobody dares to change is a policy that will be bypassed. Keep it in one place, generate it from configuration rather than hand-editing headers per page, and comment why each external source is listed. When a third-party script is added, the review question should be which directive it needs and what happens when it is removed, not simply whether the page still loads.

Report-only data is the best documentation you will get. Review the violations monthly and remove sources that no longer appear. A policy that has shrunk over a year is usually healthier than one that has only grown, because each removed source is a dependency that no longer has permission to run code on your page.

  • Generate the header from one source of truth.
  • Review report-only violations on a schedule.
  • Remove third-party sources when the integration ends, not later.

What to do when something breaks

When a page stops working after enforcement, do not remove the header. Switch that route back to report-only, read the violation report, and add the smallest source that fixes it. Removing the header entirely restores the old exposure silently, and the next person has no record of why the policy was loosened. A short rollback with a note is worth more than a quiet deletion.

Open the calculators and put these numbers to work on your own API.

Open API Bench

Frequently asked questions

Does CSP protect a JSON API endpoint?

It protects the pages that display the data. A raw JSON response has no script execution context, so the header is harmless there and useful on the HTML application that consumes it.

Why do I need 'unsafe-inline' for styles?

Many frameworks inject inline style attributes. style-src can often keep 'unsafe-inline' at low risk, while script-src should use a nonce or hash. Style-based attacks are far less damaging than script execution.

What is report-only mode?

A header that reports violations without blocking them. It lets you find what would break before enforcing, which is the difference between a staged rollout and an outage.

Sources

Related guides