spring-projects / spring-projects/spring-security

Automatic registration of ServletRequestPathFilter causes UrlBasedCorsConfigurationSource to not match URL

Open
#17,207 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage type: bug
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Spring Security version: 6.5.0 release

Describe the bug

org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration#springSecurityPathPatternParserBeanDefinitionRegistryPostProcessor now automatically registers a ServletRequestPathFilter which runs before the Spring Security filter chain. This means that org.springframework.web.cors.UrlBasedCorsConfigurationSource#resolvePath always resolves a RequestPath which is relative to the application path (org.springframework.web.util.ServletRequestPathUtils#getCachedPath calls org.springframework.http.server.RequestPath#pathWithinApplication on the cached path).

This ignores the org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath setting when you configure the UrlBasedCorsConfigurationSource using org.springframework.web.cors.UrlBasedCorsConfigurationSource#setUrlPathHelper

To Reproduce

  1. Use Spring Security 6.5.0
  2. Use a UrlPathHelper with setAlwaysUseFullPath(true)
  3. Register a CorsConfiguration on CorsConfigurationSource with a pattern which matches the full path
  4. The CorsConfiguration will not be used

Expected behavior

The full path should be matched, and the CorsConfiguration should be used.

Sample

    @Bean
    public CorsConfigurationSource corsConfigurationSource(
            @Value("${rest.cors.allowedOrigins}") List<String> allowedOrigins,
            @Value("${rest.cors.allowedMethods}") List<String> allowedMethods,
            @Value("${rest.cors.allowedHeaders}") List<String> allowedHeaders,
            @Value("${rest.cors.exposedHeaders}") List<String> exposedHeaders,
            @Value("${rest.cors.allowCredentials}") boolean allowCredentials,
            @Value("${rest.cors.maxAge}") long maxAge) {

        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(allowedOrigins);
        configuration.setAllowedMethods(allowedMethods);
        configuration.setAllowedHeaders(allowedHeaders);
        configuration.setExposedHeaders(exposedHeaders);
        configuration.setAllowCredentials(allowCredentials);
        configuration.setMaxAge(maxAge);

        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setAlwaysUseFullPath(true); //Don't chop off the starting /rest stuff

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.setUrlPathHelper(urlPathHelper);
        source.registerCorsConfiguration("/rest/**", configuration);

        return source;
    }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with WebSecurityConfiguration#springSecurityPathPatternParserBeanDefinitionRegistryPostProcessor and trace how ServletRequestPathFilter affects UrlBasedCorsConfigurationSource#resolvePath. Compare the cached RequestPath with UrlPathHelper#setAlwaysUseFullPath using the reproduction, then verify that a full-path pattern such as /rest/** matches and its CorsConfiguration is used.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.