Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No 'Access-Control-Allow-Origin' header is present on the requested resource. #29

Closed
dconger opened this issue Feb 1, 2017 · 17 comments

Comments

@dconger
Copy link

dconger commented Feb 1, 2017

We are getting a "one-off" error every hundred or so requests to our api where the Access-Control-Allow-Origin header and other headers are not present on the resp_headers. You can see in the screenshots below that the "Working" request has all the appropriate Response Headers but the "Failed" request didn't add all of the Response Headers and thus, we get this error:

screen shot 2017-02-01 at 10 45 08 am

As far as how we're implementing CorsPlug, we simply have plug CORSPlug included at the top of our endpoint.ex file with no origin or any other options specified so it just falls back to the defaults.

I've looked through the cors_plug.ex source code and no clue why these headers wouldn't be added every hundred or so requests. Any thoughts as to what the issue might be?

Screenshots

Working:

screen shot 2017-02-01 at 10 43 00 am
screen shot 2017-02-01 at 10 43 13 am

Failed:

screen shot 2017-02-01 at 10 43 23 am
screen shot 2017-02-01 at 10 43 32 am

@avocade
Copy link

avocade commented Feb 6, 2017

On second thought, breaking this out to a new issue.

But +1 for this as well.

@yordis
Copy link

yordis commented Jun 7, 2017

@mschae is this still valid? I saw that you did some release 14 days ago so I am wondering if this is fix already

@rcoedo
Copy link

rcoedo commented Jul 10, 2017

I'm having the same problem.

@mschae
Copy link
Owner

mschae commented Jul 15, 2017

I have no leads here and issues recreating the issue.

We should no longer pass back an empty header as of the release recently (will verify).

Any way I can recreate this issue?

@rcoedo
Copy link

rcoedo commented Jul 21, 2017

In my case I was having problems in an empty 1.3 phoenix application using the following configuration:

config :cors_plug,
  origin: ["http://localhost:3000", "http://localhost:3001"]

I had issues with cors_plug versions 1.4 and 1.3, and I was able to fix it by downgrading to 1.2.1

Sorry for the late response, I hope this helps! 😄

@rcoedo
Copy link

rcoedo commented Jul 21, 2017

Oh, I forgot. with that configuration I had access from localhost:3000 but I could not reach the backend from localhost:3001. It may be a problem handling the url list.

@mschae
Copy link
Owner

mschae commented Jul 21, 2017

@rcoedo can you please give me more info so I can reproduce?

  • What did you expect to see?
  • What did you see instead?

Please provide the headers you are seeing (/not seeing).

Thanks

@rcoedo
Copy link

rcoedo commented Jul 21, 2017

I saw the exact same error shown in the first comment in the console, and Access-Control-Allow-Origin was set to null for localhost:3001, but it was correct for localhost:3000

I'm sorry that I can't give you more info to reproduce this, the project was just a toy project and I already deleted it.

@doomspork
Copy link

doomspork commented Aug 18, 2017

I just ran into this issue myself and am currently working through it to hopefully find a resolution. In my particular case the issue arose when we run both http and https.

@bjunc
Copy link

bjunc commented Feb 11, 2018

I am also receiving a null origin. This happens when I attempt any method for explicitly setting allowed origins. I've tried lists, regex, function, config, etc.. All result in null. Any thoughts? I'm using v1.5.

resp_headers: [
    {"cache-control", "max-age=0, private, must-revalidate"},
    {"vary", "Origin"},
    {"access-control-allow-origin", "null"},
    {"access-control-expose-headers", ""},
    {"access-control-allow-credentials", "true"},
    {"x-request-id", "saecnu6r28v1goopcu0g516bpf7po7vv"}
  ],

@mschae
Copy link
Owner

mschae commented Feb 11, 2018

@bjunc Hard to tell from what information you're providing.

Can you provide your configuration and how you're testing it? A gist or example project would be ideal.

For everyone else who comments on here: If you are experiencing this issue please provide a gist or a sample project with instructions on how I can test this. I have currently no leads tracking this down. Thank you!

@bjunc
Copy link

bjunc commented Feb 11, 2018

My app is pretty complex at this point, so it's possible there is a config conflict. However, I can create the error pretty simply:

  1. Add plug CORSPlug, origin: ["http://localhost:3000"] to endpoint.ex.
  2. Navigate to a simple route (no pipeline, no guardian, etc.).
  3. Inspect the response in Chrome dev tools
  4. You should see access-control-allow-origin: null

If I remove the origin from the plug, then the response comes back with access-control-allow-origin: *.

It seems no matter what method I attempt to add an allowed origin, it always comes back as null. It does seem doable that I can manually set access-control-allow-origin using put_resp_header() at the end of a pipeline. However, this isn't ideal...

I'm using v1.5, with Phoenix 1.3.0.

@mschae
Copy link
Owner

mschae commented Feb 13, 2018

@bjunc The origin is a domain name, not a URL. In your case it would have to be plug CORSPlug, origin: ["localhost"]

@yordis
Copy link

yordis commented Feb 13, 2018

@mschae if that is the case, then that was the issue for me for sure ..... my fault!

@bjunc
Copy link

bjunc commented Feb 13, 2018

@mschae I ultimately ditched the origin logic, so I can't say what I had originally used. It's possible what I wrote into the issue comment was not accurate (using "http://localhost:3000" instead of just locahost). Either way, I appreciate you looking into it.

One thing worth noting though, is that the README shows with and without the scheme/protocol; which might be where some of the confusion here is coming from.

Also, it's probably worth noting in the README that null is returned when there is a mismatch between the request origin and the allowed origins. Maybe even a debug warning in the console would help.

@mschae
Copy link
Owner

mschae commented Feb 13, 2018

Sorry you @yordis and @bjunc - nevermind, that was a wrong late-hour reply on my end.

Actually the origin is supposed to be a protocol, host, port triple. Looks like that's insufficiently covered by tests, I'll fix that.

That also means that if you want to allow the same origin on multiple ports, you have to specify each port from which you want to allow tests. So if your front-end is running on a different port than your back-end, you'll have to specify the front-ends port, not the back-ends. Hope that makes sense.

@mschae
Copy link
Owner

mschae commented Feb 13, 2018

I've updated tests and the README accordingly and added the note as suggested by @bjunc.

@mschae mschae closed this as completed Feb 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants