JavaScript 和第三方 cookie

发布于 2024-09-12 16:45:35 字数 340 浏览 5 评论 0原文

假设有一个网站 foo.com,它从网站 bar.com 加载 JavaScript。现在,假设来自 bar.com 站点的 JavaScript 尝试使用 document.cookies 读取 cookie。我的印象是,使用 JavaScript,您可以读取浏览器中设置的所有 cookie,无论其来源如何。但事实证明,来自 bar.com 网站的 JavaScript 只能访问 bar.com 设置的 cookie,而不能访问任何其他网站。如果是这样的话,窃取cookie的脚本注入攻击是如何进行的呢?

Say there is a site foo.com which loads JavaScript from site bar.com. Now, say the JavaScript from site bar.com tries to read cookies using document.cookies. I was under the impression that using JavaScript, you can read all the cookies set in the browser irrespective of their source. But it turns out that the JavaScript from the site bar.com can only access cookies set by bar.com and not any other. If this is the case, how are script injection attacks which steal cookies carried out?

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

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

发布评论

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

评论(3

枕头说它不想醒 2024-09-19 16:45:35

但事实证明,bar.com 网站的 JavaScript 只能访问 bar.com 设置的 cookie,而不能访问其他网站。

那不是真的。重要的是包含

我怀疑您的问题是,当属性名为 document.cookie单数!)时,您正在访问 document.cookies

But it turns out that the JavaScript from the site bar.com can only access cookies set by bar.com and not any other.

That isn't true. What matters is where the HTML document containing the <script> element is, not the URL of the JS file that said <script> mentions in the src attribute.

I suspect your problem is that you are accessing document.cookies when the property is called document.cookie (Singular!)

伴我老 2024-09-19 16:45:35

他们在受攻击的页面内加载脚本。

例如,当博客系统中的评论受到损害时,它们会包含一个在呈现页面时执行的 script 元素。该脚本可以获取cookie并将其发送到攻击者的服务器。

这就是为什么您应该永远相信用户输入,并且至少不允许在评论中使用某些标签(或者将每个 < 翻译为 <)。但不要在客户端这样做,因为这种预防技术很容易被绕过;在服务器端测试(并更改)恶意输入。

They load scripts inside the attacked page.

For instance, when comments in a blog system get compromised, they contain a script element that is executed when the page is rendered. This script can get the cookies and send it to the attacker's server.

That's why you should never trust user input and disallow at least certain tags in comments (or translate every < to <). But don't do this on the client side, as this prevention technique can easily be circumvented; test for (and change) malicious input on the server side.

一紙繁鸢 2024-09-19 16:45:35

您只能访问为给定域名设置的cookie。来自 关于 cookie 的维基百科文章

除了名称/值对之外,还有一个 cookie
还可能包含到期日期、
路径、域名以及是否
cookie 仅用于加密
连接。 RFC 2965 强制要求 cookie
有版本号,但这是
通常省略。这几条数据
遵循 name=newvalue 对并且是
用分号分隔。例如,
服务器可以创建一个cookie
通过发送一行 Set-Cookie:
名称=新值;过期=日期;路径=/;
域=.example.org。

域名和
路径告诉浏览器该cookie
必须发送回服务器时
请求给定域的 URL 以及
小路。如果没有指定,则默认
到对象的域和路径
这是所要求的。结果,
域和路径字符串可能会告诉
浏览器发送 cookie
通常不会。为了安全
原因,仅接受cookie
如果服务器是成员
由域字符串指定的域。

如果 foo.com 发送了一个域名为 bar.com 甚至 .com 的 cookie,则 上的 JavaSCript 代码bar.com 可以读取该 cookie。然而,大多数浏览器被配置为仅在域名匹配时接受 cookie,并且会拒绝此类 cookie。

You can only access cookies which have been set for the given domain name. From the Wikipedia article on cookies:

Beside the name/value pair, a cookie
may also contain an expiration date, a
path, a domain name, and whether the
cookie is intended only for encrypted
connections. RFC 2965 mandates cookies
have a version number, but this is
usually omitted. These pieces of data
follow the name=newvalue pair and are
separated by semicolons. For example,
a cookie can be created by the server
by sending a line Set-Cookie:
name=newvalue; expires=date; path=/;
domain=.example.org.

The domain and
path tell the browser that the cookie
has to be sent back to the server when
requesting URLs of a given domain and
path. If not specified, they default
to the domain and path of the object
that was requested. As a result, the
domain and path strings may tell the
browser to send the cookie when it
normally would not. For security
reasons, the cookie is accepted only
if the server is a member of the
domain specified by the domain string.

If foo.com sent a cookie which had the domain name of bar.com, or even .com, then JavaSCript code on bar.com could read that cookie. However most browsers are configured to only accept cookies when the domain name matches, and would reject such a cookie.

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