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

fix: prevent HEAD requests from writing body in streamhandler #1113

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/Handlers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ function streamhandler(handler)
request.response::Response = handler(request)
request.response.request = request
startwrite(stream)
write(stream, request.response.body)
if request.method != "HEAD"
write(stream, request.response.body)
end
return
end
end
Expand Down
19 changes: 19 additions & 0 deletions test/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,23 @@ end # @testset
@test occursin(r"^\*/\* text/plain HEAD / HTTP/1\.1 HEAD / 127\.0\.0\.1 \d+ - HTTP/1\.1 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.* \d+/.*/\d{4}:\d{2}:\d{2}:\d{2}.* 200 0$", logs[4].message)
end

@testset "HEAD request without body" begin
sometext = "This is a big body that we don't want returned during a head"
handler = req -> begin
return HTTP.Response(200, [], sometext)
end
server = HTTP.serve!(handler; listenany=true)
port = HTTP.port(server)

response = HTTP.head("http://localhost:$port")
@test response.status == 200
@test String(response.body) == ""

response = HTTP.get("http://localhost:$port")
@test response.status == 200
@test String(response.body) == sometext

close(server)
end
Comment on lines +316 to +333
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The very interesting thing here is that this fails with

HEAD request without body: Error During Test at /home/pgeorgakopoulos/pluto/HTTP.jl/test/server.jl:316
  Got exception outside of a @test
  HTTP.RequestError:
  HTTP.Request:
  HTTP.Messages.Request:
  """
  GET / HTTP/1.1
  Host: localhost:8081
  Accept: */*
  User-Agent: HTTP.jl/1.10.0-rc1
  Content-Length: 0
  Accept-Encoding: gzip
  
  """Underlying error:
  HTTP.Parsers.ParseError(:INVALID_STATUS_LINE, "2e\r")
...
caused by: TaskFailedException
  
      nested task error: HTTP.Parsers.ParseError(:INVALID_STATUS_LINE, "2e\r")
...

caused by: HTTP.Parsers.ParseError(:INVALID_STATUS_LINE, "This is a big body that we don't want returnedHTTP/1.1 200 OK\r")
      Stacktrace:
       [1] parse_status_line!(bytes::String, response::HTTP.Messages.Response)
         @ HTTP.Parsers ~/pluto/HTTP.jl/src/Parsers.jl:206

without this PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which is very similar to a request smuggling attack.


end # module
Loading