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

Support custom trusted header bitmasks #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions src/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ private function setTrustedProxyIpAddressesToTheCallingIp(Request $request)
protected function getTrustedHeaderNames()
{
$headers = $this->headers ?: $this->config->get('trustedproxy.headers');

if (is_int($headers)) {
return $headers;
}

switch ($headers) {
case 'HEADER_X_FORWARDED_AWS_ELB':
case Request::HEADER_X_FORWARDED_AWS_ELB:
return Request::HEADER_X_FORWARDED_AWS_ELB;
break;
case 'HEADER_FORWARDED':
case Request::HEADER_FORWARDED:
return Request::HEADER_FORWARDED;
break;
default:
return Request::HEADER_X_FORWARDED_ALL;
}

// Should never reach this point
return $headers;
}
}
16 changes: 16 additions & 0 deletions tests/TrustedProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ public function test_is_reading_text_based_configurations()
});
}

public function test_can_use_custom_header_bitmasks()
{
$request = $this->createProxiedRequest();

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

################################################################
# Utility Functions
################################################################
Expand Down