Skip to content

Commit

Permalink
[11.x] Fixes trust proxy REMOTE_ADDR not working in Swoole (#52889)
Browse files Browse the repository at this point in the history
* fix: trust proxy REMOTE_ADDR not working in Swoole

* formatting

* add test case for trusted proxy with REMOTE_ADDR

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
chuoke and taylorotwell authored Sep 27, 2024
1 parent 0e88b9c commit 7aabb89
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Http/Middleware/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ protected function setTrustedProxyIpAddresses(Request $request)
*/
protected function setTrustedProxyIpAddressesToSpecificIps(Request $request, array $trustedIps)
{
$request->setTrustedProxies($trustedIps, $this->getTrustedHeaderNames());
$request->setTrustedProxies(array_reduce($trustedIps, function ($ips, $trustedIp) use ($request) {
$ips[] = $trustedIp === 'REMOTE_ADDR'
? $request->server->get('REMOTE_ADDR')
: $trustedIp;

return $ips;
}, []), $this->getTrustedHeaderNames());
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Http/Middleware/TrustProxiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ public function test_trusted_proxy_sets_trusted_proxies_with_double_wildcard_for
});
}

/**
* Test the next most typical usage of TrustedProxies:
* Trusted X-Forwarded-For header, REMOTE_ADDR for TrustedProxies.
*/
public function test_trusted_proxy_sets_trusted_proxies_with_REMOTE_ADDR()
{
$trustedProxy = $this->createTrustedProxy($this->headerAll, 'REMOTE_ADDR');
$request = $this->createProxiedRequest();

$trustedProxy->handle($request, function ($request) {
$this->assertSame('173.174.200.38', $request->getClientIp(), 'Assert trusted proxy x-forwarded-for header used with REMOTE_ADDR proxy setting');
});
}

/**
* Test the most typical usage of TrustProxies:
* Trusted X-Forwarded-For header.
Expand Down

0 comments on commit 7aabb89

Please sign in to comment.