简体   繁体   English

Veryfying codeigniter csrf 令牌

[英]Veryfying codeigniter csrf token

I have this form i am posting and I am placing my csrf token like this我有我要发布的这个表格,我正在像这样放置我的 csrf 令牌

controller method controller 方法

$csrf = array(
        'name' => $this->security->get_csrf_token_name(),
        'hash' => $this->security->get_csrf_hash()
);

passing to view like this像这样通过查看

<input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />

The docs say文档说

Tokens may be either regenerated on every submission (default): https://codeigniter.com/user_guide/libraries/security.html令牌可以在每次提交时重新生成(默认): https://codeigniter.com/user_guide/libraries/security.html

My question is how the csrf is actually verified.我的问题是如何实际验证 csrf。 When I use $this->security->get_csrf_hash() when the form is submitted, shall the value be equal to the hash submitted or how will the posted csrf hash be taken as valid?.当我在提交表单时使用$this->security->get_csrf_hash()时,该值是否应等于提交的 hash 或者如何将发布的 csrf hash 视为有效?

What you have todo is enable it in your $config['csrf_protection'] and use the form_open() function for your form.你要做的是在你的$config['csrf_protection']中启用它,并为你的表单使用form_open() function。

CodeIgniter will insert and check the CSRF automatically. CodeIgniter 将自动插入并检查 CSRF。

You can enable CSRF protection by altering your application/config/config.php file in the following way:您可以通过以下方式更改您的 application/config/config.php 文件来启用 CSRF 保护:

$config['csrf_protection'] = TRUE;

If you use the form helper, then form_open() will automatically insert a hidden csrf field in your forms.如果您使用表单助手,那么 form_open() 将自动在您的 forms 中插入一个隐藏的 csrf 字段。 If not, then you can use get_csrf_token_name() and get_csrf_hash()如果没有,那么您可以使用get_csrf_token_name()get_csrf_hash()

$csrf = array(
        'name' => $this->security->get_csrf_token_name(),
        'hash' => $this->security->get_csrf_hash()
);

<input type="hidden" name="<?=$csrf['name'];?>" value="<?=$csrf['hash'];?>" />

Tokens may be either regenerated on every submission (default) or kept the same throughout the life of the CSRF cookie.令牌可以在每次提交时重新生成(默认),也可以在 CSRF cookie 的整个生命周期内保持不变。 The default regeneration of tokens provides stricter security, but may result in usability concerns as other tokens become invalid (back/forward navigation, multiple tabs/windows, asynchronous actions, etc).令牌的默认重新生成提供了更严格的安全性,但由于其他令牌变得无效(后退/前进导航、多个选项卡/窗口、异步操作等),可能会导致可用性问题。 You may alter this behavior by editing the following config parameter您可以通过编辑以下配置参数来更改此行为

$config['csrf_regenerate'] = TRUE;

Select URIs can be whitelisted from csrf protection (for example API endpoints expecting externally POSTed content). Select URI 可以从 csrf 保护中列入白名单(例如 API 端点需要外部发布的内容)。 You can add these URIs by editing the 'csrf_exclude_uris' config parameter:您可以通过编辑'csrf_exclude_uris'配置参数来添加这些 URI:

$config['csrf_exclude_uris'] = array('api/person/add');

Regular expressions are also supported (case-insensitive):还支持正则表达式(不区分大小写):

$config['csrf_exclude_uris'] = array(
        'api/record/[0-9]+',
        'api/title/[a-z]+'
);

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

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