今天在改bug时,发现同事写的代码里存在跨站漏洞,于是加了个php防跨站函数htmlentities()。
因为以前也没有认真分析过此函数,翻了下文档,想了解透测点。
htmlentities() 转化所有适当字符为html实体
函数说明:
string htmlentities ( string $string [, int $flags = ent_compat [, string $charset [, bool $double_encode = true ]]] )
string :为输入需转换的字符
flags :和htmlspecialchars()一样,第二个参数允许你定义单双引号将做什么。它需要是默认常数ent_compat的三个参数之一,可以结合第四个ent_ignore
ent_compat
will convert double-quotes and leave single-quotes alone.
ent_quotes
will convert both double and single quotes.
ent_noquotes
will leave both double and single quotes unconverted.
ent_ignore
silently discard invalid code unit sequences instead of returning an empty string. added in php 5.3.0. this is provided for backwards compatibility; avoid using it as it may have security implications.
charset :与htmlspecialchars()一样 ,它带有一个可选的第三个参数做为字符集转换,目前,默认iso-8859-1字符集
支持的字符集列表
字符集
别名
描述
iso-8859-1
iso8859-1
西欧,latin-1
iso-8859-15
iso8859-15
西欧,latin-9。增加欧元符号,法语和芬兰语字母在 latin-1(iso-8859-1) 中缺失。
utf-8
ascii 兼容的多字节 8 位 unicode。
cp866
ibm866, 866
dos 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。
cp1251
windows-1251, win-1251, 1251
windows 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。
cp1252
windows-1252, 1252
windows 特有的西欧编码。
koi8-r
koi8-ru, koi8r
俄语。本字符集在 4.3.2 版本中得到支持。
big5
950
繁体中文,主要用于中国台湾省。
gb2312
936
简体中文,中国国家标准字符集。
big5-hkscs
繁体中文,附带香港扩展的 big5 字符集。
shift_jis
sjis, 932
日语
euc-jp
eucjp
日语
double_encode :当double_encode为关闭状态,php默认将一切都转换为html实体
返回值 返回已经编码的字符串
html_entity_decode()与htmlentities()为反向函数,一个解码,一个编码。
但是如果出现没有这些字符集呢?那么这个函数就不起作用了。那起不悲剧,于是还是自己再写个函数过滤下吧。
function inxss($url)
{
$arr = array('http','script','iframe','com','www');//过江的字符,呵,自由发挥吧。最好用正则处理,因为我的是在iframe的跨站,其实只要针对着不让它传入http://。。。这些url进来就行了
foreach($arr as $value)
{
if( strstr($url,$value))
{
exit;
}
else
{
return $url;
}
}
}
$url = htmlentities(inxss($_get[url]));双重过虑。呵
这样的话应该可以多了点安全