安全问题:HTTPS Session Cookie 'secure' Attribute Not Set


 作者:亚艾元技术部

安全检查公司检查做了的网站,0个高危漏洞,1个中危漏洞,当然还有很多轻微的问题。

这里的这个中危漏洞,就是:

任何以明文形式发送到服务器的 cookie、会话令牌或用户凭证之类的信息都可能被窃取,并用于身份盗窃或用户伪装。

HTTPS Session Cookie 'secure' Attribute Not Set


 给出的解决建议是:

cookie添加secure属性

 

我通过搜索查找,找到了一篇同样的问题:

https://drupal.stackexchange.com/questions/194805/setting-secure-cookies

 

这是Wim Leers给出的回复,他是Drupal8核心开发人员,也是acquia CTO办公室的主力开发人员:

hich cookie is this? All of Drupal 8's cookies are secure.

The exception is BigPipe's no-JS cookie, see https://www.drupal.org/node/2678628 — but there are no security consequences there.

If that's the one that is triggering this alert, then the problem is clear: your security testing tool is making blanket statements that don't make sense.

所有的Drupal cookie都是安全的,这个安全警告没有任何意义。

 

我这样回复了客户,总觉得还不够。

后来又搜索了一下,如果是apache的话,可以这样配置:

Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure


 

如果是nginx的话,需要配置:

https://github.com/AirisX/nginx_cookie_flag_module

打包安装这个模块。需要从源码build,

location / {
    set_cookie_flag Secret HttpOnly secure SameSite;
    set_cookie_flag * HttpOnly;
    set_cookie_flag SessionID SameSite=Lax secure;
    set_cookie_flag SiteToken SameSite=Strict;
}

 

由于生成站点上面的nginx已经安装好了,所以就没有从头build

 

另外一个办法,给出了这个配置:

proxy_cookie_path / "/; HTTPOnly; Secure";


我尝试了一下,结果整个cookie都不显示了,匿名用户。不过对于登录用户,还是显示的。

 

我看很多人也遇到了这样的问题,根据上面的链接里面给出的部分代码:

ini_set('session.cookie_secure', 1);
ini_set('session.cookie_httponly', 1);


 

   我查找了一下cookie_secureNodepad++,全文搜搜

/**
   * {@inheritdoc}
   */
  public function getOptions(Request $request) {
    $options = $this->options;
 
    // Generate / validate the cookie domain.
    $options['cookie_domain'] = $this->getCookieDomain($request) ?: '';
 
    // If the site is accessed via SSL, ensure that the session cookie is
    // issued with the secure flag.
    $options['cookie_secure'] = $request->isSecure();
 
    // Set the session cookie name.
    $options['name'] = $this->getName($request);
 
    return $options;
  }


namespace Drupal\Core\Session下面的SessionConfiguration。

 

$request->isSecure();

通过这段代码,我突然想到了,我们的Drupal站点是在一个负载均衡nginx的后面运行了,经过了一层代理。负载均衡设置了https,不过后台没有设置。或许这个是和代理有一点管理。

https://drupal.stackexchange.com/questions/199722/how-to-correctly-configure-settings-php-to-use-reverse-proxy-and-ssl-termination

 

https://medium.com/@lmakarov/drupal-8-and-reverse-proxies-the-base-url-drama-c5553cbc9a3e

 

https://www.drupal.org/node/425990

 

重新设置的proxy上面的nginx的配置:

 

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header X-Forwarded-Port 443;
      proxy_set_header Host $host;

    

 

为Drupal8站点添加以下配置:

  $settings['reverse_proxy'] = TRUE;
  $settings['reverse_proxy_addresses'] = array($_SERVER['REMOTE_ADDR']);

 

现在通过chrome浏览器的开发者工具,看到的header响应

Server: nginx
Transfer-Encoding: chunked
Vary: Cookie



Drupal版本: