mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 00:56:50 +00:00
Require canonical base64 URL encoding for device-linking tokens
This commit is contained in:
1 parent
e7916302ba
commit
e6aef34398
2 files changed
+30
-1
No files matched your search
+11
-1
@@ -844,15 +844,20 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final byte[] expectedSignature = getInitializedMac().doFinal(claimsAndSignature[0].getBytes(StandardCharsets.UTF_8));
|
||||
final byte[] providedSignature;
|
||||
|
||||
try {
|
||||
providedSignature = Base64.getUrlDecoder().decode(claimsAndSignature[1]);
|
||||
|
||||
if (!isCanonicallyBase64UrlEncoded(claimsAndSignature[1], providedSignature)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
} catch (final IllegalArgumentException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
final byte[] expectedSignature = getInitializedMac().doFinal(claimsAndSignature[0].getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
if (!MessageDigest.isEqual(expectedSignature, providedSignature)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@@ -888,6 +893,11 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
return Optional.of(aci);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static boolean isCanonicallyBase64UrlEncoded(final String encoded, final byte[] bytes) {
|
||||
return Base64.getUrlEncoder().encodeToString(bytes).equals(encoded);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlink a device from the given account. The device will be immediately disconnected if it is connected to any chat
|
||||
* frontend.
|
||||
|
||||
+19
@@ -1822,6 +1822,25 @@ class AccountsManagerTest {
|
||||
accountsManager.checkDeviceLinkingToken(accountsManager.generateLinkDeviceToken(account)));
|
||||
}
|
||||
|
||||
static List<Arguments> isCanonicallyBase64UrlEncoded() {
|
||||
final byte[] bytes = new byte[32];
|
||||
final String canonical = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
|
||||
|
||||
return List.of(
|
||||
Arguments.argumentSet("Canonical", canonical, bytes, true),
|
||||
Arguments.argumentSet("Missing padding", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", bytes, false),
|
||||
Arguments.argumentSet("Extra bits", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB=", bytes, false),
|
||||
Arguments.argumentSet("Extra bits, missing padding", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD", bytes, false)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isCanonicallyBase64UrlEncoded(final String encoded, final byte[] bytes, final boolean expectCanonical) {
|
||||
assertArrayEquals(bytes, Base64.getUrlDecoder().decode(encoded));
|
||||
assertEquals(expectCanonical, AccountsManager.isCanonicallyBase64UrlEncoded(encoded, bytes));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void checkVerificationTokenBadToken(final String token, final Instant currentTime) {
|
||||
|
||||
Reference in new issue
Block a user