Repository navigation
Commit 0bb8c10
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 unrelated1 parent d7dc88e commit 0bb8c10
2 files changed
Lines changed: 28 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
1045 | 1046 | | |
1046 | 1047 | | |
1047 | 1048 | | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
1048 | 1074 | | |
1049 | 1075 | | |
1050 | 1076 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
292 | 292 | | |
293 | 293 | | |
294 | 294 | | |
295 | | - | |
| 295 | + | |
296 | 296 | | |
297 | | - | |
298 | | - | |
299 | | - | |
| 297 | + | |
300 | 298 | | |
301 | 299 | | |
302 | 300 | | |
| |||
0 commit comments