mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(auth): bind SigV4 token identity and use timing-safe comparison (#389)
RDS and ElastiCache SigV4 validators accepted any valid token regardless of which user it was signed for. A token signed for user A could authenticate as user B if the signature was cryptographically valid. - RdsSigV4Validator.validate() now takes clientUsername and rejects tokens where DBUser doesn't match the connecting PostgreSQL user - ElastiCache SigV4Validator.validate() now takes expectedUsername and rejects tokens where User doesn't match the AUTH username - PostgresProtocolHandler passes clientUsername to the validator - ElastiCacheAuthProxy passes the AUTH username to the validator - Both validators use MessageDigest.isEqual() for signature comparison instead of String.equals() to prevent timing side-channel attacks - 4 new test cases: wrong-user rejection + null-username backwards compat for both RDS and ElastiCache validators
This commit is contained in:
7 files changed
+128
-30
No files matched your search
+46
-10
@@ -26,8 +26,8 @@ class SigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertTrue(validator.validate(token, "cache-cluster-01"));
|
||||
assertTrue(validator.validate(token, "CACHE-CLUSTER-01"));
|
||||
assertTrue(validator.validate(token, "cache-cluster-01", "default"));
|
||||
assertTrue(validator.validate(token, "CACHE-CLUSTER-01", "default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -44,7 +44,7 @@ class SigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertFalse(validator.validate(token, "other-cluster"));
|
||||
assertFalse(validator.validate(token, "other-cluster", "default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -62,7 +62,7 @@ class SigV4ValidatorTest {
|
||||
);
|
||||
String tamperedToken = validToken.replace("User=default", "User=other");
|
||||
|
||||
assertFalse(validator.validate(tamperedToken, "cache-cluster-01"));
|
||||
assertFalse(validator.validate(tamperedToken, "cache-cluster-01", "default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,7 +79,7 @@ class SigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertTrue(validator.validate(token, null));
|
||||
assertTrue(validator.validate(token, null, "default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -96,7 +96,7 @@ class SigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertFalse(validator.validate(token, "cache-cluster-01"));
|
||||
assertFalse(validator.validate(token, "cache-cluster-01", "default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -113,7 +113,43 @@ class SigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertFalse(validator.validate(token, "cache-cluster-01"));
|
||||
assertFalse(validator.validate(token, "cache-cluster-01", "default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateRejectsTokenForWrongUser() throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDCACHE", "secret-cache");
|
||||
|
||||
SigV4Validator validator = new SigV4Validator(iamService);
|
||||
String token = SigV4TokenTestHelper.createElastiCacheToken(
|
||||
"cache-cluster-01",
|
||||
"default",
|
||||
"AKIDCACHE",
|
||||
"secret-cache",
|
||||
Instant.now().minusSeconds(60),
|
||||
900
|
||||
);
|
||||
|
||||
assertFalse(validator.validate(token, "cache-cluster-01", "attacker"),
|
||||
"Token signed for 'default' must be rejected when client authenticates as 'attacker'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateAcceptsTokenWhenExpectedUsernameIsNull() throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDCACHE", "secret-cache");
|
||||
|
||||
SigV4Validator validator = new SigV4Validator(iamService);
|
||||
String token = SigV4TokenTestHelper.createElastiCacheToken(
|
||||
"cache-cluster-01",
|
||||
"default",
|
||||
"AKIDCACHE",
|
||||
"secret-cache",
|
||||
Instant.now().minusSeconds(60),
|
||||
900
|
||||
);
|
||||
|
||||
assertTrue(validator.validate(token, "cache-cluster-01", null),
|
||||
"Null expectedUsername should skip the user identity check");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -132,7 +168,7 @@ class SigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertTrue(validator.validate(token, "cache-cluster-01"));
|
||||
assertTrue(validator.validate(token, "cache-cluster-01", "user+name@domain.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -150,7 +186,7 @@ class SigV4ValidatorTest {
|
||||
);
|
||||
String withoutAction = validToken.replaceFirst("Action=connect&", "");
|
||||
|
||||
assertFalse(validator.validate(withoutAction, "cache-cluster-01"));
|
||||
assertFalse(validator.validate(withoutAction, "cache-cluster-01", "default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -168,6 +204,6 @@ class SigV4ValidatorTest {
|
||||
);
|
||||
String withoutSignature = validToken.replaceFirst("&X-Amz-Signature=[0-9a-f]+", "");
|
||||
|
||||
assertFalse(validator.validate(withoutSignature, "cache-cluster-01"));
|
||||
assertFalse(validator.validate(withoutSignature, "cache-cluster-01", "default"));
|
||||
}
|
||||
}
|
||||
+48
-10
@@ -43,7 +43,7 @@ class RdsSigV4ValidatorTest {
|
||||
.username("testuser")
|
||||
.build());
|
||||
|
||||
assertTrue(validator.validate(sdkToken),
|
||||
assertTrue(validator.validate(sdkToken, "testuser"),
|
||||
"Validator must accept tokens generated by the official AWS SDK RDS presigner");
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ class RdsSigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertTrue(validator.validate(token));
|
||||
assertTrue(validator.validate(token, "admin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -81,7 +81,7 @@ class RdsSigV4ValidatorTest {
|
||||
);
|
||||
String brokenToken = validToken.replace("db.example.local:5432/?", "db.example.local/?");
|
||||
|
||||
assertFalse(validator.validate(brokenToken));
|
||||
assertFalse(validator.validate(brokenToken, "admin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -99,7 +99,7 @@ class RdsSigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertFalse(validator.validate(token));
|
||||
assertFalse(validator.validate(token, "admin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,7 +118,7 @@ class RdsSigV4ValidatorTest {
|
||||
);
|
||||
String tamperedToken = validToken.replace("DBUser=admin", "DBUser=attacker");
|
||||
|
||||
assertFalse(validator.validate(tamperedToken));
|
||||
assertFalse(validator.validate(tamperedToken, "admin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -136,7 +136,7 @@ class RdsSigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertFalse(validator.validate(token));
|
||||
assertFalse(validator.validate(token, "admin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -155,7 +155,45 @@ class RdsSigV4ValidatorTest {
|
||||
);
|
||||
String withoutDbUser = validToken.replaceFirst("DBUser=admin&", "");
|
||||
|
||||
assertFalse(validator.validate(withoutDbUser));
|
||||
assertFalse(validator.validate(withoutDbUser, "admin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateRejectsTokenForWrongUser() throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDRDS", "secret-rds");
|
||||
|
||||
RdsSigV4Validator validator = new RdsSigV4Validator(iamService);
|
||||
String token = SigV4TokenTestHelper.createRdsToken(
|
||||
"db.example.local",
|
||||
5432,
|
||||
"admin",
|
||||
"AKIDRDS",
|
||||
"secret-rds",
|
||||
Instant.now().minusSeconds(60),
|
||||
900
|
||||
);
|
||||
|
||||
assertFalse(validator.validate(token, "attacker"),
|
||||
"Token signed for 'admin' must be rejected when client connects as 'attacker'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateAcceptsTokenWhenClientUsernameIsNull() throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKey("AKIDRDS", "secret-rds");
|
||||
|
||||
RdsSigV4Validator validator = new RdsSigV4Validator(iamService);
|
||||
String token = SigV4TokenTestHelper.createRdsToken(
|
||||
"db.example.local",
|
||||
5432,
|
||||
"admin",
|
||||
"AKIDRDS",
|
||||
"secret-rds",
|
||||
Instant.now().minusSeconds(60),
|
||||
900
|
||||
);
|
||||
|
||||
assertTrue(validator.validate(token, null),
|
||||
"Null clientUsername should skip the identity check (backwards compat)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -175,7 +213,7 @@ class RdsSigV4ValidatorTest {
|
||||
900
|
||||
);
|
||||
|
||||
assertTrue(validator.validate(token));
|
||||
assertTrue(validator.validate(token, "db+admin@example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -195,7 +233,7 @@ class RdsSigV4ValidatorTest {
|
||||
// Tampering with the region in the credential scope invalidates the signature
|
||||
String tamperedToken = token.replace("us-east-1", "eu-west-1");
|
||||
|
||||
assertFalse(validator.validate(tamperedToken));
|
||||
assertFalse(validator.validate(tamperedToken, "admin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -214,6 +252,6 @@ class RdsSigV4ValidatorTest {
|
||||
);
|
||||
String withoutSignature = validToken.replaceFirst("&X-Amz-Signature=[0-9a-f]+", "");
|
||||
|
||||
assertFalse(validator.validate(withoutSignature));
|
||||
assertFalse(validator.validate(withoutSignature, "admin"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user