generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 185
Closed
Labels
bugSomething isn't workingSomething isn't workingcompletedThis item is complete and has been merged/shippedThis item is complete and has been merged/shippedevent-handlerThis item relates to the Event Handler UtilityThis item relates to the Event Handler Utility
Description
Expected Behavior
- If the
Originheader is not present, , it should not set any additional headers. - If the value of the
Originheader does not match any of the values in the list of allowed origin, it should not set any additional headers. - If the resource doesn't support credentials, no need to add the
Access-Control-Allow-Credentialsheader - When using an array to configure the allowed origin, if the request origin doesn't match the allowed origin, it should not set any additional headers.
Vary: Originshould only be used when there is a need to dynamically generate theAccess-Control-Allow-Originheader. For fixed allowed origin and*, it is not necessary
Current Behavior
There are some cases where CORS middleware is not working as expected.
- If the
Originheader is not present, , it is still setting the remaining headers - If the value of the
Originheader does not match any of the values in the list of allowed origin, it is still setting the remaining headers - If the resource doesn't support credentials, it is setting the
Access-Control-Allow-Credentialsheader to false - When using an array to configure the allowed origin, if the request origin doesn't match the allowed origin, it is still setting remaining headers.
Vary: Originis added for all fixed allowed origin other than *
Code snippet
import { Router } from '@aws-lambda-powertools/event-handler/experimental-rest';
import { cors } from '@aws-lambda-powertools/event-handler/experimental-rest/middleware';
import { Logger } from '@aws-lambda-powertools/logger';
import type { Context } from 'aws-lambda';
const logger = new Logger({ serviceName: 'pong-service' });
const app = new Router();
app.use(async (_, reqCtx, next) => {
logger.info('Request received', {
headers: reqCtx.event.headers,
});
await next();
});
app.use(cors({
origin: ['http://localhost:8000', '*']
}));
app.get('/ping', async () => {
return { message: 'pong' };
});
export const handler = async (event: unknown, context: Context) =>
app.resolve(event, context);Steps to Reproduce
- Create a Lambda function and use the CORS middleware in the event handler
- Deploy the Lambda function
- Make a request with different values for the
Originheader and observe as you change the allowed Origin configuration to notice the inconsistencies
Possible Solution
- Short circuit wherever applicable to prevent additional headers being added
- Use the wildcard origin if it is in the array of allowed origin
Powertools for AWS Lambda (TypeScript) version
latest
AWS Lambda function runtime
22.x
Packaging format used
npm
Execution logs
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingcompletedThis item is complete and has been merged/shippedThis item is complete and has been merged/shippedevent-handlerThis item relates to the Event Handler UtilityThis item relates to the Event Handler Utility
Type
Projects
Status
Shipped