Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues. This is not a duplicate of [BUG] CrossFilter short-circuits OPTIONS preflight with 200 OK even for disallowed origins, bypassing the filter chain #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: false
-
Start 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:
shenyu/shenyu-web/src/main/java/org/apache/shenyu/web/filter/CrossFilter.java
Lines 68 to 114 in 6a757d4
| final String scheme = exchange.getRequest().getURI().getScheme(); | |
| final CrossFilterConfig.AllowedOriginConfig allowedOriginConfig = this.filterConfig.getAllowedOrigin(); | |
| Set<String> allowedOrigin = Optional.ofNullable(allowedOriginConfig.getPrefixes()).orElse(Collections.emptySet()) | |
| .stream() | |
| .filter(StringUtils::isNoneBlank) | |
| // scheme://prefix spacer domain | |
| .map(prefix -> String.format("%s://%s%s%s", | |
| scheme, prefix.trim(), | |
| StringUtils.defaultString(allowedOriginConfig.getSpacer(), ".").trim(), | |
| StringUtils.defaultString(allowedOriginConfig.getDomain(), "").trim())) | |
| .collect(Collectors.toSet()); | |
| // add all origin domains | |
| allowedOrigin.addAll(Optional.ofNullable(allowedOriginConfig.getOrigins()).orElse(Collections.emptySet()) | |
| .stream() | |
| .filter(StringUtils::isNoneBlank) | |
| .map(oneOrigin -> { | |
| if (ALL.equals(oneOrigin) || oneOrigin.startsWith(String.format("%s://", scheme))) { | |
| return oneOrigin.trim(); | |
| } | |
| return String.format("%s://%s", scheme, oneOrigin.trim()); | |
| }) | |
| .collect(Collectors.toSet())); | |
| allowCors = allowedOrigin.contains(origin) || allowedOrigin.contains(ALL); | |
| // if the origin is not allow check match origin again | |
| String originRegex; | |
| if (!allowCors && StringUtils.isNotBlank(originRegex = this.filterConfig.getAllowedOrigin().getOriginRegex())) { | |
| allowCors = Pattern.matches(originRegex.trim(), origin); | |
| } | |
| } | |
| if (allowCors) { | |
| // "Access-Control-Allow-Origin" | |
| headers.set(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin); | |
| // "Access-Control-Allow-Methods" | |
| this.filterSameHeader(headers, HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, | |
| this.filterConfig.getAllowedMethods()); | |
| // "Access-Control-Max-Age" | |
| this.filterSameHeader(headers, HttpHeaders.ACCESS_CONTROL_MAX_AGE, | |
| this.filterConfig.getMaxAge()); | |
| // "Access-Control-Allow-Headers" | |
| this.filterSameHeader(headers, HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, | |
| this.filterConfig.getAllowedHeaders()); | |
| // "Access-Control-Expose-Headers" | |
| this.filterSameHeader(headers, HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, | |
| this.filterConfig.getAllowedExpose()); | |
| // "Access-Control-Allow-Credentials" | |
| this.filterSameHeader(headers, HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, | |
| String.valueOf(this.filterConfig.isAllowCredentials())); |
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.
Activity
[-][BUG] CrossFilter allowedOrigin policy can reject configured origins and be overridden by upstream CORS headers[/-][+][BUG] CrossFilter allowedOrigin can omit CORS headers for a configured absolute origin[/+][-][BUG] CrossFilter allowedOrigin can omit CORS headers for a configured absolute origin[/-][+][BUG] CrossFilter can omit CORS headers for allowed origins and does not set Vary: Origin[/+]YadavKshitiz commentedon Sep 29, 2026
hello @Aias00
i would like to work on this issue. could you please assign it to me?
Aias00 commentedon Sep 29, 2026
just comment “/assign”
YadavKshitiz commentedon Sep 29, 2026
/assign