Repository navigation
fix(kms): Encrypt's plaintext guard, moved and re-coded (#991) - #1047
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
API_Encryptstates two constraints onPlaintext, 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.
resolveKeyTarget, following the rulekmsResolveEncryptionAlgorithmalready 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.kmsCheckEncryptionAlgorithmForKey. An RSA key sent noEncryptionAlgorithmhears that the defaultedSYMMETRIC_DEFAULTis incompatible withRSA_2048, rather than a maximum for a pair that cannot encrypt anything at all.Provenance
Both refusals are
ValidationError/400 fromCommonErrors.html.API_Encryptpublishes nine errors and not one of them is about a member being out of range; the closest,InvalidKeyUsageException, is about aKeyUsageor 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 onDecryptandReEncrypt, whoseCiphertextBlobdecodes 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):</<=slip fails and no oversize test can see;Required: Yesdoes not catch a present-but-empty member;InvalidCiphertextExceptionasserted absent by name, because that is the correction rather than a side effect of it;Assertions 1 and 5 together stand in for a unit test that the size table and
kmsEncryptionAlgorithmsByKeySpecagree: 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