如何使用JavaScript读取HttpOnly Cookie [英] How to read a HttpOnly cookie using JavaScript

查看:735
本文介绍了如何使用JavaScript读取HttpOnly Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用JavaScript读取安全的cookie?

我尝试使用 document.cookie 做到这一点,据我所知可以在有关安全Cookie和HttpOnly标志的文章上看到,我无法以这种方式访问​​安全的cookie。

Is there any way to read a secure cookie with JavaScript?
I tried to do it using document.cookie and as far as I can see on this article about secure cookies and HttpOnly flag, I cannot access a secure cookie this way.

有人可以建议解决方法吗?

Can anyone suggest a workaround?

推荐答案

不同的浏览器在设置HTTPOnly 标志时启用不同的安全措施。例如Opera和Safari不会阻止javascript写入cookie。但是,始终禁止在所有主要浏览器的最新版本上阅读。

Different Browsers enable different security measures when the HTTPOnly flag is set. For instance Opera and Safari do not prevent javascript from writing to the cookie. However, reading is always forbidden on the latest version of all major browsers.

但更重要的是,为什么要阅读 HTTPOnly cookie?如果您是开发人员,则只需禁用该标志并确保您测试了针对xss的代码。我建议您尽可能避免禁用此标志。 HTTPOnly 标志和 secure flag (强制将Cookie通过https发送)。

But more importantly why do you want to read an HTTPOnly cookie? If you are a developer, just disable the flag and make sure you test your code for xss. I recommend that you avoid disabling this flag if at all possible. The HTTPOnly flag and "secure flag" (which forces the cookie to be sent over https) should always be set.

如果您是攻击者,那么您想劫持会话。尽管有 HTTPOnly 标志,但是有一种劫持会话的简单方法。您仍然可以在不知道会话ID的情况下参加会话。 MySpace 萨米蠕虫就是这样做的。它使用 XHR 来读取 CSRF 令牌,然后执行授权任务。因此,攻击者几乎可以执行登录用户可以执行的任何操作。

If you are an attacker, then you want to hijack a session. But there is an easy way to hijack a session despite the HTTPOnly flag. You can still ride on the session without knowing the session id. The MySpace Samy worm did just that. It used an XHR to read a CSRF token and then perform an authorized task. Therefore, the attacker could do almost anything that the logged user could do.

人们对 HTTPOnly 标志<仍然可以利用href = https://en.wikipedia.org/wiki/Cross-site_scripting rel = noreferrer> XSS 。您应该围绕敏感功能设置障碍。例如更改密码字段应要求使用当前密码。管理员具有创建新帐户的能力,需要验证码,该验证码是 CSRF XHR 不能轻易绕过的预防技术。

People have too much faith in the HTTPOnly flag, XSS can still be exploitable. You should setup barriers around sensitive features. Such as the change password filed should require the current password. An admin's ability to create a new account should require a captcha, which is a CSRF prevention technique that cannot be easily bypassed with an XHR.

这篇关于如何使用JavaScript读取HttpOnly Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆