Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Fixes trust proxy REMOTE_ADDR not working in Swoole #52889

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading