【问题标题】:XSS filtering on CodeIgniter formCodeIgniter 表单上的 XSS 过滤
【发布时间】:2016-01-25 21:38:40
【问题描述】:

我目前正在学习框架“CodeIgniter”。但是我的表单验证有问题。首先,让我向您展示我的观点:

<form method="post" action="connexion">
  <label for="pseudo">Pseudo : </label>
  <input type="text" name="pseudo" value="" />

  <label for="mdp">Mot de passe :</label>
  <input type="password" name="mdp" value="" />

  <input type="submit" value="Envoyer" /></form>

我的控制器:

public function connexion()
{
    $this->load->library('form_validation');

    $this->form_validation->set_rules('pseudo', '"user name"', 'trim|required|min_length[5]|max_length[52]|alpha_dash|encode_php_tags|xss_clean');
    $this->form_validation->set_rules('mdp',    '"password"',       'trim|required|min_length[5]|max_length[52]|alpha_dash|encode_php_tags|xss_clean');

    if($this->form_validation->run())
    {
        $this->load->view('connexion_ok');
    }
    else
    {
        $this->load->view('form');
    }
}

当我在 set_rules() 中删除控制器中的“xss_clean”过滤器时,它运行良好,表单有效。如果存在“xss_clean”,则它不起作用,它会进入 else。我在输入中不使用特殊字符,只使用字母。

在设置中,我将其设为 true:$config['global_xss_filtering'] = TRUE;

我在某处读到“xss_clean”过滤器没用。我还能用什么?也许是帮手或别的什么? 谢谢

【问题讨论】:

  • 验证!它在其他地方
  • XSS 保护功能默认关闭。要打开此功能,您只需将全局配置(即 system/application/config/config.php)设置为 $config['global_xss_filtering'] = TRUE;

标签: php codeigniter model-view-controller view controller


【解决方案1】:

首先设置$config['global_xss_filtering'] = FALSE; 你不需要或想要这个。此配置设置已正式弃用。它可能会在未来消失。

其次,如果您使用的是 3.0.x 版本,请从验证规则中删除“xss_clean”。它不在官方支持的list of form validation rules上。

您可以使用 XSS 过滤的地方是使用 Input Class 从 POST、GET、COOKIE 或 SERVER 获取数据时。大多数输入法都有第二个参数,可以通过xss_clean() 运行数据。示例:$this-&gt;input-&gt;post('some_data', TRUE); 将获取$_POST['some_data'] 的值并通过xss_clean() 运行它。如果第二个参数为 FALSE(或省略)xss_clean() 将不会被使用。

【讨论】:

    【解决方案2】:

    我不同意 DFriend 的回答。

    CodeIgniter documentation:

    XSS 转义应该在输出而不是输入上执行!

    因此,他提出的解决方案实际上与已弃用的全局配置 $config['global_xss_filtering'] = TRUE; 相同,不同之处在于添加更多代码和手动工作,为每个 $this-&gt;input-&gt;post 添加额外的布尔参数。

    正确的方法是在你的视图上使用set_value

    &lt;input type="password" name="mdp" value="&lt;?=set_value('mdp')?&gt;" /&gt;

    This function filters XSS vulnerabilities by default:

    set_value($field[, $default = ''[, $html_escape = TRUE]])

    【讨论】:

    • 我同意。 XSS 转义应该在输出上执行,而不是在输入上。但问题是为什么验证失败而不是何时执行 XSS 转义。每个网络开发人员都应该阅读并遵循XSS (Cross Site Scripting) Prevention Cheat Sheet 网页上的建议。
    猜你喜欢
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多