X-FRAME-OPTIONS显示两次,X-XSS-PROTECTION显示错误

X-FRAME-OPTIONS is shown twice and X-XSS-PROTECTION is shown wrong

我正在尝试修复我的 headers。我在访问我的页面时检查网络请求时看到两个错误:

1) X-FRAME-OPTIONS: SAMEORIGIN 显示两次:

Cache-Control:no-cache
Connection:Keep-Alive
Content-Encoding:gzip
Content-Type:text/html; charset=UTF-8
Date:Wed, 04 Oct 2017 12:58:30 GMT
Keep-Alive:timeout=3, max=1000
Server:Apache
Set-Cookie:laravel_session=eifQ%3D%3D; expires=Wed, 04-Oct-2017 14:58:30 GMT; Max-Age=7200; path=/; secure; httponly
Set-Cookie:XSRF-TOKEN=n0%3D; expires=Wed, 04-Oct-2017 14:58:30 GMT; Max-Age=7200; path=/
Transfer-Encoding:chunked
X-CDN:Incapsula
X-Frame-Options:SAMEORIGIN * <-------------- HERE
X-Frame-Options:SAMEORIGIN * <-------------- HERE
X-Iinfo:7-6626704-6651371 NNNN CT(0 0 0) RT(1507121414380 495318) q(0 1 1 -1) r(2 2) U16
X-XSS-Protection:%E2%80%9C1;mode=block%E2%80%9D <-------- Strange Encoding here...

2) 我可以在 X-XSS-PROTECTION 的控制台上看到以下错误:

Error parsing header X-XSS-Protection: â1;mode=blockâ: expected 0 or 1 at character position 0. The default protections will be applied.

我正在使用 Laravel 5.0。自 Laravel 4.2 以来,FrameGuard.php 中间件默认不处于活动状态,但您可以根据需要选择启用它。当它被禁用时,我看到了上面的错误,我真的不明白为什么,所以我的第一个想法是通过实际使用那个中间件来覆盖那些 headers。

当我添加包含以下代码的 Illuminate\Http\Middleware\FrameGuard.php 中间件时,似乎没有任何变化:

public function handle($request, Closure $next)
{
    $response = $next($request);

    $response->headers->set('X-XSS-Protection', '1; mode=block');
    $response->headers->set('Content-Type','text/html; charset=UTF-8');
    $response->headers->set('X-Frame-Options', 'SAMEORIGIN', true);

    return $response;
}

我还使用 Socialite 提供 Facebook 身份验证。它是否有可能修改任何 headers?

除了 PHP 发送的响应之外,网络服务器可能还在响应中添加 header。我们可以通过在 public 目录中创建一个空的 HTML 文件来检查网络服务器添加了哪些 headers,例如 public/dummy.html

然后,在浏览器中访问该页面,http://example.com/dummy.html,并检查响应包含哪些 header。或者,我们可以使用 curl 命令来显示响应 headers:

$ curl -I 'http://example.com/dummy.html'

HTTP/2 200
date: Mon, 16 Oct 2017 20:34:24 GMT
...
x-xss-protection: 1; mode=block
x-frame-options: SAMEORIGIN

如果我们在此输出中看到 x-xss-protectionx-frame-options header,则表示网络服务器正在发送这些 header。 Web 服务器配置中 x-xss-protection 的值可能已损坏(看起来有人粘贴了程式化的双引号 (“…”) 而不是直引号 ("…"),服务器将其解释为一部分header 的值)。

对于 nginx,在配置文件中查找 add_header ... 指令。如果使用 Apache httpd,请检查服务器配置或 .htaccess 文件中的 Header set ... 指令。

该网站似乎也使用了 Incapsula CDN,它也可能注入了 header,但我在 Incapsula 文档中找不到任何信息表明情况如此。

Laravel Socialite 不会将这些 header 添加到响应中。