Skip to content

Commit 088ebe2

Browse files
author
Steve Riesenberg
committed
Default CsrfTokenRequestProcessor.csrfRequestAttributeName = _csrf
Issue gh-11764 Issue gh-4001
1 parent ed41a60 commit 088ebe2

File tree

7 files changed

+27
-20
lines changed

7 files changed

+27
-20
lines changed

Diff for: config/src/main/resources/org/springframework/security/config/spring-security-6.0.rnc

+6-3
Original file line numberDiff line numberDiff line change
@@ -1114,15 +1114,18 @@ csrf =
11141114
csrf-options.attlist &=
11151115
## Specifies if csrf protection should be disabled. Default false (i.e. CSRF protection is enabled).
11161116
attribute disabled {xsd:boolean}?
1117-
csrf-options.attlist &=
1118-
## The request attribute name the CsrfToken is set on. Default is to set to CsrfToken.parameterName
1119-
attribute request-attribute-name { xsd:token }?
11201117
csrf-options.attlist &=
11211118
## The RequestMatcher instance to be used to determine if CSRF should be applied. Default is any HTTP method except "GET", "TRACE", "HEAD", "OPTIONS"
11221119
attribute request-matcher-ref { xsd:token }?
11231120
csrf-options.attlist &=
11241121
## The CsrfTokenRepository to use. The default is HttpSessionCsrfTokenRepository wrapped by LazyCsrfTokenRepository.
11251122
attribute token-repository-ref { xsd:token }?
1123+
csrf-options.attlist &=
1124+
## The CsrfTokenRequestAttributeHandler to use. The default is CsrfTokenRequestProcessor.
1125+
attribute request-attribute-handler-ref { xsd:token }?
1126+
csrf-options.attlist &=
1127+
## The CsrfTokenRequestResolver to use. The default is CsrfTokenRequestProcessor.
1128+
attribute request-resolver-ref { xsd:token }?
11261129

11271130
headers =
11281131
## Element for configuration of the HeaderWritersFilter. Enables easy setting for the X-Frame-Options, X-XSS-Protection and X-Content-Type-Options headers.

Diff for: config/src/main/resources/org/springframework/security/config/spring-security-6.0.xsd

+12-7
Original file line numberDiff line numberDiff line change
@@ -3145,13 +3145,6 @@
31453145
</xs:documentation>
31463146
</xs:annotation>
31473147
</xs:attribute>
3148-
<xs:attribute name="request-attribute-name" type="xs:token">
3149-
<xs:annotation>
3150-
<xs:documentation>The request attribute name the CsrfToken is set on. Default is to set to
3151-
CsrfToken.parameterName
3152-
</xs:documentation>
3153-
</xs:annotation>
3154-
</xs:attribute>
31553148
<xs:attribute name="request-matcher-ref" type="xs:token">
31563149
<xs:annotation>
31573150
<xs:documentation>The RequestMatcher instance to be used to determine if CSRF should be applied. Default is
@@ -3166,6 +3159,18 @@
31663159
</xs:documentation>
31673160
</xs:annotation>
31683161
</xs:attribute>
3162+
<xs:attribute name="request-attribute-handler-ref" type="xs:token">
3163+
<xs:annotation>
3164+
<xs:documentation>The CsrfTokenRequestAttributeHandler to use. The default is CsrfTokenRequestProcessor.
3165+
</xs:documentation>
3166+
</xs:annotation>
3167+
</xs:attribute>
3168+
<xs:attribute name="request-resolver-ref" type="xs:token">
3169+
<xs:annotation>
3170+
<xs:documentation>The CsrfTokenRequestResolver to use. The default is CsrfTokenRequestProcessor.
3171+
</xs:documentation>
3172+
</xs:annotation>
3173+
</xs:attribute>
31693174
</xs:attributeGroup>
31703175
<xs:element name="headers">
31713176
<xs:annotation>

Diff for: config/src/test/java/org/springframework/security/config/annotation/web/configuration/DeferHttpSessionJavaConfigTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.springframework.security.config.test.SpringTestContextExtension;
3333
import org.springframework.security.web.DefaultSecurityFilterChain;
3434
import org.springframework.security.web.FilterChainProxy;
35-
import org.springframework.security.web.csrf.CsrfTokenRequestProcessor;
3635
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
3736
import org.springframework.security.web.csrf.LazyCsrfTokenRepository;
3837

Diff for: web/src/main/java/org/springframework/security/web/csrf/CsrfTokenRequestAttributeHandler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import java.util.function.Supplier;
2020

21-
import javax.servlet.http.HttpServletRequest;
22-
import javax.servlet.http.HttpServletResponse;
21+
import jakarta.servlet.http.HttpServletRequest;
22+
import jakarta.servlet.http.HttpServletResponse;
2323

2424
/**
2525
* A callback interface that is used to make the {@link CsrfToken} created by the

Diff for: web/src/main/java/org/springframework/security/web/csrf/CsrfTokenRequestProcessor.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import java.util.function.Supplier;
2020

21-
import javax.servlet.http.HttpServletRequest;
22-
import javax.servlet.http.HttpServletResponse;
21+
import jakarta.servlet.http.HttpServletRequest;
22+
import jakarta.servlet.http.HttpServletResponse;
2323

2424
import org.springframework.util.Assert;
2525

@@ -34,7 +34,7 @@
3434
*/
3535
public class CsrfTokenRequestProcessor implements CsrfTokenRequestAttributeHandler, CsrfTokenRequestResolver {
3636

37-
private String csrfRequestAttributeName;
37+
private String csrfRequestAttributeName = "_csrf";
3838

3939
/**
4040
* The {@link CsrfToken} is available as a request attribute named

Diff for: web/src/main/java/org/springframework/security/web/csrf/CsrfTokenRequestResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.security.web.csrf;
1818

19-
import javax.servlet.http.HttpServletRequest;
19+
import jakarta.servlet.http.HttpServletRequest;
2020

2121
/**
2222
* Implementations of this interface are capable of resolving the token value of a

Diff for: web/src/test/java/org/springframework/security/web/csrf/CsrfTokenRequestProcessorTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ public void handleWhenCsrfTokenIsNullThenThrowsIllegalArgumentException() {
7878

7979
@Test
8080
public void handleWhenCsrfRequestAttributeSetThenUsed() {
81-
this.processor.setCsrfRequestAttributeName("_csrf");
81+
this.processor.setCsrfRequestAttributeName("_csrf.attr");
8282
this.processor.handle(this.request, this.response, () -> this.token);
8383
assertThat(this.request.getAttribute(CsrfToken.class.getName())).isEqualTo(this.token);
84-
assertThat(this.request.getAttribute("_csrf")).isEqualTo(this.token);
84+
assertThat(this.request.getAttribute("_csrf.attr")).isEqualTo(this.token);
8585
}
8686

8787
@Test
8888
public void handleWhenValidParametersThenRequestAttributesSet() {
8989
this.processor.handle(this.request, this.response, () -> this.token);
9090
assertThat(this.request.getAttribute(CsrfToken.class.getName())).isEqualTo(this.token);
91-
assertThat(this.request.getAttribute(this.token.getParameterName())).isEqualTo(this.token);
91+
assertThat(this.request.getAttribute("_csrf")).isEqualTo(this.token);
9292
}
9393

9494
@Test

0 commit comments

Comments
 (0)