File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
actionpack/lib/action_controller/metal
activesupport/lib/active_support Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 1
1
require 'base64'
2
+ require 'active_support/security_utils'
2
3
3
4
module ActionController
4
5
# Makes it dead easy to do HTTP Basic, Digest and Token authentication.
@@ -70,7 +71,11 @@ module ClassMethods
70
71
def http_basic_authenticate_with ( options = { } )
71
72
before_action ( options . except ( :name , :password , :realm ) ) do
72
73
authenticate_or_request_with_http_basic ( options [ :realm ] || "Application" ) do |name , password |
73
- name == options [ :name ] && password == options [ :password ]
74
+ # This comparison uses & so that it doesn't short circuit and
75
+ # uses `variable_size_secure_compare` so that length information
76
+ # isn't leaked.
77
+ ActiveSupport ::SecurityUtils . variable_size_secure_compare ( name , options [ :name ] ) &
78
+ ActiveSupport ::SecurityUtils . variable_size_secure_compare ( password , options [ :password ] )
74
79
end
75
80
end
76
81
end
Original file line number Diff line number Diff line change
1
+ require 'digest'
2
+
3
+ module ActiveSupport
4
+ module SecurityUtils
5
+ # Constant time string comparison.
6
+ #
7
+ # The values compared should be of fixed length, such as strings
8
+ # that have already been processed by HMAC. This should not be used
9
+ # on variable length plaintext strings because it could leak length info
10
+ # via timing attacks.
11
+ def secure_compare ( a , b )
12
+ return false unless a . bytesize == b . bytesize
13
+
14
+ l = a . unpack "C#{ a . bytesize } "
15
+
16
+ res = 0
17
+ b . each_byte { |byte | res |= byte ^ l . shift }
18
+ res == 0
19
+ end
20
+ module_function :secure_compare
21
+
22
+ def variable_size_secure_compare ( a , b ) # :nodoc:
23
+ secure_compare ( ::Digest ::SHA256 . hexdigest ( a ) , ::Digest ::SHA256 . hexdigest ( b ) )
24
+ end
25
+ module_function :variable_size_secure_compare
26
+ end
27
+ end
You can’t perform that action at this time.
0 commit comments