在我的php网络应用程序中,假设我想要加倍努力,除了去帮派破坏者和保持我的输入的肛门保持,我还想确保没有JavaScript输出字符串我是插入html模板。
有没有一种标准方法可以确保我不将JavaScript放入生成的html内容中?
答案 0 :(得分:2)
如果您不反对外部依赖,HTML Purifier library对于大多数XSS攻击来说是一个非常好的过滤器。
答案 1 :(得分:0)
不完全是一种标准方式;因为如果你在做什么:
<img src="${path}">
,${path}
扩展为。{
http://p0wned.com/jpg.jpg" /><script src="p0wned.com/js.js"/>
无论如何,我喜欢这个正则表达式:
#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 $_ ? $_ : "";
}
答案 2 :(得分:0)
在PHP中,我将从strip_tags开始。像这样:
$output = strip_tags($input);
如果我想在用户输入中允许一些标记,我会包含它们,如下所示:
$output = strip_tags($input, '<code><em><strong>');
答案 3 :(得分:0)
我认为不可能找到像这样的javascript代码。
您必须通过某种类型的解释器传递数据,以尝试查找有效的js语句。这将是非常耗费处理器的,并且可能会产生许多误报,具体取决于文本的性质。
实体转义元字符可能是进一步保护您的应用程序免受过滤器可能遗漏的攻击的最佳方式。如果Javascript以常规文本形式加载,则无法运行。