cakephp 在重定向期间切换协议

busg9geu  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(143)

在我的CakePHP 3.10应用程序中,重定向更改了协议并破坏了应用程序。这将被部署到Azure中的应用程序服务(PHP 7.4)。
我没有看到这在另一个LAMP堆栈(RHEL,Apache 2.4,PHP 7.3,https配置)的前提下。
例如,注销应用程序。

public function logout()
{
    $this->getRequest()->getSession()->write('isAdmin',false);
    $this->Flash->success(__('You are now logged out.'));
    return $this->redirect($this->Auth->logout());
}

在检查流量期间(通过Edge的〉Inspect〉Network),我看到了以下内容(请注意,响应头位置从https更改为http):
一般情况

Request URL: https://my.domain.com/users/logout
Request Method: GET
Status Code: 302 Found
Remote Address: my.ip.here:443
Referrer Policy: strict-origin-when-cross-origin

回应信头

Cache-Control: no-store, no-cache, must-revalidate
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Sun, 19 Dec 2021 13:08:05 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Location: http://my.domain.com/
Pragma: no-cache
Server: Apache
Set-Cookie: CAKEPHP=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; HttpOnly
Set-Cookie: CAKEPHP=c2be7c7d45c9418b06356bd95796ff8f; path=/; HttpOnly
X-Powered-By: PHP/7.4.24

请求标头

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Cookie: csrfToken=09206701259fb54445122132512cf0e8f00cf2ac2f2cf42a34a49cd221b9e797c36d919696daad8d2fc77a0373e417e5e59a89a0cacc9c408ebeede1fc0b4446; CAKEPHP=c6ebd1412956de948b4857c4a0791f04
DNT: 1
Host: my.domain.com
Referer: https://my.domain.com/
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Microsoft Edge";v="96"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: same-origin
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62
4jb9z9bj

4jb9z9bj1#

与许多负载平衡器一样,Azure也喜欢终止SSL,以便对到达PHP的请求进行解密,从而使CakePHP应用程序默认用于构建完整基本URL的env('HTTPS')查找失败。

/*
 * Set the full base URL.
 * This URL is used as the base of all absolute links.
 *
 * If you define fullBaseUrl in your config file you can remove this.
 */
if (!Configure::read('App.fullBaseUrl')) {
    $s = null;
    if (env('HTTPS')) {
        $s = 's';
    }

    $httpHost = env('HTTP_HOST');
    if (isset($httpHost)) {
        Configure::write('App.fullBaseUrl', 'http' . $s . '://' . $httpHost);
    }
    unset($httpHost, $s);
}

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

链接的Azure文档建议改为检查HTTP_X_FORWARDED_PROTO标头,该标头由负载平衡器填充,但您需要考虑到,根据应用程序运行的环境,此标头也可能由客户端设置,因此我通常不建议使用它。
相反,我建议对协议进行硬编码,或者更好的方法是手动设置完整的基本URL(至少在过去,HTTP_HOST也是一个问题,因为一些服务器接受客户端发送的自定义值),例如:

Configure::write('App.fullBaseUrl', 'https://' . $httpHost);

或者在您的config/app.php(或config/app_local.php)中设置App.fullBaseUrl

相关问题