Skip to content

crypto: validate inputEncoding in Cipher/Decipher update#62954

Open
maruthang wants to merge 1 commit intonodejs:mainfrom
maruthang:fix-45189-cipher-encoding-validation
Open

crypto: validate inputEncoding in Cipher/Decipher update#62954
maruthang wants to merge 1 commit intonodejs:mainfrom
maruthang:fix-45189-cipher-encoding-validation

Conversation

@maruthang
Copy link
Copy Markdown

Cipher.update(string, badEncoding, ...) and Decipher.update with
the same shape silently produced incorrect output: the binding
skipped the unrecognized encoding and fell back to a default,
giving the user wrong ciphertext or plaintext with no signal.

Sub-cases 1 and 2 from issue #45189 (bad output encoding to
update/final) were addressed in PR #45990. This commit completes
the fix for sub-case 3 (bad input encoding) per panva's comment
deferring it to a follow-up PR for CITGM testing. When data is
a string and inputEncoding is non-null but does not normalize
to a known encoding, throw ERR_UNKNOWN_ENCODING. Buffer /
TypedArray / DataView data paths are unaffected (the binding
ignores inputEncoding for non-string data anyway).

Fixes: #45189
Refs: #45990


Note: I was unable to run the test suite locally (no built out/Release/node on this Windows host). Both touched files lint clean and pass node --check; the new test was confirmed to fail against the unpatched system Node v20 (so it actually exercises the bug). Looking forward to CI verification.

Cipher.update(string, badEncoding, ...) and Decipher.update with
the same shape silently produced incorrect output: the binding
skipped the unrecognized encoding and fell back to a default,
giving the user wrong ciphertext or plaintext with no signal.

Sub-cases 1 and 2 from issue nodejs#45189 (bad output encoding to
update/final) were addressed in PR nodejs#45990. This commit completes
the fix for sub-case 3 (bad input encoding) per panva's comment
deferring it to a follow-up PR for CITGM testing. When `data` is
a string and `inputEncoding` is non-null but does not normalize
to a known encoding, throw ERR_UNKNOWN_ENCODING. Buffer /
TypedArray / DataView data paths are unaffected (the binding
ignores `inputEncoding` for non-string data anyway).

Fixes: nodejs#45189
Refs: nodejs#45990
Signed-off-by: Maruthan G <maruthang4@gmail.com>
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/crypto

@nodejs-github-bot nodejs-github-bot added crypto Issues and PRs related to the crypto subsystem. needs-ci PRs that need a full CI run. labels Apr 25, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.60%. Comparing base (21436f0) to head (87edd60).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62954      +/-   ##
==========================================
- Coverage   89.66%   89.60%   -0.06%     
==========================================
  Files         706      704       -2     
  Lines      219391   218310    -1081     
  Branches    42068    41915     -153     
==========================================
- Hits       196712   195616    -1096     
- Misses      14578    14674      +96     
+ Partials     8101     8020      -81     
Files with missing lines Coverage Δ
lib/internal/crypto/cipher.js 97.97% <100.00%> (+0.02%) ⬆️

... and 124 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

crypto Issues and PRs related to the crypto subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crypto Cipher/Decipher validation issues

2 participants