fix(ciphers): XORCipher now encrypts newline characters - #312
Open
Ankur-Kataria wants to merge 2 commits into
Open
fix(ciphers): XORCipher now encrypts newline characters#312Ankur-Kataria wants to merge 2 commits into
Ankur-Kataria wants to merge 2 commits into
Conversation
…gorithms#308) The previous implementation used str.replace(/./g, ...) where the dot metacharacter does not match line-terminator characters (\n, \r, etc.) without the s (dotAll) flag, so those bytes were silently passed through unencrypted — an information leak and a correctness bug. Replace the regex-based replacement with Array.from(), which iterates over every character regardless of whether it is a line terminator. The cipher logic (XOR with key) and the return type are unchanged. Add two new test cases: - encrypts \n and \r correctly (previously left unchanged) - XORCipher is its own inverse, including strings with newlines
Ankur-Kataria
requested review from
appgurueu and
raklaptudirm
as code owners
September 1, 2026 14:15
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.
Description
Fixes #308 —
XORCipherwas silently skipping newline (\n,\r, etc.) characters instead of encrypting them.Root cause
The implementation used
str.replace(/./g, ...). In JavaScript/TypeScript, the.metacharacter without thes(dotAll) flag does not match line-terminator characters (\nU+000A,\rU+000D, U+2028, U+2029). Those bytes were passed through unchanged, which is both a correctness bug and an information leak.What changed
ciphers/xor_cipher.tsArray.from()ciphers/test/xor_cipher.test.ts\n/\rand self-inverse propertyTests
All 3 tests pass (
npm test -- --testPathPattern=xor_cipher):passing a string & number as an argument— existing case unchanged ✓encrypts newline and other line-terminator characters— new ✓XORCipher is its own inverse— new ✓