Skip to content

Commit

Permalink
Merge pull request from GHSA-7xx3-m584-x994
Browse files Browse the repository at this point in the history
could monopolize a thread. Previously, this could make a DoS attack more
severe.

Co-authored-by: Evan Phoenix <evan@phx.io>
  • Loading branch information
nateberkopec and evanphx committed Dec 5, 2019
1 parent d20242b commit 98a1f03
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/puma/const.rb
Expand Up @@ -118,6 +118,13 @@ module Const
# sending data back
WRITE_TIMEOUT = 10

# How many requests to attempt inline before sending a client back to
# the reactor to be subject to normal ordering. The idea here is that
# we amortize the cost of going back to the reactor for a well behaved
# but very "greedy" client across 10 requests. This prevents a not
# well behaved client from monopolizing the thread forever.
MAX_FAST_INLINE = 10

# The original URI requested by the client.
REQUEST_URI= 'REQUEST_URI'.freeze
REQUEST_PATH = 'REQUEST_PATH'.freeze
Expand Down
16 changes: 15 additions & 1 deletion lib/puma/server.rb
Expand Up @@ -466,6 +466,8 @@ def process_client(client, buffer)
clean_thread_locals = @options[:clean_thread_locals]
close_socket = true

requests = 0

while true
case handle_request(client, buffer)
when false
Expand All @@ -479,7 +481,19 @@ def process_client(client, buffer)

ThreadPool.clean_thread_locals if clean_thread_locals

unless client.reset(@status == :run)
requests += 1

check_for_more_data = @status == :run

if requests >= MAX_FAST_INLINE
# This will mean that reset will only try to use the data it already
# has buffered and won't try to read more data. What this means is that
# every client, independent of their request speed, gets treated like a slow
# one once every MAX_FAST_INLINE requests.
check_for_more_data = false
end

unless client.reset(check_for_more_data)
close_socket = false
client.set_timeout @persistent_timeout
@reactor.add client
Expand Down

0 comments on commit 98a1f03

Please sign in to comment.