mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(iam): read declared constraints before the lookup they precede (#6402)
The validation AWS runs ahead of an operation never reaches its store, so a request that breaks a declared constraint is refused whether or not the thing it names exists. Four places here had the order the other way round, and each one answered a 404 for a request AWS answers with a 400. The three Access Advisor and Organizations report readers looked the job up before reading JobId, MaxItems and Marker, so an unknown job carrying a MaxItems of 0 answered NoSuchEntity, and a JobId of the wrong length was never held to its shape at all. jobIDType is min 36 and max 36, and the fixed length reports as one constraint, "Member must have length equal to 36", which is the form lifecycleActionToken already uses for the same shape in Auto Scaling. The MaxItems check moves out of paginate so a reader can run it early, and paginate still runs it, being reached from readers that look nothing up. GenerateServiceLastAccessedDetails had the same order on its Granularity enum, reading it inside the generate that sits behind the ARN resolution, so a granularity AWS refuses answered NoSuchEntity for an ARN that named nothing. The check is its own method now and the handler runs it first. PolicySourceArn is arnType, min 20, and the simulators resolved the entity out of whatever followed the last slash, so ":user/bob" simulated against a user named bob. A value too short to be any ARN is now the length ValidationError IAM answers for a short ARN member. A value that clears 20 characters and still parses as no ARN is left alone: the member publishes no pattern, and no source records what AWS answers for one. The service-specific credential id reported both of its length bounds in one violation, so a short id was told about a 128-character ceiling it was nowhere near. Each message names the one bound the value broke, which is how every recorded IAM length violation reads: "Member must have length less than or equal to 64" for a user name over 64, and "Member must have length greater than or equal to 20" for an ARN under 20. Seven tests. Four over the wire on the readers: a JobId of the wrong length refused before any lookup on both the last-accessed reader and the entities reader, the same on the Organizations reader with no organization built at all, and an unknown job carrying a bad MaxItems, a bad Marker and then nothing wrong, which pins the 400s and the surviving 404. One on the generator, where the granularity is refused for an ARN that names nothing. One on both simulators for a PolicySourceArn under 20. And the credential id's two bounds, each asserting the other bound is absent from the message, with the credential itself still listed once the refusals are done: a validation error that deleted something on its way out would read the same from the response alone.
This commit is contained in:
1 parent
1174922522
commit
62c202f073
8 files changed
+350
-60
No files matched your search
@@ -66,6 +66,11 @@ public class IamQueryHandler {
|
||||
/** {@code MaxItems} on the readers that page through {@link #paginate}: "defaults to 100", range 1 to 1000. */
|
||||
private static final int DEFAULT_MAX_ITEMS = 100;
|
||||
private static final int MAX_MAX_ITEMS = 1000;
|
||||
/** {@code jobIDType} on every report reader: min 36 and max 36, which is a UUID string. */
|
||||
private static final int JOB_ID_LENGTH = 36;
|
||||
/** {@code arnType} on {@code PolicySourceArn}: 20 to 2048, with no pattern. */
|
||||
private static final int ARN_MIN_LENGTH = 20;
|
||||
private static final int ARN_MAX_LENGTH = 2048;
|
||||
/** {@code sortKeyType} on {@code GetOrganizationsAccessReport}. */
|
||||
private static final Set<String> SORT_KEYS = Set.of("SERVICE_NAMESPACE_ASCENDING",
|
||||
"SERVICE_NAMESPACE_DESCENDING", "LAST_AUTHENTICATED_TIME_ASCENDING",
|
||||
@@ -2159,7 +2164,7 @@ public class IamQueryHandler {
|
||||
}
|
||||
|
||||
private Response handleSimulatePrincipalPolicy(MultivaluedMap<String, String> params) {
|
||||
String policySourceArn = getParam(params, "PolicySourceArn");
|
||||
String policySourceArn = validatedPolicySourceArn(params);
|
||||
CallerContext caller = iamService.resolvePrincipalContext(policySourceArn);
|
||||
List<String> actionNames = requireActionNames(params);
|
||||
List<String> resourceArns = extractResourceArnsOrWildcard(params);
|
||||
@@ -2249,7 +2254,7 @@ public class IamQueryHandler {
|
||||
}
|
||||
|
||||
private Response handleGetContextKeysForPrincipalPolicy(MultivaluedMap<String, String> params) {
|
||||
String policySourceArn = getParam(params, "PolicySourceArn");
|
||||
String policySourceArn = validatedPolicySourceArn(params);
|
||||
// "Only context keys referenced by the identity-based policies attached to the specified
|
||||
// entity, and any additional policies that you provide, are included": a grant the account
|
||||
// applies without attaching it is therefore not read here, though a simulation evaluates
|
||||
@@ -2276,6 +2281,9 @@ public class IamQueryHandler {
|
||||
String authorization) {
|
||||
String accountId = accountResolver.resolve(authorization);
|
||||
String arn = requireParam(params, "Arn");
|
||||
// The same rule as the readers below: the granularity is a declared enum, so it is read
|
||||
// before the ARN is resolved rather than inside the generate it is passed to.
|
||||
ServiceLastAccessedService.validateGranularity(params.getFirst("Granularity"));
|
||||
AccessAdvisorTarget target = resolveAccessAdvisorTarget(accountId, arn);
|
||||
// Already sorted and expanded, including the wildcard and NotAction grants that name no
|
||||
// namespace of their own.
|
||||
@@ -2294,8 +2302,10 @@ public class IamQueryHandler {
|
||||
*/
|
||||
private Response handleGetServiceLastAccessedDetails(MultivaluedMap<String, String> params,
|
||||
String authorization) {
|
||||
String jobId = validatedJobId(params);
|
||||
validatePagingInputs(params);
|
||||
ServiceLastAccessedJob job = serviceLastAccessedService.get(
|
||||
accountResolver.resolve(authorization), requireParam(params, "JobId"));
|
||||
accountResolver.resolve(authorization), jobId);
|
||||
Page<String> page = paginate(job.getServiceNamespaces(), Function.identity(), params);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.elem("JobStatus", "COMPLETED")
|
||||
@@ -2352,10 +2362,11 @@ public class IamQueryHandler {
|
||||
*/
|
||||
private Response handleGetOrganizationsAccessReport(MultivaluedMap<String, String> params,
|
||||
String authorization) {
|
||||
String jobId = requireParam(params, "JobId");
|
||||
String jobId = validatedJobId(params);
|
||||
// Read before the lookup, because a value outside the enum is a bad request whether or not
|
||||
// the job exists: validating it after would answer NoSuchEntity for a request AWS rejects.
|
||||
String sortKey = validatedSortKey(params);
|
||||
validatePagingInputs(params);
|
||||
OrganizationsAccessReportJob job = organizationsAccessReportService.get(
|
||||
accountResolver.resolve(authorization), jobId);
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
@@ -2394,6 +2405,93 @@ public class IamQueryHandler {
|
||||
AwsNamespaces.IAM, xml.build())).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code MaxItems} if the request carries one inside the range the reference publishes, null if
|
||||
* it carries none. Read without the list it will page, so a reader can check it before it looks
|
||||
* anything up.
|
||||
*/
|
||||
private static Integer validatedMaxItems(MultivaluedMap<String, String> params) {
|
||||
String raw = params.getFirst("MaxItems");
|
||||
if (raw == null || raw.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
int limit;
|
||||
try {
|
||||
limit = Integer.parseInt(raw.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"The value " + raw + " at 'maxItems' is not a number.", 400);
|
||||
}
|
||||
if (limit < 1) {
|
||||
throw validationError(raw, "maxItems", "Member must have value greater than or equal to 1");
|
||||
}
|
||||
if (limit > MAX_MAX_ITEMS) {
|
||||
throw validationError(raw, "maxItems",
|
||||
"Member must have value less than or equal to " + MAX_MAX_ITEMS);
|
||||
}
|
||||
return limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code JobId} held to the fixed 36 characters {@code jobIDType} declares, before the job is
|
||||
* looked up. The order is the whole point: a value that breaks a declared constraint is a bad
|
||||
* request whether or not the job exists, and the validation AWS runs ahead of an operation
|
||||
* never reaches its store, so checking it afterwards answers {@code NoSuchEntity} 404 where
|
||||
* AWS answers 400.
|
||||
*
|
||||
* <p>A fixed length reports as one constraint rather than as both bounds, the form
|
||||
* {@code lifecycleActionToken} already uses for the same shape in Auto Scaling, and the
|
||||
* offending value is left out, which is how the most recent IAM recordings render one.
|
||||
*/
|
||||
private String validatedJobId(MultivaluedMap<String, String> params) {
|
||||
String jobId = requireParam(params, "JobId");
|
||||
if (jobId.length() != JOB_ID_LENGTH) {
|
||||
throw SmithyValidationErrors.validationError("Value at 'jobId' failed to satisfy "
|
||||
+ "constraint: Member must have length equal to " + JOB_ID_LENGTH);
|
||||
}
|
||||
return jobId;
|
||||
}
|
||||
|
||||
/**
|
||||
* The paging members, read ahead of the lookup whose result they page, for the reason
|
||||
* {@link #validatedJobId} is. {@link #paginate} reads them again when it runs: most of the
|
||||
* readers paged there look nothing up first, and a check only one caller performs is one the
|
||||
* next caller forgets.
|
||||
*/
|
||||
private void validatePagingInputs(MultivaluedMap<String, String> params) {
|
||||
validatedMaxItems(params);
|
||||
String marker = params.getFirst("Marker");
|
||||
if (marker != null && !marker.isBlank() && markers.cursorOf(marker.trim()) == null) {
|
||||
markerIndex(marker);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code PolicySourceArn} held to the 20 to 2048 characters {@code arnType} declares, before
|
||||
* the entity it names is resolved. IAM answers that minimum on a sibling ARN member with a
|
||||
* {@code ValidationError} naming the length, so a value too short to be any ARN is refused on
|
||||
* its shape rather than resolved to whatever follows its last slash.
|
||||
*
|
||||
* <p>A value that clears 20 characters and still parses as no ARN is left alone: the member
|
||||
* publishes a length and no pattern, and what AWS answers for one is not recorded. An absent
|
||||
* or blank value is left alone too, the resolver below reporting that one.
|
||||
*/
|
||||
private String validatedPolicySourceArn(MultivaluedMap<String, String> params) {
|
||||
String arn = getParam(params, "PolicySourceArn");
|
||||
if (arn == null || arn.isBlank()) {
|
||||
return arn;
|
||||
}
|
||||
if (arn.length() < ARN_MIN_LENGTH) {
|
||||
throw SmithyValidationErrors.validationError("Value at 'policySourceArn' failed to satisfy "
|
||||
+ "constraint: Member must have length greater than or equal to " + ARN_MIN_LENGTH);
|
||||
}
|
||||
if (arn.length() > ARN_MAX_LENGTH) {
|
||||
throw SmithyValidationErrors.validationError("Value at 'policySourceArn' failed to satisfy "
|
||||
+ "constraint: Member must have length less than or equal to " + ARN_MAX_LENGTH);
|
||||
}
|
||||
return arn;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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
|
||||
@@ -2437,9 +2535,11 @@ public class IamQueryHandler {
|
||||
*/
|
||||
private Response handleGetServiceLastAccessedDetailsWithEntities(MultivaluedMap<String, String> params,
|
||||
String authorization) {
|
||||
ServiceLastAccessedJob job = serviceLastAccessedService.get(
|
||||
accountResolver.resolve(authorization), requireParam(params, "JobId"));
|
||||
String jobId = validatedJobId(params);
|
||||
String namespace = requireParam(params, "ServiceNamespace");
|
||||
validatePagingInputs(params);
|
||||
ServiceLastAccessedJob job = serviceLastAccessedService.get(
|
||||
accountResolver.resolve(authorization), jobId);
|
||||
// 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();
|
||||
@@ -2841,22 +2941,9 @@ public class IamQueryHandler {
|
||||
// it would be against AWS. Operations that model no MaxItems at all pass through whole.
|
||||
int limit = maxItemsModeled ? DEFAULT_MAX_ITEMS : items.size();
|
||||
if (maxItemsModeled) {
|
||||
String raw = params.getFirst("MaxItems");
|
||||
if (raw != null && !raw.isBlank()) {
|
||||
try {
|
||||
limit = Integer.parseInt(raw.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"The value " + raw + " at 'maxItems' is not a number.", 400);
|
||||
}
|
||||
if (limit < 1) {
|
||||
throw validationError(raw, "maxItems",
|
||||
"Member must have value greater than or equal to 1");
|
||||
}
|
||||
if (limit > MAX_MAX_ITEMS) {
|
||||
throw validationError(raw, "maxItems",
|
||||
"Member must have value less than or equal to " + MAX_MAX_ITEMS);
|
||||
}
|
||||
Integer asked = validatedMaxItems(params);
|
||||
if (asked != null) {
|
||||
limit = asked;
|
||||
}
|
||||
}
|
||||
if (from >= items.size()) {
|
||||
|
||||
@@ -3941,19 +3941,24 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
* that matches nothing gets {@code NoSuchEntity}. Different mistakes, different answers.
|
||||
*
|
||||
* <p>The API Reference publishes a length of 20 to 128 as well as the pattern, so both are
|
||||
* checked, and both are reported when both are broken, in the order it prints them. The
|
||||
* pattern message is the one recorded against AWS; the length wording is the form AWS uses for
|
||||
* a member carrying both bounds, which {@link #validatePassword} spells the same
|
||||
* way.
|
||||
* checked, and both are reported when both are broken, in the order it prints them.
|
||||
*
|
||||
* <p>Each message names the one bound the value broke, rather than both at once: every
|
||||
* recorded IAM length violation reads that way, "Member must have length less than or equal to
|
||||
* 64" for a user name over 64 and "Member must have length greater than or equal to 20" for an
|
||||
* ARN under 20, and a short id told about a 128-character ceiling it is nowhere near would be
|
||||
* reporting a constraint it did not break. The pattern message is the one recorded against
|
||||
* AWS.
|
||||
*/
|
||||
private void requireCredentialIdFormat(String credentialId) {
|
||||
String id = credentialId == null ? "" : credentialId;
|
||||
List<String> violations = new ArrayList<>();
|
||||
if (id.length() < SERVICE_CREDENTIAL_ID_MIN_LENGTH
|
||||
|| id.length() > SERVICE_CREDENTIAL_ID_MAX_LENGTH) {
|
||||
violations.add("Member must have length less than or equal to "
|
||||
+ SERVICE_CREDENTIAL_ID_MAX_LENGTH + " and greater than or equal to "
|
||||
if (id.length() < SERVICE_CREDENTIAL_ID_MIN_LENGTH) {
|
||||
violations.add("Member must have length greater than or equal to "
|
||||
+ SERVICE_CREDENTIAL_ID_MIN_LENGTH);
|
||||
} else if (id.length() > SERVICE_CREDENTIAL_ID_MAX_LENGTH) {
|
||||
violations.add("Member must have length less than or equal to "
|
||||
+ SERVICE_CREDENTIAL_ID_MAX_LENGTH);
|
||||
}
|
||||
if (!SERVICE_CREDENTIAL_ID_PATTERN.matcher(id).matches()) {
|
||||
violations.add("Member must satisfy regular expression pattern: [\\w]+");
|
||||
|
||||
+15
-6
@@ -47,6 +47,20 @@ public class ServiceLastAccessedService {
|
||||
this.jobs = new IamJobStore<>(jobs, ServiceLastAccessedJob::getJobId);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code AccessAdvisorUsageGranularityType}, checked on its own so the handler can read it
|
||||
* before it resolves the ARN the report is for: an enum AWS declares is refused whether or not
|
||||
* the entity exists. {@link #generate} checks it too, being reachable without the handler.
|
||||
*/
|
||||
public static void validateGranularity(String granularity) {
|
||||
if (granularity != null && !GRANULARITIES.contains(granularity)) {
|
||||
throw SmithyValidationErrors.validationError(
|
||||
"Value '" + granularity + "' at 'granularity' "
|
||||
+ "failed to satisfy constraint: Member must satisfy enum value set: "
|
||||
+ "[SERVICE_LEVEL, ACTION_LEVEL]");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Records a completed job for {@code arn} together with the report it produced, and returns
|
||||
* it. The report is computed by the caller and stored here, because AWS fixes a report at
|
||||
@@ -56,12 +70,7 @@ public class ServiceLastAccessedService {
|
||||
public ServiceLastAccessedJob generate(String accountId, String arn, String granularity,
|
||||
List<String> serviceNamespaces,
|
||||
List<ServiceLastAccessedEntity> entities) {
|
||||
if (granularity != null && !GRANULARITIES.contains(granularity)) {
|
||||
throw SmithyValidationErrors.validationError(
|
||||
"Value '" + granularity + "' at 'granularity' "
|
||||
+ "failed to satisfy constraint: Member must satisfy enum value set: "
|
||||
+ "[SERVICE_LEVEL, ACTION_LEVEL]");
|
||||
}
|
||||
validateGranularity(granularity);
|
||||
Instant now = Instant.now();
|
||||
// jobIDType is min 36 / max 36, which is exactly a UUID string.
|
||||
ServiceLastAccessedJob job = new ServiceLastAccessedJob(UUID.randomUUID().toString(), arn,
|
||||
|
||||
Reference in new issue
Block a user