fix(iam): refuse a service namespace that names no AWS service (#8865)

The member's pattern admits anything word-shaped, so a value like
amazon-s3 satisfied everything the model declares and was answered 200
with an empty policy list, telling a caller the identity reaches nothing
rather than that the question was wrong.

Both operations that take a namespace hold it to the vendored catalog now,
through AwsServiceNamespaces.isPublished so nothing keeps a second list,
and each answers the sentence recorded for it: the list operation names
the rejected values inside AWS's own brackets, and the entity reader
carries AWS's own misspelling of namepace.

ServiceNamespaces is vendored for that to hold. The generator held the
whole member back because the operation answered an empty list with a
sentence of its own, and the cost was that no value in the list was read
at all: one breaking the published pattern reached the catalog check and
was answered InvalidInput where AWS answers ValidationError. The member
is read now, each value held to serviceNamespaceType and the list to its
bounds of 1 to 200, which also retires two answers that were this
package's own: an absent list is the required member the model declares,
and a list over 200 is the model's bound rather than a check beside it.

A namespace the job itself reports stays readable on
GetServiceLastAccessedDetailsWithEntities whether the catalog publishes it
or not, so the job is read first. The catalog is vendored, and a policy
may grant a service published after it: the report keeps the namespace
that policy names, so refusing it there would be this package disagreeing
with itself. Nothing else is exempt, and the list operation, which takes
namespaces rather than reporting them, holds every value to the catalog.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-18 09:55:49 -04:00
1 parent a72aedfbb0
commit 4fe226c253
7 files changed
+336 -14

No files matched your search

@@ -0,0 +1,203 @@
package io.floci.aws.services.iam;
import io.floci.aws.testing.PartitionCleanup;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
/**
* A service namespace a request names has to be one AWS publishes. The member's pattern admits
* anything word-shaped, so a value like {@code amazon-s3} satisfies the model and names no service,
* and AWS refuses it rather than reporting that the identity reaches nothing. The two operations
* that take one word the refusal differently, and both sentences are recorded.
*
* <p>{@code docs/services/iam.md} carries the recordings.
*/
@QuarkusTest
class IamServiceNamespaceCheckIntegrationTest {
private static final String IAM_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20261118/us-east-1/iam/aws4_request";
private static final String CODE = "ErrorResponse.Error.Code";
private static final String MESSAGE = "ErrorResponse.Error.Message";
private static final String TYPE = "ErrorResponse.Error.Type";
/** Word-shaped, so the declared pattern admits it, and no service of AWS's. */
private static final String NOT_A_SERVICE = "amazon-s3";
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
private static RequestSpecification iam(String action) {
return given().header("Authorization", IAM_AUTH).formParam("Action", action);
}
private static String unique(String prefix) {
return prefix + "-" + Long.toString(System.nanoTime(), 36);
}
private String createUser() {
String userName = unique("namespace-user");
iam("CreateUser").formParam("UserName", userName).when().post("/").then().statusCode(200);
cleanup.register(() -> iam("DeleteUser").formParam("UserName", userName)
.when().post("/").then().statusCode(200));
return "arn:aws:iam::000000000000:user/" + userName;
}
private String jobFor(String arn) {
return iam("GenerateServiceLastAccessedDetails").formParam("Arn", arn)
.when().post("/").then().statusCode(200)
.extract().path("GenerateServiceLastAccessedDetailsResponse."
+ "GenerateServiceLastAccessedDetailsResult.JobId");
}
/** The list operation names the rejected value inside AWS's own brackets. */
@Test
void aNamespaceThatNamesNoServiceIsRefusedByTheListOperation() {
iam("ListPoliciesGrantingServiceAccess").formParam("Arn", createUser())
.formParam("ServiceNamespaces.member.1", NOT_A_SERVICE)
.when().post("/").then().statusCode(400)
.body(CODE, equalTo("InvalidInput"))
.body(TYPE, equalTo("Sender"))
.body(MESSAGE, equalTo("Invalid service namespace: [" + NOT_A_SERVICE + "]"));
}
/** Several bad values go inside the one pair of brackets, which is the reading. */
@Test
void severalRejectedNamespacesShareTheBrackets() {
iam("ListPoliciesGrantingServiceAccess").formParam("Arn", createUser())
.formParam("ServiceNamespaces.member.1", NOT_A_SERVICE)
.formParam("ServiceNamespaces.member.2", "s3")
.formParam("ServiceNamespaces.member.3", "not-a-service")
.when().post("/").then().statusCode(400)
.body(CODE, equalTo("InvalidInput"))
.body(MESSAGE, equalTo("Invalid service namespace: ["
+ NOT_A_SERVICE + ", not-a-service]"));
}
/** The single-namespace reader words it differently, AWS's own misspelling included. */
@Test
void aNamespaceThatNamesNoServiceIsRefusedByTheEntityReader() {
String arn = createUser();
iam("GetServiceLastAccessedDetailsWithEntities").formParam("JobId", jobFor(arn))
.formParam("ServiceNamespace", NOT_A_SERVICE)
.when().post("/").then().statusCode(400)
.body(CODE, equalTo("InvalidInput"))
.body(MESSAGE, equalTo("Service namepace " + NOT_A_SERVICE
+ " is either invalid or unsupported."));
}
/**
* The job is read ahead of the namespace, because the job is part of what decides whether a
* namespace can be read: one the report lists is readable whatever the catalog says. So a job
* that does not exist is answered as a missing job, and the namespace is judged against a
* report that exists. Which of the two a request breaking both is answered is recorded
* nowhere, and this is the order that lets the two readers agree.
*/
@Test
void aJobThatDoesNotExistIsAnsweredBeforeTheNamespace() {
iam("GetServiceLastAccessedDetailsWithEntities")
.formParam("JobId", "00000000-0000-4000-8000-000000000000")
.formParam("ServiceNamespace", NOT_A_SERVICE)
.when().post("/").then().statusCode(404)
.body(CODE, equalTo("NoSuchEntity"));
}
/**
* Where the vendored shape carries the member, a value breaking its published pattern still
* answers the constraint, which runs ahead of this check.
*/
@Test
void aValueBreakingTheDeclaredPatternStillAnswersTheConstraint() {
iam("GetServiceLastAccessedDetailsWithEntities")
.formParam("JobId", "00000000-0000-4000-8000-000000000000")
.formParam("ServiceNamespace", "not a namespace")
.when().post("/").then().statusCode(400)
.body(CODE, equalTo("ValidationError"));
}
/**
* A value breaking the published pattern is answered that pattern, not the catalog. The list
* member is vendored now, so the declared constraints are read ahead of the catalog check and
* only a well-formed value reaches it.
*/
@Test
void aMalformedListMemberIsAnsweredThePublishedPattern() {
iam("ListPoliciesGrantingServiceAccess").formParam("Arn", createUser())
.formParam("ServiceNamespaces.member.1", "not a namespace")
.when().post("/").then().statusCode(400)
.body(CODE, equalTo("ValidationError"))
.body(MESSAGE, equalTo("1 validation error detected: Value at"
+ " 'serviceNamespaces.1.member' failed to satisfy constraint: Member must"
+ " satisfy regular expression pattern: [\\w-]*"));
}
/**
* An absent list is how an empty one arrives on this protocol, and the model requires the
* member, so that is what answers it. This package used to answer `InvalidInput` and a
* sentence of its own here, which no recording carries.
*/
@Test
void anAbsentListIsTheRequiredMemberTheModelDeclares() {
iam("ListPoliciesGrantingServiceAccess").formParam("Arn", createUser())
.when().post("/").then().statusCode(400)
.body(CODE, equalTo("ValidationError"))
.body(MESSAGE, equalTo("1 validation error detected: Value at"
+ " 'serviceNamespaces' failed to satisfy constraint: Member must not be"
+ " null"));
}
/**
* The catalog is vendored, so a policy may grant a service published after it. The report
* keeps the namespace that policy names, and a request for its entities is answered rather
* than refused: the job is what decides, so this emulator cannot report a service and then
* refuse to read it.
*/
@Test
void aNamespaceTheJobItselfReportsIsReadable() {
String userName = unique("namespace-future");
iam("CreateUser").formParam("UserName", userName).when().post("/").then().statusCode(200);
cleanup.register(() -> iam("DeleteUser").formParam("UserName", userName)
.when().post("/").then().statusCode(200));
iam("PutUserPolicy").formParam("UserName", userName)
.formParam("PolicyName", "grants-a-later-service")
.formParam("PolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":"
+ "[{\"Effect\":\"Allow\",\"Action\":\"somefutureservice:DoThing\","
+ "\"Resource\":\"*\"}]}")
.when().post("/").then().statusCode(200);
cleanup.register(() -> iam("DeleteUserPolicy").formParam("UserName", userName)
.formParam("PolicyName", "grants-a-later-service")
.when().post("/").then().statusCode(200));
String jobId = jobFor("arn:aws:iam::000000000000:user/" + userName);
iam("GetServiceLastAccessedDetailsWithEntities").formParam("JobId", jobId)
.formParam("ServiceNamespace", "somefutureservice")
.when().post("/").then().statusCode(200)
.body(containsString("EntityDetailsList"));
// Nothing else is exempt: a namespace neither the job nor the catalog knows is refused.
iam("GetServiceLastAccessedDetailsWithEntities").formParam("JobId", jobId)
.formParam("ServiceNamespace", NOT_A_SERVICE)
.when().post("/").then().statusCode(400)
.body(CODE, equalTo("InvalidInput"));
}
/** The control: published namespaces are answered as before, on both operations. */
@Test
void publishedNamespacesAreUntouched() {
String arn = createUser();
iam("ListPoliciesGrantingServiceAccess").formParam("Arn", arn)
.formParam("ServiceNamespaces.member.1", "s3")
.formParam("ServiceNamespaces.member.2", "dynamodb")
.when().post("/").then().statusCode(200);
iam("GetServiceLastAccessedDetailsWithEntities").formParam("JobId", jobFor(arn))
.formParam("ServiceNamespace", "s3")
.when().post("/").then().statusCode(200);
}
}
@@ -648,6 +648,12 @@ class ServiceLastAccessedIntegrationTest {
.body(not(containsString(denyName)));
}
/**
* An absent list is how an empty one arrives on this protocol, and the vendored request shape
* carries the member as the model declares it, required. So the required-member violation is
* what answers this, where this package used to answer `InvalidInput` and a sentence of its
* own that no recording carries.
*/
@Test
void listPoliciesGrantingServiceAccessRequiresAtLeastOneServiceNamespace() {
String arn = createUser("laa-nonamespace-" + suffix());
@@ -655,7 +661,10 @@ class ServiceLastAccessedIntegrationTest {
iam("ListPoliciesGrantingServiceAccess").formParam("Arn", arn)
.when().post("/").then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidInput"));
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message", equalTo("1 validation error detected: Value at"
+ " 'serviceNamespaces' failed to satisfy constraint: Member must not be"
+ " null"));
}