Repository navigation
fix(security): repair encryption key derivation for aes-256-cbc - #4
Merged
Merged
Conversation
aes-256-cbc needs an exactly-32-byte key, but the service only hashed keys *shorter* than 32 characters. Any key of 32+ characters - including the built-in default fallback (41 chars) and the common 64-char hex key - was passed through unchanged and made createCipheriv throw 'Invalid key length', so secret encryption/decryption was completely broken unless a 32-char key happened to be configured. - Normalize any non-32-character key to a 32-byte hex key (keeps the raw key when it is exactly 32 chars, preserving existing ciphertext) - Fail fast at startup when NODE_ENV=production and ENCRYPTION_KEY is unset instead of silently falling back to a known insecure default - Add EncryptionService tests covering the default, 32/40/64-char and short keys plus the production guard
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 #3
Problem
aes-256-cbcneeds a 32-byte key, butEncryptionServiceonly normalized keys shorter than 32 characters. Keys of 32+ characters (the 41-char default fallback and the common 64-char hex key) were passed through unchanged, socreateCipherivthrewInvalid key length— secret encryption/decryption was broken unless a 32-char key was configured. Production also silently used the known insecure default.Changes
src/common/encryption.service.tsnormalizeKey(): any key whose length is not exactly 32 is digested withsha256(...).digest('hex').slice(0, 32). A raw 32-character key is still used verbatim, so existing ciphertext encrypted under a 32-char key keeps working.NODE_ENV=productionandENCRYPTION_KEYis unset, instead of falling back to the insecure default.test/encryption.service.spec.ts(new)Verification (in
microservices/config-service)