Skip to content

Commit 702da30

Browse files
committed
merge revision(s) 15677:
* lib/webrick/httpservlet/filehandler.rb: should normalize path separators in path_info to prevent directory traversal attacks on DOSISH platforms. reported by Digital Security Research Group [DSECRG-08-026]. * lib/webrick/httpservlet/filehandler.rb: pathnames which have not to be published should be checked case-insensitively. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_6@15678 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 2024133 commit 702da30

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
Mon Mar 3 23:34:13 2008 GOTOU Yuuzou <gotoyuzo@notwork.org>
2+
3+
* lib/webrick/httpservlet/filehandler.rb: should normalize path
4+
separators in path_info to prevent directory traversal attacks
5+
on DOSISH platforms.
6+
reported by Digital Security Research Group [DSECRG-08-026].
7+
8+
* lib/webrick/httpservlet/filehandler.rb: pathnames which have
9+
not to be published should be checked case-insensitively.
10+
111
Mon Dec 3 08:13:52 2007 Kouhei Sutou <kou@cozmixng.org>
212

313
* test/rss/test_taxonomy.rb, test/rss/test_parser_1.0.rb,

lib/webrick/httpservlet/filehandler.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def service(req, res)
163163
end
164164
end
165165
end
166+
prevent_directory_traversal(req, res)
166167
super(req, res)
167168
end
168169

@@ -198,6 +199,22 @@ def do_OPTIONS(req, res)
198199

199200
private
200201

202+
def prevent_directory_traversal(req, res)
203+
# Preventing directory traversal on DOSISH platforms;
204+
# Backslashes (0x5c) in path_info are not interpreted as special
205+
# character in URI notation. So the value of path_info should be
206+
# normalize before accessing to the filesystem.
207+
if File::ALT_SEPARATOR
208+
# File.expand_path removes the trailing path separator.
209+
# Adding a character is a workaround to save it.
210+
# File.expand_path("/aaa/") #=> "/aaa"
211+
# File.expand_path("/aaa/" + "x") #=> "/aaa/x"
212+
expanded = File.expand_path(req.path_info + "x")
213+
expanded[-1, 1] = "" # remove trailing "x"
214+
req.path_info = expanded
215+
end
216+
end
217+
201218
def exec_handler(req, res)
202219
raise HTTPStatus::NotFound, "`#{req.path}' not found" unless @root
203220
if set_filename(req, res)
@@ -256,7 +273,7 @@ def set_filename(req, res)
256273

257274
def check_filename(req, res, name)
258275
@options[:NondisclosureName].each{|pattern|
259-
if File.fnmatch("/#{pattern}", name)
276+
if File.fnmatch("/#{pattern}", name, File::FNM_CASEFOLD)
260277
@logger.warn("the request refers nondisclosure name `#{name}'.")
261278
raise HTTPStatus::NotFound, "`#{req.path}' not found."
262279
end
@@ -310,7 +327,7 @@ def call_callback(callback_name, req, res)
310327

311328
def nondisclosure_name?(name)
312329
@options[:NondisclosureName].each{|pattern|
313-
if File.fnmatch(pattern, name)
330+
if File.fnmatch(pattern, name, File::FNM_CASEFOLD)
314331
return true
315332
end
316333
}

test/webrick/test_filehandler.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "test/unit"
22
require "webrick"
33
require "stringio"
4+
require File.join(File.dirname(__FILE__), "utils.rb")
45

56
class WEBrick::TestFileHandler < Test::Unit::TestCase
67
def default_file_handler(filename)
@@ -62,4 +63,62 @@ def test_make_partial_content
6263
res = make_range_response(filename, "bytes=0-0, -2")
6364
assert_match(%r{^multipart/byteranges}, res["content-type"])
6465
end
66+
67+
def test_filehandler
68+
config = { :DocumentRoot => File.dirname(__FILE__), }
69+
this_file = File.basename(__FILE__)
70+
TestWEBrick.start_httpserver(config) do |server, addr, port|
71+
http = Net::HTTP.new(addr, port)
72+
req = Net::HTTP::Get.new("/")
73+
http.request(req){|res|
74+
assert_equal("200", res.code)
75+
assert_equal("text/html", res.content_type)
76+
assert_match(/HREF="#{this_file}"/, res.body)
77+
}
78+
req = Net::HTTP::Get.new("/#{this_file}")
79+
http.request(req){|res|
80+
assert_equal("200", res.code)
81+
assert_equal("text/plain", res.content_type)
82+
assert_equal(File.read(__FILE__), res.body)
83+
}
84+
end
85+
end
86+
87+
def test_non_disclosure_name
88+
config = { :DocumentRoot => File.dirname(__FILE__), }
89+
this_file = File.basename(__FILE__)
90+
TestWEBrick.start_httpserver(config) do |server, addr, port|
91+
http = Net::HTTP.new(addr, port)
92+
doc_root_opts = server[:DocumentRootOptions]
93+
doc_root_opts[:NondisclosureName] = %w(.ht* *~ test_*)
94+
req = Net::HTTP::Get.new("/")
95+
http.request(req){|res|
96+
assert_equal("200", res.code)
97+
assert_equal("text/html", res.content_type)
98+
assert_no_match(/HREF="#{File.basename(__FILE__)}"/, res.body)
99+
}
100+
req = Net::HTTP::Get.new("/#{this_file}")
101+
http.request(req){|res|
102+
assert_equal("404", res.code)
103+
}
104+
doc_root_opts[:NondisclosureName] = %w(.ht* *~ TEST_*)
105+
http.request(req){|res|
106+
assert_equal("404", res.code)
107+
}
108+
end
109+
end
110+
111+
def test_directory_traversal
112+
config = { :DocumentRoot => File.dirname(__FILE__), }
113+
this_file = File.basename(__FILE__)
114+
TestWEBrick.start_httpserver(config) do |server, addr, port|
115+
http = Net::HTTP.new(addr, port)
116+
req = Net::HTTP::Get.new("/../../")
117+
http.request(req){|res| assert_equal("400", res.code) }
118+
req = Net::HTTP::Get.new(
119+
"/..%5c..%5c..%5c..%5c..%5c..%5c..%5c..%5c..%5c..%5c..%5c..%5cboot.ini"
120+
)
121+
http.request(req){|res| assert_equal("404", res.code) }
122+
end
123+
end
65124
end

version.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#define RUBY_VERSION "1.8.6"
2-
#define RUBY_RELEASE_DATE "2007-12-03"
2+
#define RUBY_RELEASE_DATE "2008-03-03"
33
#define RUBY_VERSION_CODE 186
4-
#define RUBY_RELEASE_CODE 20071203
5-
#define RUBY_PATCHLEVEL 113
4+
#define RUBY_RELEASE_CODE 20080303
5+
#define RUBY_PATCHLEVEL 114
66

77
#define RUBY_VERSION_MAJOR 1
88
#define RUBY_VERSION_MINOR 8
99
#define RUBY_VERSION_TEENY 6
10-
#define RUBY_RELEASE_YEAR 2007
11-
#define RUBY_RELEASE_MONTH 12
10+
#define RUBY_RELEASE_YEAR 2008
11+
#define RUBY_RELEASE_MONTH 3
1212
#define RUBY_RELEASE_DAY 3
1313

1414
#ifdef RUBY_EXTERN

0 commit comments

Comments
 (0)