Runtime-deprecate calling digest() on HMAC more than once #62838
Copy link
Copy link
Closed
Labels
cryptoIssues and PRs related to the crypto subsystem.Issues and PRs related to the crypto subsystem.securityIssues and PRs related to security.Issues and PRs related to security.
Description
(Upd: detected by the scanner behind @deepview-autofix)
Hash behavior is reasonable:
> hash = require('crypto').createHash('sha256').update('data')
> hash.digest()
<Buffer 3a 6e b0 79 0f 39 ac 87 c9 4f 38 56 b2 dd 2c 5d 11 0e 68 11 60 22 61 a9 a9 23 d3 bb 23 ad c8 b7>
> hash.digest()
Uncaught Error [ERR_CRYPTO_HASH_FINALIZED]: Digest already called
at Hash.digest (node:internal/crypto/hash:155:11) {
code: 'ERR_CRYPTO_HASH_FINALIZED'
}But HMAC, on the other hand, returns empty buffers on further .digest() calls, likely for compat reasons:
> hmac = require('crypto').createHmac('sha256', 'key').update('data')
> hmac.digest()
<Buffer 50 31 fe 3d 98 9c 6d 15 37 a0 13 fa 6e 73 9d a2 34 63 fd ae c3 b7 01 37 d8 28 e3 6a ce 22 1b d0>
> hmac.digest()
<Buffer >This is a footgun with potential security risks, and should be first runtime-deprecated, then removed if no breakage is detected.

Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
cryptoIssues and PRs related to the crypto subsystem.Issues and PRs related to the crypto subsystem.securityIssues and PRs related to security.Issues and PRs related to security.
Activity
panva commentedon Apr 20, 2026
Given the lack of usefulness of the second digest() call's return (and in so the likelyhood of it being depended on), shouldn't we treat this as a bug fix?panva commentedon Apr 20, 2026
The behaviour looks entirely deliberate
node/lib/internal/crypto/hash.js
Lines 185 to 190 in c3dd52a
but looking at the blame its introduction traces back to #15231 by @jasnell, its parent was just throwing the same as
Hashnode/lib/crypto.js
Line 121 in 8fa5fcc
jasnell commentedon Apr 20, 2026
Oh, we reaching way back. I don't remember what the reasoning here was. I'm +1 on deprecation of the inconsistent behavior
panva commentedon Apr 20, 2026
Test Wayback machine: #14122, a3b9f4b
panva commentedon Apr 20, 2026
Yeah, +1 deprecate it.
ChALkeR commentedon Apr 20, 2026
@panva this was also auto-detected by the scanner behind @deepview-autofix btw.
But this in fact looks intentional so I would recommend removing this in a major as a safeguard, following a deprecation cycle.
Upd: github glitched, I replied to #62838 (comment), all the other comments (end the edit on that one) just loaded. I agree with those.
panva commentedon Apr 20, 2026
Enough time to land a semver-majorPRs that contain breaking changes and should be released in the next major version.
runtime-deprecation for v27.x and then soonest with v28.x EOL the deprecation.
mizoz commentedon Apr 28, 2026
Hi! I'd love to contribute. Could you assign this issue to me?
Anshikakalpana commentedon May 4, 2026
Hi! I’d like to work on this issue. I’m preparing a PR for it.
doc: add Hmac.digest() documentation-only deprecation (DEP0206)
soulee-dev commentedon May 13, 2026
@Anshikakalpana Left review suggestions for the CI failure.
expectWarning is keyed by warning name, so the inline
DEP0181block at the bottom of test-crypto-hmac.js overwrites the new DEP0206 registration. Merging them at the top resolves it.#63162 (review)
doc: add Hmac.digest() documentation-only deprecation (DEP0206)
doc: add Hmac.digest() documentation-only deprecation (DEP0206)
doc: add Hmac.digest() documentation-only deprecation (DEP0206)
Aanushka001 commentedon May 24, 2026
Fetching comments for issue #62838...
crypto: runtime-deprecate calling Hmac.digest() more than once
crypto: runtime-deprecate calling Hmac.digest() more than once
doc: add Hmac.digest() documentation-only deprecation (DEP0206)
Merge upstream Node.js v24.18.1 into dotnet/main (#375)