Skip to content

Commit 511b283

Browse files
kenjisMGatner
authored andcommitted
test: add tests for IPv6
1 parent 900bc84 commit 511b283

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,23 @@ public function testGetIPAddressThruProxy()
728728
$this->assertSame($expected, $this->request->getIPAddress());
729729
}
730730

731+
public function testGetIPAddressThruProxyIPv6()
732+
{
733+
$expected = '123.123.123.123';
734+
$_SERVER['HTTP_X_FORWARDED_FOR'] = $expected;
735+
$_SERVER['REMOTE_ADDR'] = '2001:db8::2:1';
736+
737+
$config = new App();
738+
$config->proxyIPs = [
739+
'2001:db8::2:1' => 'X-Forwarded-For',
740+
];
741+
$this->request = new Request($config);
742+
$this->request->populateHeaders();
743+
744+
// we should see the original forwarded address
745+
$this->assertSame($expected, $this->request->getIPAddress());
746+
}
747+
731748
public function testGetIPAddressThruProxyInvalidIPAddress()
732749
{
733750
$_SERVER['HTTP_X_FORWARDED_FOR'] = '123.456.23.123';
@@ -746,6 +763,23 @@ public function testGetIPAddressThruProxyInvalidIPAddress()
746763
$this->assertSame($expected, $this->request->getIPAddress());
747764
}
748765

766+
public function testGetIPAddressThruProxyInvalidIPAddressIPv6()
767+
{
768+
$_SERVER['HTTP_X_FORWARDED_FOR'] = '2001:xyz::1';
769+
$expected = '2001:db8::2:1';
770+
$_SERVER['REMOTE_ADDR'] = $expected;
771+
772+
$config = new App();
773+
$config->proxyIPs = [
774+
'2001:db8::2:1' => 'X-Forwarded-For',
775+
];
776+
$this->request = new Request($config);
777+
$this->request->populateHeaders();
778+
779+
// spoofed address invalid
780+
$this->assertSame($expected, $this->request->getIPAddress());
781+
}
782+
749783
public function testGetIPAddressThruProxyNotWhitelisted()
750784
{
751785
$expected = '10.10.1.200';
@@ -764,6 +798,23 @@ public function testGetIPAddressThruProxyNotWhitelisted()
764798
$this->assertSame($expected, $this->request->getIPAddress());
765799
}
766800

801+
public function testGetIPAddressThruProxyNotWhitelistedIPv6()
802+
{
803+
$expected = '2001:db8::2:2';
804+
$_SERVER['REMOTE_ADDR'] = $expected;
805+
$_SERVER['HTTP_X_FORWARDED_FOR'] = '123.456.23.123';
806+
807+
$config = new App();
808+
$config->proxyIPs = [
809+
'2001:db8::2:1' => 'X-Forwarded-For',
810+
];
811+
$this->request = new Request($config);
812+
$this->request->populateHeaders();
813+
814+
// spoofed address invalid
815+
$this->assertSame($expected, $this->request->getIPAddress());
816+
}
817+
767818
public function testGetIPAddressThruProxySubnet()
768819
{
769820
$expected = '123.123.123.123';
@@ -779,6 +830,21 @@ public function testGetIPAddressThruProxySubnet()
779830
$this->assertSame($expected, $this->request->getIPAddress());
780831
}
781832

833+
public function testGetIPAddressThruProxySubnetIPv6()
834+
{
835+
$expected = '123.123.123.123';
836+
$_SERVER['HTTP_X_FORWARDED_FOR'] = $expected;
837+
$_SERVER['REMOTE_ADDR'] = '2001:db8:1234:ffff:ffff:ffff:ffff:ffff';
838+
839+
$config = new App();
840+
$config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For'];
841+
$this->request = new Request($config);
842+
$this->request->populateHeaders();
843+
844+
// we should see the original forwarded address
845+
$this->assertSame($expected, $this->request->getIPAddress());
846+
}
847+
782848
public function testGetIPAddressThruProxyOutOfSubnet()
783849
{
784850
$expected = '192.168.5.21';
@@ -794,6 +860,21 @@ public function testGetIPAddressThruProxyOutOfSubnet()
794860
$this->assertSame($expected, $this->request->getIPAddress());
795861
}
796862

863+
public function testGetIPAddressThruProxyOutOfSubnetIPv6()
864+
{
865+
$expected = '2001:db8:1235:ffff:ffff:ffff:ffff:ffff';
866+
$_SERVER['REMOTE_ADDR'] = $expected;
867+
$_SERVER['HTTP_X_FORWARDED_FOR'] = '123.123.123.123';
868+
869+
$config = new App();
870+
$config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For'];
871+
$this->request = new Request($config);
872+
$this->request->populateHeaders();
873+
874+
// we should see the original forwarded address
875+
$this->assertSame($expected, $this->request->getIPAddress());
876+
}
877+
797878
public function testGetIPAddressThruProxyInvalidConfigString()
798879
{
799880
$this->expectException(ConfigException::class);

0 commit comments

Comments
 (0)