Skip to content

Selectively disable csrf by RequestMatcher #5477

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

Closed
jzheaux opened this issue Jul 3, 2018 · 6 comments
Closed

Selectively disable csrf by RequestMatcher #5477

jzheaux opened this issue Jul 3, 2018 · 6 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@jzheaux
Copy link
Contributor

jzheaux commented Jul 3, 2018

CsrfConfigurer can already be selectively disabled using ant patterns:

csrf().ignoringAntMatchers("/no-csrf/**")

Being able to ignore by a RequestMatcher would lend more power to the user as other parts of the request would now become available as well:

csrf().ignoringRequestMatchers(request -> request.getHeader("Authorization") != null)
@jzheaux jzheaux self-assigned this Jul 3, 2018
@jzheaux jzheaux added this to the 5.1.0.M2 milestone Jul 3, 2018
jzheaux added a commit to jzheaux/spring-security that referenced this issue Jul 16, 2018
This introduces an evolution on CsrfConfigurer#ignoreAntMatchers,
allowing users to specify a RequestMatcher in the circumstance where
more than just the path needs to be analyzed to determine whether
CsrfFilter should require a token for the request.

Simply put, a user can now selectively disable csrf by request matcher
in addition to the way it can already be done with ant matchers.

Fixes: spring-projectsgh-5477
@rwinch rwinch closed this as completed in b7ccb63 Jul 16, 2018
@chrylis
Copy link

chrylis commented Sep 17, 2018

I came here to request this specific feature. Any chance of a backport to 4.3? (As far as I can tell, CsrfConfigurer should be able to expose an ignoringRequestMatchers there with no API difficulty.)

@jzheaux
Copy link
Contributor Author

jzheaux commented Sep 17, 2018

Probably not, @chrylis. There are no plans for a 4.3.x series of releases and the 4.2.x series is really just for bug fixes at this point.

You can do this by customizing the csrf filter outside of the DSL, though:

RequestMatcher myMatcher =
    new AndRequestMatcher(
        CsrfFilter.DEFAULT_CSRF_MATCHER, 
        new NegatedRequestMatcher(/* your logic */));
csrfFilter.setRequireCsrfProtectionMatcher(myMatcher);

And then either use addFilter:

CsrfFilter csrfFilter = new CsrfFilter(...);
// your customizations
http.addFilter(csrfFilter);

Or use withObjectPostProcessor:

http
    .withObjectPostProcessor(new ObjectPostProcessor<CsrfFilter>() {
        public CsrfFilter postProcess(CsrfFilter csrfFilter) {
            // your customizations
        }
    });

@chrylis
Copy link

chrylis commented Sep 25, 2018

My current approach has been to implement a custom matcher

.requireCsrfProtectionMatcher(new ExemptOAuthRequiresCsrfMatcher())

so it's working, but upgrading to 5 is not an option until authorization is implemented.

@rwinch
Copy link
Member

rwinch commented Oct 3, 2018

so it's working, but upgrading to 5 is not an option until authorization is implemented.

Can you clarify what you mean? Is there something Spring Security 5 is missing that is preventing an updated?

@chrylis
Copy link

chrylis commented Oct 24, 2018

@rwinch The project in question is an OAuth2 authorization server.

@rwinch
Copy link
Member

rwinch commented Oct 24, 2018

Yes. You are correct. You would need to use the old support for an authorization server at this point.

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

No branches or pull requests

3 participants