防止 XSS 的轻量级且安全的方法
当前项目中的每个文本字段只是普通的平面文本,没有 ubb 或 html 标签。 但是它必须是 unicode,因此我认为包含受支持字符的数组并不是最佳选择。
我知道有很多专门用于 xss 检测的类,但是并非所有 xss 都包含这 2 个字符:
char <
<
%3C
<
<
&60;
<
PA==
字符;
;
%3B
;
;
Ow==
&59;
如果我检查上面代码块中字符的所有用户输入(获取、发布、cookie),那么一切都应该是 100% 安全的? 该项目不在 mysql 上运行,它使用 cassandra,因此 mysql 注入不应该成为问题。
我确信我忘记了什么,但我不知道是什么...... 或者,当用户输入是平面文本时,构建 100% 安全的应用程序真的那么容易吗?
编辑: 列表都有点长,在这里找到第一个字符: http://ha.ckers.org/xss.html
<
%3C
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
\x3c
\x3C
\u003c
\u003C
Every text field in this current project is just normal plane text without ubb or html tags.
However it must be unicode so an array with characters which are supported isn't optimal i guess.
I know there are a lot of dedicated classes for xss detection, however don't all xss include these 2 characters:
The char <
<
%3C
<
<
&60;
<
PA==
The char ;
;
%3B
;
;
Ow==
&59;
If i check all user input (get, post, cookie) for the characters in the codeblocks above then everything should be 100% safe?
The project isn't running on mysql, its using cassandra so mysql injection shouldn't be a problem.
I'm sure i'm forgetting something but i don't know what...
Or is it really so easy to build 100% safe apps when the userinput is plane text?
Edit:
List are both a little longer, found one for the first char here:
http://ha.ckers.org/xss.html
<
%3C
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
\x3c
\x3C
\u003c
\u003C
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试全面禁止输入中的“邪恶”字符是没有意义的,尝试禁止以各种不同形式编码的版本更没有意义。您将误报并阻止有效输入,同时无法保护自己免受各种可能形式的注入孔的影响。我不确定您想通过禁止
Ow== 来防止哪种攻击
但不是&
或"
。停止 HTML 注入的正确方法是调用
htmlspecialchars()
停止 URL 组件注入的任何文本字符串是对输出到 URL 的文本字符串调用rawurlencode()
停止 SQL 注入的正确方法。就是调用相关的DB转义函数(例如mysql_real_escape_chars()
)对任何输出到 SQL 字符串文字的文本进行转义,等等,对于您可能遇到的每个不同的转义,要点是,这是一个输出。当您将文本放入新的上下文中时,必须使用适合您所拥有的上下文类型的正确函数来应用 level 函数,这不是您可以在输入阶段执行一次然后忘记的事情,因为您不这样做。不知道在输入阶段,您正在处理的文本是否最终会出现在 SQL 文本、HTML 页面、JavaScript 字符串文本、URL 参数或其他内容中。
这并不是说输入阶段验证没有用;而是说输入阶段验证没有用。你会希望它确保提交的应该是数字的字段实际上看起来像数字,或者日期,或者其他什么。但输入验证并不能解决输出转义问题,例如导致大多数 XSS 的 HTML 注入问题。为了做到这一点,你必须禁止几乎所有的标点符号,这对用户来说是非常不利的。
There is no point trying to blanket-forbid “evil” characters in input, and even less point trying to forbid versions encoded in various different forms. You will false-positive and block valid input, whilst not protecting yourself from every possible form of injection hole. I'm not sure what kind of attack you're trying to prevent by banning
Ow==
but not, say,&
or"
.The correct way to stop HTML-injection is to call
htmlspecialchars()
on any text string being output into an HTML page. The correct way to stop URL-component-injection is to callrawurlencode()
on text strings being output into a URL. The correct way to stop SQL-injection is to call the relevant DB escaping function (egmysql_real_escape_chars()
) on any text being output into an SQL string literal.And so on, for every different for of escaping you might come across. The point is, this is an output-level function that has to be applied as and when you put text into a new context, using the right function for the type of context you have. It's not something you can do once at the input stage and then forget about, because you don't know at the input stage whether the text you're handling is going to end up in an SQL literal, an HTML page, a JavaScript string literal, a URL parameter, or what.
That's not to say input-stage validation is useless; you will want it to make sure a submitted field that's supposed to be a number does actually look like a number, or a date a date, or whatever. But input validation is not a solution to output-escaping problems like the HTML-injection issue that causes most XSS. To make that work, you'd have to ban pretty much all punctuation, which would be pretty user-hostile.
不,他们不会...因为您链接到的备忘单将建议。例如(MG 嵌入式命令第二部分)
假设它更简单。你仍然需要担心 SQL 注入、XSS(这种情况较少,但是 它仍然可能存在(即使是最轻微的未正确转义的数据)和 CSRF。
No, they don't... as the cheat sheet you link to will suggest. e.g. (MG Embedded commands part II)
Let's say it's simpler. You still need to worry about SQL injection, XSS (there's less of it, but it's still potentially around at the slightest improperly escaped piece of data) and CSRF.