Skip to content

Commit 29bf28f

Browse files
committed
fixed issue with upper/lower case request methods/headers
1 parent 15fcb9c commit 29bf28f

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

packages/event-handler/src/rest/middleware/cors.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,26 @@ export const cors = (options?: CorsOptions): Middleware => {
6464
const isOptions = reqCtx.request.method === HttpVerbs.OPTIONS;
6565
// Handle preflight OPTIONS request
6666
if (isOptions) {
67-
const requestMethod = reqCtx.request.headers.get(
68-
'Access-Control-Request-Method'
69-
);
70-
const requestHeaders = reqCtx.request.headers.get(
71-
'Access-Control-Request-Headers'
72-
);
67+
const requestMethod = reqCtx.request.headers
68+
.get('Access-Control-Request-Method')
69+
?.toUpperCase();
70+
const requestHeaders = reqCtx.request.headers
71+
.get('Access-Control-Request-Headers')
72+
?.toLowerCase();
7373
if (
7474
!requestMethod ||
75-
!config.allowMethods.includes(requestMethod) ||
75+
!config.allowMethods
76+
.map((m) => m.toUpperCase())
77+
.includes(requestMethod) ||
7678
!requestHeaders ||
7779
requestHeaders
7880
.split(',')
79-
.some((header) => !config.allowHeaders.includes(header.trim()))
81+
.some(
82+
(header) =>
83+
!config.allowHeaders
84+
.map((h) => h.toLowerCase())
85+
.includes(header.trim())
86+
)
8087
) {
8188
await next();
8289
return;

packages/event-handler/tests/unit/rest/middleware/cors.test.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,6 @@ describe('CORS Middleware', () => {
126126
it('does not set CORS headers when preflight request method does not match allowed method', async () => {
127127
// Prepare
128128
const app = new Router();
129-
app.options(
130-
'/test',
131-
[
132-
cors({
133-
allowMethods: ['POST'],
134-
}),
135-
],
136-
async () => ({ foo: 'bar' })
137-
);
138129

139130
// Act
140131
const result = await app.resolve(
@@ -152,15 +143,6 @@ describe('CORS Middleware', () => {
152143
it('does not set CORS headers when preflight request header does not match allowed header', async () => {
153144
// Prepare
154145
const app = new Router();
155-
app.options(
156-
'/test',
157-
[
158-
cors({
159-
allowHeaders: ['Authorization'],
160-
}),
161-
],
162-
async () => ({ foo: 'bar' })
163-
);
164146

165147
// Act
166148
const result = await app.resolve(
@@ -184,7 +166,7 @@ describe('CORS Middleware', () => {
184166
allowHeaders: ['Authorization', 'Content-Type'],
185167
maxAge: 3600,
186168
};
187-
app.options('/test', [cors(corsConfig)], async () => ({ foo: 'bar' }));
169+
app.use(cors(corsConfig));
188170

189171
// Act
190172
const result = await app.resolve(

0 commit comments

Comments
 (0)