Laravel 5.6 TrustedProxies错误

时间:2018-02-08 09:00:40

标签: php laravel proxy laravel-5.6

我今天已从L5.5升级到L5.6(在此过程中将Symfony组件更新为v4)。此外,自Laravel 5.6官方升级指南起,我已将fideloper/proxy个软件包更新为4.0。

之后我开始收到此错误:Type error: Argument 2 passed to Symfony\Component\HttpFoundation\Request::setTrustedProxies() must be of the type integer, array given, called in /var/www/html/vendor/fideloper/proxy/src/TrustProxies.php on line 54

Symfony 4' s Symfony\Component\HttpFoundation\Request::setTrustedProxies()确实期望整数(位掩码)作为第二个参数:

/**
* Sets a list of trusted proxies.
*
* You should only list the reverse proxies that you manage directly.
*
* @param array $proxies          A list of trusted proxies
* @param int   $trustedHeaderSet A bit field of Request::HEADER_*, to set which headers to trust from your proxies
*
* @throws \InvalidArgumentException When $trustedHeaderSet is invalid
*/
public static function setTrustedProxies(array $proxies, int $trustedHeaderSet)
{
    self::$trustedProxies = $proxies;
    self::$trustedHeaderSet = $trustedHeaderSet;
}

fideloper/proxy 4.0确实给这个函数提供了一个数组而不是一个整数:

public function handle(Request $request, Closure $next)
{
    $request::setTrustedProxies([], $this->getTrustedHeaderNames()); // Reset trusted proxies between requests
    $this->setTrustedProxyIpAddresses($request);
    return $next($request);
}

/**
 * Retrieve trusted header name(s), falling back to defaults if config not set.
 *
 * @return array
 */
protected function getTrustedHeaderNames()
{
    return $this->headers ?: $this->config->get('trustedproxy.headers');
}

所以我无法理解这是fideloper/proxy中的错误还是我错过了什么?

4 个答案:

答案 0 :(得分:22)

经过一番调查(Winmerge与全新安装的Laravel 5.6进行比较)后,归结为app\Http\Middleware\TrustProxies.php文件的差异:

Laravel 5.5:

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies;

    /**
     * The current proxy header mappings.
     *
     * @var array
     */
    protected $headers = [
        Request::HEADER_FORWARDED => 'FORWARDED',
        Request::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
        Request::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
        Request::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
        Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
    ];
}

Laravel 5.6:

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies;

    /**
     * The headers that should be used to detect proxies.
     *
     * @var string
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL;
}

Ergo,根据Laravel 5.6版本设置protected $headers = Request::HEADER_X_FORWARDED_ALL;

答案 1 :(得分:13)

打开app\Http\Middleware\TrustProxies.php

更改以下

protected $headers = [
    Request::HEADER_FORWARDED => 'FORWARDED',
    Request::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
    Request::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
    Request::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
    Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
];

protected $headers = Request::HEADER_X_FORWARDED_ALL;

答案 2 :(得分:11)

the upgrade guide中所述,您需要将 App \ Http \ Middleware \ TrustProxies 中的$ headers属性设置为位属性。

常量在 Symfony \ Component \ HttpFoundation \ Request 中定义。

const HEADER_FORWARDED = 0b00001; // When using RFC 7239
const HEADER_X_FORWARDED_FOR = 0b00010;
const HEADER_X_FORWARDED_HOST = 0b00100;
const HEADER_X_FORWARDED_PROTO = 0b01000;
const HEADER_X_FORWARDED_PORT = 0b10000;
const HEADER_X_FORWARDED_ALL = 0b11110; // All "X-Forwarded-*" headers
const HEADER_X_FORWARDED_AWS_ELB = 0b11010; // AWS ELB doesn't send X-Forwarded-Host

在升级指南中,使用了HEADER_X_FORWARDED_ALL,但您可以使用位值的组合。

答案 3 :(得分:2)

请记住,当从Laravel 5.5升级到5.6时,Laravel会创建一个名为TrustProxies.php的新文件,其名称空间与TrustedProxies相同,即"App\Http\Middleware\TrustProxies"。在Laravel 5.6中,TrustedProxies.php文件正确,如上所述 TrustProxies.php不是。{1}}。但是如果它们都存在于同一名称空间中,那么Laravel将使用它首先找到的那个,那就是TrustProxies.php文件。

如果您同时使用这两项,请删除TrustProxies.php,并确保TrustedProxies.php具有上述正确的更改。

祝你有个美好的一天。 基督徒