在我的php web应用程序中,假设我想要多走一步,除了去帮派分子和保持肛门保持清洁我的输入外,我还要确保没有JavaScript正在输出的字符串中插入到html模板中。检查并不打印生成的数据中的JavaScript?
有没有一种标准的方法来确保我不把JavaScript放到生成的html内容中?
在我的php web应用程序中,假设我想要多走一步,除了去帮派分子和保持肛门保持清洁我的输入外,我还要确保没有JavaScript正在输出的字符串中插入到html模板中。检查并不打印生成的数据中的JavaScript?
有没有一种标准的方法来确保我不把JavaScript放到生成的html内容中?
不完全是一种标准的方式;因为如果你在做: <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 $_ ? $_ : "";
}
如果你不反对外部依赖的HTML Purifier library是一个相当不错的过滤大部分XSS攻击。
在PHP中,我会从strip_tags开始。像这样:
$output = strip_tags($input);
如果我想允许用户输入一些标签,我想包括他们,就像这样:
$output = strip_tags($input, '<code><em><strong>');
我不认为这是有可能找到这样的javascript代码。
您必须通过某种类型的解释器传递数据才能尝试查找有效的js语句。这将是非常密集的处理器,可能会产生很多误报,具体取决于文本的性质。
实体转义元字符可能是进一步保护您的应用程序免受过滤器可能错过的攻击的最佳方式。如果以常规文本加载Javascript,则无法运行Javascript。