Skip to content

Commit

Permalink
Fix directory traversal vulnerability in static file route [sinatra#177]
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Mar 9, 2009
1 parent 01b1f49 commit ed98aed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/sinatra/base.rb
Expand Up @@ -884,7 +884,9 @@ def metadef(message, &block)
# static files route
get(/.*[^\/]$/) do
pass unless options.static? && options.public?
path = options.public + unescape(request.path_info)
public_dir = File.expand_path(options.public)
path = File.expand_path(public_dir + unescape(request.path_info))
pass if path[0, public_dir.length] != public_dir
pass unless File.file?(path)
send_file path, :disposition => nil
end
Expand Down
15 changes: 15 additions & 0 deletions test/static_test.rb
Expand Up @@ -62,4 +62,19 @@
get "/foobarbaz.txt"
assert not_found?
end

it 'serves files when .. path traverses within public directory' do
get "/data/../#{File.basename(__FILE__)}"
assert ok?
assert_equal File.read(__FILE__), body
end

it '404s when .. path traverses outside of public directory' do
mock_app {
set :static, true
set :public, File.dirname(__FILE__) + '/data'
}
get "/../#{File.basename(__FILE__)}"
assert not_found?
end
end

0 comments on commit ed98aed

Please sign in to comment.