From 29bf249ea71a512ae6a3689f61fa5c308355cdd9 Mon Sep 17 00:00:00 2001 From: Codex Audit Date: Sun, 30 Aug 2026 10:53:28 +0200 Subject: [PATCH 1/2] Handle small alphabets on legacy Rubies --- lib/securerandom.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/securerandom.rb b/lib/securerandom.rb index 6079fdb..d78bd9c 100644 --- a/lib/securerandom.rb +++ b/lib/securerandom.rb @@ -54,6 +54,10 @@ def bytes(n) # Compatibility methods for Ruby 3.2, we can remove this after dropping to support Ruby 3.2 def alphanumeric(n = nil, chars: ALPHANUMERIC) n = 16 if n.nil? + size = chars.size + raise ArgumentError, "character length must not be negative" if 0 > n + raise ArgumentError, "character source must not be empty" if size == 0 + return chars.values_at(0).join('') * n if size < 2 choose(chars, n) end if RUBY_VERSION < '3.3' From 49c8f35ec6c75e6d8929147c281139016727ee83 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Sun, 30 Aug 2026 17:51:14 +0700 Subject: [PATCH 2/2] Reject singleton compatibility alphabets --- lib/securerandom.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/securerandom.rb b/lib/securerandom.rb index d78bd9c..cdf78e0 100644 --- a/lib/securerandom.rb +++ b/lib/securerandom.rb @@ -56,8 +56,7 @@ def alphanumeric(n = nil, chars: ALPHANUMERIC) n = 16 if n.nil? size = chars.size raise ArgumentError, "character length must not be negative" if 0 > n - raise ArgumentError, "character source must not be empty" if size == 0 - return chars.values_at(0).join('') * n if size < 2 + raise ArgumentError, "character source must contain at least two entries" if size < 2 choose(chars, n) end if RUBY_VERSION < '3.3'