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

Made HTTPS detection a bit better. #160

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/JwtAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}

/* HTTP allowed only if secure is false or server is in relaxed array. */
if ("https" !== $scheme && true === $this->options["secure"]) {
if (false === $this->isHttps($request) && true === $this->options["secure"]) {
if (!in_array($host, $this->options["relaxed"])) {
$message = sprintf(
"Insecure use of middleware over %s denied by configuration.",
Expand Down Expand Up @@ -278,6 +278,26 @@ private function decodeToken(string $token): array
}
}

/**
* Checks if the request was sent over HTTPS.
*
* This is done by checking if either the URL scheme or a
* 'X-Forwarded-Proto' header is equal to 'https' (the latter is useful
* when running behind a load balancer).
*/
private function isHttps(ServerRequestInterface $request): bool
{
$scheme = $request->getUri()->getScheme();
$x_fwd_proto_headers = $request->getHeader('X-Forwarded-Proto');

$sanitized = array_map('trim', array_map('strtolower', $x_fwd_proto_headers));

return (
'https' === $scheme
|| in_array('https', $sanitized, true)
);
}

/**
* Hydrate options from given array.
*/
Expand Down