【问题标题】:How to enable CSRF Protection for specific forms in codeigniter如何在 codeigniter 中为特定表单启用 CSRF 保护
【发布时间】:2015-02-23 02:54:39
【问题描述】:

我现在正在开发一个包含大约 200 个 html 表单的 Web 应用程序,我被要求将 CSRF 保护添加到登录和注册表单中。 我已经在配置文件中启用了 CSRF 保护,但我必须在所有 200 个表单中使用表单助手库。 很明显,将 200 个表单的开始标签从

<form action="user/foo" method="post" id="formId">

<?php echo form_open(base_url().'user/foo' array('id' => 'formId'));  ?>

所以,我想知道是否有一种方法可以用于仅为注册和登录表单启用 csrf 保护? 非常感谢。

【问题讨论】:

    标签: codeigniter csrf


    【解决方案1】:

    您可以通过编辑config.php 文件来做到这一点:

    $config['csrf_protection'] = FALSE;
    
    if (isset($_SERVER["REQUEST_URI"])) {
        if(stripos($_SERVER["REQUEST_URI"],'/login') !== FALSE || stripos($_SERVER["REQUEST_URI"],'/register') !== FALSE ) {
            $config['csrf_protection'] = TRUE;
        }
    } 
    

    在您的视图文件中,添加以下内容:

    <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" />
    

    或者干脆使用form_open()函数自动添加隐藏的CSRF令牌字段。

    【讨论】:

      【解决方案2】:

      是的,你可以,你有两个选择:

      1- 像这样修改“application/config.php”文件的简单方法:

      $config['csrf_protection'] = FALSE;
      if (isset($_SERVER["REQUEST_URI"]))
          if(stripos($_SERVER["REQUEST_URI"],'/login') !== FALSE
                  || stripos($_SERVER["REQUEST_URI"],'/register') !== FALSE
          )
              $config['csrf_protection'] = TRUE;
      

      2- 通过覆盖安全类并创建一个挂钩类,如下所示:

      2-1 应用程序/核心/My_Security.php:

      <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
      
      class MY_Security extends CI_Security
      {
          public function regenerate_csrf_hash()
          {
                  // CSRF config
                  foreach (array('csrf_expire', 'csrf_token_name', 'csrf_cookie_name') as $key) {
                      if (false !== ($val = config_item($key))) {
                          $this->{'_' . $key} = $val;
                      }
                  }
      
                  // Append application specific cookie prefix
                  if (config_item('cookie_prefix')) {
                      $this->_csrf_cookie_name = config_item('cookie_prefix') . $this->_csrf_cookie_name;
                  }
      
                  // Set the CSRF hash
                  $this->_csrf_set_hash();
                  $this->csrf_set_cookie();
      
              }
          }
      

      2-2 application/hooks/EnableOptions.php

      <?php
      
      if (!defined('BASEPATH'))
          exit('No direct script access allowed');
      
      class EnableOptions {
      
          private $ci;
      
          public function __construct(){
                  $this->ci = &get_instance();
              }
      
          public function enableCSRF()
          {
              if ($this->ci->config->item('csrf_protection') === false) {
                  $uri = $this->ci->config->item('enable_csrf_for_uris_only');
                  $segment = $this->ci->uri->segment('1'); // 1 : for controller name
      
                  if (in_array($segment, $uri)) {
                      $this->ci->config->set_item('csrf_protection', true);
                      $this->ci->security->regenerate_csrf_hash();
      
                  }
              }
      
          }
      
      }
      

      2-3 应用程序/config/hooks.php:

      $hook['post_controller_constructor'][] = array(
          'class'    => 'EnableOptions',
          'function' => 'enableCSRF',
          'filename' => 'EnableOptions.php',
          'filepath' => 'hooks',
          'params'   => array()
      );
      

      2-4 应用程序/config/config.php : 通过将“enable_hooks”修改为true来启用钩子,

      $config['enable_hooks'] = TRUE;
      

      并为 URI 添加一个额外的参数,

      $config['enable_csrf_for_uris_only'] = array('login', 'register');
      

      就是这样。

      【讨论】:

      • 我遵循这个解决方案,但是 CI 停止渲染视图,只显示空白页。任何想法?日志似乎相当不错。日志上没有奇怪的输出。
      • 我的答案是 2014 年,这个 codeigniter.com/user_guide/libraries/security.html
      • 那么,你的问题是什么^^
      猜你喜欢
      • 2019-05-07
      • 2017-07-05
      • 1970-01-01
      • 2015-04-22
      • 2011-10-15
      • 2013-09-20
      • 2012-02-03
      • 1970-01-01
      • 2015-04-26
      相关资源
      最近更新 更多