Class: SimpleRSS::RequestPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-rss/request_policy.rb

Constant Summary collapse

BLOCKED_IPV4 =
%w[
  0.0.0.0/8 10.0.0.0/8 100.64.0.0/10 127.0.0.0/8 169.254.0.0/16 172.16.0.0/12
  192.0.0.0/24 192.0.2.0/24 192.88.99.0/24 192.168.0.0/16 198.18.0.0/15
  198.51.100.0/24 203.0.113.0/24 224.0.0.0/4 240.0.0.0/4
].map { |range| IPAddr.new(range).freeze }.freeze
GLOBAL_IPV6 =
IPAddr.new("2000::/3").freeze
BLOCKED_IPV6 =
%w[2001::/23 2001:db8::/32 2002::/16 3fff::/20].map { |range| IPAddr.new(range).freeze }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy) ⇒ RequestPolicy

Returns a new instance of RequestPolicy.



19
20
21
22
23
24
25
# File 'lib/simple-rss/request_policy.rb', line 19

def initialize(policy)
  unless %i[public unrestricted].include?(policy) || policy.respond_to?(:call)
    raise ArgumentError, "network_policy must be :public, :unrestricted, or a callable"
  end

  @policy = policy
end

Class Method Details

.parse_url(value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/simple-rss/request_policy.rb', line 28

def self.parse_url(value)
  uri = URI.parse(value)
  raise SimpleRSS::PolicyError, "Expected an absolute HTTP or HTTPS URL" unless uri.is_a?(URI::HTTP)

  unless uri.hostname && !uri.hostname.to_s.empty? && uri.port&.between?(1, 65_535)
    raise SimpleRSS::PolicyError, "Expected a valid host and port"
  end
  raise SimpleRSS::PolicyError, "Credentials in URLs are not supported" if uri.userinfo

  uri.fragment = nil
  uri.path = "/" if uri.path.to_s.empty?
  uri.normalize
rescue URI::Error, TypeError
  raise SimpleRSS::PolicyError, "Malformed HTTP or HTTPS URL"
end

Instance Method Details

#address(uri) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/simple-rss/request_policy.rb', line 45

def address(uri)
  addresses = resolve(uri.hostname.to_s)
  raise SimpleRSS::RequestError, "No destination addresses were resolved" if addresses.empty?
  unless addresses.all? { |address| allowed?(uri, address) }
    raise SimpleRSS::PolicyError, "Destination address is prohibited by network_policy"
  end

  (addresses.find(&:ipv4?) || addresses.fetch(0)).to_s
end