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 + API Auth breaks #863

Open
ironhacker opened this issue Feb 28, 2025 · 9 comments
Open

CORS + API Auth breaks #863

ironhacker opened this issue Feb 28, 2025 · 9 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@ironhacker
Copy link

ironhacker commented Feb 28, 2025

I downloaded and compiled main to get the new CORS support and it works OK until I enable API Auth. Either setting works on its own, but both combined fail with a 401 Unauthorized on the preflight request. If I make the same GET request from Postman (no preflight) with API Key it's good.

export ENABLE_AUTH=true
export AUTH_DATA_SOURCE=file://auth.yaml (abbreviated here)
export AUTH_PROVIDER=apiKey (this seems required now - defaults to basic auth)

CORS + ENABLE_AUTH=false (this works fine)

CORS + ENABLE_AUTH=true (works in Postman, but fails in Browser with 401 in preflight)

My auth file looks like this. Aside: this seems confusing. Turning on auth should secure all endpoints by default. I have no idea what's leaking.

name: db2rest-security

resourceRoles:
  - resource: "/v1/rdbms/db/**"
    method: get
    roles:
      - admin
  - resource: "/v1/rdbms/db/**"
    method: post
    roles:
      - admin

apiKeys:
  - key: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
    roles: [admin]
    active: true

Do I have a configuration issue? FYI I tried adding options explicitly - didn't help.

@kdhrubo
Copy link
Collaborator

kdhrubo commented Feb 28, 2025

There are some issues with auth #848

You will hear back from us soon.

@kdhrubo kdhrubo added the needs-triage Needs triage label Feb 28, 2025
@kdhrubo kdhrubo modified the milestones: Feb2025, Mar2025 Feb 28, 2025
@kdhrubo
Copy link
Collaborator

kdhrubo commented Mar 2, 2025

@ironhacker able to replicate with a simple vue app

Image

@kdhrubo kdhrubo added bug Something isn't working and removed needs-triage Needs triage labels Mar 2, 2025
@kdhrubo
Copy link
Collaborator

kdhrubo commented Mar 2, 2025

The pre-flight request is getting blocked by Auth filter.
The CORS problem has to be solved by configuring the CORSFilter with a higher priority than the auth filter @AmeyaKulkarni2001

@AmeyaKulkarni2001
Copy link
Contributor

Hi,
i understand, let me look into this

@kdhrubo
Copy link
Collaborator

kdhrubo commented Mar 3, 2025

Solution outline

  1. Use CORS Filter - https://docs.spring.io/spring-framework/reference/web/webmvc-cors.html#mvc-cors-filter
  2. Javadoc - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/filter/CorsFilter.html
  3. Ensure this filter is only created if CORS is enabled (new parameter).
  4. This filter should have higher priority than Auth filter.
  5. The configuration should look like this:
cors:
  enabled : true
  mappings: 
     - mapping: /actor/**
       allowedOrigin: http://localhost:3000 # this should not be *
       allowedHeader: *
       allowedMethod: *
    .... more mappings
       

@AmeyaKulkarni2001
Copy link
Contributor

This bug has been fixed in the latest release, can we close this issue now?

@kdhrubo
Copy link
Collaborator

kdhrubo commented Mar 9, 2025 via email

@AmeyaKulkarni2001
Copy link
Contributor

AmeyaKulkarni2001 commented Mar 9, 2025

It seems that I do not have permission to close a issue, probably cause I have not created it @ironhacker or @kdhrubo could you close this issue?

Also should the documentation also be updated for the new parameter introduced and for how the CORS is now used?

@kdhrubo
Copy link
Collaborator

kdhrubo commented Mar 9, 2025

Yes doc has to be updated thats pending.

@ironhacker can you pls check the latest release and confirm this is resolved for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants