Loop with String#scan without creating substrings
authorNobuyoshi Nakada <nobu@ruby-lang.org>
Tue, 13 Aug 2019 03:14:28 +0000 (12:14 +0900)
committerSalvatore Bonaccorso <carnil@debian.org>
Sun, 15 Dec 2019 16:28:25 +0000 (16:28 +0000)
Create the substrings necessary parts only, instead of cutting the
rest of the buffer.  Also removed a useless, probable typo, regexp.

Gbp-Pq: Name Loop-with-String-scan-without-creating-substrings.patch

lib/webrick/httpauth/digestauth.rb
test/webrick/test_httpauth.rb

index 415c94a0c6b67cf673d1d44152c4d4836c2007f5..978091e78b97619745fbd405a8e7a3d09036fafa 100644 (file)
@@ -290,23 +290,8 @@ module WEBrick
 
       def split_param_value(string)
         ret = {}
-        while string.bytesize != 0
-          case string
-          when /^\s*([\w\-\.\*\%\!]+)=\s*\"((\\.|[^\"])*)\"\s*,?/
-            key = $1
-            matched = $2
-            string = $'
-            ret[key] = matched.gsub(/\\(.)/, "\\1")
-          when /^\s*([\w\-\.\*\%\!]+)=\s*([^,\"]*),?/
-            key = $1
-            matched = $2
-            string = $'
-            ret[key] = matched.clone
-          when /^s*^,/
-            string = $'
-          else
-            break
-          end
+        string.scan(/\G\s*([\w\-.*%!]+)=\s*(?:\"((?>\\.|[^\"])*)\"|([^,\"]*))\s*,?/) do
+          ret[$1] = $3 || $2.gsub(/\\(.)/, "\\1")
         end
         ret
       end
index b6376b5fd68dac62ea2ce2b4c5ad5df4f2cab0b5..baadaf6eb2ea4bbe2c0c2d7ece23269a5654efbd 100644 (file)
@@ -287,6 +287,28 @@ class TestWEBrickHTTPAuth < Test::Unit::TestCase
     }
   end
 
+  def test_digest_auth_invalid
+    digest_auth = WEBrick::HTTPAuth::DigestAuth.new(Realm: 'realm', UserDB: '')
+
+    def digest_auth.error(fmt, *)
+    end
+
+    def digest_auth.try_bad_request(len)
+      request = {"Authorization" => %[Digest a="#{'\b'*len}]}
+      authenticate request, nil
+    end
+
+    bad_request = WEBrick::HTTPStatus::BadRequest
+    t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+    assert_raise(bad_request) {digest_auth.try_bad_request(10)}
+    limit = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - t0)
+    [20, 50, 100, 200].each do |len|
+      assert_raise(bad_request) do
+        Timeout.timeout(len*limit) {digest_auth.try_bad_request(len)}
+      end
+    end
+  end
+
   private
   def credentials_for_request(user, password, params, body = nil)
     cnonce = "hoge"