Skip to content

Commit 0bb8c10

Browse files
authored
fix(node): bound static file reads to the size declared in content-length (#2503)
## What `StaticFileHandlerPlugin.serve()` now always limits the file read to `{ start, end }` from the stat that sets `content-length`. Before, only 206 responses were limited. A 0-byte file gets an empty body without opening a stream, because `end` would be `-1` and `createReadStream` throws when `start > end`. ## Why For a 200 response the read went to whatever end-of-file existed when the adapter pulled the stream. If the file grew between the stat and the read, the body was longer than the declared `content-length`. Node doesn't enforce the length by default, so the extra bytes went out on the keep-alive socket, and the client parsed them as the start of the next response: ``` HTTP/1.1 200 OK content-length: 11 hello world + appendedHTTP/1.1 404 Not Found ... ``` The response that was too long looks fine to its client. The next response on the connection is the one that breaks, and behind a pooled proxy that can be another user's response. Precompressed sidecars and fallback/index files go through the same code. `send` and `serve-static` avoid this by always passing `end: size - 1`. If the file shrinks instead, the body still comes out shorter than `content-length` and the client sees a truncated response. That is out of scope here and matches `send`. ## Tests Added a regression test using the fetch adapter, which returns the `Response` before the body is read. The test appends to a file, and to an empty file, between `handle()` and reading the body. It checks that the body matches the declared `content-length`. It fails without the fix (`"hello world + appended"`). - `pnpm check`: passes - `pnpm exec vitest run`: everything passes except `openapi-inference` > "names the nearest tsconfig when it does not include the file, and why", which fails on `main` too and is unrelated
1 parent d7dc88e commit 0bb8c10

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎packages/node/src/static-file-handler-plugin.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Buffer } from 'node:buffer'
22
import {
3+
appendFileSync,
34
mkdirSync,
45
mkdtempSync,
56
rmSync,
@@ -1045,6 +1046,31 @@ describe('staticFileHandlerPlugin', () => {
10451046
expect(freshRes.status).toBe(304)
10461047
})
10471048

1049+
it('never sends more bytes than content-length when the file grows after stat', async ({
1050+
onTestFinished,
1051+
}) => {
1052+
const growingPath = path.join(rootDir, 'growing.txt')
1053+
const growingEmptyPath = path.join(rootDir, 'growing-empty.txt')
1054+
writeFileSync(growingPath, 'hello world')
1055+
writeFileSync(growingEmptyPath, '')
1056+
onTestFinished(() => {
1057+
rmSync(growingPath, { force: true })
1058+
rmSync(growingEmptyPath, { force: true })
1059+
})
1060+
1061+
const handler = new FetchRPCHandler({}, { plugins: [new StaticFileHandlerPlugin({ rootDir })] })
1062+
1063+
const served = await handler.handle(new Request('https://example.com/growing.txt'))
1064+
appendFileSync(growingPath, ' + appended')
1065+
expect(served.response!.headers.get('content-length')).toBe('11')
1066+
expect(await served.response!.text()).toBe('hello world')
1067+
1068+
const servedEmpty = await handler.handle(new Request('https://example.com/growing-empty.txt'))
1069+
appendFileSync(growingEmptyPath, 'appended')
1070+
expect(servedEmpty.response!.headers.get('content-length')).toBe('0')
1071+
expect(await servedEmpty.response!.text()).toBe('')
1072+
})
1073+
10481074
describe('tracing', () => {
10491075
it('renames the active span to the mounted base path', async ({ onTestFinished }) => {
10501076
const span = {

‎packages/node/src/static-file-handler-plugin.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,9 @@ export class StaticFileHandlerPlugin<T extends Context> implements StandardHandl
292292
headers['content-length'] = `${end - start + 1}`
293293

294294
const body: ReadableStream<Uint8Array<ArrayBuffer>> =
295-
request.method === 'HEAD'
295+
request.method === 'HEAD' || end < start
296296
? new ReadableStream({ start: (controller) => controller.close() })
297-
: toWebReadableStream(
298-
createReadStream(filePath, status === 206 ? { start, end } : undefined),
299-
)
297+
: toWebReadableStream(createReadStream(filePath, { start, end }))
300298

301299
return { status, headers, body }
302300
}

0 commit comments

Comments
 (0)