Skip to content

Commit

Permalink
Use time-constant comparison for CSRF tokens in endpoint (#10157)
Browse files Browse the repository at this point in the history
This hardens the framework against a theoretical timing attack based on
comparing how quickly a request with an invalid CSRF token is rejected.

No tests since this functionality is equivalent to the previous implementation aside from timing differences that would be very fragile to verify in an automated test.

Related to #9875
  • Loading branch information
haijian-vaadin authored and vaadin-bot committed Mar 2, 2021
1 parent be69092 commit 7e309f0
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -158,7 +160,10 @@ private boolean requestForbidden(HttpServletRequest request) {
return true;
}

if (!csrfTokenInSession.equals(request.getHeader("X-CSRF-Token"))) {
String csrfTokenInRequest = request.getHeader("X-CSRF-Token");
if (csrfTokenInRequest == null || !MessageDigest.isEqual(
csrfTokenInSession.getBytes(StandardCharsets.UTF_8),
csrfTokenInRequest.getBytes(StandardCharsets.UTF_8))) {
if (getLogger().isInfoEnabled()) {
getLogger().info("Invalid CSRF token in endpoint request");
}
Expand Down

0 comments on commit 7e309f0

Please sign in to comment.