|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2022 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
46 | 46 | */
|
47 | 47 | public class XorServerCsrfTokenRequestAttributeHandlerTests {
|
48 | 48 |
|
| 49 | + /* |
| 50 | + * Token format: 3 random pad bytes + 3 padded bytes. |
| 51 | + */ |
49 | 52 | private static final byte[] XOR_CSRF_TOKEN_BYTES = new byte[] { 1, 1, 1, 96, 99, 98 };
|
50 | 53 |
|
51 | 54 | private static final String XOR_CSRF_TOKEN_VALUE = Base64.getEncoder().encodeToString(XOR_CSRF_TOKEN_BYTES);
|
@@ -188,6 +191,78 @@ public void resolveCsrfTokenValueWhenHeaderAndFormDataSetThenFormDataIsPreferred
|
188 | 191 | StepVerifier.create(csrfToken).expectNext(this.token.getToken()).verifyComplete();
|
189 | 192 | }
|
190 | 193 |
|
| 194 | + // gh-13310, gh-15184 |
| 195 | + @Test |
| 196 | + public void resolveCsrfTokenValueWhenCsrfBytesIsShorterThanRandomBytesThenReturnsNull() { |
| 197 | + /* |
| 198 | + * Token format: 3 random pad bytes + 2 padded bytes. |
| 199 | + */ |
| 200 | + byte[] actualBytes = { 1, 1, 1, 96, 99 }; |
| 201 | + String actualToken = Base64.getEncoder().encodeToString(actualBytes); |
| 202 | + this.exchange = MockServerWebExchange |
| 203 | + .builder(MockServerHttpRequest.post("/") |
| 204 | + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
| 205 | + .header(this.token.getHeaderName(), actualToken)) |
| 206 | + .build(); |
| 207 | + String tokenValue = this.handler.resolveCsrfTokenValue(this.exchange, this.token).block(); |
| 208 | + assertThat(tokenValue).isNull(); |
| 209 | + } |
| 210 | + |
| 211 | + // gh-13310, gh-15184 |
| 212 | + @Test |
| 213 | + public void resolveCsrfTokenValueWhenCsrfBytesIsLongerThanRandomBytesThenReturnsNull() { |
| 214 | + /* |
| 215 | + * Token format: 3 random pad bytes + 4 padded bytes. |
| 216 | + */ |
| 217 | + byte[] actualBytes = { 1, 1, 1, 96, 99, 98, 97 }; |
| 218 | + String actualToken = Base64.getEncoder().encodeToString(actualBytes); |
| 219 | + this.exchange = MockServerWebExchange |
| 220 | + .builder(MockServerHttpRequest.post("/") |
| 221 | + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
| 222 | + .header(this.token.getHeaderName(), actualToken)) |
| 223 | + .build(); |
| 224 | + String tokenValue = this.handler.resolveCsrfTokenValue(this.exchange, this.token).block(); |
| 225 | + assertThat(tokenValue).isNull(); |
| 226 | + } |
| 227 | + |
| 228 | + // gh-13310, gh-15184 |
| 229 | + @Test |
| 230 | + public void resolveCsrfTokenValueWhenTokenBytesIsShorterThanActualBytesThenReturnsNull() { |
| 231 | + this.exchange = MockServerWebExchange |
| 232 | + .builder(MockServerHttpRequest.post("/") |
| 233 | + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
| 234 | + .header(this.token.getHeaderName(), XOR_CSRF_TOKEN_VALUE)) |
| 235 | + .build(); |
| 236 | + CsrfToken csrfToken = new DefaultCsrfToken("headerName", "paramName", "a"); |
| 237 | + String tokenValue = this.handler.resolveCsrfTokenValue(this.exchange, csrfToken).block(); |
| 238 | + assertThat(tokenValue).isNull(); |
| 239 | + } |
| 240 | + |
| 241 | + // gh-13310, gh-15184 |
| 242 | + @Test |
| 243 | + public void resolveCsrfTokenValueWhenTokenBytesIsLongerThanActualBytesThenReturnsNull() { |
| 244 | + this.exchange = MockServerWebExchange |
| 245 | + .builder(MockServerHttpRequest.post("/") |
| 246 | + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
| 247 | + .header(this.token.getHeaderName(), XOR_CSRF_TOKEN_VALUE)) |
| 248 | + .build(); |
| 249 | + CsrfToken csrfToken = new DefaultCsrfToken("headerName", "paramName", "abcde"); |
| 250 | + String tokenValue = this.handler.resolveCsrfTokenValue(this.exchange, csrfToken).block(); |
| 251 | + assertThat(tokenValue).isNull(); |
| 252 | + } |
| 253 | + |
| 254 | + // gh-13310, gh-15184 |
| 255 | + @Test |
| 256 | + public void resolveCsrfTokenValueWhenActualBytesIsEmptyThenReturnsNull() { |
| 257 | + this.exchange = MockServerWebExchange |
| 258 | + .builder(MockServerHttpRequest.post("/") |
| 259 | + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
| 260 | + .header(this.token.getHeaderName(), "")) |
| 261 | + .build(); |
| 262 | + String tokenValue = this.handler.resolveCsrfTokenValue(this.exchange, this.token).block(); |
| 263 | + assertThat(tokenValue).isNull(); |
| 264 | + } |
| 265 | + |
191 | 266 | private static Answer<Void> fillByteArray() {
|
192 | 267 | return (invocation) -> {
|
193 | 268 | byte[] bytes = invocation.getArgument(0);
|
|
0 commit comments