【问题标题】:Codeigniter CSRF token problemCodeigniter CSRF 令牌问题
【发布时间】:2011-09-28 17:13:47
【问题描述】:

我创建了一个简单的注册/新闻通讯网站,但我遇到了一个奇怪的问题。有些人收到一个错误提示

遇到错误 操作 您的请求不被允许。

我已经尝试过 google,发现当 CSRF 设置为 true 时人们也遇到了同样的问题。然而,我不会发生在每个人身上,只是一小部分人。我正在使用 form_open 和 form_close,我可以看到隐藏字段(令牌)。

我正在使用最新版本的 Codeigniter 2.0.2

这是我的控制器

    function __construct() {
    parent::__construct();
    session_start();
}

function index() {

    $this->load->model('beta_signup_model');

    $this->form_validation->set_rules('mail','e-mail','required|valid_email|xss_clean|callback__mail_check');

    // Check for errors
    if($this->form_validation->run() == FALSE) {

        // The system found a form validation error


    } else {

        // No errors found
        $_SESSION['mail_success'] = 1;
        $_SESSION['mail'] = $this->input->post('mail');

        redirect(base_url() . 'confirm');

    }

    ///// FILLS OUT INPUT FIELDS /////

    // Loads field_populator_helper
    $this->load->helper('field_populator_helper');

    // Defines input field names
    $input_names = array(
                    'mail',
    );

    // Defines default values   
    $default_values = array(
                    'Skriv inn e-posten din..',
    );

    // Auto-populates fields with blur and focus
    $data['field_populator'] = populateFields($input_names, $default_values);

    $this->load->view('frontpage_view', $data);

}

【问题讨论】:

  • 可能该功能需要 cookie 和/或 javascript。一些有错误的人可能没有启用所需的功能,因此CSFR检查(基于令牌?)没有通过。您将哪个组件用于 CSFR?
  • 我以前也有同样的问题。每当重新提交经过验证的表单时,都会发生这种情况。可能有人在这里有解决方案。
  • 奇怪,有人有解决办法吗?

标签: php codeigniter frameworks token


【解决方案1】:

对于使用 Codeigniter 3.0 的任何人,您可以执行以下操作:

改变

$config['csrf_regenerate'] = TRUE;

$config['csrf_regenerate'] = FALSE;

这会阻止在每次提交时重新生成 CSRF 令牌。

【讨论】:

【解决方案2】:
  • 您需要更新核心安全文件或仅从当前版本的 codeigniter codeigniter core secuirty file 获取 csrf 代码。

  • 您可以将 ajax 用作: var cct = $("input[name=csrf_test_name]").val(); $.post(site_url + "user/update_product", { product_id: id , 'csrf_test_name': cct})

  • Codeigniter CSRF 不会在页面刷新时重新生成令牌。它只会在 post 上而不是在 get 上重新生成。安全测试人员发现它是一个漏洞。 如果有人对此有解决方案..请分享,这将对每个人都有帮助。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题:在 MAMP 上完全干净地安装 CI 2.1.0,并按照用户指南中的教程进行操作。

    经过大量搜索和谷歌搜索,我发现在'application/config.php'中,变量$config['cookie_prefix']必须始终设置为空,否则如果打开CSRF保护,会出现此错误发生。

    可能还涉及其他问题 - 即会话库、加密或 XSS 保护等 - 但只是将 'cookie_prefix' 留空似乎已经为我排序了。

    我希望这对其他人有所帮助。

    【讨论】:

      【解决方案4】:

      更改 config.php 中的“sess_cookie_name”可能会有所帮助,以确保它没有空格或下划线。

      $config['sess_cookie_name'] = 'mycookiename';
      

      【讨论】:

        【解决方案5】:

        我使用自己的 csrf 助手,因为我发现当将配置中的选项设置为 true 时,它​​会对我的 ajax 调用造成严重破坏。

        我使用 *xsrf_get_token_field()* 生成字段,并使用 *xsrf_check_token()* 作为表单验证中的自定义回调。

        ?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
        
        if ( ! function_exists('xsrf_get_token')) {
            /**
             * Get XSRF Token
             * 
             * Returns a token that exists for one request that verifies that
             * the action was executed by the person that requested it
             *
             * @return  string
             */
            function xsrf_get_token() {
                $ci =& get_instance();
                if ($ci->session->userdata('xsrf_hash')) {
                    $token = $ci->session->userdata('xsrf_hash');
                } else {
                    // Generate the token
                    $token = sha1(microtime().$ci->uri->uri_string());
                    // Set it in the session
                    $ci->session->set_userdata('xsrf_hash', $token);
                }
        
                //Return it
                return $token;
            }
        }
        
        if ( ! function_exists('xsrf_get_token_field')) {
            /**
             * Get XSRF Token Field
             * 
             * Returns an xhtml form element to include xsrf token.
             * You can specify the id/name attribute of the input.
             * Has a dependancy to get_xsrf_token().
             *
             * @param   string  The id/name to be used
             * @return  string
             */
            function xsrf_get_token_field($name='auth_token') {
                return '<input type="hidden" id="'.$name.'" name="'.$name.'" value="' .xsrf_get_token(). '" />';
            }
        }
        
        if ( ! function_exists('xsrf_delete_token')) {
            /**
             * Delete XSRF Token
             * 
             * Deletes the xsrf token
             *
             * @return  boolean
             */
            function xsrf_delete_token() {
                $ci =& get_instance();
                if ($ci->session->userdata('xsrf_hash')) {
                    $ci->session->unset_userdata('xsrf_hash');
                    return TRUE;
                } else {
                    return FALSE;
                }
            }
        }
        
        if ( ! function_exists('xsrf_check_token')) {
            /**
             * Get XSRF Token Field
             * 
             * Checks that the token is still valid, returns true if so. 
             * Deletes old token after valid or fail.
             * Has a dependacy to xsrf_delete_token()
             *
             * @param   string  The challenge token
             * @return  boolean
             */
            function xsrf_check_token($challenge_token) {
                // CI
                $ci =& get_instance();
                // Get the stored token
                $token = $ci->session->userdata('xsrf_hash');
                // Delete the old token
                xsrf_delete_token();
                // Returns if the token is the right token
                return ($token == $challenge_token);
            }
        }
        

        【讨论】:

        【解决方案6】:

        当隐藏字段中的令牌与 cookie 中的令牌匹配时,CSRF 有效。检查四件事:

        1. 不要使用原生 php 会话(session_start 等)。切换到 CI 中的 Session 类。 http://codeigniter.com/user_guide/libraries/sessions.html

        2. 检查 /application/config/config.php 中的 cookie 配置

        3. 检查表单中令牌的值,每次页面刷新都会不同吗?

        4. 也许尝试从https://bitbucket.org/ellislab/codeigniter-reactor/downloads下载当前版本的CI

        【讨论】:

        • 很可能是会话问题。我对 CI 的会话垃圾收集过程做了噩梦,我不知道他们已经解决了所有问题。特别是如果这个问题很难重复并且只限于少数人,我会减少会话刷新超时,看看是否更容易重现。
        猜你喜欢
        • 2011-07-19
        • 2016-01-31
        • 1970-01-01
        • 2016-07-14
        • 1970-01-01
        • 2014-10-06
        • 2014-09-25
        • 1970-01-01
        • 2018-04-07
        相关资源
        最近更新 更多