CI自动过滤掉百分号%后两位的问题解决_PHP教程

php中文网
发布: 2016-07-13 10:34:01
原创
1019人浏览过

在 codeigniter 做的网站里,想输入一段代码:

$var = sprintf("%04d", 2);
登录后复制

但是发现入库后,代码变成了

$var = sprintf("d", 2);
登录后复制

在网上环境,本地环境都测试过,最终确认是 CodeIgniter 系统的问题。下面谈一下问题解决的过程与思维方法:

1. 是 config.php 的 permitted_uri_chars 吗?

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
登录后复制

在 stackoverflow 上找到几个差不多的问题,有答案说改 config.php 的 permitted_uri_chars 就行了。

立即学习PHP免费学习笔记(深入)”;

Ahem... after looking at your sample string again. Here is why you get "The URI you submitted has disallowed characters". Short explanation: Add the ampersand & to the allowed characters list $config['permitted_uri_chars'] = 'a-z 0-9~%.:_+&-';

试过了,没效果,于是就查找应用了 $config['permitted_uri_chars'] 的代码。

2. 是 core/Input.php 的 _clean_input_keys() 函数问题吗?

	function _clean_input_keys($str)   
	{   
		$config = &get_config('config');   
		if ( ! preg_match("/^[".$config['permitted_uri_chars']."]+$/i", rawurlencode($str)))   
		{   
			exit('Disallowed Key Characters.');   
		}   
		
		// Clean UTF-8 if supported
		if (UTF8_ENABLED === TRUE)
		{
			$str = $this->uni->clean_string($str);
		}
		return $str;   
	} 
登录后复制

这个函数使用了 $config['permitted_uri_chars'] 直接过滤 post 过来的数据,很大原因就是元凶了。我把它单独出来,经过测试发现,post $var = sprintf("%04d", 2); 过来,结果还是 $var = sprintf("%04d", 2); ,%04并未被过滤,看来还得细细地找。

3. 是 xss 的防御机制吗?

stackoverflow 有个人说他完美解决了这个问题,是 xss clean 的原因。

:) God damn URLDECODE, I have looked at the code in URI.php but the xss clean is doing the job so I missed it. Thank you now everything is perfect. – RaduM

落笔AI
落笔AI

AI写作,AI写网文、AI写长篇小说、短篇小说

落笔AI 41
查看详情 落笔AI

于是我找到了 core/security.php 下的 xss_clean() 函数。把函数体代码全部注释掉,发现输入还是会把 %04 过滤掉,显然也不是 xss 的问题。

4. 问题出在 _clean_input_data() 函数

重新回到 Input.php,发现 _clean_input_data 与 _clean_input_keys 有联系。

$new_array[$this->_clean_input_keys($key)] = $this->_clean_input_data($val);
登录后复制

于是把 _clean_input_data() 的函数体注释掉,竟然输入没被过滤了。继续缩小范围,发现是这段代码惹得祸:

// Remove control characters
// 就是这个会把%0x过滤掉
$str = remove_invisible_characters($str);
登录后复制

5. 元凶找到了 remove_invisible_characters() 函数

那么 remove_invisible_characters() 这个函数是什么呢?

这个函数在 core/Common.php中,我把它揪出来:

	function remove_invisible_characters($str, $url_encoded = TRUE)
	{
		$non_displayables = array();
		
		// every control character except newline (dec 10)
		// carriage return (dec 13), and horizontal tab (dec 09)
		
		if ($url_encoded)
		{
			$non_displayables[] = '/%0[0-8bcef]/';	// url encoded 00-08, 11, 12, 14, 15
			$non_displayables[] = '/%1[0-9a-f]/';	// url encoded 16-31
		}
		
		$non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';	// 00-08, 11, 12, 14-31, 127

		do
		{
			$str = preg_replace($non_displayables, '', $str, -1, $count);
		}
		while ($count);

		return $str;
	}
登录后复制

看这么几行代码:

if ($url_encoded)
{
	$non_displayables[] = '/%0[0-8bcef]/';	// url encoded 00-08, 11, 12, 14, 15
	$non_displayables[] = '/%1[0-9a-f]/';	// url encoded 16-31
}
登录后复制

明确了吧,他会把%0与%1开头的3个字符过滤掉。直接把这个注释掉,问题解决。

记录这个问题解决的思维全过程。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/752355.htmlTechArticle在 CodeIgniter 做的网站里,想输入一段代码: $var = sprintf("%04d", 2); 但是发现入库后,代码变成了 $var = sprintf("d", 2); 在网上环境,本地环境都...
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号