fix: use StringDecoder for cipher string encoding remainder buffering#949
Merged
fix: use StringDecoder for cipher string encoding remainder buffering#949
Conversation
Restores StringDecoder usage (removed during Nitro rewrite in #573) to properly handle base64/utf8 remainder bytes across update() and final() calls. Without this, base64-encoding each chunk independently produces premature padding, causing string concat to differ from Buffer concat for inputs >= 16 bytes (one AES block). Fixes #945
Prevent silent data corruption when different output encodings are passed to update() and final(). Matches Node.js cipher behavior which asserts the encoding cannot change once a StringDecoder is initialized.
4 tasks
Contributor
🤖 End-to-End Test Results - iOSStatus: ✅ Passed 📸 Final Test ScreenshotScreenshot automatically captured from End-to-End tests and will expire in 30 days This comment is automatically updated on each test run. |
Contributor
🤖 End-to-End Test Results - AndroidStatus: ✅ Passed 📸 Final Test ScreenshotScreenshot automatically captured from End-to-End tests and will expire in 30 days This comment is automatically updated on each test run. |
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.


Summary
Fixes incorrect cipher output when using string encoding (e.g.,
'base64') withupdate()+final()string concatenation.As reported by @vic614, longer inputs where the encrypted byte count isn't divisible by 3 produce different base64 output when using string concat vs Buffer concat. The root cause:
Buffer.toString('base64')encodes each chunk independently, producing trailing=padding mid-stream that corrupts the concatenated result.Changes
ab2str()withStringDecoderin cipherupdate()andfinal()—StringDecoder.write()buffers incomplete base64 groups across calls, andStringDecoder.end()flushes the remainder with proper padding. This matches Node.js's own cipher implementation.update()andfinal()on the same cipher instance, preventing silent data corruption.string_decoderdependency (already in the dep tree viareadable-stream).32 % 3 = 2remainder)16 % 3 = 1remainder)Testing
Run the cipher test suite in the example app. The 3 new tests validate that Buffer concat and string concat produce identical results for various input sizes.
Fixes #945