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

Return a 400 Bad Request when path encoding is invalid #514

Merged
merged 1 commit into from
Nov 14, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket

## Master

- Return a `400 Bad Request` when the path encoding is invalid. [#514]

## 4.0.0.beta5

- Reduce string allocations
Expand Down
13 changes: 13 additions & 0 deletions lib/sprockets/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def call(env)
# Extract the path from everything after the leading slash
path = Rack::Utils.unescape(env['PATH_INFO'].to_s.sub(/^\//, ''))

unless path.valid_encoding?
return bad_request_response(env)
end

# Strip fingerprint
if fingerprint = path_fingerprint(path)
path = path.sub("-#{fingerprint}", '')
Expand Down Expand Up @@ -131,6 +135,15 @@ def not_modified_response(env, etag)
[ 304, cache_headers(env, etag), [] ]
end

# Returns a 400 Forbidden response tuple
def bad_request_response(env)
if head_request?(env)
[ 400, { "Content-Type" => "text/plain", "Content-Length" => "0" }, [] ]
else
[ 400, { "Content-Type" => "text/plain", "Content-Length" => "11" }, [ "Bad Request" ] ]
end
end

# Returns a 403 Forbidden response tuple
def forbidden_response(env)
if head_request?(env)
Expand Down
5 changes: 5 additions & 0 deletions test/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,9 @@ def app
delete "/assets/foo.js"
assert_equal 405, last_response.status
end

test "invalid URLs" do
get "/assets/%E2%EF%BF%BD%A6.js"
assert_equal 400, last_response.status
end
end