Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

前端安全:防范点击劫持的两种方式 #126

Closed
islishude opened this issue Oct 19, 2017 · 0 comments
Closed

前端安全:防范点击劫持的两种方式 #126

islishude opened this issue Oct 19, 2017 · 0 comments

Comments

@islishude
Copy link
Owner

islishude commented Oct 19, 2017

近期的工作都和交易有关,写代码都特别谨慎,前面说过前端如何防范跨站请求伪造攻击(CSRF),这次准备简单说说防范点击劫持。

什么点击劫持?最常见的是恶意网站使用 <iframe> 标签把我方的一些含有重要信息类如交易的网页嵌入进去,然后把 iframe 设置透明,用定位的手段的把一些引诱用户在恶意网页上点击。这样用户不知不觉中就进行了某些不安全的操作。

image

有两种方式可以防范。

使用 JS 防范

判断顶层视口的域名是不是和本页面的域名一致,如果不一致就让恶意网页自动跳转到我方的网页。当然你还可以恶心一下这些恶意网站,比如说弹窗十几次,或者跳转到某些404页面。

if (top.location.hostname !== self.location.hostname) {
    alert("您正在访问不安全的页面,即将跳转到安全页面!");
    top.location.href = self.location.href;
}

使用 HTTP 头防范

通过配置 nginx 发送 X-Frame-Options 响应头,这样浏览器就会阻止嵌入网页的渲染。更详细的可以查阅MDN上关于X-Frame-Options 响应头的内容。

add_header X-Frame-Options SAMEORIGIN;

原文首发在我的 github 博客

@islishude islishude changed the title 禁止网页被嵌入他人网页 前端安全:防范点击劫持的两种方式 Oct 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant