Skip to content

Commit 01a90ee

Browse files
normalhsbt
authored andcommitted
webrick: compile RE correctly for beginning and end match
Using ^ and $ in regexps means we can accidentally get fooled by "%0a" in HTTP request paths being decoded to newline characters. Use \A and \z to match beginning and end-of-string respectively, instead. Thanks to mame and hsbt for reporting. * lib/webrick/httpserver.rb (MountTable#compile): use \A and \z instead of ^ and $ * lib/webrick/httpserver.rb (MountTable#normalize): use \z instead of $ * test/webrick/test_httpserver.rb (test_cntrl_in_path): new test git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 0d2153f commit 01a90ee

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/webrick/httpserver.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ def compile
267267
k.sort!
268268
k.reverse!
269269
k.collect!{|path| Regexp.escape(path) }
270-
@scanner = Regexp.new("^(" + k.join("|") +")(?=/|$)")
270+
@scanner = Regexp.new("\\A(" + k.join("|") +")(?=/|\\z)")
271271
end
272272

273273
def normalize(dir)
274274
ret = dir ? dir.dup : ""
275-
ret.sub!(%r|/+$|, "")
275+
ret.sub!(%r|/+\z|, "")
276276
ret
277277
end
278278
end

test/webrick/test_httpserver.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,29 @@ def test_shutdown_with_busy_keepalive_connection
415415
}
416416
assert_equal(0, requested, "Server responded to #{requested} requests after shutdown")
417417
end
418+
419+
def test_cntrl_in_path
420+
log_ary = []
421+
access_log_ary = []
422+
config = {
423+
:Port => 0,
424+
:BindAddress => '127.0.0.1',
425+
:Logger => WEBrick::Log.new(log_ary, WEBrick::BasicLog::WARN),
426+
:AccessLog => [[access_log_ary, '']],
427+
}
428+
s = WEBrick::HTTPServer.new(config)
429+
s.mount('/foo', WEBrick::HTTPServlet::FileHandler, __FILE__)
430+
th = Thread.new { s.start }
431+
addr = s.listeners[0].addr
432+
433+
http = Net::HTTP.new(addr[3], addr[1])
434+
req = Net::HTTP::Get.new('/notexist%0a/foo')
435+
http.request(req) { |res| assert_equal('404', res.code) }
436+
exp = %Q(ERROR `/notexist\\n/foo' not found.\n)
437+
assert_equal 1, log_ary.size
438+
assert log_ary[0].include?(exp)
439+
ensure
440+
s&.shutdown
441+
th&.join
442+
end
418443
end

0 commit comments

Comments
 (0)