File tree 2 files changed +29
-3
lines changed
2 files changed +29
-3
lines changed Original file line number Diff line number Diff line change @@ -161,6 +161,13 @@ def send_file(reqfile, content_type)
161
161
reqfile = File . join ( @dir , reqfile )
162
162
return render_not_found if !F . exists? ( reqfile )
163
163
164
+ if reqfile == File . realpath ( reqfile )
165
+ # reqfile looks legit: no path traversal, no leading '|'
166
+ else
167
+ # reqfile does not look trustworthy; abort
168
+ return render_not_found
169
+ end
170
+
164
171
@res = Rack ::Response . new
165
172
@res . status = 200
166
173
@res [ "Content-Type" ] = content_type
@@ -189,10 +196,13 @@ def send_file(reqfile, content_type)
189
196
def get_git_dir ( path )
190
197
root = @config [ :project_root ] || Dir . pwd
191
198
path = File . join ( root , path )
192
- if File . exists? ( path ) # TODO: check is a valid git directory
193
- return path
199
+ if !File . exists? ( path )
200
+ false
201
+ elsif File . realpath ( path ) != path # looks like path traversal
202
+ false
203
+ else
204
+ path # TODO: check is a valid git directory
194
205
end
195
- false
196
206
end
197
207
198
208
def get_service_type
Original file line number Diff line number Diff line change @@ -183,6 +183,22 @@ def test_git_config_upload_pack
183
183
assert_equal 404 , session . last_response . status
184
184
end
185
185
186
+ def test_send_file
187
+ app1 = app
188
+ app1 . instance_variable_set ( :@dir , Dir . pwd )
189
+ # Reject path traversal
190
+ assert_equal 404 , app1 . send_file ( 'tests/../tests' , 'text/plain' ) . first
191
+ # Reject paths starting with '|', avoid File.read('|touch /tmp/pawned; ls /tmp')
192
+ assert_equal 404 , app1 . send_file ( '|tests' , 'text/plain' ) . first
193
+ end
194
+
195
+ def test_get_git_dir
196
+ # Guard against non-existent directories
197
+ assert_equal false , app . get_git_dir ( 'foobar' )
198
+ # Guard against path traversal
199
+ assert_equal false , app . get_git_dir ( '/../tests' )
200
+ end
201
+
186
202
private
187
203
188
204
def r
You can’t perform that action at this time.
0 commit comments