Microsoft AntiXSS - 是否需要解码?
HttpUtility
类提供编码和解码。但是,当我使用 MS AntiXSS 3.1 库时,我有一组仅用于编码的方法,这是否意味着可以避免解码?
例如
,应用 AntiXSS 之前:
lblName.Text = "ABC" + "<script> alert('Inject'); </script";
应用 AntiXSS 之后:
lblName.Text = AntiXSS.HTMLEncode("ABC" + "<script> alert('Inject'); </script");
因此,在应用编码之后,HTML 标记将显示在我的 Label 控件中。
这是想要的结果吗?
The HttpUtility
class provides for both encoding and decoding. But, when I use the MS AntiXSS 3.1 Library I have a set of methods only for encoding, does this mean decoding can be avoided?
For example
Before applying AntiXSS:
lblName.Text = "ABC" + "<script> alert('Inject'); </script";
After applying AntiXSS:
lblName.Text = AntiXSS.HTMLEncode("ABC" + "<script> alert('Inject'); </script");
So, after applying the encoding, the HTML tags show up in my Label control.
Is this the desired outcome?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,我认为这是期望的输出。这是因为脚本没有被执行。如果脚本已被执行,则会显示警报而不是脚本标签。所以这是安全的代码。
Yes, I think this is desired output. This is because the script is not executed. If the script would have been executed, an alert would be shown instead of the script tags. So this is safe code.
这取决于您的输入来自哪里以及您想用它做什么。很多时候,框架会在你看到东西之前为你解码 - Request.Form、Request.QueryString 等。
如果你正在从其他地方(例如数据库)读取编码字符串,那么你可能需要解码它,否则你例如,会看到双重编码;
这可能会产生意想不到的副作用,具体取决于消耗输出的内容。解码直到字符串不再改变的行为就是规范化的一个例子。
It depends where your input is coming from, and what you want to do with it. A lot of the time the framework decodes for you before you see things - Request.Form, Request.QueryString etc.
If you're reading an encoded string from somewhere else, for example a database then you may want to decode it, otherwise you'll see double encoding, for example;
which can have unintended side effects depending on what consumes the output. The act of decoding until the string no-longer changes is an example of canonicalisation.
您可以使用 HttpUtility.HtmlDecode 方法来解码 AntiXss 编码文本(或任何编码文本)。不需要显式的 AntiXss 解码。
You can use the HttpUtility.HtmlDecode method to decode AntiXss encoded text (or any encoded text). No explicit AntiXss decode is required.