简体   繁体   English

防止通过JavaScript进行点击劫持攻击

[英]preventing clickjacking attack by javascript

Clickjacking is when people trick users into clicking a button they're not supposed to, making them perform a malicious action. 点击劫持是指人们诱使用户点击他们不应该点击的按钮,从而使他们执行恶意操作。

I'm working on a product which, as an option for merchants, provides an iFrame component that can be embedded into a website to make a payment. 我正在开发一种产品,作为商家的一种选择,它提供了一个iFrame组件,该组件可以嵌入到网站中进行付款。 Signed in users will see a button in the iframe that they can click to perform an important action. 登录的用户将在iframe中看到一个按钮,他们可以单击该按钮以执行重要操作。 This action should only be called when the click is genuinely theirs. 仅当点击是真正的点击时,才应调用此操作。

i use this code to prevent clickjacking : 我使用此代码来防止点击劫持:

if (top == self ||  parent != top ||  document.location.hostname != document.domain) {  top.location.replace("https:\/\/www.mysite.com\/?404");}

can someone break into my code ? 有人可以破解我的代码吗?

note: i don't want to use x-frame-option 注意:我不想使用x-frame-option

thanks 谢谢

From an Iframe you cannot really control clicks from the parent, if they click inside the Iframe but another event is watching it, you cannot really prevent it being from a different domain. 在iframe中,您无法真正控制来自父级的点击,如果它们在iframe中单击,但又有另一个事件在监视它,则无法真正阻止它来自其他域。

But all is not lost, the Iframe itself cannot stop it, but it can be wrapped with something like this. 但是,一切并不会丢失,Iframe本身无法阻止它,但是可以用类似的方法包装它。 This is assuming jquery, might be best to translate to a native version for your application, in the interest of showing an example I will use jQuery. 这是假设jquery,可能最好将其转换为您的应用程序的本机版本,以显示一个示例,我将使用jQuery。

<div id="i_wrap"><iframe src="SRC"></iframe></div>
<script>
$('#i_wrap').on('click',function(event){
event.stopPropagation();
});
</script>

Of course this is not a cure-all, there are still ways around this. 当然,这还不能解决所有问题,但仍有很多解决方法。 You could also use a portion of the new HTML 5 cross document messaging read here on it to do some validation and possible warn the user on an unsafe site (if your iframe gets no message, then you show no button). 您还可以使用此处阅读的新HTML 5跨文档消息传递的一部分进行验证,并可能在不安全的网站上警告用户(如果iframe没有收到消息,则您不会显示任何按钮)。

Though I have no experience in the cross document messaging methods, and I am sure they probably don't allow different domains (though there may be ways around that, to an extent). 尽管我没有跨文档消息传递方法的经验,但是我确信它们可能不允许使用不同的域(尽管在某种程度上可以解决这个问题)。

Though this question is not totally clear and I may not be understanding it perfectly, if you update your question with more details I will update my answer to suit. 尽管这个问题尚不完全清楚,我可能还不太了解,但是如果您用更多详细信息更新您的问题,我将更新我的答案以适合您。

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

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