Skip to content

Commit

Permalink
fix: use time-constant comparison for CSRF tokens (#12188) (#12196)
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.

Cherry-picked from: vaadin/flow#9875

Authored-by: Tatu Lund <tatu@vaadin.com>
  • Loading branch information
Ansku authored Feb 3, 2021
1 parent 560cccc commit 232961b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion server/src/main/java/com/vaadin/server/VaadinService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -1962,7 +1964,9 @@ public static boolean isCsrfTokenValid(VaadinSession session,
.isXsrfProtectionEnabled()) {
String sessionToken = session.getCsrfToken();

if (sessionToken == null || !sessionToken.equals(requestToken)) {
if (sessionToken == null || !MessageDigest.isEqual(
sessionToken.getBytes(StandardCharsets.UTF_8),
requestToken.getBytes(StandardCharsets.UTF_8))) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void verifyUserAgent() {
// Chrome version does not necessarily match the desired version
// because of auto updates...
browserIdentifier = getExpectedUserAgentString(
getDesiredCapabilities()) + "87";
getDesiredCapabilities()) + "88";
} else if (BrowserUtil.isFirefox(getDesiredCapabilities())) {
browserIdentifier = getExpectedUserAgentString(
getDesiredCapabilities()) + "81";
Expand Down

0 comments on commit 232961b

Please sign in to comment.