假设您不希望其他网站在以下位置"构建"您的网站<iframe>:
<iframe src="http://example.org"></iframe>
Run Code Online (Sandbox Code Playgroud)
因此,您将反框架,帧破坏JavaScript插入到您的所有页面中:
/* break us out of any containing iframes */
if (top != self) { top.location.replace(self.location.href); }
Run Code Online (Sandbox Code Playgroud)
优秀!现在你自动"破解"或突破任何包含iframe.除了一个小问题.
事实证明,您的帧破坏代码可能被破坏,如下所示:
<script type="text/javascript">
var prevent_bust = 0
window.onbeforeunload = function() { prevent_bust++ }
setInterval(function() {
if (prevent_bust > 0) {
prevent_bust -= 2
window.top.location = 'http://example.org/page-which-responds-with-204'
}
}, 1)
</script>
Run Code Online (Sandbox Code Playgroud)
此代码执行以下操作:
window.onbeforeunload事件处理程序离开当前页面时,都会递增计数器setInterval()如果它看到计数器递增,则将当前位置更改为攻击者控制的服务器我的问题是 - 这更像是一个JavaScript拼图,而不是一个实际的问题 - 你怎么能打败破坏框架的破坏者呢?
我有一些想法,但在我的测试中没有任何效果:
onbeforeunload事件onbeforeunload = null …为什么send经常被称为
xhr.send(null)
Run Code Online (Sandbox Code Playgroud)
代替
xhr.send()
Run Code Online (Sandbox Code Playgroud)
?
W3,MDN和MSDN都声明它是可选的.此外,ActiveX控件似乎不需要参数:
hr=pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.6.0");
SUCCEEDED(hr) ? 0 : throw hr;
hr=pIXMLHTTPRequest->open("GET", "http://localhost/books.xml ", false);
SUCCEEDED(hr) ? 0 : throw hr;
hr=pIXMLHTTPRequest->send(); // <-- this line
SUCCEEDED(hr) ? 0 : throw hr;
Run Code Online (Sandbox Code Playgroud)
这种做法send(null)至少可以追溯到2005年的Google地图,但是在缩小范围之后,没有任何解释:
Y.asynchronousTransform = function (qc, vb, kc, Nc, Ba) {
if (m.type == 3) return;
var cc = Y.getCached(kc);
if (cc) {
cc.transformToHTML(qc, vb);
if (Nc) Nc();
return
}
var yc = qa.create(Ba); …Run Code Online (Sandbox Code Playgroud) 我正在尝试将XMLHttpRequest发送到粘贴网站.我正在发送一个包含api所需的所有字段的对象,但我一直在遇到这个问题.我已经阅读了这个问题,我想:
httpReq.setRequestHeader('Access-Control-Allow-Headers', '*');
Run Code Online (Sandbox Code Playgroud)
会解决它,但事实并非如此.有没有人有关于此错误的信息和/或我如何解决它?
这是我的代码:
(function () {
'use strict';
var httpReq = new XMLHttpRequest();
var url = 'http://paste.ee/api';
var fields = 'key=public&description=test&paste=this is a test paste&format=JSON';
var fields2 = {key: 'public', description: 'test', paste: 'this is a test paste', format: 'JSON'};
httpReq.open('POST', url, true);
console.log('good');
httpReq.setRequestHeader('Access-Control-Allow-Headers', '*');
httpReq.setRequestHeader('Content-type', 'application/ecmascript');
httpReq.setRequestHeader('Access-Control-Allow-Origin', '*');
console.log('ok');
httpReq.onreadystatechange = function () {
console.log('test');
if (httpReq.readyState === 4 && httpReq.status === 'success') {
console.log('test');
alert(httpReq.responseText);
}
};
httpReq.send(fields2);
}());
Run Code Online (Sandbox Code Playgroud)
这是确切的控制台输出:
good
ok
Failed to load …Run Code Online (Sandbox Code Playgroud) 我会通过使用jQuery $.ajax函数解决这个问题,但在这种情况下jQuery不是选项.相反,我将使用CORS请求.我觉得响应请求的网络服务器出了问题,我很难搞清楚问题所在.
这是我创建CORS请求的代码
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url, true);
httpRequest.setRequestHeader( 'Access-Control-Allow-Origin', '*');
httpRequest.setRequestHeader( 'Content-Type', 'application/json' );
httpRequest.onerror = function(XMLHttpRequest, textStatus, errorThrown) {
console.log( 'The data failed to load :(' );
console.log(JSON.stringify(XMLHttpRequest));
};
httpRequest.onload = function() {
console.log('SUCCESS!');
}
Run Code Online (Sandbox Code Playgroud)
这是console.log错误:
XMLHttpRequest无法加载 http://test.testhost.com/testpage.请求标头字段Access-Control-Allow-Origin不允许使用Access-Control-Allow-Origin.
以下是标题信息:
> Remote Address:**.**.***.**:80 Request
> URL:http://test.testdomain.com/testpage Request
> Request Method:OPTIONS
> Status Code:200 OK
Run Code Online (Sandbox Code Playgroud)
请求标题:
OPTIONS /content-network HTTP/1.1
Host: test.testhost.com
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Access-Control-Request-Method: POST
Origin: http://test.testdomain.com
User-Agent: Mozilla/5.0 (Macintosh; Intel …Run Code Online (Sandbox Code Playgroud) 我正在为我的应用程序编写一个简单的类似灯箱的插件,我需要嵌入一个链接到任意页面的iframe.问题是,许多网站(例如,facebook,nytimes,甚至stackoverflow)将检查是否嵌入在一个框架内,如果是,将刷新页面本身作为父页面.这是一个众所周知的问题,我认为没有什么可以做的.但是,如果网站支持嵌入或不支持,我希望能够事先知道.如果没有,我想在新的标签/窗口中打开页面,而不是使用iframe.
有没有一个技巧可以让我在javascript中检查这个?
也许有一个服务器端脚本可以检查链接,看看他们是否允许iframe嵌入?
我正在开发一个浏览器扩展,所以有机会做一些非常有创意的事情.我的扩展程序已加载到每个页面上,所以我认为有一种方法可以在iframe网址中传递一个参数,如果它破坏iframe,则可以通过扩展程序获取该参数.然后,我可以将域添加到不支持iframe嵌入的网站列表中.这可能有效,因为扩展未在iframe中加载.我将继续努力,但与此同时......
澄清:
我愿意接受没有办法"破坏""框架破坏者",即我知道我不能在iframe中显示一个不想在一个页面中的页面.但我希望我的应用程序优雅地失败,这意味着如果不支持iframe嵌入,则在新窗口中打开链接.理想情况下,我想检查iframe在运行时嵌入支持(javascript),但我可以看到使用代理的潜在服务器端解决方案,如上面的评论中所建议的那样.希望我能建立一个不允许嵌入iframe的网站数据库.
javascript ×5
iframe ×2
ajax ×1
cors ×1
embed ×1
framebusting ×1
html ×1
jquery ×1
response ×1