#7370·shenyu

[BUG] CrossFilter can omit CORS headers for allowed origins and does not set Vary: Origin

Author: Aias00Created Sep 29, 2026Updated Sep 29, 2026
Labelstype: bug

Is there an existing issue for this?

  • I have searched the existing issues. This is not a duplicate of #6720, which fixed the disallowed OPTIONS short-circuit behavior.

Current Behavior

With shenyu.cross.allowedAnyOrigin=false and an explicit allowedOrigin allow-list, an allowed cross-origin request can receive no CORS response headers.

For example, given an allowed origin of https://frontend.example.com, a request containing:

http
Origin: https://frontend.example.com

can receive neither:

http
Access-Control-Allow-Origin: https://frontend.example.com
Vary: Origin

Without Access-Control-Allow-Origin, browsers block frontend code from reading the cross-origin response even though the origin is explicitly allowed.

One reproducible cause is the normalization of absolute origins in CrossFilter. A configured origin is preserved only when it starts with the current request URI scheme. Otherwise the current scheme is prefixed unconditionally:

java
if (ALL.equals(oneOrigin) || oneOrigin.startsWith(String.format("%s://", scheme))) {
    return oneOrigin.trim();
}
return String.format("%s://%s", scheme, oneOrigin.trim());

When the gateway request URI scheme is http, https://frontend.example.com becomes http://https://frontend.example.com and cannot match the incoming Origin. This can occur behind TLS termination/reverse proxies or whenever the frontend and gateway legitimately use different schemes.

In addition, when an allowed Origin does match, CrossFilter dynamically sets Access-Control-Allow-Origin to the request Origin but does not add Vary: Origin. A shared cache can therefore reuse a response generated for one Origin for another Origin.

Expected Behavior

The complete expected behavior is:

Request Expected CORS response
Carries an allowed cross-origin Origin Return Access-Control-Allow-Origin with the exact matched Origin and Vary: Origin
Carries a non-allowed Origin Do not return CORS allow headers
Does not carry Origin Do not return CORS headers
Carries an allowed preflight Origin Also return the configured allow-methods and allow-headers values
Carries a non-allowed preflight Origin Do not return CORS allow headers

Origin matching should be exact across scheme, host, and port. The gateway should never reflect an arbitrary Origin before it passes the allow-list check. Origin: null should not be implicitly allowed.

When credentials are enabled, Access-Control-Allow-Origin must remain the exact matched Origin rather than *, together with:

http
Access-Control-Allow-Credentials: true

Steps To Reproduce

  1. Configure the gateway:

    yaml
    shenyu:
      cross:
        enabled: true
        allowedAnyOrigin: false
        allowedOrigin:
          origins:
            - https://frontend.example.com
        allowedMethods: "GET,POST,OPTIONS"
        allowedHeaders: "Content-Type,Authorization"
        allowCredentials: false
  2. Start the gateway with an HTTP listener and request any existing route using the configured cross-origin Origin:

    bash
    curl -i 'http://localhost:9195/<existing-route>' \
      -H 'Origin: https://frontend.example.com'
  3. Observe that Access-Control-Allow-Origin is absent because the configured absolute Origin is normalized to http://https://frontend.example.com before comparison.

  4. Configure an allowed Origin that matches the request URI scheme and repeat the request. Access-Control-Allow-Origin can then be returned, but Vary: Origin is still absent.

  5. For comparison, a non-allowed Origin and a request without Origin should not receive CORS headers:

    bash
    curl -i 'http://localhost:9195/<existing-route>' \
      -H 'Origin: https://invalid.example.com'
    
    curl -i 'http://localhost:9195/<existing-route>'

Environment

markdown
ShenYu version(s): 2.7.0 and current master (6a757d4477d22330ca38af35a1e1f87262b1ddcc)

Debug logs

No exception is logged. The issue is visible in the final response headers.

Anything else?

Relevant current-master code:

https://github.com/apache/shenyu/blob/6a757d4477d22330ca38af35a1e1f87262b1ddcc/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java#L68-L114

Suggested direction:

  • Parse configured entries as origins/URIs.
  • Preserve entries that already contain a valid scheme.
  • Prefix only genuinely scheme-less entries.
  • Add Vary: Origin whenever Access-Control-Allow-Origin is dynamically selected from the request Origin.
  • Add tests for allowed/disallowed/no-Origin requests, preflight requests, absolute origins with a different request scheme, and Origin: null.