mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 11:56:53 +00:00
Use a JWT for external attachment auth and add a maxLen claim
This commit is contained in:
1 parent
5c3a82c257
commit
7c26965ece
19 files changed
+226
-76
No files matched your search
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
package org.whispersystems.textsecuregcm.auth;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.exceptions.IncorrectClaimException;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import java.time.Instant;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.textsecuregcm.util.TestClock;
|
||||
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
|
||||
|
||||
class JwtGeneratorTest {
|
||||
private static final byte[] SECRET = TestRandomUtil.nextBytes(32);
|
||||
private static final String AUD = "test-audience";
|
||||
private static final String SUB = "test-key";
|
||||
|
||||
@Test
|
||||
public void validToken() {
|
||||
final TestClock clock = TestClock.pinned(Instant.EPOCH);
|
||||
final JwtGenerator jwtGenerator = new JwtGenerator(SECRET, clock);
|
||||
final String token = jwtGenerator.generateJwt(AUD, SUB, _ -> {});
|
||||
|
||||
final DecodedJWT decoded = JWT.require(Algorithm.HMAC256(SECRET))
|
||||
.withAudience(AUD)
|
||||
.withSubject(SUB)
|
||||
.build().verify(token);
|
||||
assertThat(decoded.getIssuedAt()).isEqualTo(Instant.EPOCH);
|
||||
assertThat(decoded.getClaims()).containsOnlyKeys("aud", "sub", "iat");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validClaims() {
|
||||
final TestClock clock = TestClock.pinned(Instant.EPOCH);
|
||||
final JwtGenerator jwtGenerator = new JwtGenerator(SECRET, clock);
|
||||
final String token = jwtGenerator.generateJwt(AUD, SUB, builder -> builder
|
||||
.withClaim("number", 17)
|
||||
.withClaim("string", "abc"));
|
||||
|
||||
final DecodedJWT decoded = JWT.require(Algorithm.HMAC256(SECRET))
|
||||
.withAudience(AUD)
|
||||
.withSubject(SUB)
|
||||
.withClaim("number", 17)
|
||||
.withClaim("string", "abc")
|
||||
.build().verify(token);
|
||||
assertThat(decoded.getIssuedAt()).isEqualTo(Instant.EPOCH);
|
||||
assertThat(decoded.getClaims()).containsOnlyKeys("aud", "sub", "iat", "number", "string");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void badAudience() {
|
||||
final TestClock clock = TestClock.pinned(Instant.EPOCH);
|
||||
final JwtGenerator jwtGenerator = new JwtGenerator(SECRET, clock);
|
||||
final String token = jwtGenerator.generateJwt("bad-audience", SUB, _ -> {});
|
||||
|
||||
assertThatExceptionOfType(IncorrectClaimException.class).isThrownBy(() -> JWT.require(Algorithm.HMAC256(SECRET))
|
||||
.withAudience(AUD)
|
||||
.withSubject(SUB)
|
||||
.build().verify(token));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void badCustomClaim() {
|
||||
final TestClock clock = TestClock.pinned(Instant.EPOCH);
|
||||
final JwtGenerator jwtGenerator = new JwtGenerator(SECRET, clock);
|
||||
final String token = jwtGenerator.generateJwt(AUD, SUB, builder -> builder
|
||||
.withClaim("number", 18));
|
||||
|
||||
assertThatExceptionOfType(IncorrectClaimException.class).isThrownBy(() -> JWT.require(Algorithm.HMAC256(SECRET))
|
||||
.withAudience(AUD)
|
||||
.withSubject(SUB)
|
||||
.withClaim("number", 17)
|
||||
.build().verify(token));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -104,6 +104,7 @@ public class BackupManagerTest {
|
||||
3, "abc", 100,
|
||||
COPY_ENCRYPTION_PARAM, TestRandomUtil.nextBytes(15));
|
||||
private static final long MAX_TOTAL_MEDIA_BYTES = DataSize.mebibytes(1).toBytes();
|
||||
private static final long MAX_UPLOAD_SIZE = 1000;
|
||||
|
||||
private final TestClock testClock = TestClock.now();
|
||||
private final BackupAuthTestUtil backupAuthTestUtil = new BackupAuthTestUtil(testClock);
|
||||
@@ -151,6 +152,8 @@ public class BackupManagerTest {
|
||||
when(dynamicConfiguration.getBackupConfiguration()).thenReturn(backupConfiguration);
|
||||
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
|
||||
|
||||
when(tusAttachmentGenerator.maxUploadSizeInBytes()).thenReturn(MAX_UPLOAD_SIZE);
|
||||
|
||||
this.backupManager = new BackupManager(
|
||||
backupsDb,
|
||||
backupAuthTestUtil.params,
|
||||
|
||||
+34
-15
@@ -6,46 +6,65 @@
|
||||
package org.whispersystems.textsecuregcm.backup;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatNoException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Map;
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.JWTVerifier;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.textsecuregcm.attachments.TusConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.secrets.SecretBytes;
|
||||
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
|
||||
|
||||
public class Cdn3BackupCredentialGeneratorTest {
|
||||
private static final long MAX_UPLOAD_LENGTH = 100;
|
||||
private static final byte[] SECRET = TestRandomUtil.nextBytes(32);
|
||||
|
||||
@Test
|
||||
public void uploadGenerator() {
|
||||
Cdn3BackupCredentialGenerator generator = new Cdn3BackupCredentialGenerator(new TusConfiguration(
|
||||
new SecretBytes(TestRandomUtil.nextBytes(32)),
|
||||
"https://example.org/upload"));
|
||||
final Cdn3BackupCredentialGenerator generator = new Cdn3BackupCredentialGenerator(new TusConfiguration(
|
||||
new SecretBytes(SECRET),
|
||||
"https://example.org/upload",
|
||||
MAX_UPLOAD_LENGTH));
|
||||
|
||||
final BackupUploadDescriptor messageBackupUploadDescriptor = generator.generateUpload("subdir/key");
|
||||
assertThat(messageBackupUploadDescriptor.signedUploadLocation()).isEqualTo("https://example.org/upload/backups");
|
||||
assertThat(messageBackupUploadDescriptor.key()).isEqualTo("subdir/key");
|
||||
assertThat(messageBackupUploadDescriptor.headers()).containsKey("Authorization");
|
||||
final String username = parseUsername(messageBackupUploadDescriptor.headers().get("Authorization"));
|
||||
assertThat(username).isEqualTo("write$backups/subdir/key");
|
||||
final String token = parseBearerToken(messageBackupUploadDescriptor.headers().get("Authorization"));
|
||||
|
||||
final DecodedJWT decoded = JWT.require(Algorithm.HMAC256(SECRET))
|
||||
.withAudience(Cdn3BackupCredentialGenerator.CDN_PATH)
|
||||
.withSubject("subdir/key")
|
||||
.withClaim("scope", "write")
|
||||
.withClaim("maxLen", MAX_UPLOAD_LENGTH)
|
||||
.build().verify(token);
|
||||
assertThat(decoded.getClaims()).containsOnlyKeys("aud", "sub", "scope", "iat", "maxLen");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readCredential() {
|
||||
Cdn3BackupCredentialGenerator generator = new Cdn3BackupCredentialGenerator(new TusConfiguration(
|
||||
new SecretBytes(TestRandomUtil.nextBytes(32)),
|
||||
"https://example.org/upload"));
|
||||
final Cdn3BackupCredentialGenerator generator = new Cdn3BackupCredentialGenerator(new TusConfiguration(
|
||||
new SecretBytes(SECRET),
|
||||
"https://example.org/upload",
|
||||
MAX_UPLOAD_LENGTH));
|
||||
|
||||
final Map<String, String> headers = generator.readHeaders("subdir");
|
||||
assertThat(headers).containsKey("Authorization");
|
||||
final String username = parseUsername(headers.get("Authorization"));
|
||||
assertThat(username).isEqualTo("read$backups/subdir");
|
||||
final DecodedJWT decoded = JWT.require(Algorithm.HMAC256(SECRET))
|
||||
.withAudience(Cdn3BackupCredentialGenerator.CDN_PATH)
|
||||
.withSubject("subdir")
|
||||
.withClaim("scope", "read")
|
||||
.build().verify(parseBearerToken(headers.get("Authorization")));
|
||||
assertThat(decoded.getClaims()).containsOnlyKeys("aud", "sub", "scope", "iat");
|
||||
}
|
||||
|
||||
private static String parseUsername(final String authHeader) {
|
||||
assertThat(authHeader).startsWith("Basic");
|
||||
final String encoded = authHeader.substring("Basic".length() + 1);
|
||||
final String cred = new String(Base64.getDecoder().decode(encoded), StandardCharsets.UTF_8);
|
||||
return cred.split(":")[0];
|
||||
private static String parseBearerToken(final String authHeader) {
|
||||
assertThat(authHeader).startsWith("Bearer");
|
||||
return authHeader.substring("Bearer".length() + 1);
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -90,6 +90,7 @@ import reactor.core.publisher.Flux;
|
||||
@ExtendWith(DropwizardExtensionsSupport.class)
|
||||
public class ArchiveControllerTest {
|
||||
|
||||
private static final long MAX_MESSAGE_BACKUP_OBJECT_SIZE = 1000L;
|
||||
private static final AccountsManager accountsManager = mock(AccountsManager.class);
|
||||
private static final BackupAuthManager backupAuthManager = mock(BackupAuthManager.class);
|
||||
private static final BackupManager backupManager = mock(BackupManager.class);
|
||||
@@ -117,6 +118,8 @@ public class ArchiveControllerTest {
|
||||
reset(backupAuthManager);
|
||||
reset(backupManager);
|
||||
|
||||
when(backupManager.maxMessageBackupUploadSize()).thenReturn(MAX_MESSAGE_BACKUP_OBJECT_SIZE);
|
||||
|
||||
when(accountsManager.getByAccountIdentifier(AuthHelper.VALID_UUID))
|
||||
.thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT));
|
||||
}
|
||||
@@ -612,8 +615,8 @@ public class ArchiveControllerTest {
|
||||
static Stream<Arguments> messagesUploadForm() {
|
||||
return Stream.of(
|
||||
Arguments.of(Optional.empty(), true),
|
||||
Arguments.of(Optional.of(BackupManager.MAX_MESSAGE_BACKUP_OBJECT_SIZE), true),
|
||||
Arguments.of(Optional.of(BackupManager.MAX_MESSAGE_BACKUP_OBJECT_SIZE + 1), false)
|
||||
Arguments.of(Optional.of(MAX_MESSAGE_BACKUP_OBJECT_SIZE), true),
|
||||
Arguments.of(Optional.of(MAX_MESSAGE_BACKUP_OBJECT_SIZE + 1), false)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -96,7 +96,7 @@ class AttachmentControllerV4Test {
|
||||
.setTestContainerFactory(new GrizzlyWebTestContainerFactory())
|
||||
.addProvider(new AttachmentControllerV4(RATE_LIMITERS,
|
||||
gcsAttachmentGenerator,
|
||||
new TusAttachmentGenerator(new TusConfiguration(new SecretBytes(TUS_SECRET), TUS_URL)),
|
||||
new TusAttachmentGenerator(new TusConfiguration(new SecretBytes(TUS_SECRET), TUS_URL, 1000)),
|
||||
EXPERIMENT_MANAGER))
|
||||
.build();
|
||||
} catch (IOException | InvalidKeyException | InvalidKeySpecException e) {
|
||||
|
||||
+1
-1
@@ -72,7 +72,7 @@ class AttachmentsGrpcServiceTest extends
|
||||
final GcsAttachmentGenerator gcsAttachmentGenerator = new GcsAttachmentGenerator(
|
||||
"some-cdn.signal.org", "signal@example.com", 1000, "/attach-here", gcsPrivateKeyPem);
|
||||
final TusAttachmentGenerator tusAttachmentGenerator =
|
||||
new TusAttachmentGenerator(new TusConfiguration(new SecretBytes(TUS_SECRET), TUS_URL));
|
||||
new TusAttachmentGenerator(new TusConfiguration(new SecretBytes(TUS_SECRET), TUS_URL, 1000));
|
||||
|
||||
return new AttachmentsGrpcService(
|
||||
experimentEnrollmentManager,
|
||||
|
||||
+5
-2
@@ -77,6 +77,8 @@ import reactor.core.publisher.Flux;
|
||||
class BackupsAnonymousGrpcServiceTest extends
|
||||
SimpleBaseGrpcTest<BackupsAnonymousGrpcService, BackupsAnonymousGrpc.BackupsAnonymousBlockingStub> {
|
||||
|
||||
private static final long MAX_MESSAGE_BACKUP_OBJECT_SIZE = 100;
|
||||
|
||||
private final UUID aci = UUID.randomUUID();
|
||||
private final byte[] messagesBackupKey = TestRandomUtil.nextBytes(32);
|
||||
private final BackupAuthTestUtil backupAuthTestUtil = new BackupAuthTestUtil(Clock.systemUTC());
|
||||
@@ -96,6 +98,7 @@ class BackupsAnonymousGrpcServiceTest extends
|
||||
try {
|
||||
when(backupManager.authenticateBackupUser(any(), any(), any()))
|
||||
.thenReturn(backupUser(presentation.getBackupId(), BackupCredentialType.MESSAGES, BackupLevel.PAID));
|
||||
when(backupManager.maxMessageBackupUploadSize()).thenReturn(MAX_MESSAGE_BACKUP_OBJECT_SIZE);
|
||||
} catch (BackupFailedZkAuthenticationException e) {
|
||||
Assertions.fail(e);
|
||||
}
|
||||
@@ -344,8 +347,8 @@ class BackupsAnonymousGrpcServiceTest extends
|
||||
static Stream<Arguments> messagesUploadForm() {
|
||||
return Stream.of(
|
||||
Arguments.of(Optional.empty(), true),
|
||||
Arguments.of(Optional.of(BackupManager.MAX_MESSAGE_BACKUP_OBJECT_SIZE), true),
|
||||
Arguments.of(Optional.of(BackupManager.MAX_MESSAGE_BACKUP_OBJECT_SIZE + 1), false)
|
||||
Arguments.of(Optional.of(MAX_MESSAGE_BACKUP_OBJECT_SIZE), true),
|
||||
Arguments.of(Optional.of(MAX_MESSAGE_BACKUP_OBJECT_SIZE + 1), false)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user