php - 一些字符在 POST 期间编码,而另一些则没有

标签 php forms codeigniter xss html-encode

长话短说

CodeIgniters 的 Security类直接操作您的全局变量,例如 $_POST,它发现 file()file() 是一个威胁,因此它对其进行 HTML 编码。

// config.php from my apps folder is the culprit
$config['global_xss_filtering'] = TRUE;

自己动手(少数人,勇敢者)

在 CodeIgniter 2.1.4 中,转到 system/core/security.php 和行 #430-442:

/*
* Sanitize naughty scripting elements
*
* Similar to above, only instead of looking for
* tags it looks for PHP and JavaScript commands
* that are disallowed.  Rather than removing the
* code, it simply converts the parenthesis to entities
* rendering the code un-executable.
*
* For example:  eval('some code')
* Becomes:      eval('some code')
*/

$str = preg_replace('#(alert|cmd|passthru|eval|exec|expression|system|fopen|fsockopen|file|file_get_contents|readfile|unlink)(\s*)\((.*?)\)#si', "\\1\\2(\\3)", $str);

观察/问题

基本上,似乎 PHP 或 Apache 将 file ()file() 视为威胁。

有没有人以前经历过这种情况或有关于为什么会发生这种情况的文档资源?

任何人都可以在他们的服务器上测试这个,看看他们是否遇到相同的行为?我已经在我的开发和测试机器上对此进行了测试。我没有机会在生产机器上进行测试,因为我们的客户连接到它。

代码

HTML

<input name="q1" type="text" value="Profile (61) (D)">
<input name="q2" type="text" value="(61) (D)">
<input name="q3" type="text" value="file (61)">
<input name="q4" type="text" value="fil (61)">
<input name="q5" type="text" value="file ()">
<input name="q6" type="text" value="file()">

JS - 可能不相关

$.ajax({
    url: '/test_post'
    ,async: true
    ,cache: false
    ,type: 'POST'
    ,data: {
        q1: $('input[name="q1"]').val(),
        q2: $('input[name="q2"]').val(),
        q3: $('input[name="q3"]').val(),
        q4: $('input[name="q4"]').val(),
        q5: $('input[name="q5"]').val(),
        q6: $('input[name="q6"]').val()
    }
    ,dataType: 'json'
    ,success: function(data){
        console.log('irrelevant');
    }
});

网络 - Chrome 中的标题选项卡 - 表单数据部分

q1: Profile (61) (D)
q2: (61) (D)
q3: file (61)
q4: fil (61)
q5: file ()
q6: file()

PHP - CodeIgniter 2.1.4 框架

echo '<pre>'.$_POST['q1'].'</pre>'; // produces: Profile &#40;61&#41; (D)
echo '<pre>'.$_POST['q2'].'</pre>'; // produces: (61) (D)
echo '<pre>'.$_POST['q3'].'</pre>'; // produces: file &#40;61&#41;
echo '<pre>'.$_POST['q4'].'</pre>'; // produces: fil (61)
echo '<pre>'.$_POST['q5'].'</pre>'; // produces: file &#40;&#41;
echo '<pre>'.$_POST['q6'].'</pre>'; // produces: file&#40;&#41;

echo '<pre>'.html_entity_decode($_POST['q1']).'</pre>'; // produces: Profile (61) (D)
echo '<pre>'.html_entity_decode($_POST['q2']).'</pre>'; // produces: (61) (D)
echo '<pre>'.html_entity_decode($_POST['q3']).'</pre>'; // produces: file (61)
echo '<pre>'.html_entity_decode($_POST['q4']).'</pre>'; // produces: fil (61)
echo '<pre>'.html_entity_decode($_POST['q5']).'</pre>'; // produces: file ()
echo '<pre>'.html_entity_decode($_POST['q6']).'</pre>'; // produces: file()

// Both of these produce same exact result
echo '<pre>'.print_r($_POST, true).'</pre>';
echo '<pre>'.print_r($this->input->post(), true).'</pre>';

浏览器测试

  • Chrome 31.0.1650.57 m
  • IE 8
  • FF 25.0

服务器信息

开发

  • Windows 7 x64
  • Apache 2.2.17
  • PHP 5.3.5

测试

  • Windows Server 2008 R2 x64
  • Apache 2.2.21
  • PHP 5.3.8

最佳答案

根据user guide , 你可以通过设置

来摆脱它
$config['global_xss_filtering'] = FALSE;

或者只是删除这一行。

恕我直言,修改 $_POST 数组是一个设计错误,但就这样吧。

关于php - 一些字符在 POST 期间编码,而另一些则没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20081165/

相关文章:

codeigniter - 如何在 codeigniter 中使用 order-by?

javascript - Fetch Api 无法从 PHP 服务器获取 Session

php - PHP Net_SSH2-exec()如何获取命令返回码

forms - Symfony 表单 ChoiceType 忽略必需的属性

JavaScript 电话号码验证(拒绝带有字母字符的输入)

php - 表单提交到目录?可能的?

arrays - 使用CodeIgniter 2.0上传多个文件(数组)

java - CodeIgniter 将类加密为 java

phpinfo() 不显示 phar 启用/禁用

php - 如何从 bitbucket 克隆一个 laravel 项目并使用 Sourcetree 在你的计算机上安装和运行?