discourse/lib/freedom_patches/net_http.rb
Alan Guo Xiang Tan 2492fe7715
FIX: Set sane default for Net::HTTP when processing a request (#28141)
This commit patches `Net::HTTP` to reduce the default timeouts of 60
seconds when we are processing a request. There are certain routes in
Discourse which makes external requests and if the proper timeouts are
not set, we risk having the Unicorn master process force restarting the
Unicorn workers once the `30` seconds timeout is reached. This can
potentially become a vector for DoS attacks and this commit is aimed at
reducing the risk here.
2024-08-06 07:12:42 +08:00

27 lines
646 B
Ruby

# frozen_string_literal: true
module NetHTTPPatch
OPEN_TIMEOUT = 5
READ_TIMEOUT = 10
WRITE_TIMEOUT = 5
# By default Net::HTTP will retry 1 time on idempotent requests but we can't afford that while processing a request
# so setting it to 0
MAX_RETIRES = 0
def initialize(*args, &block)
super(*args, &block)
## START PATCH
if Thread.current[Middleware::ProcessingRequest::PROCESSING_REQUEST_THREAD_KEY]
self.open_timeout = OPEN_TIMEOUT
self.read_timeout = READ_TIMEOUT
self.write_timeout = WRITE_TIMEOUT
self.max_retries = 0
end
## END PATCH
end
end
Net::HTTP.prepend(NetHTTPPatch)