Skip to content

feat(ec2): enforce the documented tag key and value length limits (#490) - #505

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/490-tag-length-limits
Aug 2, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/490-tag-length-limits

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

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 through RunInstances, CreateFleet, CreateImage, CreateNatGateway and 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 DeleteTags right without a special case: it names keys and treats the value as optional, so an absent Tag.N.Value is 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 the aws: prefix do not count against your tags per resource limit") and nothing in it exempts a reserved key from either length. So ec2CheckTagLengths checks reserved keys, deliberately narrower than ec2CheckTagLimit sitting a few lines above it.

Worth being exact about what that decides, because I nearly shipped a test asserting more than is true: ec2CheckTagLengths has 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-long AWS:-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 RunInstances launches no instance, a refused CreateImage creates neither the AMI nor its snapshot, a refused CreateNatGateway creates no gateway, and a refused CreateLaunchTemplate creates 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 InvalidParameterValue with HTTP 400 is by analogy with the reserved-key rejection — the other tag-restriction violation on the same operations, and CreateTags' 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 following sqsMessageTooLong'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 EQUIVALENT category 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:

create-tags Key=<129 chars>  → InvalidParameterValue: … the supplied key is 129
create-tags Key=<128 chars>  → accepted
create-tags Key=<128 emoji>  → accepted        (512 bytes, 128 characters)
create-tags Key=<129 emoji>  → refused, "the supplied key is 129"   (not 516)
create-tags Value=<257>      → InvalidParameterValue: … the supplied value is 257
run-instances --tag-specifications Key=<129 chars>
                             → refused, and the instance count is unchanged

The emoji line is the one that matters: the reported length is in characters, so it fails under len.

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

codecov Bot commented Aug 2, 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 468e785 into main Aug 2, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/490-tag-length-limits branch August 2, 2026 21:58
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.

EC2 tag key (128) and value (256) length limits are not enforced

1 participant