Skip to content

fix(iam): resolve a signed request to the principal it actually is (#411) - #570

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/411-principal-resolution
Aug 5, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/411-principal-resolution

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

First of four PRs for v0.92.0. This one makes a signed request resolve to the principal it actually is; the evaluator fixes, CloudFormation's service role, and the docs follow in B, C and D.

Refs #411.

The defect

buildCallerARN derived a principal ARN from the access key ID, producing arn:aws:iam::123456789012:user/AKIATEST12345678901. Nothing is ever stored under that name — IAM writes policies under user_policies:<UserName> — so a user alice holding an inline sqs:* Allow was:

  • allowed when evaluated as arn:aws:iam::123456789012:user/alice
  • denied when evaluated as arn:aws:iam::123456789012:user/AKIATEST…

Authorization therefore could not find any real principal's policies even on the one path where it ran. #411 has been deferred six times as "wire a principal into a test"; it was not reachable in principle.

The fix

A caller is resolved from state, in resolvePrincipal:

  1. iam:accesskey:<id> → IAMAccessKey.UserName → arn:aws:iam::<acct>:user/<UserName>. This is the record CreateAccessKey has always written.
  2. sts:session:<id> → STSSessionCredentials.PrincipalARN, the assumed-role ARN AssumeRole mints. Substrate wrote that key from the beginning and nothing had ever read it, which is why STS session credentials authorized as nothing in particular.
  3. Otherwise no principal.

Why this is decoupled from CredentialRegistry

The obvious fix — wire Credentials into the server so it can identify callers — is the trap. VerifySigV4 runs whenever Credentials != nil, and an access key the registry does not hold is rejected with InvalidClientTokenId (403). AKIAIOSFODNN7EXAMPLE is not registered, so a server that identifies callers that way is a server that 403s the README, the testing guide and every e2e check.

So the two jobs stay split, the same separation as #529's state-vs-wire types: the registry answers "which account, and is this signature valid"; state answers "which principal". Reading state costs one Get on a request that carries an Authorization header, and refuses nothing.

An STS session's account

extractAccount recognizes only an AKIA prefix as substrate's test account, so an ASIA session key was filed under 000000000000 — a different partition from the resources the caller who assumed the role had just created. The session record names the account it was issued for; that is now adopted, unless a wired registry already spoke.

sts:GetCallerIdentity and sts:GetSessionToken need no permissions

Found by running this change, not by reading a reference. With a real principal resolving for the first time, aws sts get-caller-identity as a policy-less IAM user returned AccessDeniedException. AWS documents both operations as requiring none:

GetCallerIdentity — "No permissions are required to perform this operation. If an administrator attaches a policy to your identity that explicitly denies access to the sts:GetCallerIdentity action, you can still perform this operation. Permissions are not required because the same information is returned when access is denied."

GetSessionToken — "No permissions are required for users to perform this operation… You cannot use policies to control authentication operations."

A sweep of every botocore service model finds the phrase "no permissions are required" on these two operations and nothing else, so the exemption is that pair rather than a general carve-out. It was unreachable before this PR: with every caller resolving to no principal, the evaluator never ran.

What changes for a consumer

A call made with an access key minted by CreateAccessKey is now authorized against that user's policies, and can return AccessDeniedException where it previously succeeded. That is real IAM behaviour and the point of the release — the "forgot a permission / policy too narrow" class that spore-host/spawn#70 needed a paid real-AWS smoke to catch.

Everything else is unchanged. Existence in state is the opt-in, so there is no configuration flag: a key belonging to no IAM entity resolves to no principal and is enforced against nothing. That covers AKIAIOSFODNN7EXAMPLE, test/test, the built-in test key, a deleted key, every in-process Client call, and every test that does not create an IAM principal. An assumed-role principal is also not yet enforced — that fail-open is PR B.

Tests

emulator/principal_resolution_test.go, table-driven, no wall-clock or network dependency. The two positive gates were run against unmodified main in a worktree and both fail there:

  • an access key minted by CreateAccessKey reaches the plugin as arn:aws:iam::123456789012:user/alice — asserted on the ARN, because a test that only checked non-nil passed before the fix;
  • an ASIA key from AssumeRole reaches it as arn:aws:sts::123456789012:assumed-role/worker/sess1, in account 123456789012;
  • four unenforced-caller regression gates (AKIAIOSFODNN7EXAMPLE, the built-in test key, a never-minted key, no header) resolve to no principal and still succeed — these pass on main too, which is what makes them regression gates;
  • a deleted access key stops resolving;
  • six arms an HTTP request cannot produce (nil state, corrupt records, a record naming no owner, a session naming no principal) each resolve to no principal rather than one built from half a record;
  • the no-permissions-required pair is allowed for a policy-less user while sts:AssumeRole is denied, so the exemption is that pair and not all of STS.

Mutation testing

Ten mutants, all CAUGHT. Two earlier attempts were BROKEN (a perl s/// that choked on slashes in the pattern, and a grep -c that counted lines rather than occurrences) and were rewritten before being counted — a BROKEN mutant measures nothing.

Mutant Verdict
IAM branch returns buildCallerARN (the fixed bug, restored) CAUGHT
IAM branch skipped entirely CAUGHT
STS branch dropped CAUGHT
session account not adopted CAUGHT
owner-less key record still resolves CAUGHT
resolution recoupled to registryHit CAUGHT
registry fallback applied unconditionally CAUGHT
no-permissions exemption removed CAUGHT
exemption keyed on req.Operation instead of the action CAUGHT
exemption widened to all of sts CAUGHT

The unconditional-fallback mutant fails at the setup call with a 403, which is the mechanism worth naming: it makes AKIAIOSFODNN7EXAMPLE an enforced principal with no policies, so it denies everything. That is exactly the hazard the registry-hit gate exists for.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... → 0 issues; make test (race) green; make docs-reference docs-reference-check docs-versions clean (65 plugins, no change); go vet -C test/e2e ./... and go test -C test/e2e ./... green.

Live against substrate server, with the AWS CLI:

$ AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE aws s3 mb s3://plain
make_bucket: plain                                    # non-breaking gate

$ aws iam create-user --user-name alice
$ aws iam create-access-key --user-name alice          # AKIADBUC6X6PVEACB4H1H
$ AWS_ACCESS_KEY_ID=AKIADBUC… aws sts get-caller-identity
{ "UserId": "alice", "Arn": "arn:aws:iam::123456789012:user/alice" }

$ AWS_ACCESS_KEY_ID=AKIADBUC… aws sqs list-queues
AccessDeniedException: User: arn:aws:iam::123456789012:user/alice is not
authorized to perform: sqs:ListQueues on resource: *

$ aws iam put-user-policy --user-name alice --policy-name ro \
    --policy-document '{"Statement":[{"Effect":"Allow","Action":"sqs:ListQueues","Resource":"*"}]}'
$ AWS_ACCESS_KEY_ID=<new key> aws sqs list-queues
{ "QueueUrls": [] }                                   # #411's acceptance, over HTTP
$ AWS_ACCESS_KEY_ID=<new key> aws sqs create-queue --queue-name q
AccessDeniedException: … not authorized to perform: sqs:CreateQueue

$ aws sts assume-role --role-arn arn:aws:iam::123456789012:role/worker \
    --role-session-name s1
$ AWS_ACCESS_KEY_ID=ASIA… aws sts get-caller-identity
{ "Arn": "arn:aws:sts::123456789012:assumed-role/worker/s1" }   # right account

$ AWS_ACCESS_KEY_ID=ASIA… aws sqs list-queues
{ "QueueUrls": [] }         # still fails open — PR B

The last one is the fail-open this release's PR B closes: loadPoliciesForPrincipal errors on any entity type outside user/role and CheckAccess fails open on that error, so an assumed-role is allowed with no policies at all — while IAMPlugin.authorize denies in the same case. Two loaders, two opposite answers, one ARN.

Not in this PR

PluginRegistry.RouteRequest still never calls CheckAccess, so a CloudFormation stack's resource calls remain unauthorized. That is PR C, together with #562's RoleARN.

)

The server derived a caller's principal ARN from its access key ID, yielding
arn:aws:iam::123456789012:user/AKIATEST12345678901 — a name nothing is ever
stored under. A user alice holding an inline sqs:* Allow was allowed as
user/alice and denied as user/AKIATEST…, so authorization could not find any
real principal's policies even where it ran. #411's acceptance ("create a role
with a scoped policy, make a call as that role") was unreachable in principle,
not merely unwired.

A caller is now resolved from state: iam:accesskey:<id> names the user whose
policies apply — the record CreateAccessKey has always written — and
sts:session:<id> names the assumed-role ARN AssumeRole mints, a key substrate
wrote from the beginning and nothing had ever read.

Resolution is deliberately independent of ServerOptions.Credentials. That
registry also gates SigV4 verification, and a key absent from it is rejected
with InvalidClientTokenId; identifying callers through it would have 403'd every
credential substrate documents. Reading state costs one Get on a request that
carries an Authorization header, and refuses nothing.

An STS session also now carries the account it was issued for: account
resolution recognizes only an AKIA prefix as the test account, so an ASIA
session key was filed under 000000000000 — a different partition from the
resources the caller who assumed the role had just created.

Two STS operations are exempted from evaluation, which is only reachable now.
AWS documents GetCallerIdentity as answering even against an explicit Deny
(a denial returns the same information) and GetSessionToken as authenticating
via MFA, which policies cannot gate. A sweep of every botocore service model
finds the phrase on these two and nothing else. Before this change no caller
resolved to a principal, so the evaluator never ran and neither was denied; the
first credential naming a real IAM user had `aws sts get-caller-identity`
return AccessDeniedException.

A key belonging to no IAM entity resolves to no principal and is enforced
against nothing: existence in state is the opt-in, so no flag turns this on and
every caller that never touched IAM is unaffected.

Ten mutants CAUGHT, including the fixed bug itself (the IAM branch renamed to
buildCallerARN), the STS branch dropped, the session account not adopted, and
the registry fallback applied unconditionally — which 403s AKIAIOSFODNN7EXAMPLE
at the first call, the hazard the registry-hit gate exists for.
@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

1 participant