Skip to content

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
robotdan committed Jan 23, 2025
1 parent a01f5ec commit 5fb3ef0
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions src/test/java/org/primeframework/mvc/CSRFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,6 @@ public void post_CSRFRefererFailure() {
.assertStatusCode(403); // Unauthorized
}

@Test
public void post_CSRFTokenCompatibility() throws Exception {
// Use case: a CSRF token encrypted with CBC can be decrypted using the updated method
MockUserLoginSecurityContext.roles.add("admin");
securityContext.login(new User());
configuration.csrfEnabled = true;

// Craft a CSRF token, serialize to JSON, encrypt with CBC, base64url-encode
CSRFToken token = new CSRFToken(securityContext.getSessionId(), System.currentTimeMillis());
byte[] serialized = objectMapper.writeValueAsBytes(token);
// Instantiate DefaultEncryptor with two copies of CBCCipherProvider to encrypt with CBC
Encryptor cbcEncryptor = new DefaultEncryptor(new CBCCipherProvider(configuration), new CBCCipherProvider(configuration));
byte[] encrypted = cbcEncryptor.encrypt(serialized);
String encoded = Base64.getUrlEncoder().encodeToString(encrypted);

simulator.test("/secure")
.withSingleHeader("Referer", "http://localhost:" + simulator.getPort() + "/secure")
.withCSRFToken(encoded)
.post()
.assertStatusCode(200)
.assertBody("Secure!");

// A POST request will contain the CSRF token
// - This is just testing the RequestBuilder that will try and automatically add the CSRF token
assertTrue(SecureAction.UnknownParameters.containsKey(csrfProvider.getParameterName()));
}

@Test
public void post_CSRFTokenFailure() {
MockUserLoginSecurityContext.roles.add("admin");
Expand Down Expand Up @@ -147,6 +120,33 @@ public void post_CSRFTokenSuccess() {
.assertBody("Secure!");
}

@Test
public void post_CSRFTokenCompatibility() throws Exception {
// Use case: a CSRF token encrypted with CBC can be decrypted using the updated method
MockUserLoginSecurityContext.roles.add("admin");
securityContext.login(new User());
configuration.csrfEnabled = true;

// Craft a CSRF token, serialize to JSON, encrypt with CBC, base64url-encode
CSRFToken token = new CSRFToken(securityContext.getSessionId(), System.currentTimeMillis());
byte[] serialized = objectMapper.writeValueAsBytes(token);
// Instantiate DefaultEncryptor with two copies of CBCCipherProvider to encrypt with CBC
Encryptor cbcEncryptor = new DefaultEncryptor(new CBCCipherProvider(configuration), new CBCCipherProvider(configuration));
byte[] encrypted = cbcEncryptor.encrypt(serialized);
String encoded = Base64.getUrlEncoder().encodeToString(encrypted);

simulator.test("/secure")
.withSingleHeader("Referer", "http://localhost:" + simulator.getPort() + "/secure")
.withCSRFToken(encoded)
.post()
.assertStatusCode(200)
.assertBody("Secure!");

// A POST request will contain the CSRF token
// - This is just testing the RequestBuilder that will try and automatically add the CSRF token
assertTrue(SecureAction.UnknownParameters.containsKey(csrfProvider.getParameterName()));
}

// Add for testing legacy-encrypted CSRF token which is defined as a private class in DefaultEncryptionBasedTokenCSRFProvider
private record CSRFToken(String sid, long instant) {
}
Expand Down

0 comments on commit 5fb3ef0

Please sign in to comment.