简体   繁体   English

同源政策适用于

[英]Same-origin policy is for what

I am not able to understand why we need this rule. 我不明白为什么我们需要这个规则。 If it is such import, why we can get so many workaround to address it? 如果是这样的导入,为什么我们可以获得这么多解决方法来解决呢? Like JSONP, CORS etc.? 像JSONP,CORS等?

Is there any example can demostrate the damage without this rule? 如果没有此规则,是否有任何例子可以说明损害?

If you look at the MDN article , you'll see this: 如果查看MDN文章 ,您将看到以下内容:

Cross-origin writes are typically allowed. 通常允许跨域写入。 Examples are links, redirects and form submissions. 例如链接,重定向和表单提交。 Certain rarely used HTTP requests require preflight. 某些很少使用的HTTP请求需要进行预检。

Cross-origin embedding is typically allowed. 通常允许跨域嵌入。 Examples are listed below. 示例如下。

Cross-origin reads are typically not allowed, but read access is often leaked by embedding. 通常不允许跨域读取,但是读取访问经常因嵌入而泄漏。 For example you can read the width and height of an embedded image, the actions of an embedded script, or the availability of an embedded resource. 例如,您可以读取嵌入式图像的宽度和高度,嵌入式脚本的操作或嵌入式资源的可用性。

Here's a good post on security StackExchange : 这是有关安全StackExchange的好帖子

Assume you are logged into Facebook and visit a malicious website in another browser tab. 假设您已登录Facebook,并在另一个浏览器选项卡中访问了恶意网站。 Without the same origin policy JavaScript on that website could do anything to your Facebook account that you are allowed to do. 没有相同的原始策略,该网站上的JavaScript可能会对您的Facebook帐户做任何允许您做的事情。 For example read private messages, post status updates, analyse the HTML DOM-tree after you entered your password before submitting the form. 例如,在输入密码后提交表单之前,阅读私人消息,发布状态更新,分析HTML DOM树。

Regarding your question about why there is CORS, JSONP, etc. (ie, ways to get around the same-origin policy): This allows domains to specify ways for other origins to access their APIs (read content, etc.) CORS, for example, allows servers to specify a whitelist of safe domains that are allowed to, say, read from the server. 关于您为什么会有CORS,JSONP等(即解决同源策略的方法)的问题:这允许域指定其他来源访问其API(读取内容等)的方法。例如,允许服务器指定允许从服务器读取的安全域的白名单。

Cross origin security prevents one web site from stealing information from another web site that is logged in as you. 跨源安全性可防止一个网站从您登录的另一网站窃取信息。

For example, if I create a web-site and I frame the gmail site which you are regularly logged into and my web page is allowed to reach across to the gmail frame and read all your emails in the gmail web page, then I can easily steal lots of your info. 例如,如果我创建一个网站并构筑您经常登录的gmail网站,并且允许我的网页覆盖到gmail框架并阅读gmail网页中的所有电子邮件,那么我可以轻松地窃取大量信息。 Extend this to your banking site, your paypal site, etc... All, I would have to do to get access to this is to get you to come to my malicious site once when you happened to be logged into one of these other vulnerable sites. 将其扩展到您的银行站点,贝宝站点等...所有,要做到这一点,我要做的就是让您在碰巧登录到其他易受攻击的站点之一时进入我的恶意站点。网站。 Cross origin security prevents this type of cross site access so a non-trusted web site can't arbitrarily steal information from another web page in the same browser. 跨源安全性阻止了这种类型的跨站点访问,因此,不受信任的网站无法任意从同一浏览器中的另一个网页窃取信息。

The specific danger of accessing another site's web page "in the browser" is that the web page in the browser has access to all of your login cookies for that site so it often shows data that is not normally available to the general public via any public interface without also having legitimate login credentials. 在“浏览器”中访问另一个站点的网页的特定危险是,浏览器中的网页可以访问该站点的所有登录cookie,因此它通常显示通常无法通过任何公众获得的数据接口也没有合法的登录凭据。 This is why the web page's displayed in browsers must be protected. 这就是为什么必须保护浏览器中显示的网页。

The "work-arounds" such as JSONP or CORS all require cooperation from the host site so they can only be used if the host site feels like the interface they are permitting to be used through those mechanisms is safe and appropriate. 诸如JSONP或CORS之类的“变通办法”都需要宿主站点的配合,因此只有在宿主站点感觉它们允许通过这些机制使用的接口是安全且适当的情况下,才可以使用它们。 You cannot take a regular interface and, as a client, just use that via JSONP or CORS - it won't work. 您不能使用常规接口,而作为客户端,只能通过JSONP或CORS使用该接口-它将无法工作。 Both of those mechanisms require the server to specifically enable the cross origin access mechanism. 这两种机制都要求服务器专门启用跨源访问机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM