Repository navigation
Description
What is wrong
DetachUserPolicy (emulator/iam_plugin.go:1044), DetachRolePolicy (:1182) and DetachGroupPolicy (emulator/iam_groups.go:216) accept any non-empty PolicyArn and pass it straight to the removal. None calls iamValidatePolicyARN.
Their attach counterparts all do — AttachUserPolicy (:1002), AttachRolePolicy (:1140), AttachGroupPolicy (iam_groups.go:174) — as do both Put*PermissionsBoundary through their shared putPermissionsBoundary (:2232), and as of #853 so does DeletePolicy. The three detaches are the last policy-ARN operations that do not.
Why it matters
A detach removes the ARN from the entity's list by string comparison. A malformed ARN matches nothing, so:
DetachUserPolicywith a bare policy name (read-objectsrather thanarn:aws:iam::123456789012:policy/read-objects) answers success and detaches nothing. The caller believes the permission is gone; the entity still has it.- A consumer's teardown that gets the ARN form wrong leaves every attachment in place and reports no problem — and after iam: DeletePolicy has no DeleteConflict check, so an attached policy deletes silently and every entity keeps a dangling ARN #853 the subsequent
DeletePolicynow refuses withDeleteConflict, so the failure surfaces one operation later on the resource that was not the mistake. - The asymmetry is itself the defect class: the attach of a malformed ARN is refused, the detach of the same string succeeds. A round trip that substrate accepts in one direction and silently no-ops in the other cannot be reasoned about.
Whether a well-formed ARN that is not currently attached should be refused is a separate question and is not what this asks for. AWS publishes NoSuchEntity for DetachUserPolicy, but substrate's attach deliberately does not require existence (#499, because substrate bundles 52 of roughly 1,200 AWS managed policies), and requiring the attachment to exist is a third thing again. This issue is scoped to shape only, matching what the attach operations validate.
What to do
- Call
iamValidatePolicyARN(emulator/iam_policy_arn.go:57) in each of the three handlers, beforeauthorize, exactly as the attach counterparts do, answeringInvalidInput/400 with the returned message. - Record in
docs/services.mdwhether detaching a well-formed-but-unattached ARN stays a success, so the decision is stated rather than implied by the absence of a check. AWS publishesNoSuchEntityfor the operation; substrate's existing IAM: AttachRolePolicy/AttachUserPolicy accept a PolicyArn that GetPolicy cannot resolve #499 reasoning argues against requiring existence, and those two need reconciling in prose either way.
Acceptance criteria
- A bare policy name, an ARN for another service, an ARN with no
policy/segment, a 19-character string and a 2049-character one each answerInvalidInput/400 from all three detach operations. - A well-formed ARN that is attached still detaches, asserted through
ListAttachedUserPolicies(and the group and role equivalents) rather than through state. - The behaviour for a well-formed ARN that is not attached is asserted and recorded in
docs/services.md, whichever way it is decided. - The tests assert the round trip: attaching a malformed ARN and detaching the same string cannot end in disagreement about whether it was accepted.
Surfaced while implementing #853, which added the same check to DeletePolicy and left these three noted rather than folded in.
Activity
fix(iam): an attached policy cannot be deleted (#853) (#876)
scttfrdmn commentedon Sep 15, 2026
Verified against the tree at
dcb5dddbefore implementing, and the stated consequence is false as present tense. The defect is real but it is a different one, and narrower.What is actually there
All three detach handlers already refuse an ARN that is not attached.
detachUserPolicy(emulator/iam_plugin.go:1068-1081),detachRolePolicy(:1207-1219) anddetachGroupPolicy(emulator/iam_groups.go:240-252) each walk the attached list, trackfound, and answer:So the issue's central example — "
DetachUserPolicywith a bare policy name … answers success and detaches nothing. The caller believes the permission is gone; the entity still has it" — does not happen. A bare name is not in the list,foundstays false, and the call answersNoSuchEntity/404. There is no silent no-op, and the two consequences built on top of it are void with it: nothing is left in place while reporting no problem, and nothing surfaces one operation later atDeletePolicy.The defect that is real
The asymmetry with the attach counterparts survives, as a wrong error code carrying a misleading message. 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/400API_DetachUserPolicypublishesInvalidInput/400 as "an invalid or out-of-range value was supplied for an input parameter" and givesPolicyArnLength Constraints: Minimum length of 20. Maximum length of 2048 — so a 19-character string is out of range on the published constraint alone, before any question of shape. (The page publishes no pattern forPolicyArn; substrate'siamPolicyARNPatternis assembled from the API model's own component patterns, whichiam_policy_arn.go:34-48already records as its provenance for the attach side. Same standing here.)Why the wrong code matters beyond tidiness: the two errors tell the caller to look in different places.
NoSuchEntity"the policy is not attached" points at the attachment — so a consumer handling it goes and lists attachments, finds the policy genuinely attached under its real ARN, and has been told the opposite of its actual mistake, which is the ARN string it typed.InvalidInputpoints at the parameter.The reconciliation the issue asks for is simpler than framed
Acceptance criterion 3 asks what happens to a well-formed but unattached ARN and for the answer to be reconciled with #499. That behaviour already exists and already matches AWS:
NoSuchEntity/404, which is precisely whatAPI_DetachUserPolicypublishes. And there is no tension with #499 to resolve, because the two requirements are about different things — #499 argues against requiring the policy to exist (substrate bundles 52 of roughly 1,200 managed policies, so refusing an unresolvable ARN would break the common case). A detach requires the attachment to exist, which is a fact substrate does hold in full and which AWS publishes an error for. Nothing needs deciding; it needs writing down, which is the part of the criterion that stands.Revised scope
Unchanged in substance from "What to do", minus the reconciliation:
iamValidatePolicyARNin the three handlers beforeauthorize, answeringInvalidInput/400, exactly as the attach counterparts do atiam_plugin.go:1002,:1140andiam_groups.go:174.docs/services.mdthat a well-formed-but-unattached ARN answersNoSuchEntity/404 per the published error, and that this is not in tension with IAM: AttachRolePolicy/AttachUserPolicy accept a PolicyArn that GetPolicy cannot resolve #499 — for the reason above.Criterion 4 is worth keeping exactly as written even though the round trip no longer disagrees: after the fix both directions answer
InvalidInput/400 on the same string, and asserting that is what stops the pair drifting apart again.fix(iam): validate PolicyArn shape on the three detach operations (#875…