1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
# frozen_string_literal: true
require_relative 'helper'
require 'tempfile'
require 'fileutils'
describe Rack::Directory do
DOCROOT = File.expand_path(File.dirname(__FILE__)) unless defined? DOCROOT
FILE_CATCH = proc{|env| [200, { 'Content-Type' => 'text/plain', "Content-Length" => "7" }, ['passed!']] }
attr_reader :app
def setup
@app = Rack::Lint.new(Rack::Directory.new(DOCROOT, FILE_CATCH))
end
it 'serves directories with + in the name' do
Dir.mktmpdir do |dir|
plus_dir = "foo+bar"
full_dir = File.join(dir, plus_dir)
FileUtils.mkdir full_dir
FileUtils.touch File.join(full_dir, "omg.txt")
app = Rack::Directory.new(dir, FILE_CATCH)
env = Rack::MockRequest.env_for("/#{plus_dir}/")
status, _, body = app.call env
assert_equal 200, status
str = ''.dup
body.each { |x| str << x }
assert_match "foo+bar", str
end
end
it "serve directory indices" do
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/")
res.must_be :ok?
assert_match(res, /<html><head>/)
end
it "serve directory indices with bad symlinks" do
begin
File.symlink('foo', 'test/cgi/foo')
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/")
res.must_be :ok?
assert_match(res, /<html><head>/)
ensure
File.delete('test/cgi/foo')
end
end
it "return 404 for unreadable directories" do
begin
File.write('test/cgi/unreadable', '')
File.chmod(0, 'test/cgi/unreadable')
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/unreadable")
res.status.must_equal 404
ensure
File.delete('test/cgi/unreadable')
end
end
it "pass to app if file found" do
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/test")
res.must_be :ok?
assert_match(res, /passed!/)
end
it "serve uri with URL encoded filenames" do
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/%63%67%69/") # "/cgi/test"
res.must_be :ok?
assert_match(res, /<html><head>/)
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/%74%65%73%74") # "/cgi/test"
res.must_be :ok?
assert_match(res, /passed!/)
end
it "serve uri with URL encoded null byte (%00) in filenames" do
res = Rack::MockRequest.new(Rack::Lint.new(app))
.get("/cgi/test%00")
res.must_be :bad_request?
end
it "allow directory traversal inside root directory" do
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/../rackup")
res.must_be :ok?
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/%2E%2E/rackup")
res.must_be :ok?
end
it "not allow directory traversal" do
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/../../lib")
res.must_be :forbidden?
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/%2E%2E/%2E%2E/lib")
res.must_be :forbidden?
end
it "404 if it can't find the file" do
res = Rack::MockRequest.new(Rack::Lint.new(app)).
get("/cgi/blubb")
res.must_be :not_found?
end
it "uri escape path parts" do # #265, properly escape file names
mr = Rack::MockRequest.new(Rack::Lint.new(app))
res = mr.get("/cgi/test%2bdirectory")
res.must_be :ok?
res.body.must_match(Regexp.new(Rack::Utils.escape_html(
"/cgi/test\\+directory/test\\+file")))
res = mr.get("/cgi/test%2bdirectory/test%2bfile")
res.must_be :ok?
end
it "correctly escape script name with spaces" do
Dir.mktmpdir do |dir|
space_dir = "foo bar"
full_dir = File.join(dir, space_dir)
FileUtils.mkdir full_dir
FileUtils.touch File.join(full_dir, "omg omg.txt")
app = Rack::Directory.new(dir, FILE_CATCH)
env = Rack::MockRequest.env_for(Rack::Utils.escape_path("/#{space_dir}/"))
status, _, body = app.call env
assert_equal 200, status
str = ''.dup
body.each { |x| str << x }
assert_match Rack::Utils.escape_html("/foo%20bar/omg%20omg.txt"), str
end
end
it "correctly escape script name with '" do
Dir.mktmpdir do |dir|
quote_dir = "foo'bar"
full_dir = File.join(dir, quote_dir)
FileUtils.mkdir full_dir
FileUtils.touch File.join(full_dir, "omg'omg.txt")
app = Rack::Directory.new(dir, FILE_CATCH)
env = Rack::MockRequest.env_for(Rack::Utils.escape("/#{quote_dir}/"))
status, _, body = app.call env
assert_equal 200, status
str = ''.dup
body.each { |x| str << x }
assert_match Rack::Utils.escape_html("/foo'bar/omg'omg.txt"), str
end
end
it "correctly escape script name" do
_app = app
app2 = Rack::Builder.new do
map '/script-path' do
run _app
end
end
mr = Rack::MockRequest.new(Rack::Lint.new(app2))
res = mr.get("/script-path/cgi/test%2bdirectory")
res.must_be :ok?
res.body.must_match(Regexp.new(Rack::Utils.escape_html(
"/script-path/cgi/test\\+directory/test\\+file")))
res = mr.get("/script-path/cgi/test+directory/test+file")
res.must_be :ok?
end
it "return error when file not found for head request" do
res = Rack::MockRequest.new(Rack::Lint.new(app)).
head("/cgi/missing")
res.must_be :not_found?
res.body.must_be :empty?
end
end
|