Repository navigation
fix: actually reject KeywordShiftedAlphabet keywords with repeated characters - #2037
Open
Aditya2375 wants to merge 2 commits into
Open
Aditya2375 wants to merge 2 commits into
Aditya2375 wants to merge 2 commits into
Conversation
…aracters
checkKeywordValidity returned false inside a forEach callback, which only exits the callback, so it always returned true and repeated-character keywords were accepted. With such a keyword the translation table is not a bijection and encryption loses information: encrypt('HELLO', 'ATTACK AT DAWN') gave 'HSSHLG HS LHVK', which decrypted to 'ATTACK AT CAWN'. Validate the lowercased keyword with every() so each character must be the first occurrence of itself; this also closes the 'Kk' case hole.
Regression tests for the validation fix: 'HELLO' (repeated L) must throw on encrypt and decrypt, and 'Kk' must throw because the translation table is built from the lowercased keyword.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2037 +/- ##
==========================================
+ Coverage 85.91% 85.93% +0.01%
==========================================
Files 379 379
Lines 19778 19777 -1
Branches 3016 3015 -1
==========================================
+ Hits 16993 16996 +3
+ Misses 2785 2781 -4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Describe your change:
Bug:
checkKeywordValidityinCiphers/KeywordShiftedAlphabet.jsis a no-op: it callsreturn falseinside aforEachcallback, which only exits the callback, so the function always returnstrue. Keywords with repeated characters - which the file header explicitly forbids ("Keyword must be a word where each character can occur only once") - are accepted, andcheckInputsnever throws its'Invalid keyword!'error.With a repeated-character keyword the translation table is no longer a bijection, so encryption silently loses information:
There is also a case hole:
'Kk'passes the check, but the table is built from the lowercased keyword, where the two characters collapse to a duplicate.Fix: validate the lowercased keyword and use
every(which can actually return early), so each character must be the first occurrence of itself:Tests: two new tests in
Ciphers/test/KeywordShiftedAlphabet.test.js: reject'HELLO'on both encrypt and decrypt, and reject'Kk'. All 4 tests in the file pass locally (vitest); the two new tests fail on the unpatched code. The two pre-existing round-trip tests are unaffected.Checklist:
Example:
UserProfile.jsis allowed butuserprofile.js,Userprofile.js,user-Profile.js,userProfile.jsare notFixes: #{$ISSUE_NO}.