From aa2adac67762d5329f914c413e3f487015d270e1 Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:44:43 -0400 Subject: [PATCH] fix(iam): look the user up before the service name on a credential request (#8314) CreateServiceSpecificCredential resolved the service name first, so a request that named a user who does not exist and a service that is not supported was answered for the service. AWS answers for the user: a recording validated against a real account asks for a credential as a user that was never created, once with CodeCommit and once with a service name nothing supports, and both come back NoSuchEntity with "The user with name cannot be found." The user is confirmed before the service name is read now, under the lock it was already taken under. ListServiceSpecificCredentials had the same shape, with its ServiceName filter resolved ahead of the user it lists for, and reads the two the same way round. Under AllUsers there is no user to look up, so an unsupported filter still answers for itself. What the bounds of CredentialAgeDays say is settled ahead of both by the constraints every request is held to, so only the question that needs the service is left behind it: whether this service takes the member at all. The in-operation bounds stay where they are as the backstop they became. --- docs/services/iam.md | 9 ++++ .../io/floci/aws/services/iam/IamService.java | 44 ++++++++++++------- ...viceSpecificCredentialIntegrationTest.java | 43 ++++++++++++++++++ 3 files changed, 79 insertions(+), 17 deletions(-) diff --git a/docs/services/iam.md b/docs/services/iam.md index 270d751a33..8618e563a4 100644 --- a/docs/services/iam.md +++ b/docs/services/iam.md @@ -1534,6 +1534,15 @@ These are what AWS calls Git credentials when they are used with CodeCommit. A s support them is `NoSuchEntity` with status 404 and the message "No such service `` is supported for Service Specific Credentials". +A request wrong in both halves answers for the user. Recordings validated against AWS answer a +`UserName` that names no user with "The user with name `` cannot be found." whether the +`ServiceName` beside it is supported or not, so the user is looked up before the service name is +read, on `CreateServiceSpecificCredential` and on `ListServiceSpecificCredentials` alike. Under +`AllUsers` there is no user to look up and the filter is the only thing left to be wrong about. What +the bounds of `CredentialAgeDays` say is settled ahead of both, by the constraints every request is +held to; whether the service takes the member at all cannot be asked before the service is known, +so that answer stays behind them. + That is not what the wire model would suggest. The model declares `ServiceNotSupportedException`, code `NotSupportedService`, on both `CreateServiceSpecificCredential` and `ListServiceSpecificCredentials`, and AWS never raises it: recordings validated against AWS answer diff --git a/src/main/java/io/floci/aws/services/iam/IamService.java b/src/main/java/io/floci/aws/services/iam/IamService.java index 5a4df94be7..939a9b2eeb 100644 --- a/src/main/java/io/floci/aws/services/iam/IamService.java +++ b/src/main/java/io/floci/aws/services/iam/IamService.java @@ -3905,28 +3905,35 @@ public class IamService implements SessionAccountLookup, ResourceProvider { * *

The secret half is returned here and by the reset, and nowhere else: there is no get * operation, and the list is defined without it. + * + *

A request wrong in both halves is answered for the user: recordings validated against AWS + * answer a user that does not exist beside a service that is not supported with the user, so + * the user is confirmed before the service name is read. What the bounds of + * {@code CredentialAgeDays} say is settled ahead of either, by the constraints every request + * is held to; what is left here is whether the service this is takes the member at all, which + * cannot be asked before the service is known. */ public ServiceSpecificCredential createServiceSpecificCredential( String userName, String requestedServiceName, Integer credentialAgeDays) { - String serviceName = supportedServiceName(requestedServiceName); - boolean longTermKey = LONG_TERM_API_KEY_SERVICES.contains(serviceName); - if (credentialAgeDays != null) { - if (!longTermKey) { - throw new AwsException("ValidationError", - "CredentialAgeDays is only valid for services that support long-term API " - + "keys.", 400); - } - if (credentialAgeDays < MIN_CREDENTIAL_AGE_DAYS - || credentialAgeDays > MAX_CREDENTIAL_AGE_DAYS) { - throw IamValidationErrors.validationError("credentialAgeDays", - String.valueOf(credentialAgeDays), "Member must be between " - + MIN_CREDENTIAL_AGE_DAYS + " and " + MAX_CREDENTIAL_AGE_DAYS); - } - } synchronized (serviceCredentialLock) { // Confirmed under the lock, as the other per-user credentials are: DeleteUser checks // for these holding the same lock. getUser(userName); + String serviceName = supportedServiceName(requestedServiceName); + boolean longTermKey = LONG_TERM_API_KEY_SERVICES.contains(serviceName); + if (credentialAgeDays != null) { + if (!longTermKey) { + throw new AwsException("ValidationError", + "CredentialAgeDays is only valid for services that support long-term " + + "API keys.", 400); + } + if (credentialAgeDays < MIN_CREDENTIAL_AGE_DAYS + || credentialAgeDays > MAX_CREDENTIAL_AGE_DAYS) { + throw IamValidationErrors.validationError("credentialAgeDays", + String.valueOf(credentialAgeDays), "Member must be between " + + MIN_CREDENTIAL_AGE_DAYS + " and " + MAX_CREDENTIAL_AGE_DAYS); + } + } // One scan serves both checks: the quota counts this user's credentials for this // service, the name search needs every name in the account, and no write can land // between them while this holds the lock. @@ -4070,8 +4077,9 @@ public class IamService implements SessionAccountLookup, ResourceProvider { /** Every credential of a user, or of every user when the caller asked for all of them. */ public List listServiceSpecificCredentials( String userName, String requestedServiceName, boolean allUsers) { - String serviceName = requestedServiceName == null - ? null : supportedServiceName(requestedServiceName); + // The user before the service name, as on the create: a request wrong in both halves is + // answered for the user. Under AllUsers there is no user to look up, and the filter is + // then the only thing the request can be wrong about. List found; if (allUsers) { found = serviceCredentials.scan(key -> true); @@ -4079,6 +4087,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider { getUser(userName); // validates existence found = userServiceCredentials(userName); } + String serviceName = requestedServiceName == null + ? null : supportedServiceName(requestedServiceName); return found.stream() .filter(c -> serviceName == null || serviceName.equals(c.getServiceName())) .sorted(Comparator.comparing( diff --git a/src/test/java/io/floci/aws/services/iam/ServiceSpecificCredentialIntegrationTest.java b/src/test/java/io/floci/aws/services/iam/ServiceSpecificCredentialIntegrationTest.java index b03e517f4b..1a823aee2f 100644 --- a/src/test/java/io/floci/aws/services/iam/ServiceSpecificCredentialIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/iam/ServiceSpecificCredentialIntegrationTest.java @@ -325,6 +325,49 @@ class ServiceSpecificCredentialIntegrationTest { .when().post("/").then().statusCode(400).body(containsString("ValidationError")); } + /** + * A request wrong in both halves is answered for the user. Recordings validated against AWS + * answer a user that does not exist with the user whether the service beside it is supported + * or not, so the user is looked up before the service name is read. + */ + @Test + void aRequestNamingNeitherAUserNorASupportedServiceAnswersForTheUser() { + String missing = "ssc-absent-" + suffix(); + + for (String service : List.of(CODECOMMIT, "nonexistentservice.amazonaws.com", + "lambda.amazonaws.com", "o3on3n3onosneo", "")) { + iam("CreateServiceSpecificCredential") + .formParam("UserName", missing) + .formParam("ServiceName", service) + .when().post("/").then().statusCode(404) + .body(containsString("NoSuchEntity")) + .body(containsString("The user with name " + missing + " cannot be found.")) + .body(not(containsString("No such service"))); + } + } + + /** The list reads the two the same way round, and AllUsers leaves the filter alone to be wrong. */ + @Test + void theListAnswersForTheUserBeforeTheServiceFilter() { + String missing = "ssc-absent-list-" + suffix(); + + iam("ListServiceSpecificCredentials") + .formParam("UserName", missing) + .formParam("ServiceName", "nonexistentservice.amazonaws.com") + .when().post("/").then().statusCode(404) + .body(containsString("NoSuchEntity")) + .body(containsString("The user with name " + missing + " cannot be found.")) + .body(not(containsString("No such service"))); + + iam("ListServiceSpecificCredentials") + .formParam("AllUsers", "true") + .formParam("ServiceName", "nonexistentservice.amazonaws.com") + .when().post("/").then().statusCode(404) + .body(containsString("NoSuchEntity")) + .body(containsString("No such service nonexistentservice.amazonaws.com " + + "is supported for Service Specific Credentials")); + } + /** The list takes the same filter, so it rejects the same way. */ @Test void theListRefusesAnUnsupportedServiceFilter() {