Skip to content

iam: the three DetachPolicy operations do not validate PolicyArn, so a malformed one succeeds and detaches nothing #875

Closed

Description

@scttfrdmn

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:

  • DetachUserPolicy with a bare policy name (read-objects rather than arn: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 DeletePolicy now refuses with DeleteConflict, 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, before authorize, exactly as the attach counterparts do, answering InvalidInput/400 with the returned message.
  • Record in docs/services.md whether 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 publishes NoSuchEntity for 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 answer InvalidInput/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

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

    @scttfrdmn
    OwnerAuthor

    Verified against the tree at dcb5ddd before 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) and detachGroupPolicy (emulator/iam_groups.go:240-252) each walk the attached list, track found, and answer:

    	if !found {
    		return iamErrorResponse("NoSuchEntity",
    			"The policy is not attached to the specified entity.",
    			http.StatusNotFound), nil
    	}

    So the issue's central example — "DetachUserPolicy with 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, found stays false, and the call answers NoSuchEntity/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 at DeletePolicy.

    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:

    Today AWS
    AttachUserPolicy InvalidInput/400, "ARN read-objects is not valid." InvalidInput/400
    DetachUserPolicy NoSuchEntity/404, "The policy is not attached to the specified entity." InvalidInput/400

    API_DetachUserPolicy publishes InvalidInput/400 as "an invalid or out-of-range value was supplied for an input parameter" and gives PolicyArn Length 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 for PolicyArn; substrate's iamPolicyARNPattern is assembled from the API model's own component patterns, which iam_policy_arn.go:34-48 already 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. InvalidInput points 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 what API_DetachUserPolicy publishes. 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:

    • Call iamValidatePolicyARN in the three handlers before authorize, answering InvalidInput/400, exactly as the attach counterparts do at iam_plugin.go:1002, :1140 and iam_groups.go:174.
    • Record in docs/services.md that a well-formed-but-unattached ARN answers NoSuchEntity/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.
    • Acceptance criteria 1, 2 and 4 stand as written. Criterion 3 becomes "assert and record the existing behaviour" rather than "decide it".

    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.

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