Skip to content

Commit

Permalink
adding test to ensure is reading text-based configurations and conver…
Browse files Browse the repository at this point in the history
…ting it correctly.
  • Loading branch information
rrpadilla committed Dec 19, 2018
1 parent cbb4433 commit f8b1a68
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/trustedproxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* directly to your server, use an array or a string separated by comma of IP addresses:
*/
// 'proxies' => ['192.168.1.1'],
// 'proxies' => '192.168.1.1, 192.168.1.1',
// 'proxies' => '192.168.1.1, 192.168.1.2',

/*
* Or, to trust all proxies that connect
Expand Down
4 changes: 2 additions & 2 deletions src/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function setTrustedProxyIpAddresses(Request $request)
}

// Support IPs addresses separated by comma
$trustedIps = is_string($trustedIps) ? explode(',', $trustedIps) : $trustedIps;
$trustedIps = is_string($trustedIps) ? array_map('trim', explode(',', $trustedIps)) : $trustedIps;

// Only trust specific IP addresses
if (is_array($trustedIps)) {
Expand Down Expand Up @@ -105,7 +105,7 @@ private function setTrustedProxyIpAddressesToTheCallingIp(Request $request)
/**
* Retrieve trusted header name(s), falling back to defaults if config not set.
*
* @return array
* @return int A bit field of Request::HEADER_*, to set which headers to trust from your proxies.
*/
protected function getTrustedHeaderNames()
{
Expand Down
44 changes: 40 additions & 4 deletions tests/TrustedProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public function test_trusted_proxy_sets_trusted_proxies_with_double_wildcard_for
});
}



/**
* Test the most typical usage of TrustProxies:
* Trusted X-Forwarded-For header
Expand Down Expand Up @@ -182,6 +180,44 @@ public function test_can_distrust_headers()
});
}

/**
* Test to ensure it's reading text-based configurations and converting it correctly.
*/
public function test_is_reading_text_based_configurations()
{
$request = $this->createProxiedRequest();

// trust *all* "X-Forwarded-*" headers
$trustedProxy = $this->createTrustedProxy('HEADER_X_FORWARDED_ALL', '192.168.1.1, 192.168.1.2');
$trustedProxy->handle($request, function (Request $request) {
$this->assertEquals($request->getTrustedHeaderSet(), Request::HEADER_X_FORWARDED_ALL,
'Assert trusted proxy used all "X-Forwarded-*" header');

$this->assertEquals($request->getTrustedProxies(), ['192.168.1.1', '192.168.1.2'],
'Assert trusted proxy using proxies as string separated by comma.');
});

// or, if your proxy instead uses the "Forwarded" header
$trustedProxy = $this->createTrustedProxy('HEADER_FORWARDED', '192.168.1.1, 192.168.1.2');
$trustedProxy->handle($request, function (Request $request) {
$this->assertEquals($request->getTrustedHeaderSet(), Request::HEADER_FORWARDED,
'Assert trusted proxy used forwarded header');

$this->assertEquals($request->getTrustedProxies(), ['192.168.1.1', '192.168.1.2'],
'Assert trusted proxy using proxies as string separated by comma.');
});

// or, if you're using AWS ELB
$trustedProxy = $this->createTrustedProxy('HEADER_X_FORWARDED_AWS_ELB', '192.168.1.1, 192.168.1.2');
$trustedProxy->handle($request, function (Request $request) {
$this->assertEquals($request->getTrustedHeaderSet(), Request::HEADER_X_FORWARDED_AWS_ELB,
'Assert trusted proxy used AWS ELB header');

$this->assertEquals($request->getTrustedProxies(), ['192.168.1.1', '192.168.1.2'],
'Assert trusted proxy using proxies as string separated by comma.');
});
}

################################################################
# Utility Functions
################################################################
Expand Down Expand Up @@ -219,8 +255,8 @@ protected function createProxiedRequest($serverOverRides = [])
/**
* Retrieve a TrustProxies object, with dependencies mocked.
*
* @param array $trustedHeaders
* @param array $trustedProxies
* @param null|string|int $trustedHeaders
* @param null|array|string $trustedProxies
*
* @return \Fideloper\Proxy\TrustProxies
*/
Expand Down

0 comments on commit f8b1a68

Please sign in to comment.