spring-projects / spring-projects/spring-security

UrlMapping with identical matcher applied in wrong order

Open
#5,464 2 comments 0 reactions 1 assignee View on GitHub

@jzheaux is already working on this.

Since Nov 4, 2020.

in: docs type: enhancement
Dominant language
Java
Stars
9.6k
Forks
6.3k
Avg merge
2d 11h
Merged PRs (30d)
52

Description

Summary

The docs for authorizeRequests state:

There are multiple children to the http.authorizeRequests() method each matcher is considered in the order they were declared.

However, if two matchers are declared with the exact same pattern, the second one defined actually "wins" instead of the first one.

This appears to be caused by converting a List to a Map. In AbstractConfigAttributeRequestMatcherRegistry in the createRequestMap function:

for (UrlMapping mapping : getUrlMappings()) {
  RequestMatcher matcher = mapping.getRequestMatcher();
  Collection<ConfigAttribute> configAttrs = mapping.getConfigAttrs();
  requestMap.put(matcher, configAttrs);
}

Just changing put to putIfAbsent in the last line of the for loop should resolve this issue.

Actual Behavior
@Override
protected void configure(HttpSecurity http) throws Exception {
  http
    .authorizeRequests()
    .antMatchers("A").denyAll()
    .antMatchers("A").permitAll();

The "denyAll" is ignored and any URL matching "A" will be permitted.

Expected Behavior

I would expect URLs matching "A" to be denied since that was defined first.

Version

4.2.3-RELEASE
but appears to be the same in the latest code

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.