原文:https://docs.djangoproject.com/en/1.8/ref/clickjacking/

The clickjacking middleware and decorators provide easy-to-use protection againstclickjacking. This type of attack occurs when a malicious site tricks a user into clicking on a concealed element of another site which they have loaded in a hidden frame or iframe.

通过点击劫持middleware和decorator可以很轻松的抵御点击劫持攻击。某个恶意网站将另外一个网站的页面(比如x.html)以iframe的方式覆盖在自己的网页(比如y.html)上,然后将x.html通过css设为透明,这样用户看到的是恶意网站的y.html页面,但点击时真正响应事件的是x.html,这种恶意行为即为点击劫持。

An example of clickjacking

Suppose an online store has a page where a logged in user can click “Buy Now” to purchase an item. A user has chosen to stay logged into the store all the time for convenience. An attacker site might create an “I Like Ponies” button on one of their own pages, and load the store’s page in a transparent iframe such that the “Buy Now” button is invisibly overlaid on the “I Like Ponies” button. If the user visits the attacker’s site, clicking “I Like Ponies” will cause an inadvertent click on the “Buy Now” button and an unknowing purchase of the item.

假如用户登陆到某个电商网站后,只要其点击了某个商品下方的“立即购买”,后台就会自动的完成提交订单、支付等一系列操作,而且为了方便,用户可能会使自己的帐号处于登陆状态,比如登陆时点击了“下次自动登陆”。恶意网站可能在某个页面上有个“我喜欢小矮马”的按钮,且上述网店的页面以iframe的方式覆盖在了当前页面上(透明的,用户看不到),“立即购买”的按钮位置正好与“我喜欢小矮马”重合,那么如果用户访问了恶意网站的这个页面,并点击了“我喜欢小矮马”按钮,那么就触发了“立即购买”对应的时间,然后就会莫名其妙的购买了某件商品。

Preventing clickjacking

Modern browsers honor the X-Frame-Options HTTP header that indicates whetheror not a resource is allowed to load within a frame or iframe. If the response contains the header with a value of SAMEORIGIN then the browser will only load the resource in a frame if the request originated from the same site. If the header is set to DENY then the browser will block the resource from loading in a frame no matter which site made the request.

现代浏览器在HTTP的header中加入了X-Frame-Options选项,浏览器可以根据这个选项来决定某个资源是否允许通过frame或iframe嵌套到别的网站中。如果响应头中将其值设置为了SAMEORIGIN,则告诉浏览器该资源仅允许被嵌套在同源网站,如果值为DENY,则在任何位置的嵌套都是不允许的。

Django provides a few simple ways to include this header in responses from your site:

  1. A simple middleware that sets the header in all responses.
  2. A set of view decorators that can be used to override the middleware or to only set the header for certain views.

The X-Frame-Options HTTP header will only be set by the middleware or view decorators if it is not already present in the response.

Django提供了一些简单易用的方式来把该选项加到你网站response头里:

  1. 通过中间件,该头部会加在所有响应头里
  2. 通过装饰器对指定的view加该头部(装饰器优先级高于中间件)

目前为止,只能通过以上方式来添加X-Frame-Options头。

How to use it

在所有的reponse里都加上X-Frame-Options

To set the same X-Frame-Options value for all responses in your site, put'django.middleware.clickjacking.XFrameOptionsMiddleware' to MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (...'django.middleware.clickjacking.XFrameOptionsMiddleware',...
)

This middleware is enabled in the settings file generated by startproject.

在MIDDLEWARE_CLASSES里加入django.middleware.clickjacking.XFrameOptionsMiddleware中间件,那所有的响应头里都会包含X-Frame-Options。在执行startproject后该选项即生效。

By default, the middleware will set the X-Frame-Options header to SAMEORIGIN for every outgoingHttpResponse. If you wantDENY instead, set the X_FRAME_OPTIONS setting:

X_FRAME_OPTIONS = 'DENY'

X-Frame-Options的默认值是 SAMEORIGIN,如果想改成DENY,可以在settings.py中进行设置:

X_FRAME_OPTIONS = 'DENY'

When using the middleware there may be some views where you do not want the X-Frame-Options header set. For those cases, you can use a view decorator that tells the middleware not to set the header:

如果你不想在某些view的响应里加X-Frame-Options,那么可以通过在view上加装饰器:xframe_options_exempt来搞定。代码如下:

from django.http import HttpResponse
from django.views.decorators.clickjacking import xframe_options_exempt@xframe_options_exempt
def ok_to_load_in_a_frame(request):return HttpResponse("This page is safe to load in a frame on any site.")


在view级别上加X-Frame-Options

主要是通过一系列的装饰器来实现。

from django.http import HttpResponse
from django.views.decorators.clickjacking import xframe_options_deny
from django.views.decorators.clickjacking import xframe_options_sameorigin@xframe_options_deny
def view_one(request):return HttpResponse("I won't display in any frame!")@xframe_options_sameorigin
def view_two(request):return HttpResponse("Display in a frame if it's from the same origin as me.")

Note that you can use the decorators in conjunction with the middleware. Use ofa decorator overrides the middleware.

注意,装饰器和中间件可以同时使用,但装饰器的优先级高于中间件。

Limitations

The X-Frame-Options header will only protect against clickjacking in a modern browser. Older browsers will quietly ignore the header and need otherclickjacking prevention techniques.

X-Frame-Options仅支持一些比较新的浏览器,版本较老的浏览器可以通过其他技术来方式点击劫持。

支持X-Frame-Options的浏览器

  • Internet Explorer 8+
  • Firefox 3.6.9+
  • Opera 10.5+
  • Safari 4+
  • Chrome 4.1+

Clickjacking Protection相关推荐

  1. 各种 django 静态文件的配置总结【待续】

    2019独角兽企业重金招聘Python工程师标准>>> 最近在学习django框架的使用,想引用静态css文件,怎么都引用不到,从网搜了好多,大多因为版本问题, 和我现在的使用的da ...

  2. 配置CAS应用客户端

    本文介绍JavaEE,Django, Php的CAS客户端配置方法. CAS客户端可以在这里找到,其中有些是官方维护,有些是社区维护.你也可以根据CAS协议编写一个客户端.关于CAS登陆验证流程请参阅 ...

  3. Django静态文件配置

    本文目的 最近用django开发项目,发现django的静态文件(js,css和img等)配置比较麻烦,开发环境和生产环境的配置还不一样,这里记录一下,作为备忘.我当前使用的版本是django v1. ...

  4. Django中如何防范CSRF跨站点请求伪造攻击

    CSRF概念 CSRF跨站点请求伪造(Cross-Site Request Forgery). 攻击者盗用了你的身份,以你的名义发送恶意请求,对服务器来说这个请求是完全合法的,但是却完成了攻击者所期望 ...

  5. python后台返回cookie_Django框架设置cookies与获取cookies操作详解

    本文实例讲述了Django框架设置cookies与获取cookies操作.分享给大家供大家参考,具体如下: 在Django里面,使用Cookie和Session看起来好像是一样的,使用的方式都是req ...

  6. django 1.8 官方文档翻译: 8-3 点击劫持保护

    点击劫持保护 点击劫持中间件和装饰器提供了简捷易用的,对点击劫持的保护.这种攻击在恶意站点诱导用户点击另一个站点的被覆盖元素时出现,另一个站点已经加载到了隐藏的frame或iframe中. 点击劫持的 ...

  7. Node.js+Express商业开发中的安全性考虑

    无论基于什么技术进行网站开发,安全性都是第一位的,特别是对于一些数据敏感性网站而言.本文引用的文章来自于Express.js官方网站,应当是每一个基于Express.js(或者基于此框架的更高级框架) ...

  8. django 1.8 官方文档翻译: 8-3 点击劫持保护 1

    点击劫持保护 点击劫持中间件和装饰器提供了简捷易用的,对点击劫持的保护.这种攻击在恶意站点诱导用户点击另一个站点的被覆盖元素时出现,另一个站点已经加载到了隐藏的frame或iframe中. 点击劫持的 ...

  9. http状态码 以及请求响应头相关

    1xx消息[编辑] 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非 ...

最新文章

  1. maven可选依赖(Optional Dependencies)和依赖排除(Dependency Exclusions)
  2. 如何做到长时间(4 个小时以上)精神专注?
  3. android 中的 odex 文件
  4. python时间段_python--时间段遍历
  5. C#的set 和 get 方法
  6. cls_template.php on line 1067,ecshop php5.5兼容utf-8版本
  7. gis怎么提取水系_地形还要建模,嫌电脑不够卡!直接提取它不香吗?Sketchup不建模,地形提取......
  8. msdev.exe 应用程序错误
  9. Unity编辑器汉化教程
  10. cuda驱动版本显卡对应关系
  11. 思科认证和华为认证哪个更香?
  12. SJA1000波特率计算方式
  13. 程序猿DD元旦送书:第二弹!
  14. 闲谈IPv6-IPv4的TCP和NAT让互联网变得畸形
  15. 阿里云操作系统——飞天(Apsara)
  16. 使用DexChain基金币模型实现去中心化CPU租赁及投票代理市场
  17. 汉字转拼音pinyin
  18. POI 导出Excel
  19. SQLite封锁机制
  20. 【SQL Server】grant, revoke, deny介绍及相关问题

热门文章

  1. framework/netcore 任务调度 FluentScheduler
  2. 个人网站第五版(2020年版)来了
  3. 修改注册表后不重启计算机,win10系统实现修改注册表不用重启就能生效的修复技巧...
  4. java nio keyiterator.remove()_Java SelectionKey.isValid方法代碼示例
  5. mkdir与 mkdirs区别
  6. Qrcode生成二维码相关问题
  7. 如果你太在意别人对你的看法,你将一事无成
  8. C++解析multipart/form-data
  9. 2022Java学习笔记七十三(异常处理:运行时异常、编译时异常、异常的默认处理的流程)
  10. 如何使用ABBYY将PDF文档转换为演示使用的PPT