spring-cloud / spring-cloud/spring-cloud-gateway

RewriteResponseHeader is not rewriting response header for a preflight CORS request

Open
#2,409 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

Describe the bug
I would like to rewrite any response header from
Access-Control-Allow-Origin: null to Access-Control-Allow-Origin: *

RewriteResponseHeader is not rewriting response header Access-Control-Allow-Origin: null for a CORS preflight request when the preflight request header contains Access-Control-Request-Method and Origin. Disabling other headers will not affect the result.

For other restful request (GET, POST etc), the RewriteResponseHeader will rewrite the response header Access-Control-Allow-Origin: null to Access-Control-Allow-Origin: *

Other Observation
I observed that the gateway will auto-fill response header Access-Control-Allow-Origin with value of request header Origin.

I am not sure why this is the behavior but it cause some security issue when request is with Origin: null.
Therefore I would like to rewrite the response header Access-Control-Allow-Origin: null to Access-Control-Allow-Origin: *

Other Alternatives Tried
I tried to set SetResponseHeader=Access-Control-Allow-Origin, *, but still the response header is not rewritten for a preflight request header containing Access-Control-Request-Method and Origin

Sample
I have below setting in application.yml
image

An preflight request that gateway failed to rewrite the response header
image

If we omit Access-Control-Request-Method in preflight, the response header will be rewritten
image

Non-preflight request that gateway successfully rewrite the response header
image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the RewriteResponseHeader component and trace how preflight requests containing Access-Control-Request-Method and Origin are handled. Compare that path with the successful GET or POST behavior and verify the result using the request variations and application.yml configuration described in the issue. Done means Access-Control-Allow-Origin: null is rewritten as requested for preflight responses without breaking other requests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.