@@ -1785,6 +1785,102 @@ def ip_app
17851785 req . trusted_proxy? ( "2001:470:1f0b:18f8::1" ) . must_equal false
17861786 end
17871787
1788+ it "uses rack.request.trusted_proxy env key when set to nil (default behavior)" do
1789+ # When nil, should fall back to ip_filter
1790+ env = Rack ::MockRequest . env_for ( "/" )
1791+ env [ 'rack.request.trusted_proxy' ] = nil
1792+ req = make_request ( env )
1793+
1794+ req . trusted_proxy? ( '127.0.0.1' ) . must_equal true
1795+ req . trusted_proxy? ( '10.0.0.1' ) . must_equal true
1796+ req . trusted_proxy? ( '192.168.1.1' ) . must_equal true
1797+ req . trusted_proxy? ( '1.2.3.4' ) . must_equal false
1798+ end
1799+
1800+ it "trusts all proxies when rack.request.trusted_proxy is true" do
1801+ env = Rack ::MockRequest . env_for ( "/" )
1802+ env [ 'rack.request.trusted_proxy' ] = true
1803+ req = make_request ( env )
1804+
1805+ req . trusted_proxy? ( '127.0.0.1' ) . must_equal true
1806+ req . trusted_proxy? ( '1.2.3.4' ) . must_equal true
1807+ req . trusted_proxy? ( '8.8.8.8' ) . must_equal true
1808+ req . trusted_proxy? ( '2001:470:1f0b:18f8::1' ) . must_equal true
1809+ end
1810+
1811+ it "trusts no proxies when rack.request.trusted_proxy is false" do
1812+ env = Rack ::MockRequest . env_for ( "/" )
1813+ env [ 'rack.request.trusted_proxy' ] = false
1814+ req = make_request ( env )
1815+
1816+ req . trusted_proxy? ( '127.0.0.1' ) . must_equal false
1817+ req . trusted_proxy? ( '10.0.0.1' ) . must_equal false
1818+ req . trusted_proxy? ( '192.168.1.1' ) . must_equal false
1819+ req . trusted_proxy? ( '1.2.3.4' ) . must_equal false
1820+ end
1821+
1822+ it "trusts only specified IPs when rack.request.trusted_proxy is an array" do
1823+ env = Rack ::MockRequest . env_for ( "/" )
1824+ env [ 'rack.request.trusted_proxy' ] = [ '10.0.0.1' , '192.168.1.100' ]
1825+ req = make_request ( env )
1826+
1827+ req . trusted_proxy? ( '10.0.0.1' ) . must_equal true
1828+ req . trusted_proxy? ( '192.168.1.100' ) . must_equal true
1829+ req . trusted_proxy? ( '10.0.0.2' ) . must_equal false
1830+ req . trusted_proxy? ( '192.168.1.101' ) . must_equal false
1831+ req . trusted_proxy? ( '127.0.0.1' ) . must_equal false
1832+ end
1833+
1834+ it "supports CIDR ranges in rack.request.trusted_proxy array" do
1835+ env = Rack ::MockRequest . env_for ( "/" )
1836+ env [ 'rack.request.trusted_proxy' ] = [ '10.0.0.0/24' , '192.168.1.0/28' ]
1837+ req = make_request ( env )
1838+
1839+ # 10.0.0.0/24 covers 10.0.0.0 - 10.0.0.255
1840+ req . trusted_proxy? ( '10.0.0.1' ) . must_equal true
1841+ req . trusted_proxy? ( '10.0.0.100' ) . must_equal true
1842+ req . trusted_proxy? ( '10.0.0.255' ) . must_equal true
1843+ req . trusted_proxy? ( '10.0.1.1' ) . must_equal false
1844+
1845+ # 192.168.1.0/28 covers 192.168.1.0 - 192.168.1.15
1846+ req . trusted_proxy? ( '192.168.1.5' ) . must_equal true
1847+ req . trusted_proxy? ( '192.168.1.15' ) . must_equal true
1848+ req . trusted_proxy? ( '192.168.1.16' ) . must_equal false
1849+ end
1850+
1851+ it "supports IPv6 addresses in rack.request.trusted_proxy array" do
1852+ env = Rack ::MockRequest . env_for ( "/" )
1853+ env [ 'rack.request.trusted_proxy' ] = [ '2001:db8::1' , 'fd00::/8' ]
1854+ req = make_request ( env )
1855+
1856+ req . trusted_proxy? ( '2001:db8::1' ) . must_equal true
1857+ req . trusted_proxy? ( '2001:db8::2' ) . must_equal false
1858+ req . trusted_proxy? ( 'fd00::1' ) . must_equal true
1859+ req . trusted_proxy? ( 'fd00::ffff' ) . must_equal true
1860+ req . trusted_proxy? ( 'fe00::1' ) . must_equal false
1861+ end
1862+
1863+ it "handles invalid IP addresses gracefully in rack.request.trusted_proxy" do
1864+ env = Rack ::MockRequest . env_for ( "/" )
1865+ env [ 'rack.request.trusted_proxy' ] = [ '10.0.0.1' , 'invalid-ip' ]
1866+ req = make_request ( env )
1867+
1868+ req . trusted_proxy? ( '10.0.0.1' ) . must_equal true
1869+ req . trusted_proxy? ( 'invalid-ip' ) . must_equal true # Direct string match
1870+ req . trusted_proxy? ( '192.168.1.1' ) . must_equal false
1871+ end
1872+
1873+ it "can use Rack::Config to set rack.request.trusted_proxy" do
1874+ app = lambda { |env | [ 200 , { } , [ Rack ::Request . new ( env ) . trusted_proxy? ( '8.8.8.8' ) . to_s ] ] }
1875+ config_app = Rack ::Config . new ( app ) do |env |
1876+ env [ 'rack.request.trusted_proxy' ] = true
1877+ end
1878+
1879+ mock = Rack ::MockRequest . new ( config_app )
1880+ res = mock . get '/'
1881+ res . body . must_equal 'true'
1882+ end
1883+
17881884 it "sets the default session to an empty hash" do
17891885 req = make_request ( Rack ::MockRequest . env_for ( "http://example.com:8080/" ) )
17901886 session = req . session
0 commit comments