Skip to content

Commit 62252c1

Browse files
msqrjzheaux
authored andcommitted
Default to XorCsrfChannelInterceptor in XML
Change WebSocketMessageBrokerSecurityBeanDefinitionParser to use XorCsrfChannelInterceptor by default, so WebSocket XML configuration matches the default Xor-based configuration already in WebSocketMessageBrokerSecurityConfiguration. Closes gh-17260 Signed-off-by: Matt Magoffin <matt@solarnetwork.net>
1 parent a9636c7 commit 62252c1

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

config/src/main/java/org/springframework/security/config/websocket/WebSocketMessageBrokerSecurityBeanDefinitionParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
import org.springframework.security.messaging.util.matcher.MessageMatcher;
7171
import org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher;
7272
import org.springframework.security.messaging.util.matcher.SimpMessageTypeMatcher;
73-
import org.springframework.security.messaging.web.csrf.CsrfChannelInterceptor;
73+
import org.springframework.security.messaging.web.csrf.XorCsrfChannelInterceptor;
7474
import org.springframework.security.messaging.web.socket.server.CsrfTokenHandshakeInterceptor;
7575
import org.springframework.util.AntPathMatcher;
7676
import org.springframework.util.Assert;
@@ -368,7 +368,7 @@ else if (CSRF_HANDSHAKE_HANDLER_CLASSES.contains(beanClassName)) {
368368
interceptors.add(new RootBeanDefinition(SecurityContextChannelInterceptor.class));
369369
if (!this.sameOriginDisabled) {
370370
if (!registry.containsBeanDefinition(CSRF_CHANNEL_INTERCEPTOR_BEAN_ID)) {
371-
interceptors.add(new RootBeanDefinition(CsrfChannelInterceptor.class));
371+
interceptors.add(new RootBeanDefinition(XorCsrfChannelInterceptor.class));
372372
}
373373
else {
374374
interceptors.add(new RuntimeBeanReference(CSRF_CHANNEL_INTERCEPTOR_BEAN_ID));

config/src/test/java/org/springframework/security/config/websocket/WebSocketMessageBrokerConfigTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
23+
import java.util.Base64;
2324
import java.util.HashMap;
2425
import java.util.Map;
2526
import java.util.function.Supplier;
@@ -98,6 +99,13 @@ public class WebSocketMessageBrokerConfigTests {
9899

99100
private static final String CONFIG_LOCATION_PREFIX = "classpath:org/springframework/security/config/websocket/WebSocketMessageBrokerConfigTests";
100101

102+
/*
103+
* Token format: "token" length random pad bytes + "token" (each byte UTF8 ^= 1).
104+
*/
105+
private static final byte[] XOR_CSRF_TOKEN_BYTES = new byte[] { 1, 1, 1, 1, 1, 117, 110, 106, 100, 111 };
106+
107+
private static final String XOR_CSRF_TOKEN_VALUE = Base64.getEncoder().encodeToString(XOR_CSRF_TOKEN_BYTES);
108+
101109
public final SpringTestContext spring = new SpringTestContext(this);
102110

103111
@Autowired(required = false)
@@ -126,7 +134,7 @@ public void sendWhenNoIdSpecifiedThenIntegratesWithClientInboundChannel() {
126134
public void sendWhenAnonymousMessageWithConnectMessageTypeThenPermitted() {
127135
this.spring.configLocations(xml("NoIdConfig")).autowire();
128136
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
129-
headers.setNativeHeader(this.token.getHeaderName(), this.token.getToken());
137+
headers.setNativeHeader(this.token.getHeaderName(), XOR_CSRF_TOKEN_VALUE);
130138
this.clientInboundChannel.send(message("/permitAll", headers));
131139
}
132140

@@ -198,7 +206,7 @@ public void sendWhenNoIdSpecifiedThenIntegratesWithAuthorizationManager() {
198206
public void sendWhenAnonymousMessageWithConnectMessageTypeThenAuthorizationManagerPermits() {
199207
this.spring.configLocations(xml("NoIdAuthorizationManager")).autowire();
200208
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.CONNECT);
201-
headers.setNativeHeader(this.token.getHeaderName(), this.token.getToken());
209+
headers.setNativeHeader(this.token.getHeaderName(), XOR_CSRF_TOKEN_VALUE);
202210
this.clientInboundChannel.send(message("/permitAll", headers));
203211
}
204212

0 commit comments

Comments
 (0)