我花了一些时间来了解跨域资源共享的工作原理,我无法相信它的设计如此不安全。
\n\n当托管的网站foo.com
想要通过 ajax 请求存储的资源时bar.com
,浏览器会询问bar.com
是否允许该请求。\n只有bar.com
明确允许异步请求foo.com
(通过Access-Control-Allow-Origin
响应标头)时,资源才会传递到客户端。
如果应该读取数据,则\xc2\xb4s不是安全问题。但如果数据发送到请求的服务器,那就是。
\n\n过去,如果黑客成功在网站中插入 JavaScript 代码以窃取 cookie 数据或其他信息,同源策略\n会阻止他将信息直接发送到自己的服务器。
\n\n但多亏了 CORS,黑客只需启用任意来源,就可以直接将窃取的信息发送到自己的服务器。
\n\n我知道,CORS 仍在开发中,但已经得到几乎所有主流浏览器的支持。那么,CORS为什么要这样设计呢?如果向原始服务器请求发送 ajax 请求的权限,\xc2\xb4t 会更安全吗?
\n\n在我看来,这是安全性的降低。或者不是吗?\n我发现的与 CORS 相关的所有“已知问题”都与请求的服务器上的弱配置有关。
\n我有 2 个具有以下域的 NodeJS 应用程序:
在 localhost:3000 中,我有一个文本区域和一个提交按钮。
我想将文本区域的内容(使用 postMessage)发布到 localhost:8000/(some_id),并在 localhost:8000 页面上显示内容。
然后,我想在 localhost:3000 页面中显示 localhost:8000/(some_id) 的 iFrame。
我在完成这个任务时遇到了很多麻烦。我必须使用 postMessage() 以这种方式完成它。
PS:我知道最好避免使用 iFrame,但是出于我的应用程序的目的,这是有必要使用的。
我在http://localhost:8080上设置了一个服务器,其中http://example.com可以执行 POST 请求:
'use strict';
const express = require('express');
const app = express();
const port = 8080;
// allowing CORS for example.com
app.use('/', function (req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://example.com');
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'OPTIONS, POST');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length');
res.status(200).send();
} else {
next();
}
});
// handling POST requests
app.use('/', function (req, res) {
console.log('a client did a POST request');
res.status(200);
});
app.listen(port, () => console.log ('server started on port ' + port));
Run Code Online (Sandbox Code Playgroud)
它工作正常:由于同源策略,我无法从 …
我正在开发一个单页网络应用程序。它具有ASP.NET Core 3后端和Angular 9前端。我在 Visual Studio 中的 IIS Express 上运行后端,网址为http://localhost:59280。前端在 Visual Studio Code 中运行,使用ng serve
http ://localhost:4200。之前我不需要在后端开启 CORS,因为我只在 Chrome 中测试了应用程序,添加--disable-web-security
命令行参数就足以关闭同源策略。在实时服务器上不需要 CORS,上述跨域情况仅发生在我的开发机器上。
现在我想在 Firefox 中调试前端,但由于无法关闭 Firefox 的同源策略,所以我必须在后端打开 CORS。不幸的是,它不起作用,因为我使用 Windows 身份验证,并且它会停止默认未经身份验证的CORS 预检请求。如果我可以在没有 Windows 身份验证的情况下处理 HTTP OPTIONS 请求,则可以解决此问题。我认为这可以通过在web.config中添加类似的内容来完成:
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
<authorization>
<add accessType="Allow" verbs="OPTIONS" users="*" />
</authorization>
</security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
...但我收到一条错误消息:“此配置节不能在此路径上使用。当该节被锁定在父级别时,就会发生这种情况。” 显然 web.config 与launchSettings.json冲突,后者似乎在 Visual Studio 中的 IIS Express 上运行后端时控制身份验证,使用以下两行:
{
"iisSettings": …
Run Code Online (Sandbox Code Playgroud) 我有一个chrome扩展程序,它以特殊方式监视浏览器,将一些数据发送到Web服务器.在当前配置中,这是localhost.所以内容脚本包含这样的代码:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data)...
xhr.open('GET', url, true);
xhr.send();
Run Code Online (Sandbox Code Playgroud)
其中url参数是'http:// localhost/ctrl?params'(或http://127.0.0.1/ctrl?params - 没关系).
清单文件包含跨站点请求的所有必要权限.
扩展程序在大多数网站上运行正常,但在一个网站上我收到错误:
XMLHttpRequest cannot load http://localhost/ctrl?params. Origin http://www.thissite.com is not allowed by Access-Control-Allow-Origin.
我已经试过这是这里提出(一些权限*://*/*
,http://*/*
和<all_urls>
),但没有人帮助解决问题.
所以,问题是这个特定网站可能出现什么问题(显然可能有其他网站有类似的不当行为,我想知道这个的性质),以及如何解决这个错误?
我想要的很简单.有没有办法(包括解决方法)使这项工作?
function loadXMLDoc() {
var request = new XMLHttpRequest();
var gistRawFileUrl = 'https://gist.github.com/kentcdodds/5822336/raw/6ef128c8c8d6fe416782d969efa95d36e0acf374/KentsBlog.md';
request.onreadystatechange = function() {
if (request.readyState === 4 && request.status === 200) {
var gistFileContent = request.responseText;
doSomethingCool(gistFileContent);
}
};
request.open('GET', gistRawFileUrl, true);
request.send();
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我在控制台中执行此操作,我会得到:
XMLHttpRequest cannot load https://gist.github.com/kentcdodds/5822336/raw/6ef128c8c8d6fe416782d969efa95d36e0acf374/KentsBlog.md. Origin http://stackoverflow.com is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
这是有道理的,我知道我不会让GitHub打开他们的访问控制,但是如果有一个解决方法,或者如果我做错了那就太好了.
我意识到你可以通过点击JSONP端点来获取文件的内容,但这不会给你换行符,所以曾经这样:
Hello World!
You
Rock!
Run Code Online (Sandbox Code Playgroud)
现在是这样的:
Hello World!YouRock!
Run Code Online (Sandbox Code Playgroud)
提前致谢.
编辑新行的问题是我需要修复的问题,而不是一个要点.
在过去的几个小时里,我已经阅读了一些关于相同原点政策的内容,我对这个想法有所了解,但我对我目前的设置有疑问.
我有一个页面,我们会打电话,foo.com/home
在该页面上是一个链接,用于打开一个带有网址的iframe foo.com/home/bar
.现在,foo.com/home/bar
如果我有一个超链接说www.google.com
点击时,我可以让它将iframe重定向到谷歌而不违反相同的原始政策吗?我至少不会看到这种伤害,因为这将是一个简单的重定向.
我问的原因是因为上面的设置我无法将我的iframe重定向到www.google.com
.事实上,如果这是针对相同的原产地政策可能会有人将其分解并解释如何?我会理解,如果我使用iframe将数据提交到另一个域,但我只是想让我的iframe重定向到另一个域.
我正在构建一个chrome扩展程序,我在Gmail主页的弹出窗口中显示iframe.由于Gmail主页采用HTTPS格式,因此我的iframe也应采用https格式.我通过启用mod_ssl配置了apache2,并在apache2上运行了HTTPS.我制作了一个原生的PHP页面,并尝试在Gmail页面上的框架上显示该页面.我没有遇到从localhost加载页面的问题.但是当我想使用Laravel后端时,它显示了错误.
Refused to display 'https://localhost/laravel/laravel/public/index.php/chromelogin' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
有什么建议??
为了克服提交常规 HTTP 请求时遇到的 CORS(跨源请求共享)问题,我需要在 Angular 4 中提交一个隐藏表单。我在 HTML 中做到了这一点,没有任何问题。但是,我不知道如何在 Angular 中做到这一点。这是我的组件 html 中的代码:
<form form #f="ngForm" action="https://whatever.site.I_access" method="get">
<input type="hidden" name="scope" value="openid email">
<input type="hidden" name="response_type" value="id_token token">
<input type="hidden" name="client_id" value="myClientId">
<input type="hidden" name="redirect_uri" value="https://my.redirect.com/">
<input type="hidden" name="nonce" value="aNonceValue">
<button type="submit" (click)="f.submit()">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
在我的 .ts 文件中,我实现了“提交”功能。如果将其留空,则不会提交表单。在此函数中编写的命令是什么,只是为了提交具有指定操作的表单?
onSubmit(){
console.log("form submitted");
}
Run Code Online (Sandbox Code Playgroud)
有什么线索吗?
我在开发服务器上有一个非常简单的设置(两个页面都在我的本地测试服务器上localhost:5500
),其中我有一个主页
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example Mockup</title>
</head>
<body>
<iframe src="./nested.html" id="frame"></iframe>
<script>
var iframe = document.getElementById('frame');
console.log(iframe.contentDocument.body);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
和一个嵌套页面
<html>
<body>
<div id="hello">Hello, World</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中加载主页时,写入控制台的输出是:我可以使用<body></body>
访问该元素,但我想要包含子元素的 body 元素。谁能向我解释为什么会发生这种情况#hello
iframe.contentDocument.getElementById('hello')
javascript ×5
cors ×3
iframe ×3
angular ×2
html ×2
asp.net-core ×1
cross-domain ×1
csrf ×1
forms ×1
gist ×1
github ×1
hidden ×1
http ×1
http-headers ×1
jquery ×1
laravel ×1
laravel-4 ×1
node.js ×1
preflight ×1