Skip to content

fix(iam): an attach validates the shape of its PolicyArn (#499) - #647

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/iam-attach-policy-arn
Aug 15, 2026
Merged

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

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #499

AttachUserPolicy, AttachRolePolicy and AttachGroupPolicy appended whatever PolicyArn they were given to the entity's list unconditionally. A consumer could attach a bare policy name from the console, or an S3 bucket ARN, get a 200, and only discover the mistake later through GetPolicy answering NoSuchEntity for an ARN the attach had just accepted — two operations disagreeing about the same string.

The decision

#499 listed four paths and left the choice to be recorded. This ships (1) + (2): refuse a malformed ARN, warn on a well-formed one that resolves nowhere. The reasoning lives in emulator/iam_policy_arn.go's header so it is readable next to the code it explains.

Shape is refused. InvalidInput (400) — the code the model declares for all three operations — for an ARN that does not match a policy ARN. The pattern is built from the model's own accountIdType (\d{12}), policyPathType and policyNameType ([\w+=,.@-]+) rather than by eye, and the arnType 20–2048 length bound is checked first so a 4 KB body is refused on its size.

Existence is deliberately not required. Substrate bundles 52 of roughly 1,200 AWS managed policies. Refusing an ARN that resolves nowhere would refuse every attach of the other ~1,150 — attaching AmazonAthenaFullAccess would hard-fail where AWS succeeds. That trades a confusing success for a wrong failure, and the wrong failure is worse: it breaks working consumer code rather than merely failing to catch a typo. So a well-formed unresolvable ARN succeeds and logs at WARN naming it, with a different message for an unbundled AWS managed policy (the expected case) than for a customer-managed ARN no CreatePolicy ever created (a likelier real mistake).

The partition stays permissive. [a-z0-9-]+ covers aws, aws-cn, aws-us-gov, the aws-iso variants and whatever follows; refusing an unknown partition would refuse an ARN that is well-formed for a region substrate does not model.

All three operations, not two. AttachGroupPolicy ships in this same release, so applying the check to only the two pre-existing handlers would recreate the asymmetry #499 reports inside a brand-new operation.

Ordering. The shape check precedes authorization — it needs no state, and a malformed request is invalid whatever the caller may do. The existence warning runs after the entity lookup, so an attach that is going to fail with NoSuchEntity does not also warn about a policy nothing was attaching.

Paths not taken, recorded on the issue: seeding all ~1,200 ARNs without documents (3) would make GetPolicy resolve policies whose documents are empty; a seedable strict mode (4) would be a second behaviour for the same call with no consumer asking for it yet.

Tests

emulator/iam_policy_arn_test.go, tables driving all three operations:

  • An ARN GetPolicy resolves is never refused and never warns — IAM: AttachRolePolicy/AttachUserPolicy accept a PolicyArn that GetPolicy cannot resolve #499's hard acceptance criterion, asserted for a bundled ARN and a created one. Warning about a policy the emulator holds would train a consumer to ignore the warning.
  • A malformed ARN → InvalidInput 400: nine cases, each a mistake a consumer actually makes (bare name, wrong service, role/ instead of policy/, a region where IAM has none, slash-terminated with no name, 11- and 13-digit accounts).
  • AmazonAthenaFullAccess (real, unbundled, asserted unbundled so the test fails loudly if it ever gets added) succeeds with exactly one warning naming both the operation and the ARN.
  • A customer-managed ARN that resolves nowhere gets the other message, and not the unbundled-AWS one.
  • An unknown entity → NoSuchEntity with no warning at all.
  • The attached ARN reads back through ListAttachedUserPolicies, so the warning cannot quietly become a refusal.
  • Shapes AWS accepts and substrate does not otherwise model are accepted: three partitions, a nested path, the full policyNameType charset.
  • Over the query protocol with a real form body, because a check only reachable through a hand-built JSON body is a check no SDK caller reaches.

Compatibility

A fixture attaching a made-up, non-ARN-shaped PolicyArn starts failing with InvalidInput. One attaching a real-but-unbundled AWS managed ARN keeps working, and now says so in the log.

Verification

gofmt -l . clean; go build ./...; go vet ./...; golangci-lint run ./... → 0 issues.; make test (race) green. Coverage: iamValidatePolicyARN 100%, iamPolicyARNIsAWSManaged 100%, iamWarnUnresolvedPolicyARN 91.7%; total 82.6%.

AttachUserPolicy, AttachRolePolicy and AttachGroupPolicy appended whatever
ARN they were given, so a bare policy name or a bucket ARN produced a
success that GetPolicy then contradicted with NoSuchEntity.

All three now refuse a malformed policy ARN with InvalidInput 400, built
from the model's accountIdType/policyPathType/policyNameType patterns.
Existence is not required: substrate bundles 52 of ~1,200 AWS managed
policies, so refusing an unresolvable ARN would fail every attach of the
other ~1,150 where AWS succeeds. A well-formed ARN that resolves nowhere
succeeds and logs at WARN, with different messages for an unbundled AWS
managed policy and for a customer-managed ARN never created. The warning
runs after the entity lookup, so a NoSuchEntity attach does not also warn.
@codecov

codecov Bot commented Aug 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.59459% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/iam_policy_arn.go 92.85% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 947a8ab into main Aug 15, 2026
13 checks passed
@scttfrdmn
scttfrdmn deleted the fix/iam-attach-policy-arn branch August 15, 2026 00:57
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: AttachRolePolicy/AttachUserPolicy accept a PolicyArn that GetPolicy cannot resolve

1 participant