Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/tools/emulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,29 @@ function headerStringTransform(
if (value === '') {
return {};
}
let parsed: unknown;
try {
const parsed = JSON.parse(value);
parsed = JSON.parse(value);
if (
typeof parsed !== 'object' ||
parsed === null ||
Array.isArray(parsed)
) {
throw new Error('Headers must be a JSON object');
}
return parsed as Record<string, string>;
} catch (error) {
throw new Error(
`Invalid JSON for headers: ${error instanceof Error ? error.message : String(error)}`,
);
}
const headers: Record<string, string> = {};
for (const [key, headerValue] of Object.entries(parsed)) {
if (typeof headerValue !== 'string') {
throw new Error(`Header "${key}" must have a string value`);
}
headers[key] = headerValue;
}
return headers;
}

const throttlingOptions: [string, ...string[]] = [
Expand Down
23 changes: 23 additions & 0 deletions tests/tools/emulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,29 @@ describe('emulation', () => {
'Invalid JSON for headers: Headers must be a JSON object',
);
});

it('rejects non-string extraHttpHeaders values in schema', () => {
const {args} = createHandlerMocks();
const schema = zod.object(emulate(args).schema);

for (const [json, headerName] of [
['{"X-Count": 1}', 'X-Count'],
['{"X-Flag": true}', 'X-Flag'],
['{"X-Null": null}', 'X-Null'],
['{"X-Object": {}}', 'X-Object'],
['{"X-Array": []}', 'X-Array'],
] as const) {
const result = schema.safeParse({
pageId: 1,
extraHttpHeaders: json,
});
assert.strictEqual(result.success, false);
assert.strictEqual(
result.error.issues[0].message,
`Header "${headerName}" must have a string value`,
);
}
});
});

describe('colorScheme', () => {
Expand Down
Loading