Skip to content

fix: --account / SPORE_ACCOUNT is enforced, not just accepted (#1118) - #1175

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/1118-account-guard
Oct 19, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/1118-account-guard

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1118

What was wrong

--account ("Expected AWS account ID (optional guard)"), SPORE_ACCOUNT and account = in ~/.config/spore/config.toml all resolve into SharedConfig().Account. Only spawn doctor read that value, so spawn --account 111111111111 launch … with credentials for another account launched anyway.

Where the check lives, and why there

pkg/config.CheckAccount(ctx, cfg) (new file pkg/config/account_guard.go) runs at the credential roots, not in a cobra hook:

  • spawnaws.NewClientWithRegion. This covers NewClient and cmd's loadAWSConfig.
  • pkg/config.LoadComputeAWSConfig, always.
  • pkg/config.LoadInfraAWSConfig, when infra resolves through the user's own profile (see below).
  • The other LoadDefaultConfig sites linked into the spawn binary: pkg/dns.NewClient, the pkg/cost pricer, the X-Ray exporter, pkg/provider (ec2/local), pkg/registry and pkg/pipeline. On an instance no expected account is set, so the guard does nothing there.

A config is checked before it is returned, so no command can make an AWS call with refused credentials, mutating or not. A command that never builds a config (version, completion, help) never calls STS. A root PersistentPreRunE (what truffle and lagotto do) would make SPORE_ACCOUNT in a shell profile cost an STS round trip on every spawn version. It also couldn't check the compute profile, which is a different credential set from the shared one.

Semantics (matched to truffle#208 / lagotto#180)

  • No expected account means no STS call.
  • A mismatch is refused with --account 111111111111: the current AWS credentials belong to account 222222222222; refusing to continue (check --profile / SPORE_PROFILE / AWS_PROFILE). The prefix names the source: --account, SPORE_ACCOUNT or the config file. The error is typed (*AccountMismatchError).
  • It fails closed: an STS error or an empty account is refused (… could not determine the AWS account of the current credentials: …).
  • One GetCallerIdentity per access key per process, cached. STS gets us-east-1 when no region is set.
  • Differences from truffle/lagotto: spawn also enforces SPORE_ACCOUNT and the config file. Those two enforce only the flag, and truffle#206 leaves that question open suite-wide. spawn doctor already presented all three sources as the guard, so enforcing just one would have been a new inconsistency.

The two-account model

LoadInfraAWSConfig with a dedicated infra profile (SPAWN_INFRA_PROFILE, default spore-host-infra) is spawn's control-plane account, a different account by design. Guarding it would refuse every sweep/schedule/pipeline/cancel command that pins --account. It is checked only when infra resolves through the same profile as everything else (SPAWN_INFRA_PROFILE="", or equal to the shared profile), because then it is the user's account. Compute, where instances launch, is always checked. This is a judgement call; please push back if you want infra guarded unconditionally.

spawn doctor

A refusal used to show up as "AWS credentials ✗ … run aws login", which is misleading. It now renders as credentials ✓ and account ✗ with both IDs and the source, then stops instead of probing an account the user said they didn't mean. I checked this by reading the code only: doctor os.Exit(1)s on failure, so it has no command-level test here.

Gates

  1. cmd/account_guard_test.go (existing fakes: isolateAWSEnv, recordAWS, runRootForTest, burstArgs, assertBurstRegion):
    • TestAccountGuardRefusesBeforeAnyAWSCall: spawn --account 111… burst … (and the SPORE_ACCOUNT variant) with the lookup stubbed to 222… returns an AccountMismatchError with the right want/got/source, and the recorder sees zero AWS requests. burst is the command TestBurstHonoursTheRegionFlag already proves reaches RunInstances through this recorder.
    • TestAccountGuardLetsAMatchThrough: the same burst with a matching account reaches RunInstances.
    • TestOfflineCommandsDoNotCallSTS: version, completion bash and help with --account make no lookup. Subcommand help is left out deliberately, because launch --help makes cobra add a --help flag to launchCmd's flag set and that trips TestEveryLaunchFlagHasAWindowsRule.
  2. account_guard_coverage_test.go (module root): the discovery gate. It runs go list -deps -json . for each release GOOS (linux/darwin/windows) to find every spawn-module package linked into the spawn binary. In each, every SDK config.LoadDefaultConfig call must either sit in a function that also calls CheckAccount, or pass config.WithCredentialsProvider. The second case is explicit credentials, i.e. the sweep code's assumed-role credentials minted from an already-guarded config. Calls are matched by resolved import path, not text. The gate fails if it finds no loads at all.
    • Why per load site rather than per cobra command: a command reaches AWS through a chain of helpers, so knowing which commands construct a client needs a call graph. Every command's credentials have to come from one of these load sites, though, so guarding all of them covers every command, including ones added later, without enumerating any.
    • Limit: the gate does not cover an aws.Config assembled by hand (aws.Config{Credentials: …} / aws.NewConfig()) in CLI code. I found none in non-test CLI code.
  3. pkg/config/account_guard_test.go: all three sources refuse a mismatch, a match passes, it fails closed on an STS error or an empty account, no pin means no call, compute is guarded, and infra is guarded only through the shared profile. The real STS lookup (not the seam) is GetCallerIdentity against an STS host and is cached per access key.

Watched failing against reverts that compile:

  • A. DNS guard replaced by if err := error(nil); err != nil && spawnconfig.CheckAccount != nil {: the coverage gate FAILS at pkg/dns/client.go:103.
  • B. NewClientWithRegion's guard replaced by func(context.Context, aws.Config) error { return nil }(ctx, cfg): the coverage gate FAILS at pkg/aws/client.go:92, and TestAccountGuardRefusesBeforeAnyAWSCall FAILS (both subtests). Burst went on to RunInstances and got the recorder's 403, which is the --account / SPORE_ACCOUNT is accepted but never checked (only spawn doctor reads it) #1118 behaviour.
  • C. The same neutering in LoadComputeAWSConfig: TestLoadComputeAWSConfig_EnforcesTheGuard FAILS and the coverage gate FAILS at pkg/config/endpoints.go:227.
  • D. Infra condition forced true: the dedicated infra profile subtest FAILS. Forced false: the infra through the shared profile subtest FAILS.
  • E. Fail-open on a lookup error (if err != nil && false): TestCheckAccount_FailsClosed FAILS (both cases).
  • F. An eager CheckAccount call in the root PersistentPreRunE: TestOfflineCommandsDoNotCallSTS FAILS (version, completion bash, help).

CHANGELOG

I put this under ### Fixed, not ### Security. The guard protects against operator error (wrong profile), not against an adversary. No trust boundary was crossed and nobody else could exploit its absence. It is a documented safety control that didn't work, which is a bug fix.

Checks

  • GOTOOLCHAIN=go1.26.9 make check: fmt and vet (all GOOS) are clean. It stops at the known darwin-only SA4023, so I ran GOOS=linux staticcheck ./... (clean) and then the remaining steps one by one: test-hermetic, check-nested and check-tidy all pass. check-build-tags fails here exactly as it does on an unmodified origin/main worktree (I diffed the output and it is identical apart from paths), so that failure is local-only noise.
  • semgrep scan --config=auto --error on the changed files: clean.
  • I made no AWS calls. The STS path was exercised only against an in-process responder.

Docs follow-up

After release, docs/guides/aws-auth.md (the "account guard is only partly enforced" warning) and docs/reference/environment-variables.md in spore-host/spore-host can say spawn enforces all three sources.

@scttfrdmn scttfrdmn added this to the v0.135.2 milestone Oct 19, 2026
The root --account flag ("Expected AWS account ID (optional guard)"),
SPORE_ACCOUNT and `account =` in the spore config were recorded and read
only by spawn doctor, so `spawn --account X launch` with credentials for
another account launched anyway.

pkg/config.CheckAccount now runs where credentials are resolved: every AWS
config the spawn binary builds from a credential chain is checked with STS
GetCallerIdentity (cached per access key) before it is handed out, and a
mismatch, an STS error or an empty account is a refusal naming both
accounts. Commands that build no AWS config make no STS call. A dedicated
infra profile (spawn's control-plane account) is exempt; infra through the
user's own profile and the compute profile are always checked. spawn doctor
reports the refusal as a failed account check.

Gates: a command-level test proves burst, which reaches RunInstances
through the recorder, sends no AWS request at all under a mismatch; and
TestEveryCLIConfigLoadIsAccountGuarded discovers every LoadDefaultConfig
call in the packages linked into the spawn binary (per release GOOS) and
fails if one is not guarded.

Fixes #1118
@scttfrdmn
scttfrdmn force-pushed the fix/1118-account-guard branch from 1622fd7 to c491e64 Compare October 19, 2026 06:26
@scttfrdmn
scttfrdmn merged commit b486e67 into main Oct 19, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1118-account-guard branch October 19, 2026 06:47
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.

--account / SPORE_ACCOUNT is accepted but never checked (only spawn doctor reads it)

1 participant