Repository navigation
feat(ec2): enforce the documented tag key and value length limits (#490) - #505
Merged
Merged
Conversation
A tag key longer than 128 characters or a value longer than 256 was accepted on every path: CreateTags, DeleteTags, and tag-on-create through RunInstances, CreateFleet, CreateImage, CreateNatGateway and a launch template. This was the third restriction in the table #469 read for the 50-tag count and the only one left unenforced, so a consumer generating tag values from a resource description or a git ref had no way to reach real EC2's rejection. The unit is Unicode characters, not bytes, per "Maximum key length - 128 Unicode characters in UTF-8", so the check counts runes: a key of 128 emoji is 128 characters and 512 bytes and is legal. There is no lower bound, which is what makes DeleteTags correct without a special case. The three tag checks now run through one ec2CheckTagRules function rather than at each of the nine call sites that had to be found by hand for this fix. Reserved aws: keys are not exempt from the lengths though they are from the count, which answers the question #490 raised — and the code says plainly that the choice is currently unobservable, since the reserved-key check runs first and refuses such a key before its length is measured. Provenance is the weakest of the three tag restrictions and is marked as such: the code is by analogy with the reserved-key rejection, and the message text is substrate's own — no captured real-AWS length rejection exists.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
5 tasks
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.
Closes #490
A tag key longer than 128 characters or a value longer than 256 was accepted on every path substrate offers:
CreateTags,DeleteTags, and tag-on-create throughRunInstances,CreateFleet,CreateImage,CreateNatGatewayand a launch template. This was the third restriction in the same table #469 read for the 50-tag count, and the only one left unenforced — so a consumer generating tag values from a resource description or a git ref (the case that actually overruns 256 characters) had no way to reach real EC2's rejection, and a test asserting it passed against substrate while the same code failed against AWS. The tracker's recurring shape: substrate returns success where real AWS refuses, so the error branch is unreachable.The rejection is
InvalidParameterValue, HTTP 400, naming which limit was exceeded and by how much.The unit is Unicode characters, not bytes
From the restrictions list: "Maximum key length – 128 Unicode characters in UTF-8" and "Maximum value length – 256 Unicode characters in UTF-8". So the check counts runes. A key of 128 emoji is 128 characters and 512 bytes, and it is legal; a byte-counting check refuses it, and refuses it while reporting a length the caller never sent. The two counts agree on ASCII, which is why an ASCII-only suite cannot tell them apart — hence emoji and three-byte rows in the tests, and an assertion in the fixture builder that it built the intended number of runes.
There is no lower bound — "You can set the value of a tag to an empty string, but you can't set the value of a tag to null" — so the check is upper-bound only. That is also what makes
DeleteTagsright without a special case: it names keys and treats the value as optional, so an absentTag.N.Valueis the empty string and passes.Reserved keys, and being precise about what the answer buys
#490 flagged as needing research whether the
aws:exemption extends to length. It does not: the exemption in the restrictions list is scoped to the count alone ("Tags with theaws:prefix do not count against your tags per resource limit") and nothing in it exempts a reserved key from either length. Soec2CheckTagLengthschecks reserved keys, deliberately narrower thanec2CheckTagLimitsitting a few lines above it.Worth being exact about what that decides, because I nearly shipped a test asserting more than is true:
ec2CheckTagLengthshas exactly one non-test caller,ec2CheckTagRules, which runs the reserved-key check first and returns on its error. No API path can reach the length check with a reserved key, so the choice changes no wire behaviour today. It is the right code for any future path that checks lengths alone, and it is recorded because the adjacent count check does the opposite — but it is a clarity decision, not a behavioural one, and the comment, the docs and the CHANGELOG all say so rather than implying a fidelity gain that isn't there. Where the length check does bite is the case-sensitive edge:AWS:is an ordinary user tag, so an over-longAWS:-prefixed key is refused for its length, and that row is tested.Rollback, and one refactor
As with the other two restrictions, the whole request is refused before anything is modified, and on a tag-on-create path before the resource exists — per "If tags cannot be applied during resource creation, we roll back the resource creation process." A refused
RunInstanceslaunches no instance, a refusedCreateImagecreates neither the AMI nor its snapshot, a refusedCreateNatGatewaycreates no gateway, and a refusedCreateLaunchTemplatecreates no template. A launch template is checked at creation and at each version as well as at every launch that names it, following #471, so an over-long template tag is reported once at the operation that named it.The nine call sites that had to be found by hand for this fix now go through one
ec2CheckTagRules, so the next restriction is added in one place instead of nine.Provenance
The weakest of the three tag restrictions, and marked as such in the code, the docs and the CHANGELOG. The code
InvalidParameterValuewith HTTP 400 is by analogy with the reserved-key rejection — the other tag-restriction violation on the same operations, andCreateTags' Errors section is empty so the API model supplies nothing. The message text is substrate's own: no captured real-AWS length rejection was found, in moto or LocalStack either. It interpolates the limit and the actual length followingsqsMessageTooLong's precedent, because the message is the only place a caller learns which limit they hit. Dispatch on the code.Tests
Seven new tests in
emulator/ec2_tags_test.go: adjacent boundaries (128 accepted / 129 refused, 256 / 257), the emoji and three-byte rune rows, the empty value, the reserved-vs-AWS:pair,DeleteTags' key-only asymmetry including a value-less delete that actually deletes, all six tag-on-create paths asserting both the rejection and the absent resource, and the two launch-template paths plus a template exactly at both limits that still launches with its tags.Verification
From the repo root:
gofmt -l .clean,go build ./...,go vet ./...,golangci-lint run ./...0 issues,make test(race) green,make docs-reference docs-reference-check docs-versions,npm --prefix docs run build,go test -C test/e2e ./....Mutation-tested with 30 mutations: 29 caught, 1 proven equivalent. The equivalent one is "exempt reserved keys from the length check" — unreachable by construction for the reason above, which is why the harness has an
EQUIVALENTcategory requiring a proof rather than reporting a bare survivor. Caught includes both of the plan's highest-value mutations (count bytes instead of runes, for key and value separately), every off-by-one in both directions, adding a lower bound, reordering the two checks, and dropping the check at each of the nine call sites individually.End-to-end through the AWS CLI against
substrate server:The emoji line is the one that matters: the reported length is in characters, so it fails under
len.