跪拜 Guibai
← All articles
Frontend · Browser

The One-Line CSP Header That Killed Our Page: default-src 'self' and Vite's Inline Images

By 谢小飞 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

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.

Summary

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.

Takeaways
— `default-src` acts as a fallback for any resource type without an explicit directive like `img-src` or `connect-src`; writing only `'self'` effectively applies same-origin to scripts, styles, images, fonts, and API calls.
— Vite's `assetsInlineLimit` default of 4096 bytes inlines small images as base64 `data:` URLs, which are blocked by a `'self'`-only policy because `data:` is a separate scheme.
— A working CSP fix requires per-resource directives: `img-src 'self' data: blob:;`, `font-src 'self' data:;`, `connect-src 'self';`, and strict `script-src 'self';` without `'unsafe-inline'`.
— Nginx's `add_header` only applies to a fixed set of 2xx and 3xx status codes unless the `always` flag is added, leaving error pages without the CSP header.
— `Content-Security-Policy-Report-Only` lets you collect what would be blocked in the console without actually blocking anything, so you can refine the whitelist before enabling enforcement.
— Debugging CSP refusals follows a repeatable path: read the console error, verify the response header in Network, identify the blocked URL's scheme, and map it to the applicable directive or `default-src` fallback.
Conclusions

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.

Concepts & terms
Content-Security-Policy (CSP)
A server-sent HTTP response header that tells the browser which resources a page may load and from which origins. The browser enforces it as a whitelist, blocking anything not listed.
default-src fallback
When a resource type lacks a dedicated directive like `img-src` or `connect-src`, the browser uses the value of `default-src` as the fallback policy for that resource type.
Vite assetsInlineLimit
A build option (default 4096 bytes) that inlines assets smaller than the threshold as base64 `data:` URLs directly into the bundled JavaScript, rather than emitting separate files.
Content-Security-Policy-Report-Only
A CSP response header that reports policy violations in the console or via `report-uri` without blocking anything, used to test a policy before enforcing it.
Nginx add_header 'always'
An optional flag on the `add_header` directive that makes the header apply to all response status codes, not just the default set of successful and redirect codes.
From the discussion
Featured comments
组合搭配函数

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]

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗