什么是Rails中的真实性令牌?
当前回答
什么是authentication_token ?
这是一个由rails应用程序使用的随机字符串,以确保用户是从应用程序页面请求或执行操作,而不是从其他应用程序或站点。
为什么authentication_token是必要的?
保护您的应用程序或站点免受跨站点请求伪造。
如何将authentication_token添加到表单?
如果您使用form_for标记生成表单,则会自动添加authentication_token,否则您可以使用<%= csrf_meta_tag %>。
其他回答
真实性令牌的设计使您知道您的表单是从您的网站提交的。它是由运行它的机器生成的,具有唯一的标识符,只有您的机器知道,因此有助于防止跨站请求伪造攻击。
如果您只是在rails拒绝AJAX脚本访问方面遇到困难,那么您可以使用
<%= form_authenticity_token %>
在创建表单时生成正确的令牌。
您可以在文档中阅读更多关于它的信息。
需要authenticity_token的方法
在post, put和delete等幂等方法的情况下需要authenticity_token,因为幂等方法对数据有影响。
为什么需要它
它需要防止邪恶的行为。Authenticity_token存储在会话中,每当在web页面上创建用于创建或更新资源的表单时,真实性令牌存储在隐藏字段中,并与服务器上的表单一起发送。在执行操作之前,用户发送的authenticity_token将与存储在会话中的authenticity_token进行交叉检查。如果authenticity_token相同,则进程继续,否则不执行操作。
Beware the Authenticity Token mechanism can result in race conditions if you have multiple, concurrent requests from the same client. In this situation your server can generate multiple authenticity tokens when there should only be one, and the client receiving the earlier token in a form will fail on it's next request because the session cookie token has been overwritten. There is a write up on this problem and a not entirely trivial solution here: http://www.paulbutcher.com/2007/05/race-conditions-in-rails-sessions-and-how-to-fix-them/
真实性令牌是rails防止“跨站请求伪造(CSRF或XSRF)攻击”的方法。
简单地说,它确保对web应用程序的put / POST / DELETE(可以修改内容的方法)请求是由客户端浏览器发出的,而不是来自第三方(攻击者),后者有权访问在客户端创建的cookie。
可以阻止的最小攻击示例:CSRF
在我的网站上邪恶。我说服你提交以下表格:
<form action="http://bank.com/transfer" method="post">
<p><input type="hidden" name="to" value="ciro"></p>
<p><input type="hidden" name="ammount" value="100"></p>
<p><button type="submit">CLICK TO GET PRIZE!!!</button></p>
</form>
如果您通过会话cookie登录到您的银行,那么cookie将被发送,并且在您甚至不知道的情况下进行转账。
也就是说,CSRF令牌开始发挥作用:
使用返回表单的GET响应,Rails发送一个非常长的随机隐藏参数 当浏览器发出POST请求时,它会发送参数,服务器只接受匹配的参数
因此,在一个真实的浏览器上的表单看起来是这样的:
<form action="http://bank.com/transfer" method="post">
<p><input type="hidden" name="authenticity_token" value="j/DcoJ2VZvr7vdf8CHKsvjdlDbmiizaOb5B8DMALg6s=" ></p>
<p><input type="hidden" name="to" value="ciro"></p>
<p><input type="hidden" name="ammount" value="100"></p>
<p><button type="submit">Send 100$ to Ciro.</button></p>
</form>
因此,我的攻击会失败,因为它没有发送authenticity_token参数,而且我不可能猜到它,因为它是一个巨大的随机数。
这种预防技术称为同步器令牌模式。
同源策略
但是,如果攻击者使用JavaScript发出两个请求,一个读取令牌,另一个进行转账,该怎么办?
仅使用同步器标记模式不足以防止这种情况!
这就是同源政策来拯救的地方,正如我在https://security.stackexchange.com/questions/8264/why-is-the-same-origin-policy-so-important/72569#72569上解释的那样
Rails如何发送令牌
涵盖:Rails: csrf_meta_tag如何工作?
基本上:
如果表单不是GET表单,像form_tag这样的HTML助手会为您添加一个隐藏字段 AJAX由jquery-ujs自动处理,它从csrf_meta_tags(在默认模板中)添加到头部的元元素中读取令牌,并将其添加到任何发出的请求中。 uJS还尝试在过期的缓存片段中更新表单中的令牌。
其他预防方法
check if certain headers is present e.g. X-Requested-With: What's the point of the X-Requested-With header? https://security.stackexchange.com/questions/23371/csrf-protection-with-custom-headers-and-without-validating-token Is an X-Requested-With header server check sufficient to protect against a CSRF for an ajax-driven application? check the value of the Origin header: https://security.stackexchange.com/questions/91165/why-is-the-synchronizer-token-pattern-preferred-over-the-origin-header-check-to re-authentication: ask user for password again. This should be done for every critical operation (bank login and money transfers, password changes in most websites), in case your site ever gets XSSed. The downside is that the user has to type the password multiple times, which is tiresome, and increases the chances of keylogging / shoulder surfing.
推荐文章
- 如何记录Ruby代码?
- Ruby:包含的反义词是什么?Ruby数组?
- 想要在Rails中找到没有关联记录的记录
- helper和helper_method做什么?
- 如何回滚只是一个步骤使用耙db:迁移
- 在Ruby中,proc和lambda有什么区别?
- 如何传递参数到一个Rake任务与环境在Rails?
- 获取当前正在执行的方法的名称
- 如何在Rails中计算相对时间?
- 如何在Rails迁移中将可空列更改为不可空列?
- 在Ruby中使用范围填充数组的正确方法
- “for”和“each”在Ruby中
- Ruby on Rails -从CSV文件导入数据
- 我如何复制一个哈希在Ruby?
- Ruby/Rails:将Date转换为UNIX时间戳