27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/wafris/proxy_filter.rb', line 27
def self.set_filter
user_defined_proxies = ENV["TRUSTED_PROXY_RANGES"].split(",") if ENV["TRUSTED_PROXY_RANGES"]
valid_ipv4_octet = /\.(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])/
trusted_proxies = Regexp.union(
/\A127#{valid_ipv4_octet}{3}\z/, /\A::1\z/, /\Af[cd][0-9a-f]{2}(?::[0-9a-f]{0,4}){0,7}\z/i, /\A10#{valid_ipv4_octet}{3}\z/, /\A172\.(1[6-9]|2[0-9]|3[01])#{valid_ipv4_octet}{2}\z/, /\A192\.168#{valid_ipv4_octet}{2}\z/, /\Alocalhost\z|\Aunix(\z|:)/i, /\A103\.21\.24[4-7]#{valid_ipv4_octet}\z/, /\A103\.22\.20[0-3]#{valid_ipv4_octet}\z/, /\A103\.31\.[4-7]#{valid_ipv4_octet}\z/, /\A104\.(1[6-9]|2[0-3])#{valid_ipv4_octet}{2}\z/, /\A104\.2[4-7]#{valid_ipv4_octet}{2}\z/, /\A108\.162\.192#{valid_ipv4_octet}\z/, /\A162\.15[8-9]#{valid_ipv4_octet}{2}\z/, /\A172\.(6[4-9]|7[0-1])#{valid_ipv4_octet}{2}\z/, *user_defined_proxies
)
Rack::Request.ip_filter = lambda { |ip| trusted_proxies.match?(ip) }
end
|