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
5 changes: 3 additions & 2 deletions src/http/handlers/cors/cors-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,13 @@ describe('CorsHandler', () => {
expect(sendSpy).toHaveBeenCalled();
});

it('should reject preflight with 403 when requested headers are not allowed', async () => {
it('should reject preflight with 403 when ANY requested header is not allowed', async () => {
const handler = new CorsHandler({
allowedHeaders: ['Content-Type', 'Authorization'],
});
setupRequest('https://example.com', 'OPTIONS');
mockReq.headers!['access-control-request-headers'] = 'X-Custom-Header, X-Forbidden';
// Content-Type is allowed, but X-Forbidden is not
mockReq.headers!['access-control-request-headers'] = 'Content-Type, X-Forbidden';

const handled = await handleCors(handler);

Expand Down
8 changes: 4 additions & 4 deletions src/http/handlers/cors/cors-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ export class CorsHandler {
// User explicitly configured allowedHeaders - validate requested headers
const requested = requestedHeaders.split(',').map((h) => h.trim().toLowerCase());
const allowed = this.options.allowedHeaders.map((h) => h.toLowerCase());
const validated = requested.filter((h) => allowed.includes(h));
const isValid = requested.every((h) => allowed.includes(h));

if (validated.length === 0) {
// Requested headers not allowed - reject preflight
if (!isValid) {
// One or more requested headers are not allowed - reject preflight
res.status(403).send();
return true;
}
allowedHeadersToSend = validated.join(', ');
allowedHeadersToSend = requested.join(', ');
} else if (requestedHeaders) {
// No allowedHeaders configured or empty array - echo back (permissive mode)
allowedHeadersToSend = requestedHeaders;
Expand Down
Loading