Conversation
The octet-stream upload path wrote every chunk to disk without checking the documented maxFileSize/maxTotalFileSize limits, unlike the multipart path in _handlePart. Accumulate per-file and total sizes and abort via _error with the existing FormidableError codes when a cap is exceeded, mirroring the multipart implementation. The over-limit file is removed through the shared _error cleanup, so no partial bytes remain on disk.
| import * as errors from "../FormidableError.js"; | ||
| import FormidableError from "../FormidableError.js"; |
There was a problem hiding this comment.
The two separate import statements can be merged into a single combined import, which is the pattern used throughout the codebase (e.g.
import FormidableError, * as errors from "./FormidableError.js" in Formidable.js).
| import * as errors from "../FormidableError.js"; | |
| import FormidableError from "../FormidableError.js"; | |
| import FormidableError, * as errors from "../FormidableError.js"; |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
The maxFileSize test left a TCPWRAP handle open: formidable aborts the parse mid-body, so the connection never completes on its own and server.close() only stops new ones. Jest reported "did not exit one second after the test run" locally and "A worker process has failed to exit gracefully" on CI, where the leaked socket's late callback surfaced as a failure inside the next test file, test/integration/store-files-option.test.js. Destroying the client request ends the connection. Measured: with the old teardown --detectOpenHandles reports 1 open TCPWRAP at the server.listen line; with this change it reports none, and the full suite is 15/15 suites, 95 passed / 3 skipped of 98, with no worker warning.
| }); | ||
|
|
||
| // Destroying the request above surfaces here as ECONNRESET. | ||
| request.on("error", () => {}); |
There was a problem hiding this comment.
Unexpected request errors disappear The handler ignores every client request error, not just the expected reset after
request.destroy(). If the request fails before reaching the server, the parse callback never runs, so the test times out and leaves the server open instead of reporting the connection error and cleaning up.
|
The The It reproduces locally when the suite runs in a single process. With the test file in its previous form,
Shrinking the body from 256 KB to 4 KB did not fix it, so the leak is the unfinished connection rather than the unsent bytes. Both workflow runs on this push concluded |
The octet-stream upload path does not enforce the documented
maxFileSize/maxTotalFileSizelimits, unlike the multipart path.Steps to reproduce
POST a 256KB body with
Content-Type: application/octet-streamto a form configured withmaxFileSize: 1024:ACTUAL:
errisundefined, one file is returned with size 262144, and the over-sized file is committed to disk viafile.end().EXPECTED:
err.code === 1016(biggerThanMaxFileSize), no file returned, and nothing left on disk. This matches how the multipart path already behaves.Root cause
The octet-stream plugin's
_parser.on("data", ...)handler insrc/plugins/octetstream.jswrites each chunk straight to the file with no size check, and it bypasses_handlePart()insrc/Formidable.jswhere the multipart size caps are enforced. As a result a raw octet-stream body of any size is accepted regardless of the configured limits.The README documents
maxFileSizeandmaxTotalFileSizeas limiting each file and the batch respectively, with defaults, and does not exempt octet-stream.Fix
Accumulate the per-file and running total sizes before each write and abort via
this._error(...)with the existingFormidableErrorcodesbiggerThanMaxFileSize/biggerThanTotalMaxFileSizewhen a cap is exceeded, mirroring_handlePart. In-limit uploads are unaffected. Because the octet-stream file is tracked inopenedFiles, the shared_errorcleanup callsfile.destroy(), which unlinks the partial file, so no bytes remain on disk (the same cleanup the multipart path relies on).Authority
CWE-770 (allocation without limits), plus the library's own documented contract and its multipart implementation, which enforces exactly these caps.
Tests
Added an integration case in
test/integration/octet-stream.test.js: a 256KB octet-stream body withmaxFileSize: 1024must be rejected with code 1016 and return no files. Verified it fails on the current source (the over-sized upload is accepted witherrnull) and passes with the fix, with the tmp directory left empty afterwards.Suite status: 92 passed / 3 skipped across 14 jest suites, and 11/11 node tests.
The PR appears safe to merge; the remaining findings concern import style and test-error diagnostics.
Summary
The PR adds per-file and total-size checks to octet-stream uploads and an integration test for the per-file limit. The latest change closes the test’s client request after rejection to avoid leaving the connection open.
Reviews (4) · Last reviewed commit: "test(octet-stream): close the rejected u..."