mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-11-13 12:13:42 +01:00
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:
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;
|
||||
|
||||
Reference in new issue
Block a user