fix(security): reject unregistered SigV4 access keys and unbound usernames (#2680)

* fix(security): reject unregistered SigV4 access keys and unbound usernames

SigV4Validator.java and RdsSigV4Validator.java both fell back to
`iamService.findSecretKey(accessKeyId).orElse(accessKeyId)` when the
access key wasn't registered. Since the attacker chooses accessKeyId
themselves, they can self-sign a token using secret == accessKeyId,
producing the exact signing key this fallback derives -- a full
signature-forgery auth bypass for any unregistered IAM key. Both now
fail closed when the key isn't registered.

SigV4Validator's ElastiCache username binding
(`expectedUsername != null && user != null && !expectedUsername.equals(user)`)
also skipped the check whenever the token simply omitted the User
query param, letting any expectedUsername through. Dropped the
`user != null` conjunct so a null user now correctly fails the check.

Each fix has a RED test proving the exploit against the unpatched
code (self-signed token with secret == accessKeyId; token missing
User) before asserting the fixed behavior.

RdsSigV4Validator's DBUser check was investigated but is not
vulnerable: DBUser is already required by the missing-parameters
check above it, so the null-skip path is unreachable via a crafted
token. PreSignedUrlFilter's well-known "test"/"test" credential and
its lack of a bucket-ownership check were also investigated and are
not presigned-URL-specific bugs -- see
issues/sigv4-review-remaining-findings.md for the evidence and for
everything else from the review pass deferred out of this PR.

* fix(security): restore well-known local-dev credential, sanitize log lines

The fail-closed fix in the prior commit broke the emulator's default
local-dev credential flow: AwsBasicCredentials.create("test", "test")
is used pervasively by SDK clients against RDS/ElastiCache IAM auth
(and S3) in this project's compat test suite, and was never actually
registered in IamService -- it only "worked" before because the
vulnerable fallback happened to derive the same secret ("test") from
the access key ID by coincidence (accessKeyId == secretKey == "test").

Carve out that exact, already-public pair explicitly in both
validators, mirroring the identical LEGACY_ACCESS_KEY_ID/SECRET_KEY
idiom already used by S3Service/PreSignedUrlFilter -- this does not
reopen the forgery bug, since it only ever matches the literal string
"test", never an attacker-chosen access key.

Also addresses two Copilot review comments: the debug logs added in
the fail-closed fix (and the pre-existing signature-mismatch log)
interpolated the attacker-controlled, URL-decoded accessKeyId with no
sanitization, allowing CR/LF-based log injection. Added a
sanitizeForLog helper (strips control characters) and applied it to
every log line that includes accessKeyId in both files.

* docs(security): update ledger after rebasing past PR #2657's merge

PR #2657 merged with the misleading "always fails the signature check"
comment in both SigV4Validator.java and RdsSigV4Validator.java (not
just the ElastiCache file, as first flagged). Rebasing this branch
onto upstream/main produced real conflicts on both files; resolved by
keeping the fail-closed fix and dropping the comment. Updated the
ledger from future-tense speculation to the resolved outcome.
This commit is contained in:
Sam Fakhreddine authored and GitHub committed 2026-08-28 19:59:15 -04:00
1 parent acf88794ca
commit f9d28c9649
6 files changed
+423 -15

No files matched your search

@@ -15,6 +15,7 @@ import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
/**
@@ -28,6 +29,15 @@ public class SigV4Validator {
private static final DateTimeFormatter DATETIME_FMT =
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
/**
* Well-known local-dev credential pair used pervasively by default AWS SDK clients
* (AwsBasicCredentials.create("test", "test")) against this emulator, mirrored from the
* identical fallback in S3Service/PreSignedUrlFilter. Deliberately not a fallback for any
* other unregistered access key -- only this exact, already-public pair is honored.
*/
private static final String LEGACY_ACCESS_KEY_ID = "test";
private static final String LEGACY_SECRET_KEY = "test";
private final IamService iamService;
@Inject
@@ -77,7 +87,7 @@ public class SigV4Validator {
return false;
}
if (expectedUsername != null && user != null && !expectedUsername.equals(user)) {
if (expectedUsername != null && !expectedUsername.equals(user)) {
LOG.debugv("IAM token user mismatch: expected={0}, got={1}",
expectedUsername, user);
return false;
@@ -101,12 +111,17 @@ public class SigV4Validator {
String service = credParts[3];
String credentialScope = date + "/" + region + "/" + service + "/aws4_request";
// Deliberately no fallback for a bare 12-digit account ID here: trusting an
// unregistered numeric access key paired with the well-known "test" secret would
// let any client forge an IAM-auth token for an arbitrary account and authenticate
// as any matching cache user. An unregistered key falls back to itself, unchanged
// (never a valid secret), so it always fails the signature check below.
String secretKey = iamService.findSecretKey(accessKeyId).orElse(accessKeyId);
String secretKey;
if (LEGACY_ACCESS_KEY_ID.equals(accessKeyId)) {
secretKey = LEGACY_SECRET_KEY;
} else {
Optional<String> registeredSecretKey = iamService.findSecretKey(accessKeyId);
if (registeredSecretKey.isEmpty()) {
LOG.debugv("IAM token references unregistered access key={0}", sanitizeForLog(accessKeyId));
return false;
}
secretKey = registeredSecretKey.get();
}
String canonicalQueryString = Arrays.stream(rawPairs)
.filter(p -> !rawParamName(p).equals("X-Amz-Signature"))
@@ -131,7 +146,7 @@ public class SigV4Validator {
expectedSignature.getBytes(StandardCharsets.UTF_8),
signature.getBytes(StandardCharsets.UTF_8));
if (!valid) {
LOG.debugv("IAM token signature mismatch for accessKey={0}", accessKeyId);
LOG.debugv("IAM token signature mismatch for accessKey={0}", sanitizeForLog(accessKeyId));
}
return valid;
@@ -160,6 +175,14 @@ public class SigV4Validator {
return URLDecoder.decode(value, StandardCharsets.UTF_8);
}
/**
* Strips control characters (CR, LF, etc.) from an attacker-controlled value before it is
* interpolated into a log line, preventing log injection / forged multi-line log entries.
*/
private static String sanitizeForLog(String value) {
return value == null ? null : value.replaceAll("\\p{Cntrl}", "");
}
private static byte[] deriveSigningKey(String secretKey, String date, String region,
String service) throws Exception {
byte[] kSecret = ("AWS4" + secretKey).getBytes(StandardCharsets.UTF_8);
@@ -15,6 +15,7 @@ import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
/**
@@ -30,6 +31,15 @@ public class RdsSigV4Validator {
private static final DateTimeFormatter DATETIME_FMT =
DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'").withZone(ZoneOffset.UTC);
/**
* Well-known local-dev credential pair used pervasively by default AWS SDK clients
* (AwsBasicCredentials.create("test", "test")) against this emulator, mirrored from the
* identical fallback in S3Service/PreSignedUrlFilter. Deliberately not a fallback for any
* other unregistered access key -- only this exact, already-public pair is honored.
*/
private static final String LEGACY_ACCESS_KEY_ID = "test";
private static final String LEGACY_SECRET_KEY = "test";
private final IamService iamService;
@Inject
@@ -101,12 +111,17 @@ public class RdsSigV4Validator {
String service = credParts[3];
String credentialScope = date + "/" + region + "/" + service + "/aws4_request";
// Deliberately no fallback for a bare 12-digit account ID here: trusting an
// unregistered numeric access key paired with the well-known "test" secret would
// let any client forge an IAM-auth token for an arbitrary account and authenticate
// as any matching database user. An unregistered key falls back to itself, unchanged
// (never a valid secret), so it always fails the signature check below.
String secretKey = iamService.findSecretKey(accessKeyId).orElse(accessKeyId);
String secretKey;
if (LEGACY_ACCESS_KEY_ID.equals(accessKeyId)) {
secretKey = LEGACY_SECRET_KEY;
} else {
Optional<String> registeredSecretKey = iamService.findSecretKey(accessKeyId);
if (registeredSecretKey.isEmpty()) {
LOG.debugv("RDS IAM token references unregistered access key={0}", sanitizeForLog(accessKeyId));
return false;
}
secretKey = registeredSecretKey.get();
}
// Canonical query string: sorted pairs, excluding X-Amz-Signature
String canonicalQueryString = Arrays.stream(rawPairs)
@@ -133,7 +148,7 @@ public class RdsSigV4Validator {
expectedSignature.getBytes(StandardCharsets.UTF_8),
signature.getBytes(StandardCharsets.UTF_8));
if (!valid) {
LOG.debugv("RDS IAM token signature mismatch for accessKey={0}", accessKeyId);
LOG.debugv("RDS IAM token signature mismatch for accessKey={0}", sanitizeForLog(accessKeyId));
}
return valid;
@@ -162,6 +177,14 @@ public class RdsSigV4Validator {
return URLDecoder.decode(value, StandardCharsets.UTF_8);
}
/**
* Strips control characters (CR, LF, etc.) from an attacker-controlled value before it is
* interpolated into a log line, preventing log injection / forged multi-line log entries.
*/
private static String sanitizeForLog(String value) {
return value == null ? null : value.replaceAll("\\p{Cntrl}", "");
}
private static byte[] deriveSigningKey(String secretKey, String date, String region,
String service) throws Exception {
byte[] kSecret = ("AWS4" + secretKey).getBytes(StandardCharsets.UTF_8);