faraday-restrict-ip-addresses 0.0.2 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f59a971b3f267be404539a95d9b7feda50211b7
4
- data.tar.gz: f85376e90f9fb7ff8b483e176afb2884a3010db3
3
+ metadata.gz: 1871dc387a015770b7430ad24a714b444b49d5a2
4
+ data.tar.gz: 1279fa86ad652c4bd6f89f069e711e433ca9848c
5
5
  SHA512:
6
- metadata.gz: 9b97db3e5b0a806db906853175767c7a92c0c28e39af3a4606d4d0d6654bbff1d1479c38db8f87c2dab5e533e58b491219e6f9d4909db112e324700de2acfc6d
7
- data.tar.gz: 46e04a74e074757bebbfef126d24deb0bcf05ee42467bbc0f66ac68ae381e4de84a85ed32763d6fea6a7eff78ffb5b2fceca51d907f76b10e30d4a140f4ab976
6
+ metadata.gz: bafcae7575b7fe850f6f63de7a84f60fdee1e9049794a569282bda081c9c84c7bc513ccd9d1ccea860e873d8b4716eb0b269bea0a67c5397eb0b7113554c290f
7
+ data.tar.gz: a7b797a8fb5aa51ed2ff856e57c2220a40ff81fc6da8c44bdde17447a25a4142fddd017cce9a9a6d190dafec0513844a85390cba2e8c04e333c01f96d7597cd5
data/README.md CHANGED
@@ -14,7 +14,7 @@ Usage
14
14
  ```ruby
15
15
  faraday = Faraday.new do |builder|
16
16
  builder.request :url_encoded
17
- builder.use :restrict_ip_addresses, deny_rfc6890: true,
17
+ builder.request :restrict_ip_addresses, deny_rfc6890: true,
18
18
  allow_localhost: true,
19
19
  deny: ['8.0.0.0/8',
20
20
  '224.0.0.0/7'],
@@ -1,7 +1,7 @@
1
- require_relative 'lib/faraday/restrict_ip_addresses'
1
+ require_relative 'lib/faraday/restrict_ip_addresses/version'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
- spec.add_dependency 'faraday', ['>= 0.8', '< 0.9']
4
+ spec.add_dependency 'faraday', '~>0.9.0'
5
5
  spec.add_development_dependency 'bundler', '~> 1.0'
6
6
  spec.authors = ["Ben Lavender"]
7
7
  spec.description = %q{Restrict the IP addresses Faraday will connect to}
@@ -1,10 +1,9 @@
1
- require 'faraday'
1
+ require 'faraday/restrict_ip_addresses/version'
2
2
  require 'ipaddr'
3
3
 
4
4
  module Faraday
5
5
  class RestrictIPAddresses < Faraday::Middleware
6
6
  class AddressNotAllowed < Faraday::Error::ClientError ; end
7
- VERSION = '0.0.2'
8
7
 
9
8
  RFC_1918_NETWORKS = %w(
10
9
  127.0.0.0/8
@@ -48,7 +47,7 @@ module Faraday
48
47
  end
49
48
 
50
49
  def denied?(env)
51
- addresses(env[:url].host).any? { |a| denied_ip?(a) }
50
+ addresses(env[:url].hostname).any? { |a| denied_ip?(a) }
52
51
  end
53
52
 
54
53
  def denied_ip?(address)
@@ -60,9 +59,11 @@ module Faraday
60
59
  end
61
60
 
62
61
  def addresses(hostname)
63
- Socket.gethostbyname(hostname).map { |a| IPAddr.new_ntoh(a) rescue nil }.compact
62
+ Addrinfo.getaddrinfo(hostname, nil, :UNSPEC, :STREAM).map { |a| IPAddr.new(a.ip_address) }
63
+ rescue SocketError => e
64
+ # In case of invalid hostname, return an empty list of addresses
65
+ []
64
66
  end
65
67
  end
66
-
67
- register_middleware restrict_ip_addresses: lambda { RestrictIPAddresses }
68
+ Request.register_middleware restrict_ip_addresses: lambda { RestrictIPAddresses }
68
69
  end
@@ -0,0 +1,6 @@
1
+ require 'faraday'
2
+ module Faraday
3
+ class RestrictIPAddresses < Faraday::Middleware
4
+ VERSION = '0.1.1'
5
+ end
6
+ end
@@ -8,15 +8,9 @@ describe Faraday::RestrictIPAddresses do
8
8
 
9
9
  def allowed(*addresses)
10
10
  url = URI.parse("http://test.com")
11
- ips = addresses.map { |add| IPAddr.new(add).hton }
11
+ ips = addresses.map { |add| Addrinfo.tcp(add, nil) }
12
12
 
13
- # Socket returns a bunch of other stuff with gethostbyname. ipv6 addresses,
14
- # other socket information, whatever. We ignore it all internally and return
15
- # only valid ipv4 addresses, so just append what we're checking to some
16
- # garbage data like we expect.
17
- return_addresses = ['garbage', [], 30]
18
- return_addresses += ips
19
- Socket.expects(:gethostbyname).with(url.host).returns(return_addresses)
13
+ Addrinfo.expects(:getaddrinfo).with(url.host, nil, :UNSPEC, :STREAM).returns(ips)
20
14
 
21
15
  env = { url: url }
22
16
  @rip.call(env)
@@ -92,4 +86,33 @@ describe Faraday::RestrictIPAddresses do
92
86
  denied '192.168.13.14'
93
87
  end
94
88
 
89
+ it "blacklists normalized values" do
90
+ middleware deny_rfc6890: true,
91
+ allow_localhost: false
92
+
93
+ denied '0'
94
+ denied '0x0'
95
+ denied '0x00.0'
96
+ denied '00.0'
97
+ denied '127.0.0.1'
98
+ denied '0x7f.1'
99
+ denied '0177.1'
100
+ end
101
+
102
+ it "allows addresses for which DNS fails" do
103
+ middleware deny_rfc1918: true,
104
+ deny: ['8.0.0.0/8'],
105
+ allow: ['8.5.0.0/24', '192.168.14.0/24']
106
+ url = URI.parse("http://thisisanonexistinghostname.com")
107
+ Addrinfo.expects(:getaddrinfo).with(url.host, nil, :UNSPEC, :STREAM).raises(SocketError)
108
+ @rip.call(url: url)
109
+ end
110
+
111
+ it "works for IPV6 localhost addresses" do
112
+ middleware allow_localhost: false,
113
+ deny: ['::1']
114
+
115
+ denied '::1'
116
+ denied '0:0:0:0:0:0:0:1'
117
+ end
95
118
  end
metadata CHANGED
@@ -1,35 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-restrict-ip-addresses
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Lavender
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-24 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0.8'
20
- - - "<"
17
+ - - "~>"
21
18
  - !ruby/object:Gem::Version
22
- version: '0.9'
19
+ version: 0.9.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: '0.8'
30
- - - "<"
24
+ - - "~>"
31
25
  - !ruby/object:Gem::Version
32
- version: '0.9'
26
+ version: 0.9.0
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: bundler
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -55,6 +49,7 @@ files:
55
49
  - UNLICENSE
56
50
  - faraday-restrict-ip-addresses.gemspec
57
51
  - lib/faraday/restrict_ip_addresses.rb
52
+ - lib/faraday/restrict_ip_addresses/version.rb
58
53
  - spec/restrict_ip_addresses_spec.rb
59
54
  - spec/spec_helper.rb
60
55
  homepage: https://github.com/bhuga/faraday-restrict-ip-addresses