From f053db583affa2a9c846d728c484dc917d2bd4df Mon Sep 17 00:00:00 2001 From: TiM Date: Mon, 13 Apr 2026 16:16:17 +1200 Subject: [PATCH] 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 --- .../elasticache/ElastiCacheQueryHandler.java | 2 +- .../proxy/ElastiCacheAuthProxy.java | 2 +- .../elasticache/proxy/SigV4Validator.java | 21 +++++-- .../rds/proxy/PostgresProtocolHandler.java | 2 +- .../services/rds/proxy/RdsSigV4Validator.java | 17 +++++- .../elasticache/proxy/SigV4ValidatorTest.java | 56 ++++++++++++++---- .../rds/proxy/RdsSigV4ValidatorTest.java | 58 +++++++++++++++---- 7 files changed, 128 insertions(+), 30 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java index 8481886b84..96fb2ecd37 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java @@ -222,7 +222,7 @@ public class ElastiCacheQueryHandler { return AwsQueryResponse.error("InvalidParameter", "Token parameter is required.", AwsNamespaces.EC, 400); } try { - boolean valid = sigV4Validator.validate(token, null); + boolean valid = sigV4Validator.validate(token, null, null); if (!valid) { return AwsQueryResponse.error("SignatureDoesNotMatch", "The request signature does not match.", AwsNamespaces.EC, 403); diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java index d086024d66..da8d4ab451 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java +++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java @@ -153,7 +153,7 @@ public class ElastiCacheAuthProxy { private boolean validate(String username, String password) { return switch (authMode) { - case IAM -> sigV4Validator.validate(password, groupId); + case IAM -> sigV4Validator.validate(password, groupId, username); case PASSWORD -> passwordValidator.validatePassword(username, password); case NO_AUTH -> true; }; diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4Validator.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4Validator.java index 4c43bc036e..517d63237a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4Validator.java +++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4Validator.java @@ -36,13 +36,17 @@ public class SigV4Validator { } /** - * Validates the given IAM auth token against the expected cluster ID. + * Validates the given IAM auth token against the expected cluster ID and username. * The token is a presigned URL without the scheme, e.g.: * {@code clusterId/?Action=connect&User=...&X-Amz-Signature=...} * - * @return true if the token is valid and not expired + * @param token the presigned URL token + * @param expectedGroupId the replication group ID to match against the token's host + * @param expectedUsername the Redis username from the AUTH command; + * must match the {@code User} in the token. May be null to skip. + * @return true if the token is valid, identities match, and the token is not expired */ - public boolean validate(String token, String expectedGroupId) { + public boolean validate(String token, String expectedGroupId, String expectedUsername) { try { URI uri = URI.create("http://" + token); String clusterId = uri.getHost(); @@ -60,6 +64,7 @@ public class SigV4Validator { String[] rawPairs = rawQuery.split("&"); String action = findRawParam(rawPairs, "Action"); + String user = findRawParam(rawPairs, "User"); String dateTime = findRawParam(rawPairs, "X-Amz-Date"); String expires = findRawParam(rawPairs, "X-Amz-Expires"); String credential = findRawParam(rawPairs, "X-Amz-Credential"); @@ -72,6 +77,12 @@ public class SigV4Validator { return false; } + if (expectedUsername != null && user != null && !expectedUsername.equals(user)) { + LOG.debugv("IAM token user mismatch: expected={0}, got={1}", + expectedUsername, user); + return false; + } + Instant tokenTime = Instant.from(DATETIME_FMT.parse(dateTime)); int expirySeconds = Integer.parseInt(expires); if (Instant.now().isAfter(tokenTime.plusSeconds(expirySeconds))) { @@ -111,7 +122,9 @@ public class SigV4Validator { byte[] signingKey = deriveSigningKey(secretKey, date, region, service); String expectedSignature = hexEncode(hmacSha256(signingKey, stringToSign)); - boolean valid = expectedSignature.equals(signature); + boolean valid = MessageDigest.isEqual( + expectedSignature.getBytes(StandardCharsets.UTF_8), + signature.getBytes(StandardCharsets.UTF_8)); if (!valid) { LOG.debugv("IAM token signature mismatch for accessKey={0}", accessKeyId); } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java index 325706caac..ccb6ab82b5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/PostgresProtocolHandler.java @@ -71,7 +71,7 @@ public class PostgresProtocolHandler { // Phase 4: Validate credentials boolean valid; if (iamEnabled && clientPassword.contains("X-Amz-Signature")) { - valid = sigV4.validate(clientPassword); + valid = sigV4.validate(clientPassword, clientUsername); } else { valid = passwordValidator.validate(clientUsername, clientPassword); } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4Validator.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4Validator.java index b08a794081..139be1ee11 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4Validator.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4Validator.java @@ -42,9 +42,12 @@ public class RdsSigV4Validator { * The token is a presigned URL without the scheme, e.g.: * {@code hostname:port/?Action=connect&DBUser=admin&X-Amz-Signature=...} * - * @return true if the token signature is valid and the token is not expired + * @param token the presigned URL token + * @param clientUsername the username from the PostgreSQL startup message; + * must match the {@code DBUser} in the token + * @return true if the token signature is valid, the DBUser matches, and the token is not expired */ - public boolean validate(String token) { + public boolean validate(String token, String clientUsername) { try { URI uri = URI.create("http://" + token); String host = uri.getHost(); @@ -74,6 +77,12 @@ public class RdsSigV4Validator { return false; } + if (clientUsername != null && !clientUsername.equals(dbUser)) { + LOG.debugv("RDS IAM token DBUser mismatch: client={0}, token={1}", + clientUsername, dbUser); + return false; + } + Instant tokenTime = Instant.from(DATETIME_FMT.parse(dateTime)); int expirySeconds = Integer.parseInt(expires); if (Instant.now().isAfter(tokenTime.plusSeconds(expirySeconds))) { @@ -115,7 +124,9 @@ public class RdsSigV4Validator { byte[] signingKey = deriveSigningKey(secretKey, date, region, service); String expectedSignature = hexEncode(hmacSha256(signingKey, stringToSign)); - boolean valid = expectedSignature.equals(signature); + boolean valid = MessageDigest.isEqual( + expectedSignature.getBytes(StandardCharsets.UTF_8), + signature.getBytes(StandardCharsets.UTF_8)); if (!valid) { LOG.debugv("RDS IAM token signature mismatch for accessKey={0}", accessKeyId); } diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4ValidatorTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4ValidatorTest.java index 7d5ee96269..692f52c2be 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4ValidatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/SigV4ValidatorTest.java @@ -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")); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java index 16c6da4aa8..9a1074e9bc 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/proxy/RdsSigV4ValidatorTest.java @@ -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")); } }