Skip to content

fix(kms): Encrypt's plaintext guard, moved and re-coded (#991) - #1047

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/991-encrypt-plaintext-guard
Sep 18, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/991-encrypt-plaintext-guard

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

API_Encrypt states two constraints on Plaintext, at two different levels, and substrate enforced neither.

The member's own Length Constraints: Minimum length of 1. Maximum length of 4096 is key-independent — a request violating it is malformed whatever key it names, and the page's opening sentence ("encrypts plaintext of up to 4,096 bytes") settles that the count is of decoded bytes rather than base64 characters. Below that, the page publishes per-key maxima an order of magnitude smaller that depend on the key spec and the algorithm together: SYMMETRIC_DEFAULT 4096; RSA_2048 214/190; RSA_3072 342/318; RSA_4096 470/446; SM2PKE 1024.

What changed

The two constraints are split by what each needs to know.

  • The base64 decode and the 1–4096 range now run before resolveKeyTarget, following the rule kmsResolveEncryptionAlgorithm already states for the algorithm member. A caller sending 5 KB to a key that does not exist now hears about the member it can fix without an AWS account.
  • The per-spec maximum needs the key and the resolved algorithm, so it runs after kmsCheckEncryptionAlgorithmForKey. An RSA key sent no EncryptionAlgorithm hears that the defaulted SYMMETRIC_DEFAULT is incompatible with RSA_2048, rather than a maximum for a pair that cannot encrypt anything at all.

Provenance

Both refusals are ValidationError/400 from CommonErrors.html. API_Encrypt publishes nine errors and not one of them is about a member being out of range; the closest, InvalidKeyUsageException, is about a KeyUsage or an algorithm "incompatible with the type of key material". It is deliberately not reused for the per-key maximum: the key does admit the algorithm and a shorter plaintext would succeed, so a caller reading that code would change its algorithm when what it must change is how much data it sends in one call.

The old answer for an undecodable value, InvalidCiphertextException, is not on that page. It is published on Decrypt and ReEncrypt, whose CiphertextBlob decodes are what it is for and are left untouched.

Tests

emulator/kms_plaintext_test.go, over the wire (#765), every refusal pairing code with status (#923):

  • every one of the eight published maxima accepted at exactly the maximum, with all three published response members — the case a </<= slip fails and no oversize test can see;
  • the range enforced at both ends, the empty value included, since Required: Yes does not catch a present-but-empty member;
  • InvalidCiphertextException asserted absent by name, because that is the correction rather than a side effect of it;
  • both key-independent refusals answered ahead of a key that does not exist, a disabled key and a signing key;
  • the per-spec maximum refused one byte over for every asymmetric pair, with the spec, the algorithm, the size sent and the maximum all in the message — no two of them determine the answer;
  • the algorithm-for-key check pinned as running first.

Assertions 1 and 5 together stand in for a unit test that the size table and kmsEncryptionAlgorithmsByKeySpec agree: every pair the latter admits is exercised at its maximum and one byte past it, so a pair missing from the table surfaces as a request that should have been refused and was not.

Closes #991

API_Encrypt states two constraints on Plaintext at two different levels
and substrate enforced neither: the member's own "Minimum length of 1.
Maximum length of 4096", which is key-independent, and a per-key table an
order of magnitude smaller that depends on the key spec and the
encryption algorithm together.

Split them by what each needs to know. The base64 decode and the 1-4096
range are facts about the request, so they now run before
resolveKeyTarget -- a caller sending 5 KB to a key that does not exist
hears about the member it can fix on its own. The per-spec maximum needs
the key and the resolved algorithm, so it runs after
kmsCheckEncryptionAlgorithmForKey: an RSA key sent no algorithm hears
that the defaulted SYMMETRIC_DEFAULT is incompatible, rather than a
maximum for a pair that cannot encrypt at all.

Both refusals are ValidationError/400 from CommonErrors. API_Encrypt
publishes nine errors and not one is about a member out of range;
InvalidKeyUsageException is the near miss and is deliberately not reused
for the per-key maximum, because the key does admit the algorithm and a
shorter plaintext would succeed. The old answer for an undecodable value,
InvalidCiphertextException, is not on that page at all -- it belongs to
Decrypt and ReEncrypt, whose CiphertextBlob decodes are untouched.

Closes #991
@codecov

codecov Bot commented Sep 18, 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 df93136 into main Sep 18, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/991-encrypt-plaintext-guard branch September 18, 2026 01:17
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.

KMS Encrypt answers InvalidCiphertextException, a code API_Encrypt does not publish, for a malformed Plaintext

1 participant