Skip to content

Commit 3d4bcf1

Browse files
baezzysmarcusdacoregio
authored andcommitted
fix: Restrict automatic CORS configuration to UrlBasedCorsConfigurationSource
- Update CORS configuration logic to automatically enable .cors() only if a UrlBasedCorsConfigurationSource bean is present. - Modify applyCorsIfAvailable method to check for UrlBasedCorsConfigurationSource instances.
1 parent 0190763 commit 3d4bcf1

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,14 +47,15 @@
4747
import org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter;
4848
import org.springframework.web.accept.ContentNegotiationStrategy;
4949
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
50-
import org.springframework.web.cors.CorsConfigurationSource;
50+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
5151

5252
import static org.springframework.security.config.Customizer.withDefaults;
5353

5454
/**
5555
* {@link Configuration} that exposes the {@link HttpSecurity} bean.
5656
*
5757
* @author Eleftheria Stein
58+
* @author Jinwoo Bae
5859
* @since 5.4
5960
*/
6061
@Configuration(proxyBeanMethods = false)
@@ -131,8 +132,7 @@ HttpSecurity httpSecurity() throws Exception {
131132
}
132133

133134
private void applyCorsIfAvailable(HttpSecurity http) throws Exception {
134-
String[] beanNames = this.context.getBeanNamesForType(CorsConfigurationSource.class);
135-
if (beanNames.length == 1) {
135+
if (this.context.getBeanNamesForType(UrlBasedCorsConfigurationSource.class).length > 0) {
136136
http.cors(withDefaults());
137137
}
138138
}

config/src/test/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfigurationTests.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -362,7 +362,7 @@ public void disableConfigurerWhenAppliedByAnotherConfigurerThenNotApplied() {
362362
}
363363

364364
@Test
365-
public void configureWhenCorsConfigurationSourceThenApplyCors() {
365+
public void configureWhenCorsConfigurationSourceThenApplyCors() throws Exception {
366366
this.spring.register(CorsConfigurationSourceConfig.class, DefaultWithFilterChainConfig.class).autowire();
367367
SecurityFilterChain filterChain = this.spring.getContext().getBean(SecurityFilterChain.class);
368368
CorsFilter corsFilter = (CorsFilter) filterChain.getFilters()
@@ -374,6 +374,16 @@ public void configureWhenCorsConfigurationSourceThenApplyCors() {
374374
assertThat(configSource).isInstanceOf(UrlBasedCorsConfigurationSource.class);
375375
}
376376

377+
// gh-15378
378+
@Test
379+
public void configureWhenNoUrlBasedCorsConfigThenNoCorsAppliedAndVaryHeaderNotPresent() throws Exception {
380+
this.spring.register(NonUrlBasedCorsConfig.class, DefaultWithFilterChainConfig.class).autowire();
381+
SecurityFilterChain filterChain = this.spring.getContext().getBean(SecurityFilterChain.class);
382+
assertThat(filterChain.getFilters()).noneMatch((filter) -> filter instanceof CorsFilter);
383+
384+
this.mockMvc.perform(get("/")).andExpect(header().doesNotExist("Vary"));
385+
}
386+
377387
@Test
378388
public void configureWhenAddingCustomDslUsingWithThenApplied() throws Exception {
379389
this.spring.register(WithCustomDslConfig.class, UserDetailsConfig.class).autowire();
@@ -673,6 +683,33 @@ CorsConfigurationSource corsConfigurationSource() {
673683

674684
}
675685

686+
@Configuration
687+
@EnableWebSecurity
688+
static class NonUrlBasedCorsConfig {
689+
690+
@Bean
691+
CorsConfigurationSource corsConfigurationSource() {
692+
return new CustomCorsConfigurationSource();
693+
}
694+
695+
@Bean
696+
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
697+
return http.build();
698+
}
699+
700+
}
701+
702+
static class CustomCorsConfigurationSource implements CorsConfigurationSource {
703+
704+
@Override
705+
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
706+
CorsConfiguration configuration = new CorsConfiguration();
707+
configuration.setAllowedOrigins(List.of("http://localhost:8080"));
708+
return configuration;
709+
}
710+
711+
}
712+
676713
static class DefaultConfigurer extends AbstractHttpConfigurer<DefaultConfigurer, HttpSecurity> {
677714

678715
boolean init;

docs/modules/ROOT/pages/servlet/integrations/cors.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CORS must be processed before Spring Security, because the pre-flight request do
66
If the request does not contain any cookies and Spring Security is first, the request determines that the user is not authenticated (since there are no cookies in the request) and rejects it.
77

88
The easiest way to ensure that CORS is handled first is to use the `CorsFilter`.
9-
Users can integrate the `CorsFilter` with Spring Security by providing a `CorsConfigurationSource`.
9+
Users can integrate the `CorsFilter` with Spring Security by providing a `CorsConfigurationSource`. Note that Spring Security will automatically configure CORS only if a `UrlBasedCorsConfigurationSource` instance is present.
1010
For example, the following will integrate CORS support within Spring Security:
1111

1212
[tabs]

0 commit comments

Comments
 (0)