Skip to content

fix(iam): validate PolicyArn shape on the three detach operations (#875) - #898

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/iam-detach-policy-arn-validation
Sep 15, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/iam-detach-policy-arn-validation

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

DetachUserPolicy, DetachRolePolicy and DetachGroupPolicy were the last policy-ARN operations without the #499 shape check. They now call iamValidatePolicyARN before authorize, exactly as their attach counterparts do, answering InvalidInput/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 answered NoSuchEntity/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 at DeletePolicy.

The real defect was the code, not the outcome. For the string read-objects:

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

Why 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. InvalidInput points 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."
  • 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.
  • 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.
  • The page publishes no pattern for PolicyArn. iamPolicyARNPattern is assembled from the API model's own component patterns, and iam_policy_arn.go:34-48 already 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 iamMalformedPolicyARNs shared by the attach and detach tests, and iamAttachTarget gains its detach/list counterpart 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_AttachAndDetachAgreeOnWhatIsAccepted asserts both directions reach the same status and the same error type for every case.

Tests

New emulator/iam_detach_policy_arn_test.go, all through StartTestServer and real wire calls; the attached/detached state is asserted through ListAttached{User,Role,Group}Policies rather 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 report AccessDenied for 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 detachUserPolicy alone fails 21 subtests; restoring it passes. They were not written against already-passing behaviour.

Verification

All nine gate commands, from the repo root:

gofmt -l . && go build ./... && go vet ./...          PASS
golangci-lint run ./...                                PASS
make test                                              exit 0
GOTOOLCHAIN=go1.26.6 make test                         exit 0
GOTOOLCHAIN=go1.26.6 make coverage                     exit 0
make docs-reference docs-reference-check \
     docs-versions authz-reference-check               PASS
npm --prefix docs run build                            exit 0
go vet -C test/e2e ./... && go test -C test/e2e ./...  exit 0

Coverage: 83.9% emulator (baseline held), 83.4% total (baseline 83.2%). No existing test needed relaxing.

Compatibility

A caller that sent a malformed PolicyArn to a detach now gets InvalidInput/400 where it got NoSuchEntity/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.

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

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit b61167b into main Sep 15, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/iam-detach-policy-arn-validation branch September 15, 2026 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant