ajax - HttpOnly cookie 如何与 AJAX 请求配合使用?

标签 ajax cookies httponly

如果在基于 cookie 的访问限制的站点上使用 AJAX,则 JavaScript 需要访问 cookie。 HttpOnly cookies 能在 AJAX 网站上工作吗?

编辑: Microsoft 创建了一种防止 XSS 攻击的方法,如果指定了 HttpOnly,则不允许 JavaScript 访问 cookie。 FireFox后来采用了这一点。所以我的问题是:如果您在 StackOverflow 等网站上使用 AJAX,可以选择 Http-Only cookie 吗?

编辑 2: 问题 2。如果 HttpOnly 的目的是阻止 JavaScript 访问 cookie,并且您仍然可以通过 XmlHttpRequest 对象通过 JavaScript 检索 cookie,那么有什么意义呢? HttpOnly?

编辑 3:以下是维基百科的引用:

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 HttpOnly 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].

据我了解,当您使用 HttpOnly 时,document.cookie 会被阻止。但似乎您仍然可以读取 XMLHttpRequest 对象中的 cookie 值,从而允许 XSS。 HttpOnly 如何让您更安全?通过使cookie本质上只读?

在您的示例中,我无法写入您的 document.cookie,但我仍然可以窃取您的 cookie 并使用 XMLHttpRequest 对象将其发布到我的域。

<script type="text/javascript">
    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());
</script>

编辑 4: 抱歉,我的意思是您可以将 XMLHttpRequest 发送到 StackOverflow 域,然后将 getAllResponseHeaders() 的结果保存到字符串中,用正则表达式输出 cookie,然后发布该结果到外部域。看来维基百科和黑客在这一点上都同意我的观点,但我很乐意接受再教育......

最终编辑:啊,显然两个网站都错了,这实际上是 bug in FireFox 。 IE6 & 7 实际上是目前唯一完全支持 HttpOnly 的浏览器。

重申我所学到的一切:

  • HttpOnly 限制 IE7 和 FireFox 中对 document.cookie 的所有访问(不确定其他浏览器是否如此)
  • HttpOnly 在 IE7 中从 XMLHttpObject.getAllResponseHeaders() 的响应 header 中删除 Cookie 信息。
  • XMLHttpObject 只能提交到它们源自的域,因此不存在 Cookie 的跨域发布。

编辑:此信息可能不再是最新的。

最佳答案

是的,仅 HTTP cookie 就可以满足此功能。它们仍然会收到对服务器的 XmlHttpRequest 请求。

对于 Stack Overflow,Cookie 会作为 XmlHttpRequest 请求的一部分自动提供。我不知道 Stack Overflow 身份验证提供程序的实现细节,但 cookie 数据可能会自动用于在比“投票” Controller 方法更低的级别验证您的身份。

更一般地说,AJAX 不需要 cookie。技术上只需要 XmlHttpRequest 支持(甚至是旧版浏览器上的 iframe 远程处理)即可。

但是,如果您想为支持 AJAX 的功能提供安全性,则适用与传统站点相同的规则。您需要某种方法来识别每个请求背后的用户,而 cookie 几乎总是达到此目的的手段。

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.

XmlHttpRequest 不会发出跨域请求(正是出于您所提到的各种原因)。

您通常可以注入(inject)脚本以使用 iframe 远程处理或 JSONP 将 cookie 发送到您的域,但 HTTP-Only 会再次保护 cookie,因为它无法访问。

除非您在服务器端破坏了 StackOverflow.com,否则您将无法窃取我的 cookie。

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?

考虑这种情况:

  • 我找到了将 JavaScript 代码注入(inject)页面的途径。
  • Jeff 加载页面,我的恶意 JavaScript 修改他的 cookie 以匹配我的。
  • 杰夫对您的问题给出了出色的答案。
  • 因为他提交的是我的 Cookie 数据而不是他的,所以答案将成为我的。
  • 您对“我的”出色答案投了赞成票。
  • 我的真实账户明白了这一点。

使用 HTTP-Only cookie,第二步将是不可能的,从而挫败了我的 XSS 尝试。

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...

这是正确的。您仍然可以通过这种方式劫持 session 。不过,即使是能够成功执行针对您的 XSS 攻击的人也确实大大减少了。

但是,如果您回到我的示例场景,您可以看到 HTTP-Only确实成功地切断了依赖于修改客户端 cookie 的 XSS 攻击(并不罕见)。

归结为这样一个事实:a) 没有任何一项改进可以解决所有漏洞,b) 没有任何系统永远是完全安全的。 HTTP-Only 抵御 XSS 的有用工具。

同样,即使对 XmlHttpRequest 的跨域限制不能 100% 成功地阻止所有 XSS 攻击,您仍然不会梦想取消该限制。

关于ajax - HttpOnly cookie 如何与 AJAX 请求配合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27972/

相关文章:

javascript - 通过一系列对象方法来处理 ajax 成功回调的干净方法是什么?

javascript - JQuery AJAX 和 OOP JS 作用域问题

javascript - 如何使用 Ajax .serialize() 数据将表单发布到 PHP?

java - 将 HTTPOnly cookie 与 android 中的其他 cookie 区分开来

javascript - 如何在 JSON stringify 中使用特殊字符?

Codeigniter - 不允许的关键字符,调整代码

java - 正确的 cookie 管理

php - 当 httponly 为 true 时,Cookies 抵抗 setcookie() 删除

tomcat - 带有 httpOnly 标志的 session cookie

javascript - 为什么我的脚本应该只在 IE 上触发,却在 Mozilla 上触发?