From f8b1a68e2ca5b227211769f75d5db02ce2ea92e2 Mon Sep 17 00:00:00 2001 From: Rodolfo Ruiz Date: Tue, 18 Dec 2018 17:39:27 -0800 Subject: [PATCH] adding test to ensure is reading text-based configurations and converting it correctly. --- config/trustedproxy.php | 2 +- src/TrustProxies.php | 4 ++-- tests/TrustedProxyTest.php | 44 ++++++++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/config/trustedproxy.php b/config/trustedproxy.php index 9eccc0d..e242b0d 100644 --- a/config/trustedproxy.php +++ b/config/trustedproxy.php @@ -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 diff --git a/src/TrustProxies.php b/src/TrustProxies.php index 690633c..470f86c 100644 --- a/src/TrustProxies.php +++ b/src/TrustProxies.php @@ -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)) { @@ -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() { diff --git a/tests/TrustedProxyTest.php b/tests/TrustedProxyTest.php index 68e6c35..e526936 100644 --- a/tests/TrustedProxyTest.php +++ b/tests/TrustedProxyTest.php @@ -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 @@ -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 ################################################################ @@ -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 */