question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allow origin isn't sufficient to resolve CORS issue

See original GitHub issue

Description

Adding allow origin and allow headers at template.yaml and returning Access-Control-Allow-Origin in the lambda response body seems insufficient to resolve cross origin issue after deploying the lambda service.

Steps to reproduce

sample of template.yaml

Globals:
  Function:
    Timeout: 60
  Api:
    Cors:
      AllowMethods: "'POST,OPTIONS'"
      AllowHeaders: "'*'"
      AllowOrigin: "'*'"
      AllowCredentials: "'*'"

Resources:
  EndpointFunction:
    Type: AWS::Serverless::Function
      CodeUri: api/v1/
      Handler: endpoint.lambda_handler
      Runtime: ruby2.5
      Events:
        Endpoint:
          Type: Api
          Properties:
            Path: /api/v1/endpoint
            Method: post

sample of endpoint.rb

def lambda_handler(event:, context:)
  {
    statusCode: 201,
    headers: {
      'Access-Control-Allow-Origin' => '*',
      'Content-Type' => 'application/json'
    }
  }
end

Observed result

Access to XMLHttpRequest at 'https://<random-sha>.execute-api.<aws-region>amazonaws.com/prod/api/v1/endpoint' from origin 'https://<domain>' 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.

Update: This turned out to be a url of lambda service is being case sensitive. Aside from that, there is currently no way to set multiple domains for allow origin. Also, the hassle way of defining an option function doesn’t seem to work either at least local #1434

Expected result

able to make request from the browser

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. sam --version: 0.22.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
han-so1omoncommented, May 29, 2020

@kimyu92 were you able to make requests from the browser? if possible could you point me to your code so that I may study it? I’m having Cross-Origin Request Blocked: ... CORS header ‘Access-Control-Allow-Origin’ missing for a simple POST request

1reaction
jfusscommented, Nov 12, 2019

@kimyu92

in the lambda response body seems insufficient to resolve cross origin issue after deploying the lambda service.

Are you seeing CORS issues with the Lambda Service or locally with SAM CLI? It sounds like this is an issue you are having with the Service.

Aside from that, there is currently no way to set multiple domains for allow origin.

This is a limitation of API Gateway: https://stackoverflow.com/a/39628785

Also, the hassle way of defining an option function doesn’t seem to work either at least local #1434

We have a open PR addressing this issue: https://github.com/awslabs/aws-sam-cli/pull/1464

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 Ways to Fix the CORS Error — and How the Access-Control ...
Fix one: install the Allow-Control-Allow-Origin plugin. The quickest fix you can make is to install the moesif CORS extension .
Read more >
CORS header 'Access-Control-Allow-Origin' does not match ...
The origin making the request does not match the origin permitted by the Access-Control-Allow-Origin header. This error can also occur if ...
Read more >
Fixing Common Problems with CORS and JavaScript
Tutorial: This post walks through troubleshooting and fixing common problems associated with calling REST APIs from JavaScript.
Read more >
CORS errors and how to solve them - Topcoder
Still facing a CORS error? ... Open a network tab in your console. ... In the response header look for the Access-Control-Allow-Origin header....
Read more >
Fixing CORS issues with fetch [duplicate] - Stack Overflow
(Reason: CORS request did not succeed) Is that supposed the headers 'Access-Control-Allow-Origin':''* was supposed to fix this issue?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found