Skip to content

Commit 9038b4b

Browse files
committed
Merge branch 'rrpadilla-master'
2 parents 10e2170 + 207af4b commit 9038b4b

File tree

3 files changed

+76
-17
lines changed

3 files changed

+76
-17
lines changed

config/trustedproxy.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@
1515
* of your proxy (e.g. if using ELB or similar).
1616
*
1717
*/
18-
'proxies' => null, // [<ip addresses>,], '*'
18+
'proxies' => null, // [<ip addresses>,], '*', '<ip addresses>,'
1919

2020
/*
2121
* To trust one or more specific proxies that connect
22-
* directly to your server, use an array of IP addresses:
22+
* directly to your server, use an array or a string separated by comma of IP addresses:
2323
*/
24-
# 'proxies' => ['192.168.1.1'],
24+
// 'proxies' => ['192.168.1.1'],
25+
// 'proxies' => '192.168.1.1, 192.168.1.2',
2526

2627
/*
2728
* Or, to trust all proxies that connect
2829
* directly to your server, use a "*"
2930
*/
30-
# 'proxies' => '*',
31+
// 'proxies' => '*',
3132

3233
/*
3334
* Which headers to use to detect proxy related data (For, Host, Proto, Port)
@@ -36,10 +37,14 @@
3637
*
3738
* - Illuminate\Http\Request::HEADER_X_FORWARDED_ALL (use all x-forwarded-* headers to establish trust)
3839
* - Illuminate\Http\Request::HEADER_FORWARDED (use the FORWARDED header to establish trust)
40+
* - Illuminate\Http\Request::HEADER_X_FORWARDED_AWS_ELB (If you are using AWS Elastic Load Balancer)
41+
*
42+
* - 'HEADER_X_FORWARDED_ALL' (use all x-forwarded-* headers to establish trust)
43+
* - 'HEADER_FORWARDED' (use the FORWARDED header to establish trust)
44+
* - 'HEADER_X_FORWARDED_AWS_ELB' (If you are using AWS Elastic Load Balancer)
3945
*
4046
* @link https://symfony.com/doc/current/deployment/proxies.html
4147
*/
4248
'headers' => Illuminate\Http\Request::HEADER_X_FORWARDED_ALL,
4349

44-
4550
];

src/TrustProxies.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,19 @@ protected function setTrustedProxyIpAddresses(Request $request)
6666
{
6767
$trustedIps = $this->proxies ?: $this->config->get('trustedproxy.proxies');
6868

69-
// Only trust specific IP addresses
70-
if (is_array($trustedIps)) {
71-
return $this->setTrustedProxyIpAddressesToSpecificIps($request, $trustedIps);
72-
}
73-
7469
// Trust any IP address that calls us
75-
// `**` for backwards compatibility, but is depreciated
70+
// `**` for backwards compatibility, but is deprecated
7671
if ($trustedIps === '*' || $trustedIps === '**') {
7772
return $this->setTrustedProxyIpAddressesToTheCallingIp($request);
7873
}
74+
75+
// Support IPs addresses separated by comma
76+
$trustedIps = is_string($trustedIps) ? array_map('trim', explode(',', $trustedIps)) : $trustedIps;
77+
78+
// Only trust specific IP addresses
79+
if (is_array($trustedIps)) {
80+
return $this->setTrustedProxyIpAddressesToSpecificIps($request, $trustedIps);
81+
}
7982
}
8083

8184
/**
@@ -102,10 +105,25 @@ private function setTrustedProxyIpAddressesToTheCallingIp(Request $request)
102105
/**
103106
* Retrieve trusted header name(s), falling back to defaults if config not set.
104107
*
105-
* @return array
108+
* @return int A bit field of Request::HEADER_*, to set which headers to trust from your proxies.
106109
*/
107110
protected function getTrustedHeaderNames()
108111
{
109-
return $this->headers ?: $this->config->get('trustedproxy.headers');
112+
$headers = $this->headers ?: $this->config->get('trustedproxy.headers');
113+
switch ($headers) {
114+
case 'HEADER_X_FORWARDED_AWS_ELB':
115+
case Request::HEADER_X_FORWARDED_AWS_ELB:
116+
return Request::HEADER_X_FORWARDED_AWS_ELB;
117+
break;
118+
case 'HEADER_FORWARDED':
119+
case Request::HEADER_FORWARDED:
120+
return Request::HEADER_FORWARDED;
121+
break;
122+
default:
123+
return Request::HEADER_X_FORWARDED_ALL;
124+
}
125+
126+
// Should never reach this point
127+
return $headers;
110128
}
111129
}

tests/TrustedProxyTest.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ public function test_trusted_proxy_sets_trusted_proxies_with_double_wildcard_for
7070
});
7171
}
7272

73-
74-
7573
/**
7674
* Test the most typical usage of TrustProxies:
7775
* Trusted X-Forwarded-For header
@@ -182,6 +180,44 @@ public function test_can_distrust_headers()
182180
});
183181
}
184182

183+
/**
184+
* Test to ensure it's reading text-based configurations and converting it correctly.
185+
*/
186+
public function test_is_reading_text_based_configurations()
187+
{
188+
$request = $this->createProxiedRequest();
189+
190+
// trust *all* "X-Forwarded-*" headers
191+
$trustedProxy = $this->createTrustedProxy('HEADER_X_FORWARDED_ALL', '192.168.1.1, 192.168.1.2');
192+
$trustedProxy->handle($request, function (Request $request) {
193+
$this->assertEquals($request->getTrustedHeaderSet(), Request::HEADER_X_FORWARDED_ALL,
194+
'Assert trusted proxy used all "X-Forwarded-*" header');
195+
196+
$this->assertEquals($request->getTrustedProxies(), ['192.168.1.1', '192.168.1.2'],
197+
'Assert trusted proxy using proxies as string separated by comma.');
198+
});
199+
200+
// or, if your proxy instead uses the "Forwarded" header
201+
$trustedProxy = $this->createTrustedProxy('HEADER_FORWARDED', '192.168.1.1, 192.168.1.2');
202+
$trustedProxy->handle($request, function (Request $request) {
203+
$this->assertEquals($request->getTrustedHeaderSet(), Request::HEADER_FORWARDED,
204+
'Assert trusted proxy used forwarded header');
205+
206+
$this->assertEquals($request->getTrustedProxies(), ['192.168.1.1', '192.168.1.2'],
207+
'Assert trusted proxy using proxies as string separated by comma.');
208+
});
209+
210+
// or, if you're using AWS ELB
211+
$trustedProxy = $this->createTrustedProxy('HEADER_X_FORWARDED_AWS_ELB', '192.168.1.1, 192.168.1.2');
212+
$trustedProxy->handle($request, function (Request $request) {
213+
$this->assertEquals($request->getTrustedHeaderSet(), Request::HEADER_X_FORWARDED_AWS_ELB,
214+
'Assert trusted proxy used AWS ELB header');
215+
216+
$this->assertEquals($request->getTrustedProxies(), ['192.168.1.1', '192.168.1.2'],
217+
'Assert trusted proxy using proxies as string separated by comma.');
218+
});
219+
}
220+
185221
################################################################
186222
# Utility Functions
187223
################################################################
@@ -219,8 +255,8 @@ protected function createProxiedRequest($serverOverRides = [])
219255
/**
220256
* Retrieve a TrustProxies object, with dependencies mocked.
221257
*
222-
* @param array $trustedHeaders
223-
* @param array $trustedProxies
258+
* @param null|string|int $trustedHeaders
259+
* @param null|array|string $trustedProxies
224260
*
225261
* @return \Fideloper\Proxy\TrustProxies
226262
*/

0 commit comments

Comments
 (0)