Skip to content

iam: DeletePolicy has no DeleteConflict check, so an attached policy deletes silently and every entity keeps a dangling ARN #853

Closed

Description

@scttfrdmn

DeletePolicy deletes a policy that is still attached, and every entity keeps an ARN pointing at nothing.

deletePolicy (emulator/iam_plugin.go:1382-1414) goes straight from the raw == nil → NoSuchEntity/404 check to p.state.Delete(...). There is no attachment check at all:

	if raw == nil {
		return iamErrorResponse("NoSuchEntity", ...
	}

	if err := p.state.Delete(goCtx, iamNamespace, iamPolicyKey(params.PolicyArn)); err != nil {
		return nil, fmt.Errorf("delete policy: %w", err)
	}

	return iamXMLEmptyResponse("DeletePolicy"), nil

API_DeletePolicy states the precondition in its second paragraph:

Before you can delete a managed policy, you must first detach the policy from all users, groups, and roles that it is attached to.

and publishes the refusal:

DeleteConflict — The request was rejected because it attempted to delete a resource that has attached subordinate entities. The error message describes these entities. HTTP Status Code: 409

Seven other IAM delete paths already check, so the pattern is in the same file: iam_plugin.go:369, :385, :671, :691, :905, :915, :2939. This one is the outlier.

What the silent delete leaves behind

An attach writes only the ARN onto {user,group,role}_policies:<acct>/<name>; nothing writes a back-reference onto the policy. So after a delete:

  • every entity's attached-policy list still names the deleted ARN, and ListAttachedUserPolicies still reports it;
  • the authorization evaluator loads no document for it, so the entity silently loses the permissions the policy granted — a test asserting a deny now passes for the wrong reason;
  • GetPolicy on the ARN answers NoSuchEntity, so the state is self-contradictory across two operations;
  • and since iam: GetPolicy reports the stored AttachmentCount (always 0) while ListPolicies derives it #847 AttachmentCount is derived from those same lists, a re-created policy of the same ARN inherits the stale attachments.

That last point is why this is worth doing after #847 rather than before: the count now makes the inconsistency observable through the API instead of only in state.

Two smaller findings on the same handler

  • No ARN shape validation. DeletePolicy publishes InvalidInput/400 and PolicyArn carries the usual 20–2048 length constraint, but the handler passes any non-empty string to iamPolicyKey. iamValidatePolicyARN (emulator/iam_policy_arn.go) is the existing check, already used by the three attach operations and, as of iam: Put{User,Role}PermissionsBoundary accepts a policy ARN that names no policy #846, by both Put*PermissionsBoundary.
  • A bundled AWS managed ARN answers NoSuchEntity. The handler reads state only, so DeletePolicy on arn:aws:iam::aws:policy/PowerUserAccess reports the policy does not exist while GetPolicy on the same ARN returns it. What AWS answers for a delete of an AWS managed policy is not published on the page, so the right code here needs its own sourcing before anything is implemented; recorded so the two operations' disagreement is not discovered again from scratch.

Acceptance criteria

  • DeletePolicy answers DeleteConflict/409 when the policy is attached to any user, group or role, with a message naming the attached entities as AWS's description says it does.
  • The refusal is derived from the same three {user,group,role}_policies: prefixes iamPolicyAttachmentCounts reads, so a detach makes the delete succeed immediately and the two cannot disagree about what "attached" means.
  • A malformed PolicyArn answers InvalidInput/400 through iamValidatePolicyARN, matching the attach operations.
  • Tests: attach → delete refuses with 409 and the policy is still readable through GetPolicy; detach → delete succeeds; a policy attached to a group only is also refused (all three kinds, since one prefix scanned would pass a two-kind test); and AttachmentCount reads 0 for the deleted-then-recreated ARN rather than inheriting stale attachments.
  • What AWS answers for a delete of a bundled AWS managed ARN is either sourced from an AWS page or recorded as substrate's reading, not guessed.

Found while implementing #847 and #846 (v0.115.0); not fixed there, to keep that PR to the two reporting defects it is about.

Activity

  1. added this to the v0.116.0 milestone on Sep 14, 2026
  2. added
    type: bug
    on Sep 14, 2026
  3. scttfrdmn commented on Sep 15, 2026

    @scttfrdmn
    OwnerAuthor

    Verified against main at 3503416 before implementing. Every substantive claim in the body holds. Five corrections and one addition, recorded here so the PR does not carry them silently.

    Corrections

    1. Line numbers have all drifted. deletePolicy is at emulator/iam_plugin.go:1430-1462, not :1382-1414. The body's quoted code is verbatim-correct and there is still no attachment check and no ARN check.

    2. The seven sibling sites are exact in count, but every line number moved. All seven DeleteConflict/409 arms are still there:

    Site Operation Refusal
    iam_plugin.go:373 DeleteUser attached policies — bare message
    iam_plugin.go:390 DeleteUser group memberships — names them
    iam_plugin.go:679 DeleteRole attached policies — bare message
    iam_plugin.go:700 DeleteRole instance profiles — names them
    iam_plugin.go:922 DeleteGroup users in group — names them
    iam_plugin.go:931 DeleteGroup attached policies — bare message
    iam_plugin.go:3063 DeleteInstanceProfile roles — bare message

    :2939 in the body is :3063 today.

    3. Four of those seven do not name the entities, so "as the seven sibling arms already do" overstates the precedent by half. Every arm refusing over attached policies uses the bare "Cannot delete entity, must detach all policies first."; only the three refusing over a membership name what to remove, and DeleteRole's carries the rationale explicitly: "The reference says 'The error message describes these entities', so name them: a caller has to know which profiles to detach from." This issue's AC 1 is right and the precedent for it is those three, not all seven. Worth stating because the naming is the part that takes work here — see 4.

    4. iamPolicyAttachmentCounts cannot satisfy AC 1 and AC 2 together as written. It reads exactly the right three prefixes (iam_list_policies.go:213-234), but it accumulates counts[arn]++ and discards which entity and which kind each membership came from, so it can answer whether a policy is attached and not to what. AC 2's requirement — that the refusal and the count cannot disagree about what "attached" means — is met by a sibling helper over the same three iamAttachedPoliciesPrefix keys and the same loadPolicyList, returning the entity names per kind. Not by changing iamPolicyAttachmentCounts, whose signature five callers depend on and whose per-request hoisting is load-bearing for GetPolicy and ListPolicies.

    5. ListEntitiesForPolicy is not implemented, and that raises the stakes on AC 1. A repo-wide search finds it only in the generated authz data — no dispatch arm, no handler. API_DeletePolicy's own description tells the caller to "use ListEntitiesForPolicy" to find what to detach, so in substrate the 409 message is the only way a caller can learn it. That is why naming the entities is not a nicety here.

    Addition: AC 4's last clause is discharged structurally, not by an assertion

    "AttachmentCount reads 0 for the deleted-then-recreated ARN rather than inheriting stale attachments" becomes true by construction once the refusal lands: a successful delete now implies zero attachments, so there is nothing stale left to inherit. The test is still worth writing — it asserts the invariant rather than the mechanism — but it is a consequence of AC 1, not separate work.

    The two things being recorded rather than implemented

    The version precondition is vacuous. AWS's description requires deleting all non-default versions first, but publishes no error code for it — DeleteConflict's own text is about "attached subordinate entities" — and substrate models exactly one version per policy (iam_policy_versions.go:21-24). So it gets a comment, not code.

    AC 5, the bundled AWS-managed ARN: recorded as substrate's reading, because no AWS source states the code. API_DeletePolicy is silent. The strongest published statement is "You cannot change the permissions defined in AWS managed policies" (Managed policies and inline policies), which establishes that the customer does not administer them but publishes no code and does not mention deletion. So the guard ships stated as substrate's reading, using the iamPolicyARNIsAWSManaged predicate that already exists (iam_policy_arn.go:72-74) — what matters for this issue is that DeletePolicy stops answering NoSuchEntity for an ARN GetPolicy resolves from the catalog (iam_plugin.go:1402), i.e. that the two operations stop contradicting each other about whether the policy exists.

    AC 3 confirmed as stated

    iamValidatePolicyARN exists and is applied at four call sites serving five operations — AttachUserPolicy (iam_plugin.go:1002), AttachRolePolicy (:1140), AttachGroupPolicy (iam_groups.go:174), and putPermissionsBoundary (:2232), which both PutUserPermissionsBoundary and PutRolePermissionsBoundary route through. DeletePolicy passes any non-empty string straight to iamPolicyKey. The body's claim is exact.

    Also noted for a separate issue rather than folded in: the three detach operations — detachUserPolicy (:1044), detachRolePolicy (:1182), detachGroupPolicy (iam_groups.go:216) — do not validate the ARN either, so a malformed one is accepted and silently matches nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions