-
Notifications
You must be signed in to change notification settings - Fork 69
fix: silence streamed Suspense boundary error message in prod #482
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
2 commits
Select commit
Hold shift + click to select a range
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
80 changes: 80 additions & 0 deletions
80
packages/server/test/dev/streaming-error-isolation.test.js
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,80 @@ | ||
| /** | ||
| * Streamed Suspense boundary error isolation (#478). | ||
| * | ||
| * The page-level streaming path (`ssr.js` `streamingHtmlResponse`) renders each | ||
| * pending boundary and, when one REJECTS or throws, emits an error placeholder | ||
| * in its slot. That message must follow the same policy as the rest of SSR | ||
| * (`render-server.js` `defaultSSRErrorTemplate`): dev surfaces the message so | ||
| * the failure is obvious, prod stays SILENT so no internal detail (a DB error, | ||
| * a stack-derived path) leaks to the client. | ||
| * | ||
| * Driven through the real `createRequestHandler` pipeline against a fixture | ||
| * page whose top-level `Suspense` boundary rejects with a recognizable secret. | ||
| */ | ||
| import { test, before, after } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { dirname, join, resolve } from 'node:path'; | ||
| import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
|
|
||
| import { createRequestHandler } from '../../src/dev.js'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
| const HTML_URL = pathToFileURL(resolve(__dirname, '../../../core/src/html.js')).toString(); | ||
| const SUSPENSE_URL = pathToFileURL(resolve(__dirname, '../../../core/src/suspense.js')).toString(); | ||
|
|
||
| const SECRET = 'SECRET-DB-ERROR-7f3a9'; | ||
|
|
||
| // A page whose only content is a Suspense boundary that rejects after a tick. | ||
| // The deferred rejection (vs an eager `Promise.reject`) keeps it from being an | ||
| // unhandled rejection before the streaming loop awaits it. The streaming loop | ||
| // awaits `p.promise`, the rejection throws into its catch, and the boundary's | ||
| // slot gets the dev/prod-gated error placeholder. | ||
| const REJECTING_PAGE = | ||
| `import { html } from ${JSON.stringify(HTML_URL)};\n` + | ||
| `import { Suspense } from ${JSON.stringify(SUSPENSE_URL)};\n` + | ||
| `export default function P() {\n` + | ||
| ` const slow = new Promise((_, reject) => setTimeout(() => reject(new Error('${SECRET}')), 5));\n` + | ||
| ` return html\`<main><h1>page</h1>\${Suspense({ fallback: html\`<p>loading</p>\`, children: slow })}</main>\`;\n` + | ||
| `}\n`; | ||
|
|
||
| let tmpRoot; | ||
| before(() => { tmpRoot = mkdtempSync(join(tmpdir(), 'webjs-stream-err-')); }); | ||
| after(() => { rmSync(tmpRoot, { recursive: true, force: true }); }); | ||
|
|
||
| function makeApp(files) { | ||
| const appDir = mkdtempSync(join(tmpRoot, 'app-')); | ||
| for (const [rel, body] of Object.entries(files)) { | ||
| const abs = join(appDir, rel); | ||
| mkdirSync(dirname(abs), { recursive: true }); | ||
| writeFileSync(abs, body); | ||
| } | ||
| return appDir; | ||
| } | ||
|
|
||
| test('PROD: a rejected streamed boundary does NOT leak the error message', async () => { | ||
| const appDir = makeApp({ 'app/page.js': REJECTING_PAGE }); | ||
| const app = await createRequestHandler({ appDir, dev: false }); | ||
| const res = await app.handle(new Request('http://x/')); | ||
| assert.equal(res.status, 200, 'the page still streams a 200 (the boundary is isolated)'); | ||
| const body = await res.text(); | ||
| assert.ok(body.includes('page'), 'the non-suspended content rendered'); | ||
| assert.ok(body.includes('loading'), 'the boundary fallback was flushed'); | ||
| assert.ok( | ||
| !body.includes(SECRET), | ||
| `prod must NOT leak the boundary error message; body contained it:\n${body}`, | ||
| ); | ||
| }); | ||
|
|
||
| test('DEV: a rejected streamed boundary surfaces the error message', async () => { | ||
| const appDir = makeApp({ 'app/page.js': REJECTING_PAGE }); | ||
| const app = await createRequestHandler({ appDir, dev: true }); | ||
| const res = await app.handle(new Request('http://x/')); | ||
| assert.equal(res.status, 200, 'the page still streams a 200'); | ||
| const body = await res.text(); | ||
| assert.ok( | ||
| body.includes(SECRET), | ||
| `dev must surface the boundary error message for debugging; body:\n${body}`, | ||
| ); | ||
| }); |
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.