fix(s3): answer an expired session with ExpiredToken when verifying signatures (#5435)

When S3 verified a signature, it answered a request signed with temporary
credentials past their expiry as if the key did not exist: 403
InvalidAccessKeyId for a header-signed request, and 403
SignatureDoesNotMatch for a presigned POST and, under
validate-signatures alone, for a presigned URL. S3's error list has 400
ExpiredToken, "The provided token has expired.", for an expired token,
and IAM enforcement already answers S3 with it.

The secret-key lookup finds nothing for an expired session, so when it
comes back empty the three placements (header, presigned URL, presigned
POST) now ask IamService whether the key and token belong to a session
that has expired on its clock and is still stored, and answer 400
ExpiredToken if so. The token has to match. Sent with another token or
none, an expired session is still answered as a key that is not
registered, so a caller that does not hold the credential learns nothing
about it. Once Floci has removed an expired session it no longer knows
the key, as with enforcement.

IamServiceTest moves a session past its expiry and checks that the new
lookup answers only to the session's own token, and the boundary test
now holds it to the same instant as the other lookups.
S3ValidateSignaturesIntegrationTest signs with Sign-In credentials
through a header, a presigned URL and a presigned POST: each is accepted
until the clock passes their expiry, then answered 400 ExpiredToken. An
expired session sent with another token still gets InvalidAccessKeyId.
Each placement's test fails without its part of the change, and the
token tests fail when the token is not checked. S3RequestSigner can now
write a presigned URL's query string, so the presigned URL test signs
the way an SDK presigner does.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-11 09:12:52 -05:00
1 parent 7fa1a506c7
commit 013a9214b5
9 files changed
+220 -19

No files matched your search

@@ -4002,6 +4002,19 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
.map(SessionCredential::getSecretAccessKey));
}
/**
* Whether this temporary access key and session token are those of a session that has expired
* on the injected clock but is still stored, which {@link #findSecretKey(String, String)} finds
* no secret for. AWS answers such a credential as expired, not as a key it does not know. The
* token has to match, so a caller that does not hold the credential learns nothing about it.
*/
public boolean isExpiredSessionToken(String accessKeyId, String sessionToken) {
return findSessionAnyAccount(accessKeyId)
.filter(session -> isExpired(session, clock.instant()))
.filter(session -> hasMatchingSessionToken(session, sessionToken))
.isPresent();
}
/**
* The session token issued alongside a temporary credential, for callers that must tell a
* missing token apart from one that does not match.
@@ -186,7 +186,13 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
}
} else {
String accessKeyId = credParts[0];
String secretKey = resolveSecretKey(accessKeyId, queryParams.getFirst("X-Amz-Security-Token"));
String sessionToken = queryParams.getFirst("X-Amz-Security-Token");
String secretKey = resolveSecretKey(accessKeyId, sessionToken);
if (secretKey == null && S3PostPolicySigner.isExpiredSession(iamService, accessKeyId, sessionToken)) {
requestContext.abortWith(errorResponse(400, "ExpiredToken",
S3PostPolicySigner.EXPIRED_TOKEN_MESSAGE));
return;
}
if (secretKey != null) {
if (!verifySigV4Signature(requestContext, signature, secretKey)) {
requestContext.abortWith(errorResponse(403, "SignatureDoesNotMatch",
@@ -3665,8 +3665,11 @@ public class S3Controller {
}
String accessKeyId = credential.split("/", 2)[0];
Optional<String> secretKey = S3PostPolicySigner.resolveSecretKey(
iamService, accessKeyId, fields.get("x-amz-security-token"));
String sessionToken = fields.get("x-amz-security-token");
Optional<String> secretKey = S3PostPolicySigner.resolveSecretKey(iamService, accessKeyId, sessionToken);
if (secretKey.isEmpty() && S3PostPolicySigner.isExpiredSession(iamService, accessKeyId, sessionToken)) {
throw new AwsException("ExpiredToken", S3PostPolicySigner.EXPIRED_TOKEN_MESSAGE, 400);
}
if (secretKey.isEmpty()
|| !S3PostPolicySigner.verifySignature(policy, credential, signature, secretKey.get())) {
throw new AwsException("SignatureDoesNotMatch",
@@ -3677,7 +3680,7 @@ public class S3Controller {
// Route through the same bucket-policy/ACL check every other S3 write path uses, so this
// path automatically inherits any future strengthening there too.
s3Service.authorizeObjectWrite(bucket, key, "s3:PutObject",
new S3Service.RequestAuthorization(true, accessKeyId, fields.get("x-amz-security-token")));
new S3Service.RequestAuthorization(true, accessKeyId, sessionToken));
validatePolicyExpiration(policy);
validatePolicyConditions(policy, bucket, fields, contentLength);
@@ -43,7 +43,8 @@ import java.util.Optional;
* <p>Error codes, statuses and ordering follow real S3: a structurally bad header is
* {@code 400 AuthorizationHeaderMalformed}; a request with no date is {@code 403 AccessDenied};
* a missing {@code x-amz-content-sha256} is {@code 400 InvalidRequest}; an unregistered key is
* {@code 403 InvalidAccessKeyId}; a request more than fifteen minutes from server time is
* {@code 403 InvalidAccessKeyId}, and a session past its expiry, sent with its own token,
* {@code 400 ExpiredToken}; a request more than fifteen minutes from server time is
* {@code 403 RequestTimeTooSkewed}; a signature that does not verify is
* {@code 403 SignatureDoesNotMatch}; and a body that does not match its declared hash is
* {@code 400 XAmzContentSHA256Mismatch}.
@@ -169,9 +170,13 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {
}
declaredHash = declaredHash.trim();
Optional<String> secretKey = S3PostPolicySigner.resolveSecretKey(
iamService, accessKeyId, ctx.getHeaderString("X-Amz-Security-Token"));
String sessionToken = ctx.getHeaderString("X-Amz-Security-Token");
Optional<String> secretKey = S3PostPolicySigner.resolveSecretKey(iamService, accessKeyId, sessionToken);
if (secretKey.isEmpty()) {
if (S3PostPolicySigner.isExpiredSession(iamService, accessKeyId, sessionToken)) {
abort(ctx, 400, "ExpiredToken", S3PostPolicySigner.EXPIRED_TOKEN_MESSAGE);
return;
}
abort(ctx, 403, "InvalidAccessKeyId",
"The AWS Access Key Id you provided does not exist in our records.");
return;
@@ -22,6 +22,9 @@ final class S3PostPolicySigner {
static final String LEGACY_ACCESS_KEY_ID = "test";
static final String LEGACY_SECRET_KEY = "test";
/** S3's error list: {@code ExpiredToken}, 400 Bad Request. */
static final String EXPIRED_TOKEN_MESSAGE = "The provided token has expired.";
private S3PostPolicySigner() {
}
@@ -35,6 +38,15 @@ final class S3PostPolicySigner {
return Optional.empty();
}
/**
* Whether a credential {@link #resolveSecretKey} found no secret for is a session past its
* expiry, sent with its own token. S3 answers it with {@code 400 ExpiredToken}
* ({@link #EXPIRED_TOKEN_MESSAGE}), not as a key it does not know.
*/
static boolean isExpiredSession(IamService iamService, String accessKeyId, String sessionToken) {
return iamService != null && iamService.isExpiredSessionToken(accessKeyId, sessionToken);
}
/**
* Verifies that {@code credential} is a well-formed S3 SigV4 credential scope:
* exactly {@code accessKeyId/date/region/s3/aws4_request}, with the service and