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 Preflight responses missing Access-Control-Request-Headers #16208

Closed
YunaBraska opened this issue Apr 2, 2021 · 13 comments · Fixed by #29682
Closed

CORS Preflight responses missing Access-Control-Request-Headers #16208

YunaBraska opened this issue Apr 2, 2021 · 13 comments · Fixed by #29682
Assignees
Labels
area/security kind/bug Something isn't working
Milestone

Comments

@YunaBraska
Copy link

Describe the bug

I am unable to find the right setting to automatically set the Access-Control-Request-Headers in the Preflight/Options response. I tried using the setting quarkus.http.cors.headers by yaml config but that doesn't produce any header response on the OPTIONS endpoints (https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS#preflighted_requests_in_cors).
Wildcards aren't valid anymore.

Expected behavior

Quarkus generates all valid headers Access-Control-Request-Headers e.g. from the config quarkus.http.cors.headers and outputs this to the OPTION response.
Example

OPTIONS /resources/post-here/ HTTP/1.1
Host: bar.example
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Connection: keep-alive
Origin: https://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: X-PINGOTHER, Content-Type

Actual behavior

Access-Control-Request-Headers are missing

my request:

OPTIONS /users/login HTTP/1.1
Host: localhost:8081
User-Agent: insomnia/2021.2.2
Content-Type: application/json
Access-Control-Request-Headers: content-type
Accept: */*
Content-Length: 0

my current response

HTTP/1.1 200 OK
Content-Length: 24
Access-Control-Allow-Origin: *
Allow: HEAD, POST, GET, OPTIONS
Content-Type: text/plain;charset=UTF-8

Fronend/Browser blocks the request ccess to XMLHttpRequest at 'http://localhost:8081/users/login' from origin 'http://localhost:8080' has been blocked by CORS policy

My config:

quarkus:
  http:
    access-log:
      enabled: true
    cors:
      ~: true
      origins: "*"
      methods: "POST, GET, PUT, OPTIONS, DELETE"
      headers: "accept, origin, authorization, content-type, Content-Type, x-requested-with, access-control-request-headers"
      exposed-headers: "accept, origin, authorization, content-type, Content-Type, x-requested-with, access-control-request-headers"

To Reproduce


### Screenshots
(If applicable, add screenshots to help explain your problem.)

## Environment (please complete the following information):

### Output of `uname -a` or `ver`

### Output of `java -version`

### GraalVM version (if different from Java)

### Quarkus version or git rev

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

## Additional context
(Add any other context about the problem here.)
@YunaBraska YunaBraska added the kind/bug Something isn't working label Apr 2, 2021
@sberyozkin
Copy link
Member

@YunaBraska, it looks like it is a formatting issue.
The code which deals with this header is here. So it takes headers and then adds the request header which is contained in those headers to Access-Control-Request-Headers

So it should certainly return Content-Type.
Can you please try plain text application.properties format just to eliminate the formatting issue ?

@YunaBraska
Copy link
Author

YunaBraska commented Apr 2, 2021

Properties looks like the same...

My config

quarkus.http.access-log.enabled=true
quarkus.http.cors=true
quarkus.http.cors.origins=*
quarkus.http.cors.methods=POST, GET, PUT, OPTIONS, DELETE
quarkus.http.cors.headers=Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With

Browser Request:

Request URL: http://localhost:8081/users/login
Request Method: OPTIONS
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,de;q=0.8,en-US;q=0.7
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: localhost:8081
Origin: http://localhost:8080
Referer: http://localhost:8080/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36

Quarkus response:

HTTP/1.1 200 OK
Content-Length: 24
Access-Control-Allow-Origin: *
Allow: HEAD, POST, GET, OPTIONS
Content-Type: text/plain;charset=UTF-8

Browser error:
Access to XMLHttpRequest at 'http://localhost:8081/users/login' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

@sberyozkin
Copy link
Member

@YunaBraska Can you remove between the values ? The code does not trim at the moment (will have to be fixed)

@YunaBraska
Copy link
Author

Hm it doesn't work either, its totally ignoring my CORS config.... My current workaround is to create a @Provider like this CorsOverwriteHeaders implements ContainerResponseFilter and put the config hardcoded in there :(

@MarkusKramer
Copy link

I'm seeing the same issue. However, the issue only seems to occur when compiling to a native image. When running in dev mode (JRE based) CORS is working fine.
I'm also using a custom CORS filter a workaround now.

@giacomobartoli
Copy link

@MarkusKramer experiencing the same issue. Did you find a solution or workaround?
Currently it works fine on localhost, but the error appears when deployed to open shift.
Using Quarkus 1.6.0.

@YunaBraska
Copy link
Author

@MarkusKramer experiencing the same issue. Did you find a solution or workaround?

Currently it works fine on localhost, but the error appears when deployed to open shift.

Using Quarkus 1.6.0.

For me it works fine until the native image

@MarkusKramer
Copy link

I'm using this as workaround (writting in Kotlin):

import javax.ws.rs.container.ContainerResponseContext
import javax.ws.rs.container.ContainerResponseFilter
import javax.ws.rs.ext.Provider

@Provider
class CorsFilter : ContainerResponseFilter {

    override fun filter(requestContext: ContainerRequestContext, responseContext: ContainerResponseContext) {
        responseContext.headers.add("Access-Control-Allow-Origin", "*")
        responseContext.headers.add("Access-Control-Allow-Headers", "*")
        responseContext.headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
    }

}

It works in dev mode and native image, from locahost and from remote hosts.

@MarkusKramer
Copy link

Just wanted to confirm that I'm still seeing this issue with quarkus 2.5.0.Final. The browser requests "Access-Control-Request-Headers: content-type,x-build", quarkus responds with "access-control-allow-headers: content-type" only, despite having set "quarkus.http.cors=true" in the application config.

@AliLozano
Copy link

AliLozano commented Sep 10, 2022

Just wanted to confirm that I'm still seeing this issue with quarkus 2.5.0.Final. The browser requests "Access-Control-Request-Headers: content-type,x-build", quarkus responds with "access-control-allow-headers: content-type" only, despite having set "quarkus.http.cors=true" in the application config.

Just happened to me, I tested it in dev and quarkus returns x-build in the allow headers, but when I run it with docker it only returns content-type (Tested in 2.12.1.Final)

@geoand
Copy link
Contributor

geoand commented Nov 11, 2022

@sberyozkin what should be done with this issue?

@sberyozkin sberyozkin self-assigned this Dec 4, 2022
@sberyozkin
Copy link
Member

Will be looking at this issue next week

@sberyozkin
Copy link
Member

HI All,

I've opened a PR confirming Quarkus correctly manages this CORS header, #29682
This PR will close this issue once approved/merged.
Please re-open the issue if the problem persists - but only with the reproducer :-)
Thanks

@quarkus-bot quarkus-bot bot added this to the 2.16 - main milestone Dec 6, 2022
@gsmet gsmet modified the milestones: 2.16 - main, 2.15.0.Final Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/security kind/bug Something isn't working
Projects
None yet
7 participants