Skip to content

Commit

Permalink
Handle null byte when serving requests for paths with null bytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
makushline committed Jan 17, 2020
1 parent 192bc9b commit d7313ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/sinatra/base.rb
Expand Up @@ -1057,13 +1057,16 @@ 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 unless File.file?(path)

env['sinatra.static_file'] = path
public_dir = settings.public_folder
return if public_dir.nil?
path = "#{public_dir}#{URI_INSTANCE.unescape(request.path_info)}"
return unless valid_path?(path)
expanded_path = File.expand_path(path)
return unless File.file?(expanded_path)

env['sinatra.static_file'] = expanded_path
cache_control(*settings.static_cache_control) if settings.static_cache_control?
send_file path, options.merge(:disposition => nil)
send_file expanded_path, options.merge(:disposition => nil)
end

# Run the block with 'throw :halt' support and apply result to the response.
Expand Down
5 changes: 5 additions & 0 deletions test/static_test.rb
Expand Up @@ -55,6 +55,11 @@ class StaticTest < Minitest::Test
end

it 'does not serve directories' do
get "/foo%00"
assert not_found?
end

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

0 comments on commit d7313ca

Please sign in to comment.