@rsc-kit/mcp 0.30.53 → 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.
@@ -250,8 +250,14 @@ Three rules apply to all of them:
250
250
 
251
251
  **Rotating one** is a deploy, and each says so differently:
252
252
 
253
- - `RSC_HOST_CALL_SECRET` - the renderer and the backend must change together.
254
- Between the two, every `rpc()` is refused, so deploy both at once.
253
+ - `RSC_HOST_CALL_SECRET` - the renderer sends it and the backend checks it,
254
+ so the backend goes first. Give the backend the new value, with the old one
255
+ as `RSC_HOST_CALL_SECRET_PREVIOUS`: it answers either. Then give the
256
+ renderer the new value, and once every renderer has it, remove
257
+ `RSC_HOST_CALL_SECRET_PREVIOUS`. No call is refused along the way. The
258
+ renderer's own health probe takes either too. Read from `@rsc-kit/core`
259
+ 0.30.54, `rsc-kit/go` v0.17.0 and `rsc-kit/laravel` 0.21; before those, the
260
+ two must change together.
255
261
  - `RSC_SIGNING_SECRET` - pages already open hold names signed with the old
256
262
  key. Set the old value as `RSC_SIGNING_SECRET_PREVIOUS` for the deploy that
257
263
  rotates it: it is accepted when checking a signature and never used to make
@@ -322,6 +328,31 @@ timeouts. A proxy in front can send them straight to the backend:
322
328
  The same list is `build.backendPaths` in the `routes.json` beside the entries,
323
329
  for a host that reads the route table.
324
330
 
331
+ ### A header on every request to the backend
332
+
333
+ When something in front of the backend admits the renderer only with a header
334
+ of its own, such as a private preview's access token, set
335
+ `RSC_BACKEND_HEADERS` on the renderer to a JSON object:
336
+
337
+ ```ini
338
+ RSC_BACKEND_HEADERS={"X-Preview-Token":"..."}
339
+ ```
340
+
341
+ - They go on every request the renderer makes to `RSC_BACKEND`: host calls,
342
+ and the urls it forwards. They go to no other origin.
343
+ - The renderer reads them once, when it starts, so a new value needs a
344
+ restart.
345
+ - A visitor's header with the same name is replaced, never passed through.
346
+ - Malformed JSON stops the server at startup, with an error that names the
347
+ variable but never its value.
348
+ - Names rsc-kit sets itself (`X-RSC-*`, `Cookie`, `Authorization`, `Host`,
349
+ `Content-Type`, the forwarding headers) are refused.
350
+ - `httpHostCalls({ headers })` takes the same, for a host that embeds the
351
+ engine itself.
352
+
353
+ Read from `@rsc-kit/core` 0.30.54. Production and a backend on loopback rarely
354
+ need it.
355
+
325
356
  ### Pages an edge may serve from its cache
326
357
 
327
358
  rsc-kit implements the [edge contract](/reference/edge-contract): one marker
@@ -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/go.md CHANGED
@@ -50,9 +50,21 @@ if err != nil {
50
50
 
51
51
  mux := http.NewServeMux()
52
52
  mux.Handle("POST /__rsc/host-call", callback)
53
- http.ListenAndServe("127.0.0.1:8080", mux)
53
+
54
+ // PORT when the platform names one, 8080 otherwise, on every address: a
55
+ // host that sends backend urls straight here (backendPaths) reaches it at
56
+ // the machine's own address, not loopback. RSC_BACKEND stays
57
+ // http://127.0.0.1:8080 - the renderer still calls over loopback.
58
+ http.ListenAndServe(":"+cmp.Or(os.Getenv("PORT"), "8080"), mux)
54
59
  ```
55
60
 
61
+ It listens on every address, not loopback alone. Once an app names
62
+ [`backendPaths`](/hosts/backend-answered-pages/), a proxy in front may send
63
+ those urls straight to the backend at the machine's own address, and a
64
+ backend bound to `127.0.0.1` can't be reached. The secret is what guards
65
+ the host-call endpoint, and the proxy refuses it from outside
66
+ ([deployment](/hosts/deployment/#sending-the-backends-urls-past-the-renderer)).
67
+
56
68
  The renderer forwards any url its route tree does not own to `RSC_BACKEND`,
57
69
  so a Go route on that mux — `/login`, a webhook, an upload — is reachable at
58
70
  the renderer's origin too.
@@ -102,6 +114,7 @@ Never set `RSC_DEBUG` in production: a trace names your files.
102
114
  | --- | --- | --- |
103
115
  | `RSC_BACKEND` | the renderer | Where the Go endpoint is, e.g. `http://127.0.0.1:8080`. Falls back to `APP_URL`. Read from `.env` by `vite`, and from the environment by the built server; the url forwarding is fixed at build time. |
104
116
  | `RSC_HOST_CALL_SECRET` | both | The shared secret. The renderer sends it; your Go code passes it to `NewCallbackHandler`. Without it neither side talks. |
117
+ | `RSC_HOST_CALL_SECRET_PREVIOUS` | Go | The secret being rotated away from, answered as well while set, so a rotation refuses no call. [Rotating one](/hosts/deployment/). |
105
118
  | `RSC_HOST_CALL_PATH` | the renderer | The endpoint's path. Default `/__rsc/host-call`. |
106
119
  | `RSC_DEBUG` | Go | `1` sends a failure's trace with its answer. Development only. |
107
120
  | `RSC_TRUST_FORWARDED` | the renderer | `1` passes the visitor's `X-Forwarded-For` through to Go. Only behind a proxy you control that writes it. |
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.
package/guides/laravel.md CHANGED
@@ -577,7 +577,9 @@ the assets — which travels with the deployment; nothing in it is read from the
577
577
  source tree at runtime.
578
578
 
579
579
  Both processes read `RSC_HOST_CALL_SECRET`, and the renderer also needs
580
- `APP_URL` (or `RSC_BACKEND`) to know where Laravel is. Run it with the app's
580
+ `APP_URL` (or `RSC_BACKEND`) to know where Laravel is. To rotate the secret,
581
+ Laravel takes `RSC_HOST_CALL_SECRET_PREVIOUS` as well
582
+ ([rotating one](/hosts/deployment/)). Run it with the app's
581
583
  `.env` and it has both:
582
584
 
583
585
  ```ini title="/etc/systemd/system/rsc-renderer.service"
@@ -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
 
@@ -92,6 +92,12 @@ unconfigured secret has to be rejected before the comparison rather than
92
92
  trusted to it. Better still, do not register the endpoint at all when no
93
93
  secret is configured — absent, not open.
94
94
 
95
+ **Answer the previous secret too, while `RSC_HOST_CALL_SECRET_PREVIOUS` is
96
+ set.** That is how the secret rotates without a refused call: the backend takes
97
+ the new one and the old, the renderers move to the new one, then the old is
98
+ removed ([rotating one](/hosts/deployment/)). Compare it the same way, unset
99
+ matching nothing, and never send it.
100
+
95
101
  ## The reply
96
102
 
97
103
  JSON, and the fields keep the outcomes apart so the renderer never reads a
@@ -410,6 +416,12 @@ is the one it cannot.
410
416
  - **Answer within 30 seconds.** A render blocked on a host that never answers
411
417
  is a hung request, and the renderer gives up at 30s (`timeoutMs` on
412
418
  `httpHostCalls`, for a host that embeds the engine itself).
419
+ - **Starting after the renderer is fine.** A refused connection is tried again
420
+ for up to 2 seconds, because a refused connection means the call never
421
+ reached you, so nothing runs twice. This covers a backend started beside
422
+ the renderer in one container, still coming up when the first page renders.
423
+ Nothing else is retried: a timeout or a reset may have run the call
424
+ (`refusedRetryMs` on `httpHostCalls`; 0 turns it off).
413
425
  - **One process is not enough if you proxy.** A server that proxies a page
414
426
  to the renderer holds a worker for the whole render, and the render calls
415
427
  back to that same server for its data. With one worker, nobody is left to
@@ -462,12 +474,12 @@ the page it is asked about is `/conformance/orders/7` with `{order}` bound to
462
474
  `"7"`. Then serve them and run:
463
475
 
464
476
  Start it with `RSC_PURGE_URL=http://127.0.0.1:8790/`, where the suite
465
- listens for its purges, then:
477
+ listens for its purges, and `RSC_HOST_CALL_SECRET_PREVIOUS=old`, then:
466
478
 
467
479
  ```sh
468
480
  npx -y -p @rsc-kit/core rsc-kit-conformance \
469
481
  --endpoint http://127.0.0.1:8123/__rsc/host-call --secret test \
470
- --manifest rsc-host.json --purge-port 8790
482
+ --previous-secret old --manifest rsc-host.json --purge-port 8790
471
483
  ```
472
484
 
473
485
  It checks the whole contract on this page:
@@ -483,7 +495,9 @@ It checks the whole contract on this page:
483
495
  a browser can check, and a refusal landing on its field;
484
496
  - **the request** - `Cookie` and `Authorization` forwarded, the visitor's
485
497
  address in place of the renderer's, a cookie set by a
486
- call reaching the page, no secret or the wrong one refused with 403, an
498
+ call reaching the page, no secret or the wrong one refused with 403, the
499
+ previous one answered while `RSC_HOST_CALL_SECRET_PREVIOUS` names it (so a
500
+ rotation fails no call; without `--previous-secret` this fails), an
487
501
  unknown function 404, a malformed body 400;
488
502
  - **batches** - every call answered, a refusal only its own, and more than 50
489
503
  refused with 413 before any runs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsc-kit/mcp",
3
- "version": "0.30.53",
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.53",
37
+ "create-rsc-kit": "^0.31.0",
38
38
  "zod": "^3.25"
39
39
  },
40
40
  "devDependencies": {