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

Prevent static middleware from attempting to serve a request with a null byte #23045

Merged
merged 1 commit into from
Jan 15, 2016
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
6 changes: 5 additions & 1 deletion actionpack/lib/action_dispatch/middleware/static.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def initialize(root, cache_control)

def match?(path)
path = URI.parser.unescape(path)
return false unless path.valid_encoding?
return false unless valid_path?(path)

paths = [path, "#{path}#{ext}", "#{path}/index#{ext}"].map { |v|
Rack::Utils.clean_path_info v
Expand Down Expand Up @@ -86,6 +86,10 @@ def gzip_file_path(path)
false
end
end

def valid_path?(path)
path.valid_encoding? && !path.include?("\0")
end
end

# This middleware will attempt to return the contents of a file's body from
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/dispatch/static_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def test_handles_urls_with_ascii_8bit_on_win_31j
assert_equal "Hello, World!", get("/doorkeeper%E3E4".force_encoding('ASCII-8BIT')).body
end

def test_handles_urls_with_null_byte
assert_equal "Hello, World!", get("/doorkeeper%00").body
end

def test_sets_cache_control
response = get("/index.html")
assert_html "/index.html", response
Expand Down