Skip to content
Open
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 @@ -324,7 +324,7 @@ protected boolean isAllowedRequestUri(final String uri, CorsConfiguration config
protected boolean isAllowedOrigin(final String origin, CorsConfiguration configuration) {
for (Pattern pattern : configuration.getAllowedOriginPatterns()) {
// Making sure that the pattern matches
if (pattern.matcher(origin).find()) {
if (pattern.matcher(origin).matches()) {
return true;
}
}
Expand Down Expand Up @@ -366,6 +366,16 @@ private CorsConfiguration resolveDefaultCorsConfiguration() {
return getDefaultConfiguration();
}

private String anchorPattern(String pattern) {
if (!pattern.startsWith("^")) {
pattern = "^" + pattern;
}
if (!pattern.endsWith("$")) {
pattern = pattern + "$";
}
return pattern;
}
Comment on lines +369 to +377

private void compileAllowedOriginsAndUris(CorsConfiguration configuration, String type) {
if (configuration.getAllowedUris() != null) {
for (String allowedUri : configuration.getAllowedUris()) {
Expand All @@ -380,7 +390,7 @@ private void compileAllowedOriginsAndUris(CorsConfiguration configuration, Strin
if (configuration.getAllowedOrigins() != null) {
for (String allowedOrigin : configuration.getAllowedOrigins()) {
try {
configuration.getAllowedOriginPatterns().add(Pattern.compile(allowedOrigin));
configuration.getAllowedOriginPatterns().add(Pattern.compile(anchorPattern(allowedOrigin)));
log.debug("Origin '%s' is allowed for a %s CORS requests.".formatted(allowedOrigin, type));
Comment on lines 390 to 394
} catch (PatternSyntaxException patternSyntaxException) {
log.error("Invalid regular expression pattern in cors.{}.allowed.origins: {}", type, allowedOrigin, patternSyntaxException);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2172,7 +2172,7 @@ void logOutCorsPreflight() throws Exception {
*/
@Test
void logOutCorsPreflightForIdentityZone() throws Exception {
corsFilter.getFilter().setCorsXhrAllowedOrigins(asList("^localhost$", "^*\\.localhost$"));
corsFilter.getFilter().setCorsXhrAllowedOrigins(asList("^localhost$", "^.*\\.localhost$"));
corsFilter.getFilter().setCorsXhrAllowedUris(singletonList("^/logout.do$"));
Comment on lines +2175 to 2176
corsFilter.getFilter().initialize();

Expand Down Expand Up @@ -2266,7 +2266,7 @@ void logOutCorsPreflightWithUnallowedOrigin() throws Exception {
@Test
void xhrCorsPreflightForNonDefaultZoneWhenZoneSpecificCorsPolicyIsNull() throws Exception {
// setting the default zone CORS policy
corsFilter.getFilter().setCorsXhrAllowedOrigins(asList("^localhost$", "^*\\.localhost$"));
corsFilter.getFilter().setCorsXhrAllowedOrigins(asList("^localhost$", "^.*\\.localhost$"));
corsFilter.getFilter().setCorsXhrAllowedUris(singletonList("^/logout.do$"));
corsFilter.getFilter().initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ void logOutCorsPreflight(ZoneResolutionMode mode) throws Exception {
void logOutCorsPreflightForIdentityZone(ZoneResolutionMode mode) throws Exception {
String subdomain = "testzone1";
IdentityZone zone = MockMvcUtils.createOtherIdentityZone(subdomain, mockMvc, webApplicationContext, false, IdentityZoneHolder.getCurrentZoneId());
corsFilter.getFilter().setCorsXhrAllowedOrigins(asList("^localhost$", "^*\\.localhost$"));
corsFilter.getFilter().setCorsXhrAllowedOrigins(asList("^localhost$", "^.*\\.localhost$"));
List<String> allowedUris = mode == ZoneResolutionMode.ZONE_PATH ? asList("^/logout.do$", "^/z/[^/]+/logout.do$") : singletonList("^/logout.do$");
corsFilter.getFilter().setCorsXhrAllowedUris(allowedUris);
Comment on lines +2625 to 2627
corsFilter.getFilter().initialize();
Expand Down Expand Up @@ -2734,7 +2734,7 @@ void logOutCorsPreflightWithUnallowedOrigin(ZoneResolutionMode mode) throws Exce
@EnumSource(ZoneResolutionMode.class)
void xhrCorsPreflightForNonDefaultZoneWhenZoneSpecificCorsPolicyIsNull(ZoneResolutionMode mode) throws Exception {
// setting the default zone CORS policy
corsFilter.getFilter().setCorsXhrAllowedOrigins(asList("^localhost$", "^*\\.localhost$"));
corsFilter.getFilter().setCorsXhrAllowedOrigins(asList("^localhost$", "^.*\\.localhost$"));
// For ZONE_PATH mode, the request path is /z/{subdomain}/logout.do, so we need to allow that pattern
List<String> allowedUris = mode == ZoneResolutionMode.ZONE_PATH
? asList("^/logout.do$", "^/z/[^/]+/logout.do$")
Expand Down
Loading