Skip to content

Improve support for port numbers in allowedOriginPattern of CorsConfiguration #26927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -176,7 +177,9 @@ else if (this.allowedOrigins == DEFAULT_PERMIT_ALL && CollectionUtils.isEmpty(th
* it always sets the {@code Access-Control-Allow-Origin} response header to
* the matched origin and never to {@code "*"}, nor to any other pattern, and
* therefore can be used in combination with {@link #setAllowCredentials}
* set to {@code true}.
* set to {@code true}. Patterns also support list of allowed ports for origin,
* e.g. {@code "https://*.domain1.com:[8080,8081]"}. Additionally wildcard is supported
* in case any generic port should be allowed {@code "https://*.domain1.com:[*]"}.
* <p>By default this is not set.
* @since 5.3
*/
Expand Down Expand Up @@ -647,6 +650,8 @@ public List<String> checkHeaders(@Nullable List<String> requestHeaders) {
*/
private static class OriginPattern {

private static final Pattern PORT_LIST_PATTERN = Pattern.compile("(.*):\\[(\\*|[\\d,]+)]");

private final String declaredPattern;

private final Pattern pattern;
Expand All @@ -657,8 +662,25 @@ private static class OriginPattern {
}

private static Pattern toPattern(String patternValue) {
//if pattern ends with allowed ports list
Matcher matcher = PORT_LIST_PATTERN.matcher(patternValue);
String portList = null;
if (matcher.matches()) {
patternValue = matcher.group(1);
portList = matcher.group(2);
}

patternValue = "\\Q" + patternValue + "\\E";
patternValue = patternValue.replace("*", "\\E.*\\Q");

if (ALL.equals(portList)) {
//there is a corner case. If '*' is specified, then origins with implicit default port (e.g. "https://test.com") should also match.
patternValue += "(:\\d+)?";
}
else if (portList != null) {
patternValue += ":(" + portList.replace(',', '|') + ")";
}

return Pattern.compile(patternValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ public void checkOriginPatternAllowed() {
config.addAllowedOriginPattern("https://*.domain.com");
assertThat(config.checkOrigin("https://example.domain.com")).isEqualTo("https://example.domain.com");

config.addAllowedOriginPattern("https://*.port.domain.com:[*]");
assertThat(config.checkOrigin("https://example.port.domain.com")).isEqualTo("https://example.port.domain.com");
assertThat(config.checkOrigin("https://example.port.domain.com:1234")).isEqualTo("https://example.port.domain.com:1234");

config.addAllowedOriginPattern("https://*.specific.port.com:[8080,8081]");
assertThat(config.checkOrigin("https://example.specific.port.com:8080")).isEqualTo("https://example.specific.port.com:8080");
assertThat(config.checkOrigin("https://example.specific.port.com:8081")).isEqualTo("https://example.specific.port.com:8081");
assertThat(config.checkOrigin("https://example.specific.port.com:1234")).isNull();

config.setAllowCredentials(false);
assertThat(config.checkOrigin("https://example.domain.com")).isEqualTo("https://example.domain.com");
}
Expand All @@ -352,6 +361,9 @@ public void checkOriginPatternNotAllowed() {

config.setAllowedOriginPatterns(new ArrayList<>());
assertThat(config.checkOrigin("https://domain.com")).isNull();

config.setAllowedOriginPatterns(Collections.singletonList("https://*.specific.port.com:[8080,8081]"));
assertThat(config.checkOrigin("https://example.specific.port.com:1234")).isNull();
}

@Test
Expand Down