-
Notifications
You must be signed in to change notification settings - Fork 0
feat(security): merge project CSP into the platform floor instead of replacing it #3422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
25f6d21
feat(security): merge project CSP into the platform floor instead of …
kojiwakayama 847782b
fix(security,docs): satisfy the docs and typecheck gates the local ch…
kojiwakayama 5202498
docs(api-reference): regenerate security.md after the CSP surface change
kojiwakayama 6659da6
fix(security): close the remaining review findings on the CSP merge
kwakayama c6b2798
docs(security): use ensure for the policy verification step
kwakayama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| --- | ||
| title: "Security headers and CSP" | ||
| description: "Veryfront applies a Content-Security-Policy by default. Use this guide to allow Google Fonts, analytics, and other third-party origins." | ||
| order: 11 | ||
| --- | ||
|
|
||
| Every hosted Veryfront project is served with a Content-Security-Policy and a set of hardening headers. You do not switch them on; they apply in production whether or not you configure anything. What you do configure is the extra origins your own site needs. | ||
|
|
||
| ## The default policy | ||
|
|
||
| In production, Veryfront serves this policy: | ||
|
|
||
| ```http | ||
| default-src 'self'; | ||
| script-src 'self' 'nonce-<generated>' https://esm.sh; | ||
| style-src 'self' 'unsafe-inline'; | ||
| style-src-attr 'unsafe-inline'; | ||
| img-src 'self' https://images.veryfront.com https://cdn.veryfront.com data:; | ||
| font-src 'self' data:; | ||
| connect-src 'self' https://esm.sh; | ||
| media-src 'self' blob:; | ||
| worker-src 'self' blob:; | ||
| object-src 'none'; | ||
| frame-src 'self'; | ||
| frame-ancestors 'none'; | ||
| base-uri 'self'; | ||
| form-action 'self' | ||
| ``` | ||
|
|
||
| Alongside it: `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`, `Referrer-Policy: strict-origin-when-cross-origin`, `Strict-Transport-Security`, and `Cross-Origin-Opener-Policy` / `Cross-Origin-Resource-Policy` set to `same-origin`. | ||
|
|
||
| Development serves no CSP at all, so HMR and dev tooling are never blocked and a local allowance can never widen your production policy. | ||
|
|
||
| Two directives are worth understanding: | ||
|
|
||
| - **`script-src` includes `https://esm.sh`** because the renderer writes React imports from that CDN into every document. A fresh nonce is generated per response for the framework's own inline bootstrap. | ||
| - **`frame-ancestors`** is `'none'` on your own domain. On `*.veryfront.com` addresses it instead allows the Studio origins, so the Studio preview iframe works. | ||
|
|
||
| ## Adding an origin | ||
|
|
||
| Set `security.csp` in `veryfront.config.ts`. Values are **added to** the defaults, so you never restate them: | ||
|
|
||
| ```ts | ||
| export default { | ||
| security: { | ||
| csp: { | ||
| styleSrc: ["https://fonts.googleapis.com"], | ||
| fontSrc: ["https://fonts.gstatic.com"], | ||
| }, | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| That is the complete Google Fonts setup: `fonts.googleapis.com` serves the stylesheet, `fonts.gstatic.com` serves the font files, and both directives keep everything they already had. | ||
|
|
||
| Directive names may be camelCase (`fontSrc`) or the CSP spelling (`font-src`). Both work; camelCase matches the rest of your config. You do not need to repeat `'self'`; it is already there. | ||
|
|
||
| A few more examples: | ||
|
|
||
| ```ts | ||
| export default { | ||
| security: { | ||
| csp: { | ||
| // An analytics endpoint your client code posts to | ||
| connectSrc: ["https://analytics.example.com"], | ||
| // Embedding YouTube | ||
| frameSrc: ["https://www.youtube.com"], | ||
| // Images from your own CDN | ||
| imgSrc: ["https://cdn.example.com"], | ||
| }, | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| Misspelling a directive fails configuration loading rather than silently doing nothing. Browsers ignore unrecognized directive names, so `fontSource: [...]` would otherwise look configured and protect nothing. | ||
|
|
||
| ## What you cannot remove | ||
|
|
||
| Some sources are structural: the renderer writes those URLs into the documents it serves, so a project that dropped them would break only its own site. `'self'`, the nonce, `https://esm.sh` in `script-src`, and the platform image origins are always present. No `security.csp` setting can remove them. (`VERYFRONT_CSP`, described below, is an operations-level exception.) | ||
|
|
||
| Everything else is a convenience you can drop. | ||
|
|
||
| ## Tightening the policy | ||
|
|
||
| To remove the platform's optional sources for one directive, set it to `null`: | ||
|
|
||
| ```ts | ||
| export default { | ||
| security: { | ||
| csp: { | ||
| // Serve no inline styles. Keeps 'self', drops 'unsafe-inline'. | ||
| styleSrc: null, | ||
| }, | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| `null` removes the optional half of a directive and keeps the required half. It cannot lock you out of your own site. | ||
|
|
||
| Before doing this, check what your components actually need. `'unsafe-inline'` is in the default `style-src` because many React component libraries, including Veryfront's own, create styles at runtime. Removing it is safe only if you are certain yours do not. | ||
|
|
||
| ## Replacing the policy entirely | ||
|
|
||
| Setting `VERYFRONT_CSP` in the environment replaces the whole policy, including the sources the renderer needs: | ||
|
|
||
| ```bash | ||
| VERYFRONT_CSP="default-src 'self'; script-src 'self' 'nonce-{NONCE}'" | ||
| ``` | ||
|
|
||
| `{NONCE}` is substituted with the per-response nonce. Omitting `https://esm.sh` from `script-src` will stop your pages hydrating, so this is an operations-level escape hatch for policies you intend to own completely, not the way to add an origin. Use `security.csp` for that. | ||
|
|
||
| ## Verify it worked | ||
|
|
||
| Read the policy your site is actually serving: | ||
|
|
||
| ```bash | ||
| curl -sS -D - -o /dev/null https://your-site.example/ | grep -i content-security-policy | ||
| ``` | ||
|
|
||
| Ensure the origin you added appears in the directive you added it to, alongside the existing sources. | ||
|
|
||
| Then load the site with the browser console open. CSP violations name the directive that blocked the request, which maps directly onto the config key: a `style-src` violation is fixed with `styleSrc`, a `font-src` violation with `fontSrc`. | ||
|
|
||
| Preview deployments serve the same policy as production, so a CSP problem shows up on your preview URL before it reaches your live site. | ||
|
|
||
| ## Related | ||
|
|
||
| - [Configuration](./configuration.md): the full `veryfront.config.ts` reference | ||
| - [Middleware](./middleware.md): CORS, rate limiting, and auth checks | ||
| - [Deploying](./deploying.md): preview and production environments | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.