0

I am building a collection of lambdas with proxy integration, using AWS Serverless (SAM). I am trying my Lambdas from a frontend I have in Blazor WASM. When a GET request is issued, then I get my results on my browser without CORS issues.

But I cannot issue a PUT request without getting the dreaded:

Access to fetch at 'https://myapi.execute-api.eu-central-1.amazonaws.com/Prod/updatevictual?userId=georanto@gmail.com&victualId=da1b2daa-3a73-425e-812e-e2f164f54507' from origin 'https://localhost:7260' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

If I create a dedicated OPTIONS Lambda for the PUT related end-point (as suggested here) then it works. I cannot have an OPTIONS Lambda because I intend to add authentication and the OPTIONS lambda does not work with it.

According to this it should be enough to send the headers as a response. And in the case of GET it is!

To fill in my response headers(adjusting for .net6), I set my cors by attaching the headers at the returned request thus:

private static APIGatewayProxyResponse AllowCors(this APIGatewayProxyResponse response)
{
    response.Headers ??= new Dictionary<string, string>();
    response.Headers.Add("Access-Control-Allow-Headers",
        "Content-Type, Authorization, X-Amz-Date, X-Api-Key, X-Amz-Security-Token");
    response.Headers.Add("Access-Control-Allow-Methods", "*");
    response.Headers.Add("Access-Control-Allow-Origin", "*");
    response.Headers.Add("Access-Control-Allow-Credentials", "false");
    return response;
}

I am also logging my responses in Cloudwatch and the put response after the postman request is thus:

2022-05-12T06:08:30.297Z    ecffb8ac-0cd9-4626-be96-6260e7a76d47    info    Responding with:[{
"statusCode": 201,
"headers": {
    "Access-Control-Allow-Headers": "Content-Type, Authorization, X-Amz-Date, X-Api-Key, X-Amz-Security-Token",
    "Access-Control-Allow-Methods": "*",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Credentials": "false"
},
"multiValueHeaders": null,
"body": null,
"isBase64Encoded": false}]

When I issue the request in Postman I see in the response's headers that the CORS headers are set the way they were supposed to:

enter image description here

So I don't think that this is a code issue.

I have also tried to put all the methods (i.e. response.Headers.Add("Access-Control-Allow-Methods", "OPTIONS, DELETE, GET, HEAD, PATCH, POST, PUT") but also didn't work.

Any other ideas what could that be?

georanto
  • 111
  • 11

1 Answers1

0

I "think" I had the same issue but I'm not familiar with .net... in your cloudfront distribution configuration, be sure to have a caching behavior that allows for put/patch/etc. and configure in that behavior a cache policy that depends on your headers

Cyril Cheney
  • 116
  • 2
  • I have not yet published my frontend to cloudfront. I run it locally and try to access my backend on the cloud. – georanto May 12 '22 at 19:20