@rsc-kit/mcp 0.30.54 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,119 @@
1
+ # Content Security Policy
2
+
3
+ > A strict policy on every page, with the engine allowing its own inline code by hash or nonce.
4
+
5
+ A Content-Security-Policy that refuses inline scripts is the best defence a
6
+ page has against injected markup turning into injected code. The catch is
7
+ that a React page has inline code of its own: the script that boots it, and,
8
+ where a page streams, the scripts React writes to move each finished part into
9
+ place. Those have to be allowed without allowing everything.
10
+
11
+ Name the policy, and the engine adds what each page's own inline code needs:
12
+
13
+ ```ts title="vite.config.ts"
14
+ rscKit({
15
+ contentSecurityPolicy: {},
16
+ })
17
+ ```
18
+
19
+ `{}` is a strict default:
20
+
21
+ ```
22
+ default-src 'self'; base-uri 'self'; object-src 'none'; style-src-attr 'unsafe-inline'
23
+ ```
24
+
25
+ To which every page adds a `script-src` and a `style-src` naming its own
26
+ inline code. Your own directives replace the default:
27
+
28
+ ```ts title="vite.config.ts"
29
+ rscKit({
30
+ contentSecurityPolicy: {
31
+ directives: {
32
+ 'default-src': ["'self'"],
33
+ 'img-src': ["'self'", 'data:', 'https://images.example.com'],
34
+ 'connect-src': ["'self'", 'https://api.example.com'],
35
+ 'frame-ancestors': ["'none'"],
36
+ 'base-uri': ["'self'"],
37
+ 'object-src': ["'none'"],
38
+ 'style-src-attr': ["'unsafe-inline'"],
39
+ },
40
+ },
41
+ })
42
+ ```
43
+
44
+ A directive takes a list or a space-separated string. Add `reportOnly: true`
45
+ to send it as `Content-Security-Policy-Report-Only` while you find out what
46
+ it would refuse.
47
+
48
+ ## What the engine adds, and when
49
+
50
+ How a page's inline code is allowed depends on whether its body is whole
51
+ before its headers leave.
52
+
53
+ | The page | Allowed by | Why |
54
+ | --- | --- | --- |
55
+ | Stored at build | the hash of each inline script and style in it | the same bytes for everyone, so one header for everyone, and caches keep sharing it |
56
+ | [Kept](/guides/isr) by `revalidate` | the same, from the body that is kept | rendered with no nonce, because the copy is shared |
57
+ | A [shell](/guides/ppr) with its holes resumed | the shell's hashes, and a nonce for the holes | the holes are rendered for this request |
58
+ | Rendered per request | a nonce | it streams: its last script is not known when the header goes |
59
+
60
+ A nonce is 128 random bits, new for every response, and is only ever put on a
61
+ response no cache may share. React puts it on every script it writes, and on
62
+ the styles it hoists.
63
+
64
+ The payload a page hydrates from is not code. It travels in the page as
65
+ `<script type="application/json">` data blocks, which a browser never runs and
66
+ a policy never judges, so a page's data needs nothing allowed.
67
+
68
+ A directive that already has `'unsafe-inline'` is left as you wrote it: a hash
69
+ or nonce beside it would switch it off. A policy with no `script-src` and no
70
+ `default-src` restricts no scripts, and is not given one.
71
+
72
+ ## Your own inline code
73
+
74
+ The engine allows the inline code it writes. Inline code you write:
75
+
76
+ - **A `<script>` or `<style>` in a page the build stores**, a shell included,
77
+ is hashed with everything else, and runs.
78
+ - **One inside a Suspense hole, or on a page rendered per request**, is
79
+ refused: React gives a nonce to the scripts it writes, not to the elements
80
+ you render.
81
+ - **One brought by a navigation** is refused on every page: the browser
82
+ inserts it into a document whose policy was decided before it existed.
83
+
84
+ So write CSS in a stylesheet and code in a module, which `'self'` allows.
85
+ [Third-party scripts](/guides/third-party-scripts) loaded by `src` need their
86
+ origin in `script-src`. If your app writes `<style>` tags, put
87
+ `'unsafe-inline'` in `style-src` and the engine will leave it alone.
88
+
89
+ `style-src-attr 'unsafe-inline'` is in the default because React's
90
+ `style={{ ... }}` is a style attribute, which no hash or nonce can allow. An
91
+ attribute cannot run code. Leave it out and every `style` prop is refused.
92
+
93
+ ## Where it is sent
94
+
95
+ - **A server, anywhere**: Node, Bun, Docker, a Worker, a function. The host
96
+ sets the header on each page; nothing depends on the platform.
97
+ - **An edge cache** that implements the
98
+ [edge contract](/reference/edge-contract) sends a stored page's policy with
99
+ its copy. A shell goes only to an edge that makes a nonce for each
100
+ response (`nonceShells`), which it puts in the policy and sends to your app
101
+ for the holes. An edge that cannot leaves those pages to your app.
102
+ - **A static export** has no server to send a header, so each page carries its
103
+ policy as a `<meta http-equiv="Content-Security-Policy">` with its own
104
+ hashes. A `<meta>` cannot carry `frame-ancestors`, `report-uri`,
105
+ `report-to` or `sandbox`, and cannot be report-only; set those as headers
106
+ on the host if it can.
107
+ - **`vite dev`** sends the same policy, so what it would refuse in production
108
+ is refused while you work, plus `'unsafe-eval'` in `script-src`, which
109
+ React's development build needs, and the dev server's origin and its
110
+ socket when the page comes from another one (`devOrigin`). Never in a
111
+ build. Every page renders per request in dev, so every page is allowed by
112
+ its nonce, and Vite's own inline styles (a CSS update, the error overlay)
113
+ carry it too. Your own inline `<script>` or `<style>` is refused in dev
114
+ even on a page the build would store and hash.
115
+
116
+ A middleware that sets its own `Content-Security-Policy` keeps it, and the
117
+ engine adds nothing to it. `contentSecurityPolicy` and a
118
+ `Content-Security-Policy` in [`headers`](/guides/response-headers) are the
119
+ same header twice, so the build refuses both together.
@@ -324,6 +324,15 @@ at the same moment either way — but page-level metrics that key on `load` will
324
324
  read differently, and a proxy with a short response timeout needs to allow for
325
325
  the whole render rather than just the shell.
326
326
 
327
+ Under a [Content Security Policy](/guides/content-security-policy), the
328
+ shells move from `shells` to `nonceShells` in `edge.json`. The holes resumed
329
+ into a shell are React's inline scripts, written for each request, and only a
330
+ nonce allows those, so only an edge that makes one per response serves them:
331
+ it asks with `Edge-Accept: nonce`, puts its nonce in the page's policy, and
332
+ sends it on the resume as `Edge-Nonce`. An edge that does not reads `shells`
333
+ alone, and your origin answers those pages. Static pages are listed either
334
+ way, each with its policy and its own hashes.
335
+
327
336
  ## Guarded routes are never cached
328
337
 
329
338
  The shell endpoint answers `404` for any route that declares middleware. Such a
package/guides/index.json CHANGED
@@ -64,6 +64,11 @@
64
64
  "title": "Rendering per request",
65
65
  "description": "Marking work that belongs to the visitor, not to the build."
66
66
  },
67
+ {
68
+ "slug": "content-security-policy",
69
+ "title": "Content Security Policy",
70
+ "description": "A strict policy on every page, with the engine allowing its own inline code by hash or nonce."
71
+ },
67
72
  {
68
73
  "slug": "deployment",
69
74
  "title": "Deploying",
package/guides/isr.md CHANGED
@@ -136,7 +136,9 @@ The page is judged after it renders, and sent private when:
136
136
 
137
137
  - It read the visitor: `cookies()`, `headers()`, `request()` or `connection()`,
138
138
  anywhere in the render, a Suspense'd child included.
139
- - Its answer set a cookie, or carries a CSP nonce.
139
+ - Its answer set a cookie, or carries a CSP nonce. A page kept under
140
+ [`contentSecurityPolicy`](/guides/content-security-policy) never does: it is
141
+ rendered without one and allowed by its hashes.
140
142
  - Its `middleware.ts` declares Laravel or Go route middleware, which runs
141
143
  against the visitor's session.
142
144
  - It answered anything but a 200 or a 404, or its render failed.
@@ -72,7 +72,7 @@ same header wins.
72
72
 
73
73
  Only the headers an edge can send with a cached page are accepted:
74
74
  `Content-Language`, `X-Robots-Tag`, `Content-Security-Policy`,
75
- `Permissions-Policy`, `Referrer-Policy`, `Strict-Transport-Security`,
75
+ `Content-Security-Policy-Report-Only`, `Permissions-Policy`, `Referrer-Policy`, `Strict-Transport-Security`,
76
76
  `X-Frame-Options`, `X-Content-Type-Options`, `Cross-Origin-Opener-Policy`,
77
77
  `Cross-Origin-Embedder-Policy`, `Cross-Origin-Resource-Policy` and
78
78
  `X-DNS-Prefetch-Control`. Anything else is a build error that says so.
@@ -83,10 +83,10 @@ A header that depends on the request goes in a middleware instead. A
83
83
  middleware runs per request, so a page under one is never served from an
84
84
  edge's cache: a root `middleware.ts` keeps every page out.
85
85
 
86
- A `Content-Security-Policy` is the one that needs more than a header: a
87
- page stored at build time cannot carry a per-request nonce. That is
88
- [on the list](https://github.com/rsc-kit/rsc-kit/issues), and until then a
89
- policy that allows your own origin's scripts is the honest one.
86
+ A `Content-Security-Policy` needs more than a fixed header, because each page
87
+ has inline code of its own to allow. Set it with
88
+ [`contentSecurityPolicy`](/guides/content-security-policy) instead, which adds
89
+ each page's hashes, and a nonce where it streams.
90
90
 
91
91
  ## Cookie options
92
92
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsc-kit/mcp",
3
- "version": "0.30.54",
3
+ "version": "0.31.0",
4
4
  "description": "An MCP server over what an rsc-kit build decided: the routes, why each one is static or not, and what it costs the browser.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@modelcontextprotocol/sdk": "^1.30.0",
37
- "create-rsc-kit": "^0.30.54",
37
+ "create-rsc-kit": "^0.31.0",
38
38
  "zod": "^3.25"
39
39
  },
40
40
  "devDependencies": {