Skip to content

Commit 5966af4

Browse files
committed
Simplify form and log property handling.
1 parent b067808 commit 5966af4

4 files changed

Lines changed: 50 additions & 30 deletions

File tree

‎packages/void/src/runtime/log.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function format(message: string, boundFields: ReadonlyArray<Fields> = [], fields
5858
}
5959

6060
for (const [key, value] of Object.entries(source)) {
61-
if (key === 'msg' || key === 'toJSON' || key === '__proto__' || key === 'constructor') {
61+
if (key === 'msg' || key === 'toJSON') {
6262
continue;
6363
}
6464

‎packages/void/src/runtime/validator.ts‎

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ type FormEntry = string | File;
5050
const MAX_FORM_DEPTH = 32;
5151
const MAX_FORM_NODES = 10_000;
5252
const MAX_FORM_INDEX = 10_000;
53-
const DANGEROUS_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
5453

5554
function isFormContentType(contentType: string | undefined): boolean {
5655
const mediaType = contentType?.split(';', 1)[0]?.trim().toLowerCase();
@@ -109,26 +108,6 @@ function isContainerRecord(value: unknown): value is Record<string, unknown> {
109108
);
110109
}
111110

112-
function own(record: Record<string, unknown>, key: string): unknown {
113-
if (DANGEROUS_KEYS.has(key)) {
114-
return Object.getOwnPropertyDescriptor(record, key)?.value;
115-
}
116-
return record[key];
117-
}
118-
119-
function setOwn(record: Record<string, unknown>, key: string, value: unknown): void {
120-
if (DANGEROUS_KEYS.has(key)) {
121-
Object.defineProperty(record, key, {
122-
value,
123-
writable: true,
124-
enumerable: true,
125-
configurable: true,
126-
});
127-
return;
128-
}
129-
record[key] = value;
130-
}
131-
132111
/**
133112
* Rebuild the object `objectToFormData` flattened. Numeric segments and `[]`
134113
* become arrays. Scalars are left as the client encoded them.
@@ -155,7 +134,7 @@ function formRecordToObject(
155134
if (Array.isArray(parent)) {
156135
return segment === '' ? undefined : parent[Number(segment)];
157136
}
158-
return own(parent, segment);
137+
return Object.hasOwn(parent, segment) ? parent[segment] : undefined;
159138
};
160139

161140
const write = (
@@ -174,7 +153,12 @@ function formRecordToObject(
174153
parent[index] = value;
175154
return;
176155
}
177-
setOwn(parent, segment, value);
156+
Object.defineProperty(parent, segment, {
157+
value,
158+
writable: true,
159+
enumerable: true,
160+
configurable: true,
161+
});
178162
};
179163

180164
const assign = (segments: Array<string>, value: FormEntry): void => {

‎packages/void/test/unit/define-handler.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,40 @@ describe('defineHandler.withValidator', () => {
463463
});
464464
});
465465

466+
it.each(['multipart', 'urlencoded'])(
467+
'preserves prototype-named form fields as ordinary own properties in %s bodies',
468+
async (encoding) => {
469+
let received: unknown;
470+
const handler = defineHandler.withValidator({ body: passSchema() })((_c, { body }) => {
471+
received = body;
472+
return body;
473+
});
474+
const app = createValidatedApp('post', '/test', handler);
475+
const fields = ['__proto__', 'constructor', 'prototype', 'toString', 'hasOwnProperty'];
476+
const form = encoding === 'multipart' ? new FormData() : new URLSearchParams();
477+
for (const field of fields) {
478+
form.append(`${field}[value]`, field);
479+
}
480+
form.append('constructor[prototype][polluted]', 'form value');
481+
const res = await app.request(
482+
new Request('http://localhost/test', { method: 'POST', body: form }),
483+
);
484+
485+
expect(res.status).toBe(200);
486+
expect(await res.json()).toEqual({
487+
...Object.fromEntries(fields.map((field) => [field, { value: field }])),
488+
constructor: { value: 'constructor', prototype: { polluted: 'form value' } },
489+
});
490+
const parsed = received as Record<string, unknown>;
491+
expect(Object.getPrototypeOf(parsed)).toBe(Object.prototype);
492+
for (const field of fields) {
493+
expect(Object.hasOwn(parsed, field)).toBe(true);
494+
expect(Object.getPrototypeOf(parsed[field])).toBe(Object.prototype);
495+
}
496+
expect(Object.hasOwn(Object.prototype, 'polluted')).toBe(false);
497+
},
498+
);
499+
466500
it.each([
467501
['multipart', 'indexed'],
468502
['urlencoded', 'indexed'],

‎packages/void/test/unit/runtime-log.test.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ describe('runtime log', () => {
102102
const parsed = JSON.parse(arg);
103103
expect(parsed.msg).toBe('visible');
104104
expect('level' in parsed).toBe(false);
105+
expect(Object.hasOwn(parsed, '__proto__')).toBe(true);
105106
});
106107

107108
it('throwing enumerable getters do not crash — fallback to safe shape', () => {
@@ -123,15 +124,15 @@ describe('runtime log', () => {
123124
expect(parsed.error).toBe('log: fields not serializable');
124125
});
125126

126-
it('a `constructor` key in fields is skipped', () => {
127-
logger.info('ctor', { constructor: 'evil', keep: 'yes' });
127+
it.each(['__proto__', 'constructor', 'prototype'])('preserves %s as an own log field', (key) => {
128+
logger.info('visible', { [key]: 'value', keep: 'yes' });
128129

129130
const arg = (console.info as ReturnType<typeof vi.fn>).mock.calls[0][0] as string;
130131
const parsed = JSON.parse(arg);
131-
expect(parsed.msg).toBe('ctor');
132+
expect(parsed.msg).toBe('visible');
132133
expect(parsed.keep).toBe('yes');
133-
// Object.prototype already has `constructor`, so check own properties only.
134-
expect(Object.hasOwn(parsed, 'constructor')).toBe(false);
134+
expect(Object.hasOwn(parsed, key)).toBe(true);
135+
expect(parsed[key]).toBe('value');
135136
});
136137

137138
it('non-stringifiable values (circular refs) do not crash — fallback to safe shape', () => {
@@ -241,7 +242,7 @@ describe('runtime log', () => {
241242
component: 'worker',
242243
msg: 'ignored',
243244
toJSON: () => ({ hacked: true }),
244-
constructor: 'ignored',
245+
constructor: 'custom',
245246
fn: () => {},
246247
})
247248
.info('safe');
@@ -251,6 +252,7 @@ describe('runtime log', () => {
251252
expect(JSON.parse(arg)).toEqual({
252253
msg: 'safe',
253254
component: 'worker',
255+
constructor: 'custom',
254256
});
255257
});
256258

0 commit comments

Comments
 (0)