Preserve literal form field names when converting to JSON

Use an object with a null prototype so inherited property names are not
mistaken for existing fields and __proto__ remains a literal JSON key.
Cover __proto__, constructor, and toString with regression cases.
This commit is contained in:
Zhizhen He committed 2026-10-16 01:42:28 +08:00
1 parent 277bbe1baa
commit 8a85fb3dcd
2 files changed
+16 -1

No files matched your search

@@ -92,6 +92,21 @@ describe("convertRequestBody", () => {
});
});
test.each(["__proto__", "constructor", "toString"])(
"preserves the form field %s when converting to JSON",
(name) => {
const body = convertRequestBody({
fromBodyType: BODY_TYPE_FORM_URLENCODED,
toBodyType: BODY_TYPE_JSON,
body: { form: [{ enabled: true, name, value: "hello" }] },
});
expect(body).toEqual({
text: JSON.stringify({ [name]: "hello" }, null, 2),
});
},
);
test("preserves text when converting to form bodies cannot build form pairs", () => {
const body = convertRequestBody({
fromBodyType: BODY_TYPE_XML,
@@ -161,7 +161,7 @@ function formBodyToUrlEncodedText(form: unknown[]): string {
}
function formBodyToObject(form: unknown[]) {
const result: Record<string, unknown> = {};
const result: Record<string, unknown> = Object.create(null);
for (const pair of form) {
if (!isRecord(pair)) continue;