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

@@ -7,6 +7,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Every IAM service namespace, read once from the vendored {@code aws/iam-service-namespaces.json}.
@@ -34,6 +35,7 @@ public final class AwsServiceNamespaces {
private static final class Holder {
static final List<String> NAMESPACES = load();
static final Set<String> LOOKUP = Set.copyOf(NAMESPACES);
}
private AwsServiceNamespaces() {
@@ -50,6 +52,15 @@ public final class AwsServiceNamespaces {
return Holder.NAMESPACES;
}
/**
* Whether AWS publishes {@code namespace} as a service namespace, for an operation that takes
* one from a caller and has to refuse what names no service. The test lives here rather than
* in the caller so the catalog is read in one place and nothing hand-maintains a second list.
*/
public static boolean isPublished(String namespace) {
return namespace != null && Holder.LOOKUP.contains(namespace);
}
private static List<String> load() {
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(RESOURCE_NAME)) {
if (in == null) {
@@ -88,8 +88,6 @@ public class IamQueryHandler {
private static final Set<String> SORT_KEYS = Set.of("SERVICE_NAMESPACE_ASCENDING",
"SERVICE_NAMESPACE_DESCENDING", "LAST_AUTHENTICATED_TIME_ASCENDING",
"LAST_AUTHENTICATED_TIME_DESCENDING");
/** {@code serviceNamespaceListType}: minimum 1 item, maximum 200. */
private static final int MAX_SERVICE_NAMESPACES = 200;
private static final int MAX_TAG_LIST_MEMBERS = 50;
private static final int MAX_TAG_KEY_LENGTH = 128;
private static final int MAX_TAG_VALUE_LENGTH = 256;
@@ -2835,6 +2833,47 @@ public class IamQueryHandler {
return arn;
}
/**
* The namespaces a request names, held to the ones AWS publishes. The member's pattern admits
* anything word-shaped, so a value like {@code amazon-s3} satisfies the model and still names
* no service, and AWS refuses it rather than reporting that the identity reaches nothing.
*
* <p>Recorded on {@code ListPoliciesGrantingServiceAccess} on 2026-11-03: {@code InvalidInput}
* 400 with a sender fault and {@code Invalid service namespace: [<namespace>]}, the brackets
* being AWS's own. The recording covers one bad value; several are rendered inside the one
* pair of brackets, which is this emulator's reading of a sentence AWS writes for the plural.
*/
private static void requirePublishedNamespaces(List<String> namespaces) {
List<String> unknown = namespaces.stream()
.filter(namespace -> !AwsServiceNamespaces.isPublished(namespace))
.toList();
if (!unknown.isEmpty()) {
throw new AwsException("InvalidInput",
"Invalid service namespace: [" + String.join(", ", unknown) + "]", 400);
}
}
/**
* The same rule for the member that takes one namespace, which AWS words differently and
* spells {@code namepace}, recorded on {@code GetServiceLastAccessedDetailsWithEntities} on
* 2026-11-03. The misspelling is AWS's and is kept, a client matching the message having to
* match what the service sends.
*
* <p>A namespace the job itself reports is readable whether the catalog publishes it or not.
* The catalog is vendored, so it is as new as the last regeneration, where a policy may grant
* a service published after it: the report would list that namespace and a request for its
* entities would then be refused, which is this emulator disagreeing with itself. The job is
* what decides, which is why it is read first. Nothing else is exempt: a namespace the job
* does not report and the catalog does not publish is still refused.
*/
private static void requireReadableNamespace(String namespace, ServiceLastAccessedJob job) {
if (AwsServiceNamespaces.isPublished(namespace) || job.getServiceNamespaces().contains(namespace)) {
return;
}
throw new AwsException("InvalidInput",
"Service namepace " + namespace + " is either invalid or unsupported.", 400);
}
/**
* {@code SortKey} if the request carries one the enum permits, null if it carries none. A
* request that carries the member is held to the enum whatever it carries: an empty value is
@@ -2883,6 +2922,7 @@ public class IamQueryHandler {
validatePagingInputs("GetServiceLastAccessedDetailsWithEntities", params);
ServiceLastAccessedJob job = serviceLastAccessedService.get(
accountResolver.resolve(authorization), jobId);
requireReadableNamespace(namespace, job);
// The report named the services it covers, so an entity is only reported for one of those.
List<ServiceLastAccessedEntity> reported = job.getServiceNamespaces().contains(namespace)
? job.getEntities() : List.of();
@@ -2917,12 +2957,12 @@ public class IamQueryHandler {
String authorization) {
String accountId = accountResolver.resolve(authorization);
String arn = requireParam(params, "Arn");
// The vendored request shape carries the member now, so the declared constraints are read
// ahead of this method: an absent list is the required member it declares, a list over 200
// is its own bound, and a value breaking the published pattern is that pattern. What is
// left here is the one rule the model cannot state, which services AWS publishes.
List<String> namespaces = getMemberList(params, "ServiceNamespaces");
if (namespaces.isEmpty()) {
throw new AwsException("InvalidInput",
"The request must include at least one service namespace.", 400);
}
checkListLength(namespaces.size(), "serviceNamespaces", MAX_SERVICE_NAMESPACES);
requirePublishedNamespaces(namespaces);
List<GrantingPolicy> candidates = policiesForIdentity(accountId, arn);
// The response list carries one entry per requested namespace, so that is what a Marker
// walks through. The request models no MaxItems, so the table gives it no limit and the