Skip to content

Commit 82c0ddc

Browse files
committed
Polish
- Add Reactive equivalent - Update copyright Issue gh-13310
1 parent e21da06 commit 82c0ddc

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,7 +88,7 @@ private static String getTokenValue(String actualToken, String token) {
8888
System.arraycopy(actualBytes, randomBytesSize, xoredCsrf, 0, tokenSize);
8989

9090
byte[] csrfBytes = xorCsrf(randomBytes, xoredCsrf);
91-
return Utf8.decode(csrfBytes);
91+
return (csrfBytes != null) ? Utf8.decode(csrfBytes) : null;
9292
}
9393

9494
private static String createXoredCsrfToken(SecureRandom secureRandom, String token) {
@@ -105,6 +105,9 @@ private static String createXoredCsrfToken(SecureRandom secureRandom, String tok
105105
}
106106

107107
private static byte[] xorCsrf(byte[] randomBytes, byte[] csrfBytes) {
108+
if (csrfBytes.length < randomBytes.length) {
109+
return null;
110+
}
108111
int len = Math.min(randomBytes.length, csrfBytes.length);
109112
byte[] xoredCsrf = new byte[len];
110113
System.arraycopy(csrfBytes, 0, xoredCsrf, 0, csrfBytes.length);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -182,6 +182,16 @@ public void resolveCsrfTokenValueWhenHeaderAndFormDataSetThenFormDataIsPreferred
182182
StepVerifier.create(csrfToken).expectNext(this.token.getToken()).verifyComplete();
183183
}
184184

185+
@Test
186+
public void resolveCsrfTokenIsInvalidThenReturnsNull() {
187+
this.exchange = MockServerWebExchange.builder(MockServerHttpRequest.post("/")
188+
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
189+
.body(this.token.getParameterName() + "=" + XOR_CSRF_TOKEN_VALUE)).build();
190+
CsrfToken token = new DefaultCsrfToken("headerName", "paramName", "a");
191+
Mono<String> csrfToken = this.handler.resolveCsrfTokenValue(this.exchange, token);
192+
assertThat(csrfToken.block()).isNull();
193+
}
194+
185195
private static Answer<Void> fillByteArray() {
186196
return (invocation) -> {
187197
byte[] bytes = invocation.getArgument(0);

0 commit comments

Comments
 (0)