Skip to content

fix(ec2): RunInstances must reject a launch that resolves no AMI (#412) - #425

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/412-runinstances-imageid
Aug 1, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/412-runinstances-imageid

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

What

RunInstances never validated that an AMI resolved, so a launch with an empty
ImageId returned 200 here and failed against real AWS. It now fails with
MissingParameter / "The request must contain the parameter ImageId", HTTP 400.

The consumer's error branch was unreachable, so the bug shipped and was caught only
by a paid smoke test — exactly the failure mode this project exists to prevent.

The check runs after launch-template resolution

That placement is the whole design decision. AWS documents ImageId as
Required: No precisely because a launch template may supply it, so an up-front
check would be wrong — it would reject a valid launch. At the post-resolution point
the check means "no AMI resolved from any source", which is the actual AWS rule.

The pre-existing TestEC2Plugin_RunInstances_ViaLaunchTemplate still passing is the
evidence the check sits in the right place; the new table pins it from both sides.

A second, deeper defect

Writing the test surfaced a bug the one-line check could not see: the
ImageId="" row still returned 200, launching an instance from
<imageId>1</imageId>.

ParseAWSRequest maps bare keys (?uploads, ?delete) to the sentinel "1" so a
caller can detect their presence via a map lookup. But it derived the bare-key set
from r.URL.RawQuery while applying the sentinel to r.Form — which merges the
query string with the form body
. The entire AWS Query protocol (EC2, IAM, STS,
SQS, SNS, CloudWatch) travels in the body, so ImageId= never arrived as empty; it
arrived as "1".

Every query-protocol parameter sent explicitly empty was corrupted the same way,
which made emptiness structurally unverifiable by any plugin above the parser.

The fix inverts the test to a positive one: a key is bare only if it appears with no
= in the raw query. An "explicitly empty" set built from the query string cannot
say anything about body keys, which is why the original direction could not be made
correct. The query-string half of this was already fixed (#200); this is the
body-side companion.

Tests

TestEC2_RunInstances_MissingImageId — 6 rows, asserting status, code and
message so AWS's wording cannot silently drift:

Case Expect
No ImageId, no launch template 400 MissingParameter
ImageId explicitly empty 400 — the shape that shipped the bug
Template resolves but carries no AMI 400
Template name does not exist 400
Explicit ImageId 200 (regression guard)
Template supplies the AMI 200 (pins the placement)

TestParseAWSRequest_EmptyValueFormBodyParam and
TestParseAWSRequest_BareQueryKeyWithFormBody — direct parser regressions, beside
the existing #200 query-string pair. The second guards the risk in the parser
change: S3 POST operations rely on bare keys resolving to "1" when a body is
also present.

Verified both fixes are load-bearing: reverting the parser change alone makes two
rows fail; the full suite covers every S3 bare-key path.

Blast radius

Every "RunInstances" occurrence across authz_test.go, ce_plugin_test.go,
ec2_networkinterface_test.go, ec2_notfound_test.go, ec2_plugin_test.go and
server_test.go supplies either ImageId or a launch template. The one exception
(authz_test.go:572) calls auth.CheckAccess directly and never reaches the
plugin. betty_cfn.go uses a non-empty ami-00000000 default, so CFN deploys are
unaffected. The "1" sentinel is consumed only for S3 bare keys (policy, acl),
which arrive in the URL query string.

Scoped strictly to emptiness — no ami- format validation. There is no
ec2ImageIDKind today, ~12 existing tests use non-hex fixture IDs (ami-base,
ami-test, ami-1), and AWS still accepts legacy ID forms.

Verification

gofmt clean · go build ./... · go vet ./... · golangci-lint run ./... (0
issues) · make test (race) · make docs-reference-check · test/e2e — all green.

Closes #412

RunInstances never checked that an AMI resolved, so a launch with an empty
ImageId "succeeded" against substrate and failed against real AWS. The
consumer's error branch was therefore unreachable and the bug shipped,
caught only by a paid smoke test.

The check runs *after* launch-template resolution. AWS documents ImageId
as "Required: No" precisely because a template may supply it, so an
up-front check would be wrong: a launch naming a template that carries an
AMI is valid, and one that resolves to nothing is not. The existing
TestEC2Plugin_RunInstances_ViaLaunchTemplate passing is the evidence the
check sits in the right place.

Writing the test surfaced a second, deeper defect that the one-line check
could not see. ParseAWSRequest maps bare keys (?uploads, ?delete) to the
sentinel "1" so a caller can detect their presence, but it derived the
bare-key set from the URL's raw query while applying the sentinel to
r.Form — which merges query *and* body. The entire AWS Query protocol
(EC2, IAM, STS, SQS, SNS, CloudWatch) travels in the body, so "ImageId="
never arrived as empty; it arrived as "1", and RunInstances launched an
instance from an AMI literally named 1. Every query-protocol parameter
sent explicitly empty was corrupted the same way, which made emptiness
unverifiable by any plugin above the parser.

Inverted the test to a positive one: only a key that appears with no "="
in the raw query is bare. An "explicitly empty" set built from the query
cannot say anything about body keys, which is why the original direction
could not be correct. The query-string half was already fixed (#200);
this is the body-side companion.

ImageId is an optional *string in the typed SDKs, so aws.String("")
serializes as absent from the wire and reaches the service rather than
failing client-side — the exact shape that shipped the reported bug.

Scoped strictly to emptiness: no ami- format validation, since ~12
existing tests use non-hex fixture IDs and AWS accepts legacy forms.

Closes #412
@codecov

codecov Bot commented Aug 1, 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 a6bbe28 into main Aug 1, 2026
15 checks passed
@scttfrdmn
scttfrdmn deleted the fix/412-runinstances-imageid branch August 1, 2026 05:37
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.

RunInstances: validate required parameters (ImageId) like AWS does

1 participant