fix(sts): refuse a session whose token exceeds the limit (#8883)

An oversized session was minted outright and PackedPolicySize was
published as a flat zero on every session, where AWS holds a session
token to one limit of 4096 bytes, refuses a token that will not fit with
PackedPolicyTooLarge 400 naming the size and the limit, and publishes
that member as the percentage of the limit the token consumes, the same
value as SessionTokenUtilization.

Both are answered now on the four operations that carry state into the
token they mint, the refusal read before anything is minted. The measure
is this emulator's own: the token it will mint, which
MinimumSessionTokenSize can ask to be longer, plus the state the request
carries into it in UTF-8 bytes. AWS assembles and compresses the real
thing, whose size follows the input in neither direction, so the numbers
differ; the IAM page records the one size recorded here, 26092 bytes for
about 19000 of input, so a later recording can calibrate the reading.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-18 16:06:45 -04:00
1 parent b57b837286
commit d2facef773
3 files changed
+340 -11

No files matched your search

@@ -34,6 +34,7 @@ import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.security.interfaces.RSAPublicKey;
import java.time.Clock;
@@ -46,6 +47,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
/**
@@ -142,6 +144,15 @@ public class StsQueryHandler {
public Response handle(String action, MultivaluedMap<String, String> params) {
LOG.debugv("STS action: {0}", action);
// A session whose token will not fit is refused before anything is minted, on every
// operation that carries state into the token it mints.
if (PACKED_SESSION_OPERATIONS.contains(action)) {
AssembledSession session = assembledSession(params);
if (session.tooLarge()) {
return session.refusal();
}
}
return switch (action) {
case "AssumeRole" -> handleAssumeRole(params);
case "GetCallerIdentity" -> handleGetCallerIdentity(params);
@@ -224,11 +235,94 @@ public class StsQueryHandler {
.elem("Arn", assumedRoleArn)
.elem("AssumedRoleId", assumedRoleId)
.end("AssumedRoleUser")
.elem("PackedPolicySize", "0")
.elem("PackedPolicySize", assembledSession(params).utilization())
.build();
return Response.ok(AwsQueryResponse.envelope("AssumeRole", AwsNamespaces.STS, result)).build();
}
/**
* The one limit AWS enforces on a session token, which its refusal names as well. AWS "has
* replaced the packed policy size and the overall session token size limits with a single
* token size limit of 4,096 bytes": "the assembled session token must fit within 4,096 bytes",
* and "the separate packed policy limit, which made failures hard to predict, has been
* removed".
*/
private static final int SESSION_TOKEN_LIMIT = 4096;
/** The operations that carry state into the token they mint and model the refusal for it. */
private static final Set<String> PACKED_SESSION_OPERATIONS = Set.of(
"AssumeRole", "AssumeRoleWithSAML", "AssumeRoleWithWebIdentity", "GetFederationToken");
/**
* What a request's session token comes to, and what that costs against the limit it is held
* to.
*
* <p>AWS "serializes and compresses your session policies and tags when assembling the token",
* refuses a token that will not fit with {@code PackedPolicyTooLarge} 400, whose "error
* message reports the session token size and the maximum allowed size, both in bytes", and
* reports what a session that does fit consumed: {@code SessionTokenUtilization} is "the
* percentage of the 4,096-byte limit consumed", and {@code PackedPolicySize} "now reports the
* same value as SessionTokenUtilization", kept "for backward compatibility" where the API
* reference calls it deprecated. This emulator refused nothing and published that member as a
* flat zero on every session, so a caller could neither see what its session cost nor be told
* it was over.
*
* <p>The measure is this emulator's own: the token it will mint for the request, whose length
* {@code MinimumSessionTokenSize} can ask to be raised, plus the state the request asks to
* carry into it, in UTF-8 bytes. AWS's assembly is unpublished and its size does not read off
* the input in either direction, since "compression results vary based on the actual content,
* not just its length" and "two sets of tags with identical character counts can produce
* different token sizes", while the one size recorded here ran larger than the bytes going in,
* 26092 for a request carrying about 19000. So this number will differ from AWS's, and
* {@code docs/services/iam.md} records the figure that was recorded so a later one can
* calibrate the reading.
*/
private record AssembledSession(int bytes) {
boolean tooLarge() {
return bytes > SESSION_TOKEN_LIMIT;
}
/**
* The member's own unit, truncated as AWS's own recorded pair reports it: a token of 2532
* bytes is published as 61.
*/
String utilization() {
return Integer.toString(bytes * 100 / SESSION_TOKEN_LIMIT);
}
Response refusal() {
return AwsQueryResponse.error("PackedPolicyTooLarge",
"Serialized token too large for session. Token size of " + bytes
+ " bytes exceeds the maximum allowed limit of "
+ SESSION_TOKEN_LIMIT + " bytes.", AwsNamespaces.STS, 400);
}
}
/**
* What the request's token comes to: the length this emulator will mint for it, and the state
* it asks to carry, which is the inline document, every policy ARN, and every tag key and
* value. {@code TransitiveTagKeys} names tags already counted, so it adds nothing of its own,
* which is what AWS says of its own packing as well: "the transitive status of a session tag
* does not impact its packed binary size".
*/
private AssembledSession assembledSession(MultivaluedMap<String, String> params) {
StringBuilder state = new StringBuilder();
String document = params.getFirst("Policy");
if (document != null) {
state.append(document);
}
for (AwsQueryLists.IndexedMember arn : AwsQueryLists.members(params, "PolicyArns", null)) {
String value = params.getFirst(arn.prefix() + ".arn");
if (value != null) {
state.append(value);
}
}
tagClaims(params).forEach((key, value) -> state.append(key).append(value));
return new AssembledSession(sessionTokenLength(params)
+ state.toString().getBytes(StandardCharsets.UTF_8).length);
}
/**
* What a call's MFA parameters authenticated: the instant the device was used, or the denial
* that answers a device the caller cannot present. Both {@code GetSessionToken} and
@@ -528,7 +622,7 @@ public class StsQueryHandler {
.elem("Arn", assumedRoleArn)
.elem("AssumedRoleId", assumedRoleId)
.end("AssumedRoleUser")
.elem("PackedPolicySize", "0")
.elem("PackedPolicySize", assembledSession(params).utilization())
.elem("Provider", provider)
.elem("Audience", audience)
.elem("SubjectFromWebIdentityToken", subject)
@@ -726,7 +820,7 @@ public class StsQueryHandler {
String result = new XmlBuilder()
.raw(credentialsXml(accessKeyId, secretKey, sessionToken, expiration))
.start("AssumedRoleUser").elem("Arn", assumedRoleArn).elem("AssumedRoleId", assumedRoleId).end("AssumedRoleUser")
.elem("PackedPolicySize", "0").elem("Issuer", verified.issuer()).elem("Audience", "urn:amazon:webservices")
.elem("PackedPolicySize", assembledSession(params).utilization()).elem("Issuer", verified.issuer()).elem("Audience", "urn:amazon:webservices")
.elem("NameQualifier", verified.nameQualifier()).elem("SubjectType", verified.subjectType()).elem("Subject", verified.subject()).build();
return Response.ok(AwsQueryResponse.envelope("AssumeRoleWithSAML", AwsNamespaces.STS, result)).build();
}
@@ -782,7 +876,7 @@ public class StsQueryHandler {
.elem("FederatedUserId", federatedUserId)
.elem("Arn", federatedUserArn)
.end("FederatedUser")
.elem("PackedPolicySize", "0")
.elem("PackedPolicySize", assembledSession(params).utilization())
.build();
return Response.ok(AwsQueryResponse.envelope("GetFederationToken", AwsNamespaces.STS, result)).build();
}
@@ -1233,15 +1327,22 @@ public class StsQueryHandler {
}
/**
* The session token for a request: the length its {@code MinimumSessionTokenSize} asks for,
* never under the 200 characters this emulator mints by default. Every operation that models
* the member answers it from here, so the floor is written once, and the model holds the value
* to the 0 to 4096 it declares ahead of this. A request asking for less than the floor is
* answered with the floor, which is what "minimum" asks for.
* The session token for a request, the length {@link #sessionTokenLength} gives it. Every
* operation that models the member answers it from here, so the floor is written once.
*/
private String sessionToken(MultivaluedMap<String, String> params) {
return randomSecret(Math.max(SESSION_TOKEN_LENGTH,
getIntParam(params, "MinimumSessionTokenSize", 0)));
return randomSecret(sessionTokenLength(params));
}
/**
* How long the token for a request will be: the length its {@code MinimumSessionTokenSize}
* asks for, never under the 200 characters this emulator mints by default, which is what a
* minimum asks for, and never over the limit a token is held to, so a value outside the 0 to
* 4096 the model declares is answered by the shape rather than read as a size here.
*/
private int sessionTokenLength(MultivaluedMap<String, String> params) {
return Math.max(SESSION_TOKEN_LENGTH,
Math.min(getIntParam(params, "MinimumSessionTokenSize", 0), SESSION_TOKEN_LIMIT));
}
private String randomSecret(int length) {