crypto: prevent Hmac.digest() from returning uninitialized memory after stream use - #65112
Open
mcollina wants to merge 1 commit into
Open
crypto: prevent Hmac.digest() from returning uninitialized memory after stream use#65112mcollina wants to merge 1 commit into
mcollina wants to merge 1 commit into
Conversation
Hmac.prototype._flush was aliased to Hash.prototype._flush, which finalizes the native HMAC context but never sets the JavaScript-side kFinalized flag. After an Hmac has been used as a stream, a subsequent Hmac.prototype.digest() call therefore still believes the object has not been finalized and calls into C++ a second time. On that second call the native context has already been reset, so the digest buffer is never written and Digest::MAX_SIZE bytes of uninitialized stack memory are returned to JavaScript. Hash is not affected because Hash::HashDigest caches its digest (refs nodejs#28245); Hmac never received the equivalent protection. Give Hmac its own _flush that sets kFinalized so repeat digest() calls after stream use are handled by the existing DEP0206 guard. As defense in depth, also set buf.len = 0 on the native side when the context has already been reset so unwritten bytes can never be emitted.
Collaborator
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65112 +/- ##
==========================================
- Coverage 92.01% 90.31% -1.71%
==========================================
Files 379 759 +380
Lines 166972 248295 +81323
Branches 25554 46859 +21305
==========================================
+ Hits 153639 224236 +70597
- Misses 13041 15450 +2409
- Partials 292 8609 +8317
🚀 New features to boost your workflow:
|
jasnell
approved these changes
Aug 8, 2026
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.
Hmac.prototype._flushwas aliased toHash.prototype._flush, which finalizes the native HMAC context but never sets the JavaScript-sidekFinalizedflag. After anHmachas been used as a stream, a subsequentHmac.prototype.digest()call therefore still believes the object has not been finalized and calls into C++ a second time. On that second call the native context has already been reset, so the digest buffer is never written andDigest::MAX_SIZEbytes of uninitialized stack memory are returned to JavaScript.Hashis not affected becauseHash::HashDigestcaches its digest (refs #28245);Hmacnever received the equivalent protection.This gives
Hmacits own_flushthat setskFinalized, so repeatdigest()calls after stream use are handled by the existing DEP0206 guard. As defense in depth, it also setsbuf.len = 0on the native side when the context has already been reset, so unwritten bytes can never be emitted. A regression test covers the stream-then-digest()path.