Skip to content
Advertisement

Protocol being switched during redirects

In my CakePHP 3.10 application, the redirects are changing the protocols and breaking the app. This is being deployed to an App Service (PHP 7.4) in Azure.

I’m not seeing this on another LAMP stack (RHEL, Apache 2.4, PHP 7.3, https configured) on premise.

Example, logging out of the application.

JavaScript

During inspection of the traffic (via Edge’s > Inspect > Network), here’s what I see (notice Response Header Location change from https to http):

General

JavaScript

Response Headers

JavaScript

Request Headers

JavaScript

Advertisement

Answer

Like many load balancers, the Azure ones also like to terminate SSL, so that the requests that are reaching PHP will be unencrypted, making the env('HTTPS') lookup fail that CakePHP applications use by default to build the full base URL.

JavaScript

https://github.com/cakephp/app/blob/3.10.1/config/bootstrap.php#L131-L148

The linked Azure docs suggest checking the HTTP_X_FORWARDED_PROTO header instead, which is being populated by the load balancer, but you need to take into account that depending on the environment that the application runs in, this header could also have been set by the client, so I wouldn’t generally recommend using it.

I would instead suggest to hardcode the protocol, or better yet, set the complete full base URL manually (at least in the past HTTP_HOST has been a problem too, as some servers accepted custom values sent from the client), eg:

JavaScript

or set App.fullBaseUrl in your config/app.php (or config/app_local.php).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement