[BUG] CrossFilter can omit CORS headers for allowed origins and does not set Vary: Origin
Is there an existing issue for this?
- I have searched the existing issues. This is not a duplicate of #6720, which fixed the disallowed
OPTIONSshort-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:
Origin: https://frontend.example.comcan receive neither:
Access-Control-Allow-Origin: https://frontend.example.com
Vary: OriginWithout 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:
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:
Access-Control-Allow-Credentials: trueSteps To Reproduce
Configure the gateway:
shenyu: cross: enabled: true allowedAnyOrigin: false allowedOrigin: origins: - https://frontend.example.com allowedMethods: "GET,POST,OPTIONS" allowedHeaders: "Content-Type,Authorization" allowCredentials: falseStart the gateway with an HTTP listener and request any existing route using the configured cross-origin Origin:
curl -i 'http://localhost:9195/<existing-route>' \ -H 'Origin: https://frontend.example.com'Observe that
Access-Control-Allow-Originis absent because the configured absolute Origin is normalized tohttp://https://frontend.example.combefore comparison.Configure an allowed Origin that matches the request URI scheme and repeat the request.
Access-Control-Allow-Origincan then be returned, butVary: Originis still absent.For comparison, a non-allowed Origin and a request without Origin should not receive CORS headers:
curl -i 'http://localhost:9195/<existing-route>' \ -H 'Origin: https://invalid.example.com' curl -i 'http://localhost:9195/<existing-route>'
Environment
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:
Suggested direction:
- Parse configured entries as origins/URIs.
- Preserve entries that already contain a valid scheme.
- Prefix only genuinely scheme-less entries.
- Add
Vary: OriginwheneverAccess-Control-Allow-Originis 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.
Source: apache/shenyu