检查httponly cookie是否存在于JavaScript中

发布于 2025-02-07 04:31:05 字数 243 浏览 2 评论 0原文

正如问题所说,您可以找出JavaScript中是否存在cookie是否是httponly?我不需要访问其中的信息,只知道它有一个。

有关这种情况的更多信息是,最初有一个使用cookie作为身份验证令牌的Web服务器,并且设置为httponly,因为客户端未使用它,因此将其添加到安全性中。

但是,现在需要更改客户需要知道它是否具有cookie(因为网站可以工作而无需登录用户 隐藏其他人。

事物并 cookie并拒绝用户。

As the question says can you find out if a cookie exists within Javascript if it is a HttpOnly? I don't need to access the information inside of it, just know it has one.

A little more information on the situation is that there was originally a web server which used a cookie as an authentication token, and it was set to httponly as it was not used by the client so it added to the security.

However now there is a change needed where the client needs to know if it has the cookie (as the site can work without the user being logged in, but if they are logged in (the auth cookie would exist) the site needs to display certain things and hide others.

There are other security precautions in place on the web server so there is no harm in the scenario where the client has an incorrect auth cookie, but the site makes it look like they are logged in, as it would delete the cookie and reject the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月朦胧 2025-02-14 04:31:05

我遇到了同样的问题。我用服务器设置了另一个cookie,而不是httponly,每当它刷新Httponly session cookie时,具有相同的max-age且无敏感的数据。现在,如果其中一个存在,另一个人也是如此,客户可以知道httponly对应物是否在那里。

I had the same problem. I solved it with the server setting another cookie, not httponly, every time it refreshed the httponly session cookie, with the same max-age and no sensitive data. Now, if one of them is present, the same goes for the other, and the client can know if the httponly counterpart is there.

北渚 2025-02-14 04:31:05

您可以通过尝试将其设置为使用JavaScript的值来间接检查是否存在,如果无法设置它,则HTTP只有cookie必须在那里(或用户阻止cookie)。

function doesHttpOnlyCookieExist(cookiename) {
  var d = new Date();
  d.setTime(d.getTime() + (1000));
  var expires = "expires=" + d.toUTCString();

  document.cookie = cookiename + "=new_value;path=/;" + expires;
  return document.cookie.indexOf(cookiename + '=') == -1;
}

You can indirectly check to see if it exists by trying to set it to a value with javascript if it can't be set, then the HTTP Only Cookie must be there (or the user is blocking cookies).

function doesHttpOnlyCookieExist(cookiename) {
  var d = new Date();
  d.setTime(d.getTime() + (1000));
  var expires = "expires=" + d.toUTCString();

  document.cookie = cookiename + "=new_value;path=/;" + expires;
  return document.cookie.indexOf(cookiename + '=') == -1;
}
何处潇湘 2025-02-14 04:31:05

只要您需要检查cookie是否存在,就可以向需要身份验证的服务器发送请求&检查响应。如果它类似401未经授权403禁止,那么cookie可能不存在&您可以提示用户登录。

另一方面,如果存在cookie,则将由浏览器自动发送,从而产生200 OK响应。

Whenever you need to check whether the cookie exists or not, you can send a request to the server that requires authentication & check the response. If its something like 401 Unauthorized or 403 Forbidden, then the cookie probably doesn't exist & you can prompt the user for login.

On the other hand, if the cookie exists, it'll be automatically sent by the browser resulting in a 200 OK response.

长发绾君心 2025-02-14 04:31:05

不,请参阅下面的Rob的评论。

请参阅此信息,您可能已经看到了 - “ noreferrer“ Secure_and_httponly

无法通过非HTTP方法访问Httponly cookie,例如
通过JavaScript进行呼叫(例如,引用“ document.cookie”)...

编辑:删除undfected响应,我写了一个您可能不使用的脚本::)

No. And see Rob's comments below.

See this, which you probably already saw - http://en.wikipedia.org/wiki/HTTP_cookie#Secure_and_HttpOnly

An HttpOnly cookie is not accessible via non-HTTP methods, such as
calls via JavaScript (e.g., referencing "document.cookie")...

Edit: Removed undefined response, I wrote a script that you may not be using :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文