|
| 1 | +import { expect, type Page } from "@playwright/test"; |
| 2 | +import getPort from "get-port"; |
| 3 | + |
| 4 | +import { js } from "./helpers/create-fixture.js"; |
| 5 | +import { test } from "./helpers/vite.js"; |
| 6 | +import { implementations, setupRscTest } from "./rsc/utils.js"; |
| 7 | + |
| 8 | +async function expectNonceSupport(page: Page, nonce: string) { |
| 9 | + const scripts = page.locator("script"); |
| 10 | + const count = await scripts.count(); |
| 11 | + expect(count).toBeGreaterThan(0); |
| 12 | + for (let index = 0; index < count; index++) { |
| 13 | + expect( |
| 14 | + await scripts |
| 15 | + .nth(index) |
| 16 | + .evaluate((script: HTMLScriptElement) => script.nonce), |
| 17 | + ).toBe(nonce); |
| 18 | + } |
| 19 | + |
| 20 | + await page.getByRole("button", { name: "Count: 0" }).click(); |
| 21 | + await expect(page.getByRole("button", { name: "Count: 1" })).toBeVisible(); |
| 22 | +} |
| 23 | + |
| 24 | +test.describe("RSC CSP nonces", () => { |
| 25 | + test.describe("RSC Framework", () => { |
| 26 | + test("adds the nonce to document scripts and hydrates under a strict CSP", async ({ |
| 27 | + page, |
| 28 | + vitePreview, |
| 29 | + }) => { |
| 30 | + const { port } = await vitePreview( |
| 31 | + async () => ({ |
| 32 | + "app/entry.ssr.tsx": js` |
| 33 | + import { createFromReadableStream } from "@vitejs/plugin-rsc/ssr"; |
| 34 | + import { renderToReadableStream } from "react-dom/server.edge"; |
| 35 | + import { |
| 36 | + unstable_routeRSCServerRequest as routeRSCServerRequest, |
| 37 | + unstable_RSCStaticRouter as RSCStaticRouter, |
| 38 | + } from "react-router"; |
| 39 | +
|
| 40 | + export async function generateHTML( |
| 41 | + request: Request, |
| 42 | + serverResponse: Response, |
| 43 | + ) { |
| 44 | + const nonce = crypto.randomUUID(); |
| 45 | + const response = await routeRSCServerRequest({ |
| 46 | + request, |
| 47 | + serverResponse, |
| 48 | + createFromReadableStream, |
| 49 | + nonce, |
| 50 | + async renderHTML(getPayload, options) { |
| 51 | + const payload = getPayload(); |
| 52 | + const bootstrapScriptContent = |
| 53 | + await import.meta.viteRsc.loadBootstrapScriptContent("index"); |
| 54 | +
|
| 55 | + return renderToReadableStream( |
| 56 | + <RSCStaticRouter |
| 57 | + getPayload={getPayload} |
| 58 | + nonce={options.nonce} |
| 59 | + />, |
| 60 | + { |
| 61 | + ...options, |
| 62 | + bootstrapScriptContent, |
| 63 | + formState: await payload.formState, |
| 64 | + signal: request.signal, |
| 65 | + }, |
| 66 | + ); |
| 67 | + }, |
| 68 | + }); |
| 69 | + response.headers.set( |
| 70 | + "Content-Security-Policy", |
| 71 | + "script-src 'self' 'nonce-" + nonce + "'", |
| 72 | + ); |
| 73 | + return response; |
| 74 | + } |
| 75 | + `, |
| 76 | + "app/routes/_index.tsx": js` |
| 77 | + "use client"; |
| 78 | +
|
| 79 | + import { useState } from "react"; |
| 80 | +
|
| 81 | + export default function Index() { |
| 82 | + const [count, setCount] = useState(0); |
| 83 | + return ( |
| 84 | + <button onClick={() => setCount(count + 1)}> |
| 85 | + Count: {count} |
| 86 | + </button> |
| 87 | + ); |
| 88 | + } |
| 89 | + `, |
| 90 | + }), |
| 91 | + "rsc-vite-framework", |
| 92 | + ); |
| 93 | + |
| 94 | + const response = await page.goto(`http://localhost:${port}`); |
| 95 | + const policy = response?.headers()["content-security-policy"]; |
| 96 | + const nonce = policy?.match(/'nonce-([^']+)'/)?.[1]; |
| 97 | + expect(nonce).toBeTruthy(); |
| 98 | + await expectNonceSupport(page, nonce!); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + implementations.forEach((implementation) => { |
| 103 | + test.describe(`RSC Data (${implementation.name})`, () => { |
| 104 | + let port: number; |
| 105 | + let stop: (() => void) | undefined; |
| 106 | + |
| 107 | + test.beforeAll(async () => { |
| 108 | + port = await getPort(); |
| 109 | + stop = await setupRscTest({ |
| 110 | + implementation, |
| 111 | + port, |
| 112 | + files: { |
| 113 | + "src/entry.ssr.tsx": js` |
| 114 | + import { createFromReadableStream } from "@vitejs/plugin-rsc/ssr"; |
| 115 | + import { renderToReadableStream } from "react-dom/server.edge"; |
| 116 | + import { |
| 117 | + unstable_routeRSCServerRequest as routeRSCServerRequest, |
| 118 | + unstable_RSCStaticRouter as RSCStaticRouter, |
| 119 | + } from "react-router"; |
| 120 | +
|
| 121 | + export default async function handler( |
| 122 | + request: Request, |
| 123 | + serverResponse: Response, |
| 124 | + ) { |
| 125 | + const nonce = crypto.randomUUID(); |
| 126 | + const bootstrapScriptContent = |
| 127 | + await import.meta.viteRsc.loadBootstrapScriptContent("index"); |
| 128 | + const response = await routeRSCServerRequest({ |
| 129 | + request, |
| 130 | + serverResponse, |
| 131 | + createFromReadableStream, |
| 132 | + nonce, |
| 133 | + async renderHTML(getPayload, options) { |
| 134 | + const payload = getPayload(); |
| 135 | + return renderToReadableStream( |
| 136 | + <RSCStaticRouter |
| 137 | + getPayload={getPayload} |
| 138 | + nonce={options.nonce} |
| 139 | + />, |
| 140 | + { |
| 141 | + ...options, |
| 142 | + bootstrapScriptContent, |
| 143 | + signal: request.signal, |
| 144 | + formState: await payload.formState, |
| 145 | + }, |
| 146 | + ); |
| 147 | + }, |
| 148 | + }); |
| 149 | + response.headers.set( |
| 150 | + "Content-Security-Policy", |
| 151 | + "script-src 'self' 'nonce-" + nonce + "'", |
| 152 | + ); |
| 153 | + return response; |
| 154 | + } |
| 155 | + `, |
| 156 | + "src/routes/home.client.tsx": js` |
| 157 | + "use client"; |
| 158 | +
|
| 159 | + import { useState } from "react"; |
| 160 | +
|
| 161 | + export default function Counter() { |
| 162 | + const [count, setCount] = useState(0); |
| 163 | + return ( |
| 164 | + <button onClick={() => setCount(count + 1)}> |
| 165 | + Count: {count} |
| 166 | + </button> |
| 167 | + ); |
| 168 | + } |
| 169 | + `, |
| 170 | + "src/routes/home.tsx": js` |
| 171 | + import Counter from "./home.client"; |
| 172 | +
|
| 173 | + export default function HomeRoute() { |
| 174 | + return <Counter />; |
| 175 | + } |
| 176 | + `, |
| 177 | + }, |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + test.afterAll(() => { |
| 182 | + stop?.(); |
| 183 | + }); |
| 184 | + |
| 185 | + test("adds the nonce to document scripts and hydrates under a strict CSP", async ({ |
| 186 | + page, |
| 187 | + }) => { |
| 188 | + const response = await page.goto(`http://localhost:${port}`); |
| 189 | + const policy = response?.headers()["content-security-policy"]; |
| 190 | + const nonce = policy?.match(/'nonce-([^']+)'/)?.[1]; |
| 191 | + expect(nonce).toBeTruthy(); |
| 192 | + await expectNonceSupport(page, nonce!); |
| 193 | + }); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments