Skip to content

Coerce user_inputs to Array[String] at public boundaries#117

Merged
orien merged 1 commit into
masterfrom
fix-user-inputs-coercion
May 28, 2026
Merged

Coerce user_inputs to Array[String] at public boundaries#117
orien merged 1 commit into
masterfrom
fix-user-inputs-coercion

Conversation

@orien
Copy link
Copy Markdown
Member

@orien orien commented May 28, 2026

Context

The JS library coerces null user inputs to [] at the entry point (null==t&&(t=[])). Ruby has no equivalent guard. Passing nil as user_inputs to Tester#test raises NoMethodError (nil.select) inside Omnimatch#matches.

The respond_to?(:downcase) filter used to sanitise user inputs admits Symbols — Symbol#downcase exists in Ruby — but Symbols crash in Trie#insert on each_char. The same broken filter exists in Data#add_word_list, so TesterBuilder#add_word_list('x', [:foo]) also crashes at build time.

Changes

  • Tester#test: coerce user_inputs via Array(user_inputs).select { |i| i.is_a?(String) }. Accepts nil, a single String, or a mixed array; non-Strings are silently ignored.
  • TesterBuilder#add_word_list: same Array() coercion, removing the ArgumentError for non-Array input.
  • Omnimatch#matches: remove the now-redundant filter; tighten RBS to Array[String].
  • Update RBS signatures for Tester#test and TesterBuilder#add_word_list to 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.
  • Callers previously relying on ArgumentError from add_word_list(name, nil) will no longer get one.

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.
@orien orien merged commit 97da0e8 into master May 28, 2026
10 checks passed
@orien orien deleted the fix-user-inputs-coercion branch May 28, 2026 01:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant