我当前的解决方案:
function isAccessToWindowRestricted(w) {
try {
return !w.location.href;
} catch (e) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
还有更好的想法吗?有没有一种不使用 try-catch 的“合法”方法?
我需要在服务器上禁用同源策略。作为背景:我已经通过使用禁用网络安全标志启动 chrome 来验证一切正常。一切都按预期进行。
这是我在 nginx 方面所做的:
upstream phpfcgi {
server unix:/var/run/php5-fpm.sock; #for PHP-FPM running on UNIX socket
}
server {
listen 80;
root /var/www/yammi2;
index index.html index.php index.htm;
server_name myserver.ch;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials 'true';
add_header Access-Control-Allow-Headers 'Content-Type,accept,x-wsse,origin';
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';
# strip app.php/ prefix if it is present
rewrite ^/app\.php/?(.*)$ /$1 permanent;
location / {
index app.php;
try_files $uri @rewriteapp;
}
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
# pass the PHP scripts …
Run Code Online (Sandbox Code Playgroud) 我成功地将文件从 发送localhost:8888
到localhost:8080
(生产中的不同域),但在传输完成后我无法读取 HTTP 响应。
未捕获的安全错误:无法从“HTMLIFrameElement”读取“contentDocument”属性:阻止来源为“ http://localhost:8888 ”的框架访问来源为“ http://localhost:8080 ”的框架。请求访问的框架将“document.domain”设置为“localhost”,但正在访问的框架没有设置。两者都必须将“document.domain”设置为相同的值才能允许访问。
为了发送文件,为了兼容性支持,我试图让它适用于<form>
基于文件上传;没有XHR
根据。这是基本的 HTML 结构:
<form target="file-iframe" enctype="multipart/form-data" method="POST" action="invalid">
<input type="file" id="file-input" class="file-input" title="select files">
</form>
<iframe src="javascript:false;" id="file-iframe" name="file-iframe"></iframe>
Run Code Online (Sandbox Code Playgroud)
要将<iframe>
元素插入到 DOM 中,我执行以下操作:
document.domain = document.domain;
var domainHack = 'javascript:document.write("<script type=text/javascript>document.domain=document.domain;</script>")';
var html = '<iframe id="file-iframe" name="file-iframe"></iframe>';
var parent = document.getElementById('wrapper');
var iframe = UTILS.createDomElement(html, parent);
iframe.src = domainHack;
UTILS.attachEvent(iframe, 'load', function(e) {
// this throws the above SecurityError
var doc = …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 angualar js 应用程序与后端 spring boot 集成,其中我面临着预检请求不允许重定向
这是部署在 openshift 上,我通过在控制器方法中添加一些注释来配置启用 cors,这帮助我解决了:请求在传入请求中没有“Access-Control-Allow-Origin”标头:CORS 策略问题。
@CrossOrigin(allowedHeaders = "*", origins = "*", exposedHeaders =
"Access-Control-Allow-Origin", methods = {
RequestMethod.POST, RequestMethod.GET, RequestMethod.PUT,
RequestMethod.DELETE, RequestMethod.HEAD,
RequestMethod.OPTIONS, RequestMethod.PATCH, RequestMethod.TRACE })
@RestController
public class Controller {
@Autowired
Service botService;
@Autowired
Environment env;
@CrossOrigin()
@RequestMapping(value = "/jwtToken", method = {
RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<UnifiedService> botConntor(
@RequestBody UnifiedInput input, HttpServletRequest request) {
UnifiedBPMService output = botService.processBotRequest(input, request);
return new ResponseEntity<UnifiedService>(output, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
我在实际的角度应用程序中得到的错误是: …
当用户通过单击 iframe 中的链接重定向时,我想从 iframe 获取 URL。iframe 的来源与 Web 应用程序不同。
例如:
<iframe src="startingUrl" class="embed-responsive-item" id="iframe" sandbox="" allowfullscreen</iframe>
Run Code Online (Sandbox Code Playgroud)
我在 iframe 上添加了一个加载侦听器,以检测用户何时重定向到此 iframe 中的其他 url:
const iframe = document.getElementById("iframe");
iframe.addEventListener("load", (evt) => {
const location = iframe.contentWindow.location;
console.log(location); // this gives me a Location object where I can see the href property
console.log(location.href); // this gives me a SecurityError: Permission denied to get property "href" on cross-origin object, I also tried to get a copy of the object but that doesn't work either. …
Run Code Online (Sandbox Code Playgroud) 我正在使用端口转发在 android studio 模拟器中运行我的本地主机 Web 应用程序。但从服务器我收到后端 API 的 CORS 错误。我使用 CORS 禁用扩展或通过打开禁用安全标志的 google chrome 解决了桌面上的问题。如何在移动设备或 android studio 模拟器的 google chrome 中禁用 CORS?
发送 cookie 的默认值为SameSite=Lax
,这意味着 GET 请求会发送 cookie,但 POST 会被阻止。
对于跨源 GET 请求,响应会由于 而被阻止Same-Origin-Policy
,除非响应包含Access-Control-Allow-Origin
。
为什么还不够Access-Control-Allow-Origin
?
为什么你想回来Access-Control-Allow-Origin: someDomain.com
却不回来Access-Control-Allow-Credentials
?
为什么允许来自受信任域的跨源 GET 请求,但仅在发送 cookie 时才阻止响应?
当我在 Chrome 中完全关闭安全性时,它没有任何效果。
Safari 有类似的选项,它做到了。
在开发前端 Web 应用程序期间,我不会遇到 CROS 错误。
我会从 GUI 来完成,而不是从终端开始,从一些奇异的选项开始。不是这样的:在 Chrome 中禁用同源策略
如果在网站上http://www.mysite.com
有一个外部的js文件添加为
<script src="http://www.yoursite.com/new.js"></script>
Run Code Online (Sandbox Code Playgroud)
在http://www.yoursite.com/new.js
js文件中,有一个对脚本的ajax调用http://www.yoursite.com/new.js
在这种情况下,是否存在同源策略安全问题,因为它在另一个网站的网站中调用脚本?
我试图理解为什么不允许没有凭据的跨域请求(默认情况下,没有设置服务器以返回Access-Control-Allow-Origin标头).如果使用凭据请求,则非常简单 - 如果您已登录,则可以代表您在其他网站上执行某些恶意操作,例如在Facebook上.
例如这个请求:
xhr = new XMLHttpRequest();
xhr.open('GET', 'http://www.google.com');
xhr.send();
Run Code Online (Sandbox Code Playgroud)
产生错误(我在Chrome网站的控制台中执行此错误):
XMLHttpRequest无法加载http://www.google.com/.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http://stackoverflow.com "访问.
因此,服务器必须向此请求发送适当的头(例如Access-Control-Allow-Origin:*)才能正常工作.
这只是一个简单的请求,不会发送cookie.什么是这种限制的意义?如果允许这样的CORS,可能会发生什么安全问题?
没有凭据 - 我的意思是不发送cookie.XMLHTTPRequest的默认设置是withCredentials = false,这意味着在请求 - 链接中没有发送cookie .
javascript ×5
cors ×3
iframe ×3
ajax ×2
cross-domain ×2
csrf ×1
html ×1
http ×1
java ×1
nginx ×1
openshift ×1
spring-boot ×1
xss ×1