Coerce user_inputs to Array[String] at public boundaries#117
Merged
Conversation
Zxcvbn.test and TesterBuilder#add_word_list now accept nil and non-Array values (single String, mixed arrays) via Array() coercion, matching the JS library's null==t&&(t=[]) behaviour. The is_a?(String) filter replaces respond_to?(:downcase), which incorrectly admitted Symbols — they have Symbol#downcase but crash in Trie#insert on each_char.
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.
Context
The JS library coerces
nulluser inputs to[]at the entry point (null==t&&(t=[])). Ruby has no equivalent guard. Passingnilasuser_inputstoTester#testraisesNoMethodError(nil.select) insideOmnimatch#matches.The
respond_to?(:downcase)filter used to sanitise user inputs admits Symbols —Symbol#downcaseexists in Ruby — but Symbols crash inTrie#insertoneach_char. The same broken filter exists inData#add_word_list, soTesterBuilder#add_word_list('x', [:foo])also crashes at build time.Changes
Tester#test: coerceuser_inputsviaArray(user_inputs).select { |i| i.is_a?(String) }. Acceptsnil, a singleString, or a mixed array; non-Strings are silently ignored.TesterBuilder#add_word_list: sameArray()coercion, removing theArgumentErrorfor non-Array input.Omnimatch#matches: remove the now-redundant filter; tighten RBS toArray[String].Tester#testandTesterBuilder#add_word_listto reflect the broader accepted types.Consequences
Tester#test(password, nil)no longer raises.Tester#test(password, [:foo, 42])no longer crashes — non-Strings are silently ignored.TesterBuilder#add_word_list('x', nil)and('x', 'word')are accepted; nil is treated as an empty list.ArgumentErrorfromadd_word_list(name, nil)will no longer get one.