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
Collaborator
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65112 +/- ##
==========================================
- Coverage 92.01% 90.13% -1.88%
==========================================
Files 379 751 +372
Lines 166972 252304 +85332
Branches 25554 47442 +21888
==========================================
+ Hits 153639 227425 +73786
- Misses 13041 16197 +3156
- Partials 292 8682 +8390
🚀 New features to boost your workflow:
|
jasnell
approved these changes
Aug 8, 2026
mcollina
force-pushed
the
fix-hmac-stream-digest-uninitialized-memory
branch
from
August 17, 2026 16:41
e410836 to
e41218a
Compare
aduh95
reviewed
Aug 18, 2026
aduh95
reviewed
Aug 18, 2026
ShogunPanda
approved these changes
Aug 19, 2026
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. Signed-off-by: Matteo Collina <hello@matteocollina.com>
mcollina
force-pushed
the
fix-hmac-stream-digest-uninitialized-memory
branch
from
August 21, 2026 18:32
e41218a to
49e88ce
Compare
mertcanaltin
approved these changes
Aug 21, 2026
panva
approved these changes
Aug 23, 2026
Collaborator
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.