The One-Line CSP Header That Killed Our Page: default-src 'self' and Vite's Inline Images
One seemingly reasonable security header can silently break a production page when it hits `data:` URLs from Vite bundling and API endpoints outside the same origin. Teams need to treat CSP configuration as a front-end concern, not just an ops task, and use Report-Only mode to avoid an outage by surprise.
Adding `Content-Security-Policy "default-src 'self';"` to an Nginx config is one line. It's also enough to kill a page: every resource without a dedicated directive inherits that same-origin-only rule, so API requests to a different domain and images Vite inlined as base64 `data:` URLs all get blocked at the browser. The server never sees the requests, and the console fills with refusal errors.
The root cause is `default-src`'s fallback behavior. When `img-src`, `connect-src`, `script-src`, and friends are absent, the browser applies `default-src` to those resource types. Vite's `assetsInlineLimit` (default 4096 bytes) turns small icons into `data:` URIs, which are not `'self'`. Fixing it means enumerating each resource type with its own directive, like `img-src 'self' data: blob:;` and `connect-src 'self';`, while keeping `script-src` strict. Nginx's `always` flag ensures the header applies even on error pages, and `Content-Security-Policy-Report-Only` lets a team see what would be blocked before enforcing anything.
The incident is a cross-team contract failure: ops added a security header that looked correct, but only frontend developers know the actual resource-loading patterns, so a CSP whitelist should be reviewed by both sides before rollout.
The recommendation to keep `script-src` strict while allowing `'unsafe-inline'` for styles is a pragmatic acceptance that modern frameworks generate inline styles at runtime, making a blanket strict policy impractical.
Vite's asset inlining is an optimization that quietly introduces a non-same-origin resource type, which means any team adopting Vite should audit for `data:` and `blob:` URLs before enabling a restrictive security policy.
This one hits home; I'd taken a fall into the default-src fallback issue before understanding it. Running Report-Only first and then tightening is a habit that can really save you.
[silly grin]