Php 编码点火器AJAX CSRF保护

Php 编码点火器AJAX CSRF保护,php,security,codeigniter,constructor,csrf,Php,Security,Codeigniter,Constructor,Csrf,我在后端使用codeigniter 最近我听说了“CSRF”一词,并决定保护这些请求 我的站点上几乎所有的操作都是通过Ajax进行的&有时我使用DOM操作创建/附加页面内容[这里有一个疑问,我如何将CSRF令牌注入到视图文件?] Okkey,在那之后我如何验证 假设我将这些值添加为令牌并传递给服务器,那么我可以使用构造函数来检查和验证这一点吗 例: 有没有可能做一些事情来联系这个 请给我推荐一些好主意和惯例 多谢各位 对于我的ajax调用,我通常执行两个检查 确保它是一个ajax请求,使用一个小

我在后端使用codeigniter

最近我听说了“CSRF”一词,并决定保护这些请求

我的站点上几乎所有的操作都是通过Ajax进行的&有时我使用DOM操作创建/附加页面内容[这里有一个疑问,我如何将CSRF令牌注入到视图文件?]

Okkey,在那之后我如何验证

假设我将这些值添加为令牌并传递给服务器,那么我可以使用构造函数来检查和验证这一点吗

例:

有没有可能做一些事情来联系这个

请给我推荐一些好主意和惯例


多谢各位

对于我的ajax调用,我通常执行两个检查

确保它是一个ajax请求,使用一个小的帮助文件

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('ajax_check')) {
    /**
     * Check AJAX
     * 
     * Checks to see if you (or the royal I) are dealing with an AJAX call.
     * 
     * @return boolean
     */
    function ajax_check() {
        if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
            return TRUE;
        } else {
            show_404();
            return FALSE;
        }
    }
}

if ( ! function_exists('ajax_response')) {
    /**
     * JSON Response Wrapper
     * 
     * Wraps up any data nicely for sending back to an ajax call
     *
     * @return  string
     */
    function ajax_response($status, $data) {
        if (!is_array($data)) {
            $data = array();
        }
        // Set the JSON header appropriately
        header('Content-Type: application/json');
        // Echo out the array into json
        echo json_encode(array_merge(array('status' => $status), $data));
        exit;
    }
}

if ( ! function_exists('ajax_force_fail')) {
    /**
     * Force AJAX Failure
     * 
     * If you ever need to, force an AJAX to fail
     */
    function ajax_force_fail() {
        $_ci =& get_instance();
        $_ci->output->set_status_header(500);
    }
}
以及与xsrf类似的方法。 文件:

鉴于,

<form action="" method="post">
    <?php echo xsrf_get_token_field(); ?>
    ...
</form>

...

可能会有所帮助。在视图中,您必须回显xsrf_get_token_字段(),因为它返回html,而不是直接回显。my bad,已经更改了答案。这很好,但我不能直接在视图文件上使用php代码,因为它是由java脚本[某些部分]生成的。您可以向js添加ajax调用来获取xsrf token,使用xsrf_get_token()或者您可以将该标记保存在HTML文档中的某个位置,然后在使用javascript生成视图部分时从那里读取该标记。也许把它放在页脚的某个地方,或者作为隐藏的输入?
public function some_function() {
    $this->load->helper('ajax');
    ajax_check();

    try {
        // do something
        ajax_response('success', array('data' => $some_var));
    } catch (Exception $e) {
        ajax_response('failure', array('data' => $e->getMessage()));
    }
}
<?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);
    }
}
public function some_other_function() {
    $this->form_validation->set_rules('username', 'Username', 'required|callback_check_token');
    if($this->form_validation->run() == TRUE ) {
        // do something
    } else {
        // something else
    }
}

// callback function
public function check_token($val) {
    if (xsrf_check_token($val) == TRUE) {
        return TRUE;
    } else {
        $this->form_validation->set_message('check_token', 'Oops');
        return FALSE;
    }
}
<form action="" method="post">
    <?php echo xsrf_get_token_field(); ?>
    ...
</form>