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

CORS Options request requires authorization --> Preflight will always fail #1087

Closed
Phoinix-Dev opened this issue Jan 20, 2022 · 5 comments
Closed

Comments

@Phoinix-Dev
Copy link

Hello
I have a problem making a CORS request to the server 0.8.0 due to 401 unauthorized OPTIONS request, a CORS request is impossible.

Expected behaviour:
To make a CORS request in JS, it's expected that the OPTIONS request does not require authorization. The problem is, that a CORS authorization always requires a preflight, due to the custom Auth header. As the preflight itself will make an OPTIONS request that required authorization itself, its impossible to make the request.

Current behaviour:
Currently, the OPTIONS request for CORS receives a 401 unauthorized --> Request impossible.

Is there a built-in solution for that problem?
Thank you.

@Phoinix-Dev Phoinix-Dev changed the title CORS Options request required authorization --< CORS Options request requires authorization --> Preflight will always fail Jan 20, 2022
@ByteHamster
Copy link
Member

When switching to basic auth instead of digest auth, your password is sent in plain text (over the secure https channel if available) directly with the first request. Then there is no need for back-and-forth to exchange random numbers like with digest auth. No idea if that helps with CORS, though.

@ckulka
Copy link

ckulka commented Jan 28, 2022

Hi @Phoenix-100, saw the same while working on ckulka/baikal-docker#13. The solution was to handle the preflight (OPTIONS) requests in Apache or Nginx instead of Baikal's PHP code.

I came up with a working Nginx configuration, hope it helps:
https://github.com/ckulka/infcloud-docker/blob/dd03df55bb04a76a698e34efb438e571e9866ab2/examples/baikal-nginx.conf#L34-L72

@Phoinix-Dev
Copy link
Author

Thank you very much for your answers.
I could not find any way to solve this, without having to change the server code.
As a woraround, I did exactly what @ckulka recommended and made a rule using the Apache Rewrite-Engine.

@ckulka
Copy link

ckulka commented Mar 28, 2022

Glad it worked out! Can you post the Apache configuration snippet you added?

I bet others run into similar issues and you could help them out when they find this here.

@Phoinix-Dev
Copy link
Author

Of course, but I have to mention that I don't check where the request is coming from or going to.
It's really just a workaround.

RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ /html [R=200,L]

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, content-type, authorization"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS, REPORT"

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

3 participants