Skip to content

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

Closed

Description

@Aias00

Is there an existing issue for this?

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.com

can receive neither:

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:

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: true

Steps To Reproduce

  1. 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
  2. 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'
  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:

    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:

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: 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.

Activity

  1. added
    type: bug
    on Sep 29, 2026
  2. changed the title [-][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[/+] on Sep 29, 2026
  3. changed the title [-][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[/+] on Sep 29, 2026
  4. YadavKshitiz commented on Sep 29, 2026

    @YadavKshitiz

    hello @Aias00
    i would like to work on this issue. could you please assign it to me?

  5. Aias00 commented on Sep 29, 2026

    @Aias00
    ContributorAuthor

    hello @Aias00
    i would like to work on this issue. could you please assign it to me?

    just comment “/assign”

  6. YadavKshitiz commented on Sep 29, 2026

    @YadavKshitiz

    /assign

  7. deleted a comment from github-actions on Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions