简体   繁体   English

当 Set-Cookie 具有 HttpOnly 和 Secure 时 Session 问题

[英]Session issue when Set-Cookie has HttpOnly & Secure

Assume user is already logged into system.假设用户已经登录系统。 User performed the some activity and based on that they receive one email.用户执行了一些活动并基于他们收到一个 email。 Email has one link, if user clicks that they will be redirected to appropriate screen. Email 有一个链接,如果用户点击,他们将被重定向到相应的屏幕。 But due to some reason when user try the hyperlink they logged out from system and have to login again.但是由于某种原因,当用户尝试使用超链接时,他们从系统中注销并不得不再次登录。

After debugging it turns out that we have below rule in apache conf file.调试后发现我们在 apache conf 文件中有以下规则。

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

After removing above line everything works as expected.删除上述行后,一切都按预期工作。

So i am trying to find out the reason behind it (ie why session issue occurs if above rule is in place).所以我试图找出它背后的原因(即如果上述规则到位,为什么会出现 session 问题)。 Also how i should add that rule without impacting session because http only & secure cookie is required as per security guide line.另外,我应该如何在不影响 session 的情况下添加该规则,因为根据安全指南,仅 http 和安全 cookie 是必需的。

You need to change SameSite=strict to SameSite=lax (or just remove it, since lax is default value)您需要将SameSite=strict更改为SameSite=lax (或将其删除,因为lax是默认值)

The strict value will prevent the cookie from being sent by the browser to the target site in all cross-site browsing context, even when following a regular link. strict值将阻止浏览器在所有跨站点浏览上下文中将 cookie 发送到目标站点,即使在遵循常规链接时也是如此。
... ...

The default lax value provides a reasonable balance between security and usability for websites that want to maintain user's logged-in session after the user arrives from an external link.对于希望在用户从外部链接到达后保持用户登录 session 的网站,默认的lax值在安全性和可用性之间提供了合理的平衡。

https://www.owasp.org/index.php/SameSite https://www.owasp.org/index.php/SameSite

I had the same issue and use following code.我有同样的问题并使用以下代码。

Note: Content-Security-Policy is to filter what type of data server should be allow to access and you can remove that line if you want.注意:Content-Security-Policy 用于过滤应允许访问的数据服务器类型,您可以根据需要删除该行。

#fixing vulnerability issues
TraceEnable Off
Header set X-Frame-Options SAMEORIGIN
Header always edit Set-Cookie (.*) "$1; HTTPOnly"
Header always edit Set-Cookie (.*) "$1; Secure"
Header set Content-Security-Policy "frame-ancestors 'none'; default-src 'self' http://onecrm; script-src 'unsafe-inline' 'unsafe-eval' *; connect-src *; img-src data: *; style-src 'unsafe-inline' *;"

More reading: Set-Cookie , Cookies for dummies , SameSite cookie attribute更多阅读: Set-CookieCookies for dummies , SameSite cookie 属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM