Repository navigation
fix(ec2): RunInstances must reject a launch that resolves no AMI (#412) - #425
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This was referenced Aug 1, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
RunInstancesnever validated that an AMI resolved, so a launch with an emptyImageIdreturned 200 here and failed against real AWS. It now fails withMissingParameter/ "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
ImageIdasRequired: 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_ViaLaunchTemplatestill passing is theevidence 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>.ParseAWSRequestmaps bare keys (?uploads,?delete) to the sentinel"1"so acaller can detect their presence via a map lookup. But it derived the bare-key set
from
r.URL.RawQuerywhile applying the sentinel tor.Form— which merges thequery 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; itarrived 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 cannotsay 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 andmessage so AWS's wording cannot silently drift:
ImageId, no launch templateMissingParameterImageIdexplicitly emptyImageIdTestParseAWSRequest_EmptyValueFormBodyParamandTestParseAWSRequest_BareQueryKeyWithFormBody— direct parser regressions, besidethe 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 isalso 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 acrossauthz_test.go,ce_plugin_test.go,ec2_networkinterface_test.go,ec2_notfound_test.go,ec2_plugin_test.goandserver_test.gosupplies eitherImageIdor a launch template. The one exception(
authz_test.go:572) callsauth.CheckAccessdirectly and never reaches theplugin.
betty_cfn.gouses a non-emptyami-00000000default, so CFN deploys areunaffected. 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 noec2ImageIDKindtoday, ~12 existing tests use non-hex fixture IDs (ami-base,ami-test,ami-1), and AWS still accepts legacy ID forms.Verification
gofmtclean ·go build ./...·go vet ./...·golangci-lint run ./...(0issues) ·
make test(race) ·make docs-reference-check·test/e2e— all green.Closes #412