aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/string/access.rb5
-rw-r--r--activesupport/lib/active_support/secure_compare_rotator.rb2
-rw-r--r--activesupport/test/secure_compare_rotator_test.rb13
3 files changed, 17 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb
index 4ca24028b0..4894193665 100644
--- a/activesupport/lib/active_support/core_ext/string/access.rb
+++ b/activesupport/lib/active_support/core_ext/string/access.rb
@@ -44,7 +44,7 @@ class String
# str.from(0).to(-1) # => "hello"
# str.from(1).to(-2) # => "ell"
def from(position)
- self[position..-1]
+ self[position, length]
end
# Returns a substring from the beginning of the string to the given position.
@@ -61,7 +61,8 @@ class String
# str.from(0).to(-1) # => "hello"
# str.from(1).to(-2) # => "ell"
def to(position)
- self[0..position]
+ position = [position + length, -1].max if position < 0
+ self[0, position + 1]
end
# Returns the first character. If a limit is supplied, returns a substring
diff --git a/activesupport/lib/active_support/secure_compare_rotator.rb b/activesupport/lib/active_support/secure_compare_rotator.rb
index 14a0aee947..97110d41f7 100644
--- a/activesupport/lib/active_support/secure_compare_rotator.rb
+++ b/activesupport/lib/active_support/secure_compare_rotator.rb
@@ -37,7 +37,7 @@ module ActiveSupport
@value = value
end
- def secure_compare!(other_value, on_rotation: @rotation)
+ def secure_compare!(other_value, on_rotation: @on_rotation)
secure_compare(@value, other_value) ||
run_rotations(on_rotation) { |wrapper| wrapper.secure_compare!(other_value) } ||
raise(InvalidMatch)
diff --git a/activesupport/test/secure_compare_rotator_test.rb b/activesupport/test/secure_compare_rotator_test.rb
index 8acf13e38f..d80faea128 100644
--- a/activesupport/test/secure_compare_rotator_test.rb
+++ b/activesupport/test/secure_compare_rotator_test.rb
@@ -41,4 +41,17 @@ class SecureCompareRotatorTest < ActiveSupport::TestCase
assert_equal(true, wrapper.secure_compare!("and_another_one", on_rotation: -> { @witness = true }))
end
end
+
+ test "#secure_compare! calls the on_rotation proc that given in constructor" do
+ @witness = nil
+
+ wrapper = ActiveSupport::SecureCompareRotator.new("old_secret", on_rotation: -> { @witness = true })
+ wrapper.rotate("new_secret")
+ wrapper.rotate("another_secret")
+ wrapper.rotate("and_another_one")
+
+ assert_changes(:@witness, from: nil, to: true) do
+ assert_equal(true, wrapper.secure_compare!("and_another_one"))
+ end
+ end
end