vpndetection-rails 2.0.12 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d511872aa8a56af6ea3c30fb1eb836ed375675ea33a0b42034a2d2994fd0b5b
4
- data.tar.gz: 6584b5f34d60360fa5238861dded3d02fd18b354294cb30f353a2d984c6e7d73
3
+ metadata.gz: 07d5e7f61527230dafda869e0d485ff5110d17f4ab9a604ec6c3f849936098de
4
+ data.tar.gz: d180e9320cd0b384ba041b73a1cec1db0777b67a22f7e2158c57531783a61fa8
5
5
  SHA512:
6
- metadata.gz: d451eb799f2ff065469f7e04d4ea256304fd0779da596f89e6388f94c6da2dcd3505bb179d88f6b9053067ffe10348e7261cd1365b3910ef94ca6a6d9dc16266
7
- data.tar.gz: f0eb5bc90e4e58172d99f12e71b27817c41677b205d80de1f143f8cc32ce50aabc3022dbb5dc37cf3fb73fda7b74e1481e21ac4b7a534ad1075dea9303779f7a
6
+ metadata.gz: 3a95e9cd846e5e082d0774cf64cb70ad924fa265b480799523605a5af80c6084662f74d6877b1b992015fdc94d017a30f9be0e2e5bd7df9f04658e40e308af3e
7
+ data.tar.gz: bc6188deef708beca49c3db019cc9af4a4714b3bb264429477cb8932e5048197538479558241f25dcc2cb85a5dae03bc18ce99faec73b6c4da010c78720be51c
data/README.md CHANGED
@@ -70,7 +70,7 @@ on_blocked: ->(request, lookup) { [303, { "location" => "/no-vpn" }, []] }
70
70
 
71
71
  This is the setting that decides whether any of the above works, and it is the one thing only you can get right.
72
72
 
73
- **In Rails**, the default reads `env["action_dispatch.remote_ip"]` — the answer `ActionDispatch::RemoteIp` computed from `X-Forwarded-For` minus your `config.action_dispatch.trusted_proxies`. That is the right fix behind a load balancer: tell Rails which proxies are yours and it resolves the visitor for you. `Rack::Request#ip` does *not* read that entry, so reading Rails' own is what makes your `trusted_proxies` mean anything here. **With nothing in front of the app server, that default can be forged**: `RemoteIp` believes `X-Forwarded-For` without checking that the connection came from a proxy, so any visitor picks the address that gets looked up. There, pass `ip_selector: ->(request) { request.ip }`.
73
+ **In Rails**, the default reads `env["action_dispatch.remote_ip"]` — the answer `ActionDispatch::RemoteIp` computed from `X-Forwarded-For` minus your `config.action_dispatch.trusted_proxies`. That is the right fix behind a load balancer: tell Rails which proxies are yours and it resolves the visitor for you. `Rack::Request#ip` does *not* read that entry, so reading Rails' own is what makes your `trusted_proxies` mean anything here. **With nothing in front of the app server, that default can be forged**: `RemoteIp` believes `X-Forwarded-For` without checking that the connection came from a proxy, so any visitor picks the address that gets looked up. There, pass `ip_selector: ->(request) { request.ip }`. The middleware warns once when it sees that setup: a request carrying `X-Forwarded-For` or `Client-IP` from a public address outside `trusted_proxies`.
74
74
 
75
75
  **In a bare Rack app** there is no such entry and the default falls back to `Rack::Request#ip`. Rack's `ip` returns the connecting address, `REMOTE_ADDR`, unless that is a trusted proxy, which by default means a loopback or private address. Only then does it read `X-Forwarded-For`, and it takes the right-most entry that is not a trusted proxy. The test suite pins both. Behind an edge on a public address, or one that appends rather than overwrites, name your edge's header instead:
76
76
 
@@ -21,6 +21,9 @@ module VPNDetection
21
21
  def initialize(app, **options)
22
22
  @app = app
23
23
  @on_blocked = options.delete(:on_blocked) || method(:refuse)
24
+ # Only the default selector reads Rails' remote_ip.
25
+ @check_forgery = !options.key?(:ip_selector)
26
+ @on_warn = options[:on_warn]
24
27
  @core = VPNDetection::Middleware::Core.new(
25
28
  Rails.default_ip_selector, **options
26
29
  )
@@ -28,6 +31,7 @@ module VPNDetection
28
31
 
29
32
  def call(env)
30
33
  request = ::Rack::Request.new(env)
34
+ warn_forgeable(request) if @check_forgery
31
35
  lookup = @core.evaluate(request)
32
36
  return @app.call(env) if lookup.nil?
33
37
 
@@ -39,6 +43,20 @@ module VPNDetection
39
43
 
40
44
  private
41
45
 
46
+ # Once: the setup is the same on every request.
47
+ def warn_forgeable(request)
48
+ peer = Rails.forgeable_peer(request)
49
+ return if peer.nil?
50
+
51
+ @check_forgery = false
52
+ message = "a request carrying X-Forwarded-For came from #{peer}, a public address outside " \
53
+ 'config.action_dispatch.trusted_proxies, so Rails takes the visitor from that header, ' \
54
+ 'which anyone connecting directly can set. If a proxy sits in front of this app, ' \
55
+ 'add its addresses to trusted_proxies; if nothing does, pass ' \
56
+ 'ip_selector: ->(request) { request.ip }.'
57
+ @on_warn ? @on_warn.call(message) : Kernel.warn("[vpndetection] #{message}")
58
+ end
59
+
42
60
  def refuse(_request, _lookup)
43
61
  body = JSON.generate({ 'error' => 'access denied' })
44
62
  [403, { 'content-type' => 'application/json' }, [body]]
@@ -25,12 +25,17 @@ module VPNDetection
25
25
  # and using Rack's alone would silently ignore the trusted_proxies you
26
26
  # configured.
27
27
  #
28
+ # **With nothing in front of the app server, Rails' answer can be forged**:
29
+ # `RemoteIp` believes `X-Forwarded-For` from any peer outside
30
+ # trusted_proxies, so a visitor names the address looked up. The middleware
31
+ # says so once when it sees that setup.
32
+ #
28
33
  # Outside Rails there is no such entry and this falls back to
29
- # `Rack::Request#ip`. **Be aware that Rack's own `ip` already trusts
30
- # `X-Forwarded-For`**: it returns the left-most entry after dropping
31
- # private and loopback addresses, which is whatever the caller sent.
32
- # Measured, not assumed. In a bare Rack app behind nothing, prefer
33
- # {.header_ip_selector} or your own.
34
+ # `Rack::Request#ip`. Under Rack 3 that is the peer itself unless the peer
35
+ # is a trusted proxy (loopback or private by default). Rack 2 returned the
36
+ # left-most `X-Forwarded-For` entry after dropping private and loopback
37
+ # addresses, which is whatever the caller sent: there, behind nothing,
38
+ # prefer {.header_ip_selector} or your own.
34
39
  def default_ip_selector
35
40
  SELECTORS.default
36
41
  end
@@ -45,6 +50,39 @@ module VPNDetection
45
50
  request.ip
46
51
  end
47
52
 
53
+ # @api private
54
+ # The peer, when Rails' RemoteIp let a header that peer sent name the
55
+ # visitor: `X-Forwarded-For` or `Client-IP` from a public address outside
56
+ # the proxies RemoteIp trusts, its defaults or trusted_proxies.
57
+ def forgeable_peer(request)
58
+ remote_ip = request.get_header('action_dispatch.remote_ip')
59
+ return nil if remote_ip.nil? || !forwarded?(request)
60
+
61
+ peer = request.get_header('REMOTE_ADDR').to_s
62
+ return nil unless public_address?(peer)
63
+
64
+ # RemoteIp's own list, from the object it leaves in env; Rails has kept
65
+ # it there as @proxies since 4.0.
66
+ proxies = remote_ip.instance_variable_get(:@proxies)
67
+ return nil unless proxies.respond_to?(:any?)
68
+ return nil if proxies.any? { |proxy| proxy === peer } # rubocop:disable Style/CaseEquality
69
+
70
+ peer
71
+ end
72
+
73
+ # @api private
74
+ def forwarded?(request)
75
+ %w[HTTP_X_FORWARDED_FOR HTTP_CLIENT_IP].any? { |name| !request.get_header(name).to_s.empty? }
76
+ end
77
+
78
+ # @api private
79
+ def public_address?(ip)
80
+ IPAddr.new(ip)
81
+ !VPNDetection::Bogon.bogon?(ip)
82
+ rescue IPAddr::Error
83
+ false
84
+ end
85
+
48
86
  # An address from `X-Forwarded-For`, ignoring Rails' trusted-proxy list.
49
87
  #
50
88
  # The LEFT-MOST entry (depth 0) is whatever the caller sent, because proxies
@@ -2,6 +2,6 @@
2
2
 
3
3
  module VPNDetection
4
4
  module Rails
5
- VERSION = '2.0.12'
5
+ VERSION = '2.1.1'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vpndetection-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.12
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mslm Dev
@@ -29,7 +29,7 @@ dependencies:
29
29
  requirements:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 5.6.3
32
+ version: 5.6.4
33
33
  - - "<"
34
34
  - !ruby/object:Gem::Version
35
35
  version: '6'
@@ -39,7 +39,7 @@ dependencies:
39
39
  requirements:
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: 5.6.3
42
+ version: 5.6.4
43
43
  - - "<"
44
44
  - !ruby/object:Gem::Version
45
45
  version: '6'