admin管理员组文章数量:1134247
Original Question
Is there any way to read a HTTPOnly1 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?
1To clarify, there are two types of secure cookies:
Secure as in sent over the https:// protocol — i.e. cookie is not sent in plaintext. Known as the "secure flag". The question is not about these cookies.
Secure as in the cookie cannot be read by JavaScript running in the browser — i.e.
document.cookie
will not work. Known as the "HttpOnly" flag. The question is about these cookies.
Original Question
Is there any way to read a HTTPOnly1 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?
1To clarify, there are two types of secure cookies:
Secure as in sent over the https:// protocol — i.e. cookie is not sent in plaintext. Known as the "secure flag". The question is not about these cookies.
Secure as in the cookie cannot be read by JavaScript running in the browser — i.e.
document.cookie
will not work. Known as the "HttpOnly" flag. The question is about these cookies.
- 2 From wiki: A secure cookie is only used when a browser is visiting a server via HTTPS. – Pavel Hodek Commented Nov 9, 2011 at 11:39
- 9 @Pavel Hodek That's the wrong flag. The "secure" cookie flag has nothing to do with the HTTPOnly security flag. They have an awful naming system. – rook Commented Nov 9, 2011 at 18:28
- 1 The title of this questions ought to be changed to include the "HttpOnly" flag. As it stands it looks like the question is about the "Secure" flag. – Tom Commented Nov 12, 2018 at 14:18
4 Answers
Reset to default 203Different 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.
But more importantly why do you want to read an HTTPOnly
cookie? If you are a developer, then disable the flag - if the value isn't a sensitive value like an authentication token then it doesn't need to have this security flag enabled. The HTTPOnly
security precaution is to help defend against XSS, but even with this flag enabled you need to still test your code for XSS vulnerabilities because this class of bugs are still fully exploitable. I recommend avoiding disabling security features, but sometimes the HTTPOnly
flag gets in the way of application behavior, and if it isn't an authentication token or personal information (PII) then it likely doesn't need this flag.
If you are an attacker, then you want to hijack a session. But there is an easy way to hijack a session, even when the HTTPOnly
flag is enabled. You can still ride on the session without knowing the session id which is how the The MySpace Samy worm spread. This worm used an XHR to read a CSRF token and then perform an authorized task of posting itself to your wall - thus infecting everyone on your feed. In a session riding attack, the attacker can do almost anything that the logged user could do - even without access to the session id stored as a cookie value.
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 always require the current password. Requiring a 2fa step for sensitive administrative features can make it more difficult for an attacker to access using stolen credentials or via CSRF aka "session riding" attacks.
While adding a CAPTCHA can make CSRF more difficult, if the attacker has XSS they will be able to bypass a CAPTCHA challenge response by using a BeEF proxy to view and then bypass the CAPTCHA. A Same-Origin Policy bypass maybe used to view a CAPTCHA's challenge response (or a CSRF token), a good example is Universal-Cross-Site-Scripting or UXSS. Requiring 2fa raises the bar over a CAPTCHA, as it would still require additional human interaction and that introduces another opportunity for the session-riding attack to be discovered and stopped.
The whole point of HttpOnly cookies is that they can't be accessed by JavaScript.
The only way (except for exploiting browser bugs) for your script to read them is to have a cooperating script on the server that will read the cookie value and echo it back as part of the response content. But if you can and would do that, why use HttpOnly cookies in the first place?
You can not. Httponly cookies' purpose is being inaccessible by script.
httpOnly means that the cookies are secure and they are inaccessible by the script.
本文标签: securityHow to read a HttpOnly cookie using JavaScriptStack Overflow
版权声明:本文标题:security - How to read a HttpOnly cookie using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736812813a1953962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论