diff --git a/lib/rack/utils.rb b/lib/rack/utils.rb index c05a94042..1808660cd 100644 --- a/lib/rack/utils.rb +++ b/lib/rack/utils.rb @@ -346,6 +346,18 @@ def byte_ranges(env, size) end module_function :byte_ranges + # Constant time string comparison. + def secure_compare(a, b) + return false unless bytesize(a) == bytesize(b) + + l = a.unpack("C*") + + r, i = 0, -1 + b.each_byte { |v| r |= v ^ l[i+=1] } + r == 0 + end + module_function :secure_compare + # Context allows the use of a compatible middleware at different points # in a request handling stack. A compatible middleware must define # #context which should take the arguments env and app. The first of which diff --git a/test/spec_utils.rb b/test/spec_utils.rb index 3a3ee8f4f..f98515470 100644 --- a/test/spec_utils.rb +++ b/test/spec_utils.rb @@ -301,6 +301,11 @@ def kcodeu Rack::Utils.bytesize("FOO\xE2\x82\xAC").should.equal 6 end + should "should perform constant time string comparison" do + Rack::Utils.secure_compare('a', 'a').should.equal true + Rack::Utils.secure_compare('a', 'b').should.equal false + end + should "return status code for integer" do Rack::Utils.status_code(200).should.equal 200 end