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:
TiM authored and GitHub committed 2026-04-12 23:16:17 -05:00
1 parent 7d3bab5327
commit f053db583a
7 files changed
+128 -30

No files matched your search

@@ -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);
@@ -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;
};
@@ -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);
}
@@ -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);
}
@@ -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);
}