`Request/Response.formData()` throws ERR_ASSERTION on a multipart file part when `globalThis.File` is replaced
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
- #6212 來自 @xgauravyaduvanshii —— 已關閉,未合併
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 停滯
- 技術堆疊
- javascript, node.js
研究方向
Start with the bundled Undici multipart form data parser and the webidl.is.File brand check described in the issue. Reproduce with the dependency free plain.mjs example on Node v24 or v26, then compare with v22. Done means formData() returns the file part with content hello even after globalThis.File is replaced; an open linked pull request indicates work is already underway.
由索引模型根據 Issue 內容生成。
描述
Version
v24.22.2 and v26.13.1. v22.23.3 is fine.
Platform
macOS (darwin arm64). Not platform-specific as far as I can tell.
Subsystem
fetch (bundled undici)
What steps will reproduce the bug?
plain.mjs, no dependencies:
const body =
"--b\r\n" +
'Content-Disposition: form-data; name="f"; filename="a.txt"\r\n' +
"Content-Type: text/plain\r\n\r\n" +
"hello\r\n" +
"--b--\r\n";
const parse = () =>
new Response(body, { headers: { "content-type": "multipart/form-data; boundary=b" } }).formData();
console.log((await parse()).get("f") instanceof File); // true
globalThis.File = class File extends Blob {
constructor(bits, name, opts) { super(bits, opts); this.name = name; }
};
await parse(); // v24/v26: AssertionError [ERR_ASSERTION]
AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert(typeof value === "string" && webidl.is.USVString(value) || webidl.is.File(value))
at multipartFormDataParser (node:internal/deps/undici/undici:...)
How often does it reproduce?
Always.
What is the expected behavior?
formData() returns a FormData whose "f" entry is a file with the content hello, as on v22. It shouldn't depend on what the global File binding currently points to.
What do you see instead?
multipartFormDataParser creates the part with new File(...), which looks up the global File at call time. It then checks the result with webidl.is.File, which is MakeTypeAssertion(File) taken from Node's own File when the module loads. Once the global is replaced, the object it just created fails the check.
Additional information
This is what happens under DOM test environments: vitest and jest with jsdom install the DOM's File on globalThis. Any Request.formData() on a multipart body with a file part then fails, which includes every MSW request handler that reads an upload.
The vitest + jsdom project below reproduces it in that setting (npm i && npx vitest run): it passes on v22.23.3 and fails on v24.22.2 and v26.13.1.
vitest + jsdom reproduction
package.json
{
"name": "undici-jsdom-formdata-repro",
"private": true,
"type": "module",
"scripts": { "test": "vitest run" },
"devDependencies": { "jsdom": "27.4.0", "vitest": "4.1.12" }
}
vitest.config.mjs
export default { test: { environment: "jsdom", execArgv: ["--no-experimental-webstorage"] } };
formdata.test.mjs
import { expect, test } from "vitest";
// Under jsdom, globalThis.File is jsdom's. Node's Request.formData() then fails on a file part.
test("Request.formData() parses a multipart file part under jsdom", async () => {
const body =
"--b\r\n" +
'Content-Disposition: form-data; name="f"; filename="a.txt"\r\n' +
"Content-Type: text/plain\r\n\r\n" +
"hello\r\n" +
"--b--\r\n";
const req = new Request("http://example.invalid/", {
method: "POST",
headers: { "content-type": "multipart/form-data; boundary=b" },
body,
});
const part = (await req.formData()).get("f");
expect(await part.text()).toBe("hello");
});
A possible fix: construct the part with the same internal File the brand check uses, instead of the global binding.
- 主要語言
- JavaScript
- 星號
- 124k
- 分支
- 38k
- 平均合併
- 4 天 2 小時
- 30 天內合併 PR
- 266
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
nodejs/node 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 69/100
維護者通常 1 天內回覆
-
fs: document readStream.start and readStream.end可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉doc
難度 1/5 1 小時以內 新手友好度 90/100
維護者通常 1 天內回覆
-
doc: document the Buffer.prototype.*Slice() methods可能已有人在做 @AyushCipher 今天認領。 未關閉doc
難度 2/5 1-3 小時 新手友好度 86/100
維護者通常 1 天內回覆
-
`styleText` fast path bypasses `options` and `options.validateStream` validation可能已有人在做 @GBra-4-669 於 8 天前認領。 未關閉util
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
riscv64
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
相似的 Issue
-
bug triage needed
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 2 天內回覆
-
resource-submission validation-passed
難度 1/5 1 小時以內 新手友好度 82/100
hesreallyhim/awesome-claude-code#3773 · 1 則留言 ·
-
難度 2/5 1-3 小時 新手友好度 73/100
tabatkins/railroad-diagrams#129 ·
-
web: api() turns a null or object-valued error body into a TypeError or "[object Object]" and loses the status可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉bug
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆