Skip to content

Commit

Permalink
Handle null byte when serving static files (#1574)
Browse files Browse the repository at this point in the history
Handle null byte when serving requests for paths with null bytes.
  • Loading branch information
makushline committed Mar 13, 2020
1 parent 1f29a6d commit 3cc2394
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/sinatra/base.rb
Expand Up @@ -1058,8 +1058,11 @@ def route_missing
# Attempt to serve static files from public directory. Throws :halt when
# a matching file is found, returns nil otherwise.
def static!(options = {})
return if (public_dir = settings.public_folder).nil?
path = File.expand_path("#{public_dir}#{URI_INSTANCE.unescape(request.path_info)}" )
return if (public_dir = settings.public_folder).nil?
path = "#{public_dir}#{URI_INSTANCE.unescape(request.path_info)}"
return unless valid_path?(path)

path = File.expand_path(path)
return unless File.file?(path)

env['sinatra.static_file'] = path
Expand Down
5 changes: 5 additions & 0 deletions test/static_test.rb
Expand Up @@ -59,6 +59,11 @@ class StaticTest < Minitest::Test
assert not_found?
end

it 'passes to the next handler when the path contains null bytes' do
get "/foo%00"
assert not_found?
end

it 'passes to the next handler when the static option is disabled' do
@app.set :static, false
get "/#{File.basename(__FILE__)}"
Expand Down

0 comments on commit 3cc2394

Please sign in to comment.