<p>JavaScript needs access to cookies if AJAX is used on a site with access restrictions based on cookies. Will HttpOnly cookies work on an AJAX site? </p>

<p><em>Edit:</em> Microsoft created a way to prevent XSS attacks by disallowing JavaScript access to cookies if HttpOnly is specified. FireFox later adopted this. So my question is: If you are using AJAX on a site, like StackOverflow, are Http-Only cookies an option?</p>

<p><em>Edit 2:</em> Question 2. If the purpose of HttpOnly is to prevent JavaScript access to cookies, and you can still retrieve the cookies via JavaScript through the XmlHttpRequest Object, <strong>what is the point of HttpOnly</strong>?</p>

<p><em>Edit 3:</em> Here is a quote from Wikipedia:</p>

<blockquote>
  <p>When the browser receives such a cookie, it is supposed to use it as usual in the following HTTP exchanges, but not to make it visible to client-side scripts.[32] The <code>HttpOnly</code> flag is not part of any standard, and is not implemented in all browsers. Note that there is currently no prevention of reading or writing the session cookie via a XMLHTTPRequest. [33].</p>
</blockquote>

<p>I understand that <code>document.cookie</code> is blocked when you use HttpOnly. But it seems that you can still read cookie values in the XMLHttpRequest object, allowing for XSS. How does HttpOnly make you any safer than? By making cookies essentially read only? </p>

<p>In your example, I cannot write to your <code>document.cookie</code>, but I can still steal your cookie and post it to my domain using the XMLHttpRequest object.</p>

<pre><code>&lt;script type="text/javascript"&gt;
    var req = null;
    try { req = new XMLHttpRequest(); } catch(e) {}
    if (!req) try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
    if (!req) try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
    req.open('GET', 'http://stackoverflow.com/', false);
    req.send(null);
    alert(req.getAllResponseHeaders());
&lt;/script&gt;
</code></pre>

<p><em>Edit 4:</em> Sorry, I meant that you could send the XMLHttpRequest to the StackOverflow domain, and then save the result of getAllResponseHeaders() to a string, regex out the cookie, and then post that to an external domain. It appears that Wikipedia and ha.ckers concur with me on this one, but I would love be re-educated...</p>

<p><em>Final Edit:</em> Ahh, apparently both sites are wrong, this is actually a <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=380418" rel="noreferrer">bug in FireFox</a>. IE6 &amp; 7 are actually the only browsers that currently fully support HttpOnly.</p>

<p>To reiterate everything I've learned:</p>

<ul>
<li>HttpOnly restricts all access to document.cookie in IE7 &amp; and FireFox (not sure about other browsers)</li>
<li>HttpOnly removes cookie information from the response headers in XMLHttpObject.getAllResponseHeaders() in IE7.</li>
<li>XMLHttpObjects may only be submitted to the domain they originated from, so there is no cross-domain posting of the cookies.</li>
</ul>

<p><em>edit: This information is likely no longer up to date.</em></p>

[{'answer_id': 28000, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 2, 'selected': False, 'text': "<p>Not necessarily, it depends what you want to do.  Could you elaborate a bit? AJAX doesn't need access to cookies to work, it can make requests on its own to extract information, the page request that the AJAX call makes could access the cookie data &amp; pass that back to the calling script without Javascript having to directly access the cookies</p>\n"}, {'answer_id': 28010, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 0, 'selected': False, 'text': "<p>No, the page that the AJAX call requests has access to cookies too &amp;  that's what checks whether you're logged in.</p>\n\n<p>You can do other authentication with the Javascript, but I wouldn't trust it, I always prefer putting any sort of authentication checking in the back-end.</p>\n"}, {'answer_id': 28021, 'author': 'pkchukiss', 'author_id': 2504504, 'author_profile': 'https://Stackoverflow.com/users/2504504', 'pm_score': 1, 'selected': False, 'text': "<p>Cookies are automatically handled by the browser when you make an AJAX call, so there's no need for your Javascript to mess around with cookies.</p>\n"}, {'answer_id': 28031, 'author': 'Polsonby', 'author_id': 137, 'author_profile': 'https://Stackoverflow.com/users/137', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <blockquote>\n    <p>Therefore I am assuming JavaScript needs access to your cookies.</p>\n  </blockquote>\n</blockquote>\n\n<p>All HTTP requests from your browser transmit your cookie information for the site in question. JavaScript can both set and read cookies. Cookies are not by definition required for Ajax applications, but they are required for most web applications to maintain user state.</p>\n\n<p>The formal answer to your question as phrased - "Does JavaScript need access to cookies if AJAX is used?" - is therefore "no". Think of enhanced search fields that use Ajax requests to provide auto-suggest options, for example. There is no need of cookie information in that case.</p>\n'}, {'answer_id': 28032, 'author': 'Glenn Slaven', 'author_id': 2975, 'author_profile': 'https://Stackoverflow.com/users/2975', 'pm_score': 2, 'selected': False, 'text': "<p>As clarification - from the server's perspective, the page that is requested by an AJAX request is essentially no different to a standard HTTP get request done by the user clicking on a link.  All the normal request properties: user-agent, ip, session, cookies, etc. are passed to the server.</p>\n"}, {'answer_id': 29092, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 6, 'selected': False, 'text': '<p>Yes, HTTP-Only cookies would be fine for this functionality.  They will still be provided with the XmlHttpRequest\'s request to the server.</p>\n\n<p>In the case of Stack Overflow, the cookies are automatically provided as part of the XmlHttpRequest request.  I don\'t know the implementation details of the Stack Overflow authentication provider, but that cookie data is probably automatically used to verify your identity at a lower level than the "vote" controller method.</p>\n\n<p>More generally, cookies are <strong>not</strong> required for AJAX.  XmlHttpRequest support (or even iframe remoting, on older browsers) is all that is technically required.</p>\n\n<p>However, if you want to provide security for AJAX enabled functionality, then the same rules apply as with traditional sites.  You need some method for identifying the user behind each request, and cookies are almost always the means to that end.</p>\n\n<blockquote>\n  <p>In your example, I cannot write to your document.cookie, but I can still steal your cookie and post it to my domain using the XMLHttpRequest object.</p>\n</blockquote>\n\n<p>XmlHttpRequest won\'t make cross-domain requests (for exactly the sorts of reasons you\'re touching on).</p>\n\n<p>You could normally inject script to send the cookie to your domain using iframe remoting or JSONP, but then HTTP-Only protects the cookie again since it\'s inaccessible.</p>\n\n<p>Unless you had compromised StackOverflow.com on the server side, you wouldn\'t be able to steal my cookie.</p>\n\n<blockquote>\n  <p>Edit 2: Question 2. If the purpose of Http-Only is to prevent JavaScript access to cookies, and you can still retrieve the cookies via JavaScript through the XmlHttpRequest Object, what is the point of Http-Only?</p>\n</blockquote>\n\n<p>Consider this scenario:</p>\n\n<ul>\n<li>I find an avenue to inject JavaScript code into the page.</li>\n<li>Jeff loads the page and my malicious JavaScript modifies his cookie to match mine.</li>\n<li>Jeff submits a stellar answer to your question.</li>\n<li>Because he submits it with my cookie data instead of his, the answer will become mine.</li>\n<li>You vote up "my" stellar answer.</li>\n<li>My real account gets the point.</li>\n</ul>\n\n<p>With HTTP-Only cookies, the second step would be impossible, thereby defeating my XSS attempt.</p>\n\n<blockquote>\n  <p>Edit 4: Sorry, I meant that you could send the XMLHttpRequest to the StackOverflow domain, and then save the result of getAllResponseHeaders() to a string, regex out the cookie, and then post that to an external domain. It appears that Wikipedia and ha.ckers concur with me on this one, but I would love be re-educated...</p>\n</blockquote>\n\n<p>That\'s correct.  You can still session hijack that way.  It does significantly thin the herd of people who can successfully execute even that XSS hack against you though.</p>\n\n<p>However, if you go back to my example scenario, you can see where HTTP-Only <em>does</em> successfully cut off the XSS attacks which rely on modifying the client\'s cookies (not uncommon).</p>\n\n<p>It boils down to the fact that a) no single improvement will solve <em>all</em> vulnerabilities and b) no system will <em>ever</em> be completely secure.  HTTP-Only <strong>is</strong> a useful tool in shoring up against XSS.</p>\n\n<p>Similarly, even though the cross domain restriction on XmlHttpRequest isn\'t 100% successful in preventing all XSS exploits, you\'d still never dream of removing the restriction.</p>\n'}, {'answer_id': 590311, 'author': 'thomasrutter', 'author_id': 53212, 'author_profile': 'https://Stackoverflow.com/users/53212', 'pm_score': 3, 'selected': False, 'text': "<p>Yes, they are a viable option for an Ajax based site.  Authentication cookies aren't for manipulation by scripts, but are simply included by the browser on all HTTP requests made to the server.</p>\n\n<p>Scripts don't need to worry about what the session cookie says - as long as you are authenticated, then any requests to the server initiated by either a user or the script will include the appropriate cookies.  The fact that the scripts cannot themselves know the content of the cookies doesn't matter.</p>\n\n<p>For any cookies that are used for purposes other than authentication, these can be set without the HTTP only flag, if you want script to be able to modify or read these.  You can pick and choose which cookies should be HTTP only, so for example anything non-sensitive like UI preferences (sort order, collapse left hand pane or not) can be shared in cookies with the scripts.</p>\n\n<p>I really like the HTTP only cookies - it's one of those proprietary browser extensions that was a really neat idea.</p>\n"}, {'answer_id': 673871, 'author': 'Jay', 'author_id': 79737, 'author_profile': 'https://Stackoverflow.com/users/79737', 'pm_score': 0, 'selected': False, 'text': "<p>Yes, cookies are very useful for Ajax.</p>\n\n<p>Putting the authentication in the request URL is bad practice. There was a news item last week about getting the authentication tokens in the URL's from the google cache.</p>\n\n<p>No, there is no way to prevent attacks. Older browsers still allow trivial access to cookies via javascript. You can bypass http only, etc. Whatever you come up with can be gotten around given enough effort. The trick is to make it too much effort to be worthwhile.</p>\n\n<p>If you want to make your site more secure (there is no perfect security) you could use an authentication cookie that expires. Then, if the cookie is stolen, the attacker must use it before it expires. If they don't then you have a good indication there's suspicious activity on that account. The shorter the time window the better for security but the more load it puts on your server generating and maintaining keys.</p>\n"}, {'answer_id': 975881, 'author': 'davenpcj', 'author_id': 4777, 'author_profile': 'https://Stackoverflow.com/users/4777', 'pm_score': 2, 'selected': False, 'text': "<p>There's a bit more to this.</p>\n\n<p>Ajax doesn't strictly require cookies, but they can be useful as other posters have mentioned. Marking a cookie HTTPOnly to hide it from scripts only partially works, because not all browsers support it, but also because there are common workarounds.</p>\n\n<p>It's odd that the XMLHTTPresponse headers are giving the cookie, technically the server doesn't have to return the cookie with the response. Once it's set on the client, it stays set until it expires. Though there are schemes in which the cookie is changed with every request to prevent re-use. So you may be able to avoid that workaround by changing the server to not provide the cookie on the XMLHTTP responses.</p>\n\n<p>In general though, I think HTTPOnly should be used with some caution. There are cross site scripting attacks where an attacker arranges for a user to submit an ajax-like request originating from another site, using simple post forms, without the use of XMLHTTP, and your browser's still-active cookie would authenticate the request.</p>\n\n<p>If you want to be sure that an AJAX request is authenticated, the request itself AND the HTTP headers need to contain the cookie. Eg through the use of scripts or unique hidden inputs. HTTPOnly would hinder that.</p>\n\n<p>Usually the interesting reason to want HTTPOnly is to prevent third-party content included on your webpage from stealing cookies. But there are many interesting reasons to be very cautious about including third-party content, and filter it aggressively.</p>\n"}]
['https://Stackoverflow.com/questions/27972', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/26/']