javascript - 同源政策 - 仍然没有得到它

标签 javascript same-origin-policy

起初我认为同源策略意味着从不同域加载的 JS 无法在页面上执行。但读完this后, thisthis我变得很困惑。声明是,如果您在脚本标签的 src 属性中加载 JS,则加载的脚本与加载页面具有相同的来源,这意味着现在所有内容都来自 CDN 或第三方(Google aaytics 等)拥有对 DOM 等的完全访问权限。对我来说,这看起来像是一个可怕的安全漏洞。这意味着,如果 CDN 或类似内容遭到破坏,攻击者可能会提供恶意 JS,例如从多个站点窃取用户名/密码。那么我的理解正确与否?

最佳答案

The claim is that if you load JS in a script tag's src property the loaded script has the same origin as the loading page,

是的。来源是由 HTML 文档的来源来定义的,而不是其他任何东西。

which means everything coming from a CDN or third party (Google anaytics etc.) now has complete access to the DOM etc.

是的。这就是为什么您必须完全信任您允许在页面上执行的任何 JS 的来源。

To me that looks like a horrifying security vulnerability. It means if a CDN or similar is ever breached an attacker could serve up malicious JS which could, for example, steal usernames/passwords from multiple sites.

这是一个风险。 It has happened in the past .

如果您选择使用 CDN,那么您必须相信他们的安全性足够好,这样的情况就不会发生。

<小时/>

至于同源策略实际上做了什么:See this answer .

关于javascript - 同源政策 - 仍然没有得到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45488666/

相关文章:

javascript - 使用 AJAX 加载数据

javascript - Flickr API OAuth 访问 token 请求和 Access-Control-Allow-Origin

javascript - kogrid 中的主/详细功能

javascript - 当用户在卸载时注销时,他们也会在重新加载时注销

javascript - 如何在 iFrame 上使用 postMessage

javascript - XMLHttpRequest无法加载XXX No'Access-Control-Allow-Origin' header

javascript - document.domain 相同但仍然出现同源错误

JavaScript YUI3 使用全局变量?

javascript - 打开后实现可折叠的 body 填充变化

javascript - React Native渲染函数给出错误: Unexpected token