Skip to content

Add support checking same security matchers human reviewers #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

GuusArts
Copy link
Owner

@GuusArts GuusArts commented Apr 9, 2025

Closes spring-projectsgh-15982

Summary by Sourcery

Add validation to prevent duplicate security matchers in Spring Security configuration

New Features:

  • Implement validation to detect and prevent multiple security filter chains with identical request matchers

Enhancements:

  • Add equals and hashCode methods to OrRequestMatcher to support matcher comparison
  • Improve filter chain validation to check for duplicate security matchers

Tests:

  • Add test case to verify that configuring multiple security filter chains with the same matcher throws an exception

Copy link

sourcery-ai bot commented Apr 9, 2025

Reviewer's Guide by Sourcery

This pull request introduces a mechanism to detect and prevent the configuration of multiple security filter chains with identical request matchers. It achieves this by adding a validator to the FilterChainProxy that checks for duplicate matchers during the application's startup. Additionally, the OrRequestMatcher class was updated to include implementations for equals and hashCode.

Sequence diagram for FilterChainProxy initialization with validator

sequenceDiagram
  participant WebSecurity
  participant FilterChainProxy
  participant DefaultFilterChainValidator

  WebSecurity->>FilterChainProxy: setFilterChainValidator(new DefaultFilterChainValidator())
  FilterChainProxy->>DefaultFilterChainValidator: new DefaultFilterChainValidator()
  WebSecurity->>FilterChainProxy: afterPropertiesSet()
  FilterChainProxy->>DefaultFilterChainValidator: validate(filterChains)
  loop For each filterChain in filterChains
    DefaultFilterChainValidator->>filterChain: getRequestMatcher()
  end
  alt Duplicate matchers found
    DefaultFilterChainValidator-->>WebSecurity: Throw IllegalArgumentException
  else No duplicate matchers
    DefaultFilterChainValidator-->>FilterChainProxy: (Validation successful)
  end
Loading

Updated class diagram for OrRequestMatcher

classDiagram
  class OrRequestMatcher {
    -List~RequestMatcher~ requestMatchers
    +OrRequestMatcher(requestMatchers: List~RequestMatcher~)
    +matches(request: HttpServletRequest): boolean
    +matcher(request: HttpServletRequest): MatchResult
    +equals(o: Object): boolean
    +hashCode(): int
    +toString(): String
  }
Loading

File-Level Changes

Change Details Files
Added a check to prevent the configuration of multiple security matchers with the same pattern, throwing a BeanCreationException if duplicates are found.
  • Added MultipleSecurityMatchersConfig to demonstrate the scenario that throws a BeanCreationException.
  • Added a test case configureWhenSameSecurityMatchersConfiguredThenThrowsBeanCreationException to verify the exception is thrown when duplicate security matchers are configured.
config/src/test/java/org/springframework/security/config/annotation/web/builders/WebSecurityTests.java
Modified DefaultFilterChainValidator to check for duplicate request matchers across different filter chains.
  • Added checkForDuplicateMatchers method to validate that no two filter chains use the same request matcher.
  • The checkPathOrder method now checks if the current element is an instance of DefaultSecurityFilterChain before proceeding with the check.
  • The checkForDuplicateMatchers method now checks if the current element is an instance of DefaultSecurityFilterChain before proceeding with the check.
config/src/main/java/org/springframework/security/config/http/DefaultFilterChainValidator.java
Implemented equals and hashCode methods in OrRequestMatcher to allow for proper comparison of OrRequestMatcher instances.
  • Added equals method to compare the requestMatchers field.
  • Added hashCode method to generate a hash code based on the requestMatchers field.
web/src/main/java/org/springframework/security/web/util/matcher/OrRequestMatcher.java
Set DefaultFilterChainValidator to FilterChainProxy to enable validation of filter chains.
  • Added filterChainProxy.setFilterChainValidator(new DefaultFilterChainValidator()); to set the validator.
config/src/main/java/org/springframework/security/config/annotation/web/builders/WebSecurity.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @GuusArts - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The exception message in DefaultFilterChainValidator could be improved to suggest using requestMatchers instead of securityMatcher when defining multiple security filter chains.
  • Consider adding an equals and hashCode implementation to AnyRequestMatcher.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@GuusArts GuusArts changed the title Add support checking same security matchers Add support checking same security matchers human reviewers Apr 9, 2025
@GuusArts GuusArts closed this Apr 11, 2025
@GuusArts GuusArts reopened this Apr 11, 2025
Copy link

snyk-io bot commented Apr 11, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

code/snyk check is complete. No issues have been found. (View Details)

@GuusArts GuusArts requested a review from Copilot May 14, 2025 09:55
@GuusArts GuusArts closed this May 14, 2025
@GuusArts GuusArts reopened this May 14, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request introduces validation to prevent the configuration of multiple security filter chains with identical request matchers. Key changes include adding equals and hashCode methods to the OrRequestMatcher, enhancing the filter chain validator to check for duplicate matchers, and adding a corresponding negative test case.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
web/src/main/java/org/springframework/security/web/util/matcher/OrRequestMatcher.java Added equals and hashCode methods for proper matcher comparison
config/src/test/java/org/springframework/security/config/annotation/web/builders/WebSecurityTests.java Added test to verify that duplicate security matchers throw a BeanCreationException
config/src/main/java/org/springframework/security/config/http/DefaultFilterChainValidator.java Modified duplicate matcher check and adjusted error messaging for clarity
config/src/main/java/org/springframework/security/config/annotation/web/builders/WebSecurity.java Integrated the new filter chain validator into the filter chain proxy

+ " matcher " + chain.getRequestMatcher() + ". If you are using multiple <http> namespace "
+ "elements, you must use a 'pattern' attribute to define the request patterns to which they apply.");
if (test instanceof DefaultSecurityFilterChain securityFilterChain) {
if (chain.getRequestMatcher().equals(securityFilterChain.getRequestMatcher())) {
Copy link
Preview

Copilot AI May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider refactoring the duplicate matcher check into a separate helper method to improve readability and reduce nested logic.

Suggested change
if (chain.getRequestMatcher().equals(securityFilterChain.getRequestMatcher())) {
if (hasDuplicateMatcher(chain, securityFilterChain)) {

Copilot uses AI. Check for mistakes.

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fail when several filter chains have the same securityMatcher
2 participants