Skip to content

Commit 931b041

Browse files
committed
fix: use header() instead of getServer() for header values
getIPAddress() does not work with OpenSwoole.
1 parent d8cf84a commit 931b041

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

system/HTTP/RequestTrait.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,13 @@ public function getIPAddress(): string
6767
$this->ipAddress = $this->getServer('REMOTE_ADDR');
6868

6969
if ($proxyIPs) {
70-
foreach (['HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP'] as $header) {
71-
if (($spoof = $this->getServer($header)) !== null) {
70+
foreach (['x-forwarded-for', 'client-ip', 'x-client-ip', 'x-cluster-client-ip'] as $header) {
71+
$spoof = null;
72+
$headerObj = $this->header($header);
73+
74+
if ($headerObj !== null) {
75+
$spoof = $headerObj->getValue();
76+
7277
// Some proxies typically list the whole chain of IP
7378
// addresses through which the client has reached us.
7479
// e.g. client_ip, proxy_ip1, proxy_ip2, etc.

tests/system/HTTP/RequestTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ public function testGetIPAddressThruProxy()
613613
$config->proxyIPs = '10.0.1.200,192.168.5.0/24';
614614
$_SERVER['HTTP_X_FORWARDED_FOR'] = $expected;
615615
$this->request = new Request($config);
616+
$this->request->populateHeaders();
616617

617618
// we should see the original forwarded address
618619
$this->assertSame($expected, $this->request->getIPAddress());
@@ -626,6 +627,7 @@ public function testGetIPAddressThruProxyInvalid()
626627
$config->proxyIPs = '10.0.1.200,192.168.5.0/24';
627628
$_SERVER['HTTP_X_FORWARDED_FOR'] = $expected;
628629
$this->request = new Request($config);
630+
$this->request->populateHeaders();
629631

630632
// spoofed address invalid
631633
$this->assertSame('10.0.1.200', $this->request->getIPAddress());
@@ -639,6 +641,7 @@ public function testGetIPAddressThruProxyNotWhitelisted()
639641
$config->proxyIPs = '10.0.1.200,192.168.5.0/24';
640642
$_SERVER['HTTP_X_FORWARDED_FOR'] = $expected;
641643
$this->request = new Request($config);
644+
$this->request->populateHeaders();
642645

643646
// spoofed address invalid
644647
$this->assertSame('10.10.1.200', $this->request->getIPAddress());
@@ -652,6 +655,7 @@ public function testGetIPAddressThruProxySubnet()
652655
$config->proxyIPs = ['192.168.5.0/24'];
653656
$_SERVER['HTTP_X_FORWARDED_FOR'] = $expected;
654657
$this->request = new Request($config);
658+
$this->request->populateHeaders();
655659

656660
// we should see the original forwarded address
657661
$this->assertSame($expected, $this->request->getIPAddress());
@@ -665,6 +669,7 @@ public function testGetIPAddressThruProxyOutofSubnet()
665669
$config->proxyIPs = ['192.168.5.0/28'];
666670
$_SERVER['HTTP_X_FORWARDED_FOR'] = $expected;
667671
$this->request = new Request($config);
672+
$this->request->populateHeaders();
668673

669674
// we should see the original forwarded address
670675
$this->assertSame('192.168.5.21', $this->request->getIPAddress());

0 commit comments

Comments
 (0)