Php 在生成的数据中检查是否打印JavaScript?
在我的PHPWeb应用程序中,假设我想多做一点努力,除了进行帮派扫荡和对输入进行清理外,我还想确保在插入html模板的字符串中没有输出JavaScriptPhp 在生成的数据中检查是否打印JavaScript?,php,javascript,security,Php,Javascript,Security,在我的PHPWeb应用程序中,假设我想多做一点努力,除了进行帮派扫荡和对输入进行清理外,我还想确保在插入html模板的字符串中没有输出JavaScript 有没有一种标准的方法来确保我不会将JavaScript放入生成的html内容中?不完全是一种标准的方法;因为如果你在做什么: ,并将${path}扩展为 http://p0wned.com/jpg.jpg“/> 无论如何,我喜欢这个正则表达式: #from http://www.perlmonks.org/?node_id=161281 su
有没有一种标准的方法来确保我不会将JavaScript放入生成的html内容中?不完全是一种标准的方法;因为如果你在做什么:
,并将${path}
扩展为
http://p0wned.com/jpg.jpg“/>
无论如何,我喜欢这个正则表达式:
#from http://www.perlmonks.org/?node_id=161281
sub untag {
local $_ = $_[0] || $_;
# ALGORITHM:
# find < ,
# comment <!-- ... -->,
# or comment <? ... ?> ,
# or one of the start tags which require correspond
# end tag plus all to end tag
# or if \s or ="
# then skip to next "
# else [^>]
# >
s{
< # open tag
(?: # open group (A)
(!--) | # comment (1) or
(\?) | # another comment (2) or
(?i: # open group (B) for /i
( TITLE | # one of start tags
SCRIPT | # for which
APPLET | # must be skipped
OBJECT | # all content
STYLE # to correspond
) # end tag (3)
) | # close group (B), or
([!/A-Za-z]) # one of these chars, remember in (4)
) # close group (A)
(?(4) # if previous case is (4)
(?: # open group (C)
(?! # and next is not : (D)
[\s=] # \s or "="
["`'] # with open quotes
) # close (D)
[^>] | # and not close tag or
[\s=] # \s or "=" with
`[^`]*` | # something in quotes ` or
[\s=] # \s or "=" with
'[^']*' | # something in quotes ' or
[\s=] # \s or "=" with
"[^"]*" # something in quotes "
)* # repeat (C) 0 or more times
| # else (if previous case is not (4))
.*? # minimum of any chars
) # end if previous char is (4)
(?(1) # if comment (1)
(?<=--) # wait for "--"
) # end if comment (1)
(?(2) # if another comment (2)
(?<=\?) # wait for "?"
) # end if another comment (2)
(?(3) # if one of tags-containers (3)
</ # wait for end
(?i:\3) # of this tag
(?:\s[^>]*)? # skip junk to ">"
) # end if (3)
> # tag closed
}{}gsx; # STRIP THIS TAG
return $_ ? $_ : "";
}
#来自http://www.perlmonks.org/?node_id=161281
过渡时期援助小组{
本地$|=$|[0]| |$|;
#算法:
#查找<,,
#评论,,
#或评论,,
#或需要相应的开始标记之一
#结束标记加上所有结束标记
#或者如果\s或=”
#然后跳到下一步“
#其他[^>]
# >
{
<#打开标签
(?:#开放组(A)
(!--)注释(1)或
(\?)|#另一条评论(2)或
(?i:#开放组(B)用于/i
(标题|#开始标记之一
脚本|#为哪个
必须跳过小程序|#
对象|#所有内容
风格#对应
)#末端标签(3)
)|#封闭组(B),或
([!/A-Za-z])#这些角色中的一个,还记得第(4)条吗
)#封闭组(A)
(?(4)#如果先前的案例是(4)
(?:#开放组(C)
(?!#下一个不是:(D)
[\s=]#\s或“=”
[“`]#带引号
)#关闭(D)
[^>]|#且不关闭标记或
[\s=]#\s或“=”带
`[^`]*`|#引号中的某物`或
[\s=]#\s或“=”带
“[^']*”|#引号中的某物”或
[\s=]#\s或“=”带
“[^”]*“#引号中的某些内容”
)*#重复(C)0次或以上
|#否则(如果之前的案例不是(4))
*?#任何字符的最小值
)#如果上一个字符为(4),则结束
(?(1)#如果评论(1)
(?#标记已关闭
}{}gsx;#去掉这个标签
返回$?$:;
}
如果您不反对外部依赖,那么对于大多数XSS攻击来说,是一个非常好的过滤器。在PHP中,我从strip_标记开始。如下所示:
$output = strip_tags($input);
如果我想在用户输入中允许一些标记,我会将它们包括在内,如下所示:
$output = strip_tags($input, '<code><em><strong>');
$output=strip_标签($input,);
我认为不可能找到这样的javascript代码
您必须通过某种类型的解释器来传递数据,以试图找到有效的js语句。这将非常占用处理器,并且可能会根据文本的性质产生许多误报
实体转义元字符可能是进一步保护应用程序免受筛选器可能遗漏的攻击的最佳方法。如果Javascript作为常规文本加载,则无法运行