Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle null byte when serving static files #1574

Merged
merged 2 commits into from Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/sinatra/base.rb
Expand Up @@ -1057,8 +1057,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 = {})
jkowens marked this conversation as resolved.
Show resolved Hide resolved
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?
jkowens marked this conversation as resolved.
Show resolved Hide resolved
end

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