CSRF Laravel Cross Site Request Forgery protection¶

Laravel 使得防止应用 遭到跨站请求伪造攻击变得简单。 Laravel 自动为每一个被应用管理的有效用户会话生成一个 CSRF “令牌”,该令牌用于验证授权用 户和发起请求者是否是同一个人。 任何时候在 Laravel 应用中定义 HTML 表单,都需要在表单中引入 CSRF 令牌字段,这样 CSRF 保护中间件才能够正常验证请求。想要生成包含 CSRF 令牌的隐藏输入字段,可以使用辅助函 数 csrf_field 来实现:

中间件组 web 中的中间件 VerifyCsrfToken 会自动为我们验证请求输入的 token 值和 Session 中存储的 token 是否一致。
<form method="POST" action="/profile"> {{ csrf_field() }}
...
</form>

 

跨站请求伪造(Cross-Site Request Forgery, CSRF),恶意网站通过脚本向当前用户浏览器打开的其它页面的 URL 发起恶意请求,由于同一浏览器进程下 Cookie 可见性,导致用户身份被盗用,完成恶意网站脚本中指定的操作。

漏洞危害

  • 信息泄露:如登录ID,隐私信息等。
  • 恶意操作:如加好友,加购物车,删除数据等。

 

开放平台文档中心 https://docs.open.alipay.com/399/106918/

 Cross Site Request Forgery protection | Django documentation | Django https://docs.djangoproject.com/en/3.0/ref/csrf/

Cross Site Request Forgery protection

The CSRF middleware and template tag provides easy-to-use protection against Cross Site Request Forgeries. This type of attack occurs when a malicious website contains a link, a form button or some JavaScript that is intended to perform some action on your website, using the credentials of a logged-in user who visits the malicious site in their browser. A related type of attack, ‘login CSRF’, where an attacking site tricks a user’s browser into logging into a site with someone else’s credentials, is also covered.

The first defense against CSRF attacks is to ensure that GET requests (and other ‘safe’ methods, as defined by RFC 7231#section-4.2.1) are side effect free. Requests via ‘unsafe’ methods, such as POST, PUT, and DELETE, can then be protected by following the steps below.

 

Why might a user encounter a CSRF validation failure after logging in?

For security reasons, CSRF tokens are rotated each time a user logs in. Any page with a form generated before a login will have an old, invalid CSRF token and need to be reloaded. This might happen if a user uses the back button after a login or if they log in a different browser tab.

 

posted @ 2017-03-31 13:44  papering  阅读(219)  评论(0编辑  收藏  举报