mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 12:38:29 +08:00
This commit is contained in:
parent
500d0f6daf
commit
cf0a0945e4
|
@ -1,7 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class FinalDestination
|
class FinalDestination::HTTP < Net::HTTP
|
||||||
module SSRFSafeNetHTTP
|
|
||||||
def connect
|
def connect
|
||||||
original_open_timeout = @open_timeout
|
original_open_timeout = @open_timeout
|
||||||
return super if @ipaddr
|
return super if @ipaddr
|
||||||
|
@ -11,8 +10,7 @@ class FinalDestination
|
||||||
# This iteration through addresses would normally happen in Socket#tcp
|
# This iteration through addresses would normally happen in Socket#tcp
|
||||||
# We do it here because we're tightly controlling addresses rather than
|
# We do it here because we're tightly controlling addresses rather than
|
||||||
# handing Socket#tcp a hostname
|
# handing Socket#tcp a hostname
|
||||||
ips =
|
ips = FinalDestination::SSRFDetector.lookup_and_filter_ips(@address, timeout: @connect_timeout)
|
||||||
FinalDestination::SSRFDetector.lookup_and_filter_ips(@address, timeout: @connect_timeout)
|
|
||||||
|
|
||||||
ips.each_with_index do |ip, index|
|
ips.each_with_index do |ip, index|
|
||||||
debug "[FinalDestination] Attempting connection to #{ip}..."
|
debug "[FinalDestination] Attempting connection to #{ip}..."
|
||||||
|
@ -40,8 +38,3 @@ class FinalDestination
|
||||||
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HTTP < ::Net::HTTP
|
|
||||||
include SSRFSafeNetHTTP
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
|
@ -7,15 +7,7 @@ WebMock::HttpLibAdapterRegistry.instance.register(
|
||||||
|
|
||||||
def self.enable!
|
def self.enable!
|
||||||
FinalDestination.send(:remove_const, :HTTP)
|
FinalDestination.send(:remove_const, :HTTP)
|
||||||
|
FinalDestination.send(:const_set, :HTTP, Net::HTTP)
|
||||||
# At this point, `Net::HTTP` has already been patched by WebMock so we need to re-declare `FinalDestination::HTTP`
|
|
||||||
# but inherit from the patched `Net::HTTP` class. This is to allow requests made using `FinalDestination::HTTP` to be
|
|
||||||
# intercepted by WebMock.
|
|
||||||
FinalDestination.send(
|
|
||||||
:const_set,
|
|
||||||
:HTTP,
|
|
||||||
Class.new(Net::HTTP) { include FinalDestination::SSRFSafeNetHTTP },
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.disable!
|
def self.disable!
|
||||||
|
|
Loading…
Reference in New Issue
Block a user