Description
With credentials enabled, Spring rejects allowedOrigins("*") but accepts setAllowedOriginPatterns(List.of("*")). That pattern matches every request origin, and Spring returns the matched origin in the response header.
Potential impact
If the browser can send an authentication cookie, an untrusted site may read the user’s data from any endpoint using this policy.
Remediation
For APIs that need credentials, list the exact trusted origins with setAllowedOrigins. Keep server-side authentication, authorization and CSRF protection in place.
Examples
These excerpts build a configuration object to connect to the application's CORS handling. Also configure the required methods and headers.
Before
import java.util.List;
import org.springframework.web.cors.CorsConfiguration;
public class UnsafeCorsConfig {
CorsConfiguration build() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOriginPatterns(List.of("*"));
config.setAllowCredentials(true);
return config;
}
}
After
import java.util.List;
import org.springframework.web.cors.CorsConfiguration;
public class SafeCorsConfig {
CorsConfiguration build() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(List.of("https://app.example.com"));
config.setAllowCredentials(true);
return config;
}
}
This configuration allows credentialed cross-origin reads from https://app.example.com only. Browser cookie policies still govern whether a cookie is sent.
When using @CrossOrigin, replace originPatterns="*" with explicit trusted origins.