Repository navigation
fix(iam): validate PolicyArn shape on the three detach operations (#875) - #898
Merged
Merged
Conversation
DetachUserPolicy, DetachRolePolicy and DetachGroupPolicy were the last policy-ARN operations without the #499 shape check, so a malformed ARN answered NoSuchEntity/404 "the policy is not attached" where the attach counterpart answered InvalidInput/400 for the same string. The 404 pointed the caller at the attachment when the mistake was the ARN it typed. #875's stated consequence -- that a malformed ARN detached silently and successfully -- was false as present tense; all three already refused an unattached ARN. Corrected on the issue before implementing. A well-formed but unattached ARN still answers NoSuchEntity/404, which is also published, and is now asserted so the new check cannot turn it into a 400. That is not in tension with #499: substrate holds every attachment in full, so absence is reportable, while policy existence is not. The malformed-ARN table is now shared by the attach and detach tests, with a test asserting both directions reach the same verdict.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DetachUserPolicy,DetachRolePolicyandDetachGroupPolicywere the last policy-ARN operations without the #499 shape check. They now calliamValidatePolicyARNbeforeauthorize, exactly as their attach counterparts do, answeringInvalidInput/400.#875's stated consequence was false as present tense
Corrected on the issue before writing any code (comment). The report was that a malformed ARN "answers success and detaches nothing. The caller believes the permission is gone; the entity still has it." It did not. All three handlers already walked the attached list, tracked
found, and answeredNoSuchEntity/404 —iam_plugin.go:1068-1081,:1207-1219,iam_groups.go:240-252. There was no silent no-op, and the two consequences built on top of it are void with it: nothing was left in place while reporting no problem, and nothing surfaced one operation later atDeletePolicy.The real defect was the code, not the outcome. For the string
read-objects:AttachUserPolicyInvalidInput/400, "ARN read-objects is not valid."InvalidInput/400DetachUserPolicyNoSuchEntity/404, "The policy is not attached to the specified entity."InvalidInput/400InvalidInput/400Why that matters beyond tidiness: the two errors send the caller to different places.
NoSuchEntity"the policy is not attached" points at the attachment, so a consumer handling it lists attachments, finds the policy genuinely attached under its real ARN, and has been told the opposite of its actual mistake — which was the ARN string it typed.InvalidInputpoints at the parameter.Provenance
Verified from
API_DetachUserPolicy(fetched, not recalled):InvalidInput/400 is published, described as "The request was rejected because an invalid or out-of-range value was supplied for an input parameter."PolicyArnLength Constraints: Minimum length of 20. Maximum length of 2048. So a 19-character string is out of range on the published constraint alone.NoSuchEntity/404 is published, "referenced a resource entity that does not exist. The error message describes the resource." — which is the behaviour kept for a well-formed-but-unattached ARN.PolicyArn.iamPolicyARNPatternis assembled from the API model's own component patterns, andiam_policy_arn.go:34-48already records that as its provenance for the attach side; the same standing carries here. No new unverified claim is introduced by this PR.A well-formed but unattached ARN still answers NoSuchEntity/404
Unchanged, and now asserted rather than incidental, so the new check cannot quietly turn every unattached detach into a 400.
Acceptance criterion 3 asked for this to be reconciled with #499's refusal to require the policy to exist. There is nothing to reconcile, because the two are about different facts, and that is what the PR records instead. Substrate holds every attachment it was told about, in full, so an absent attachment is a fact it can report exactly. Policy existence is a fact it cannot report, bundling 52 of roughly 1,200 managed policies — which is why the attach warns instead of refusing. A detach requires the attachment, never the policy.
One table, not two
The malformed-ARN cases move to a package-level
iamMalformedPolicyARNsshared by the attach and detach tests, andiamAttachTargetgains itsdetach/listcounterpart names so the pairing is structural rather than positional.That is criterion 4 taken literally. Two tables would let a case be added to one side only — which is how the pair came to disagree in the first place.
TestDetachPolicy_AttachAndDetachAgreeOnWhatIsAcceptedasserts both directions reach the same status and the same error type for every case.Tests
New
emulator/iam_detach_policy_arn_test.go, all throughStartTestServerand real wire calls; the attached/detached state is asserted throughListAttached{User,Role,Group}Policiesrather than from state, per #765.RefusesAMalformedARN— ten cases × three operations.AttachAndDetachAgreeOnWhatIsAccepted— the round-trip criterion.AWellFormedAttachedARNStillDetaches— the check refuses a shape without refusing the operation.AWellFormedUnattachedARNAnswersNoSuchEntity— records the behaviour above.TheShapeCheckPrecedesAuthorization— mirrors the attach ordering test; swapping the two would reportAccessDeniedfor a request invalid regardless.OverTheQueryProtocol— a form body, since a check that only fires for hand-built JSON is one no SDK caller reaches.Verified the tests catch the defect: removing the guard from
detachUserPolicyalone fails 21 subtests; restoring it passes. They were not written against already-passing behaviour.Verification
All nine gate commands, from the repo root:
Coverage: 83.9% emulator (baseline held), 83.4% total (baseline 83.2%). No existing test needed relaxing.
Compatibility
A caller that sent a malformed
PolicyArnto a detach now getsInvalidInput/400 where it gotNoSuchEntity/404. Any such caller was sending a string AWS refuses, and was already refused — only the code and the message change. A well-formed ARN behaves exactly as before in both the attached and the unattached case.Closes #875.