Skip to content

Commit

Permalink
Some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiyt committed Sep 1, 2023
1 parent 7f0b048 commit 7d3d68b
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions lib/ssrf_filter/ssrf_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,14 @@ class CRLFInjection < Error
::SsrfFilter::Patch::SSLSocket.apply!

original_url = url
scheme_whitelist = options[:scheme_whitelist] || DEFAULT_SCHEME_WHITELIST
resolver = options[:resolver] || DEFAULT_RESOLVER
allow_unfollowed_redirects = options.fetch(:allow_unfollowed_redirects) { DEFAULT_ALLOW_UNFOLLOWED_REDIRECTS }
max_redirects = options[:max_redirects] || DEFAULT_MAX_REDIRECTS
scheme_whitelist = options.fetch(:scheme_whitelist, DEFAULT_SCHEME_WHITELIST)
resolver = options.fetch(:resolver, DEFAULT_RESOLVER)
allow_unfollowed_redirects = options.fetch(:allow_unfollowed_redirects, DEFAULT_ALLOW_UNFOLLOWED_REDIRECTS)
max_redirects = options.fetch(:max_redirects, DEFAULT_MAX_REDIRECTS)
url = url.to_s

redirects = 0

loop do
response = nil
(max_redirects + 1).times do
uri = URI(url)

unless scheme_whitelist.include?(uri.scheme)
Expand All @@ -132,17 +131,11 @@ class CRLFInjection < Error
raise PrivateIPAddress, "Hostname '#{hostname}' has no public ip addresses" if public_addresses.empty?

response, url = fetch_once(uri, public_addresses.sample.to_s, method, options, &block)

break response if url.nil?

if max_redirects <= redirects
break response if allow_unfollowed_redirects

raise TooManyRedirects, "Got #{max_redirects} redirects fetching #{original_url}"
end

redirects += 1
return response if url.nil?
end

return response if allow_unfollowed_redirects
raise TooManyRedirects, "Got #{max_redirects} redirects fetching #{original_url}"
end
end

Expand Down Expand Up @@ -208,10 +201,10 @@ def self.fetch_once(uri, ip, verb, options, &block)
url = response['location']
# Handle relative redirects
url = "#{uri.scheme}://#{hostname}:#{uri.port}#{url}" if url.start_with?('/')
return response, url
else
return response, nil
url = nil
end
return response, url
end
end
end
Expand Down

0 comments on commit 7d3d68b

Please sign in to comment.