简体   繁体   English

使用HTML编码字符进行ASP.NET请求验证

[英]ASP.NET request validation with HTML encoded characters

I have a textbox in a form which needs to accept input with HTML tags. 我有一个表单中的文本框,需要接受带有HTML标记的输入。

Submitting input with HTML tags in makes the app throw a HttpRequestValidationException , unless we use HttpUtility.HtmlEncode . 使用HTML标签提交输入会使应用程序抛出HttpRequestValidationException ,除非我们使用HttpUtility.HtmlEncode Easy so far. 容易到目前为止。

However, the input may also contain symbols, such as the 'degrees' symbol (°). 但是,输入也可能包含符号,例如“度”符号(°)。 When these are also HTML encoded, they become numeric escape codes, in this example ° 当这些也是HTML编码时,它们变成数字转义码,在这个例子中° . These codes also cause HttpRequestValidationException to be thrown, but the question is why? 这些代码也会导致抛出HttpRequestValidationException ,但问题是为什么?

I can't see why numeric escape codes are thought of as potentially dangerous, especially as ° 我不明白为什么数字转义码被认为是有潜在危险的,特别是作为° works as input just fine. 作为输入就好了。

I seem to be stuck, as leaving the input as-is fails due to the tags, and HTML encoding the input fails due to the numeric escapes. 我似乎被卡住了,因为由于标签而导致输入失败,并且输入的HTML编码由于数字转义而失败。 My solution so far has been to HTML encode, then regex replace the escape sequences with their HTML decoded forms, but I'm not sure if this is a safe solution, as I assume the escape sequences are seen as dangerous for a reason. 到目前为止我的解决方案是HTML编码,然后正则表达式用它们的HTML解码形式替换转义序列,但我不确定这是否是一个安全的解决方案,因为我认为转义序列被视为危险的原因。

ASP.NET considers html char escapes (&#xxx) dangerous for the same reason it considers angled bracket dangerous ie XSS. ASP.NET认为html char escapes(&#xxx)是危险的,因为它认为角度括号危险即XSS。 Using above escape, you can include any character (for example, angled bracket). 使用上面的转义,您可以包含任何字符(例如,有角度的括号)。 Here's summary of what request validation does in 1.1 and 2.0. 以下是1.1和2.0中请求验证的概要

In legitimate cases such as your case, you can choose any of below 在您的案件等合法案件中,您可以选择以下任何一种情况

  1. Choose your own handling as described by you 根据您的描述选择您自己的处理方式
  2. Disable request validation at page level (<%@ Page validateRequest="false") 在页面级别禁用请求验证(<%@ Page validateRequest =“false”)
  3. In .NET 4, substitute your own request validation using RequestValidator class. 在.NET 4中,使用RequestValidator类替换您自己的请求验证。

This is due to ASP.NET builtin Cross Site Scripting validation capabilities. 这是由于ASP.NET内置的跨站点脚本验证功能。 There is some kind of a list of what's allowed and what's not by ASP.NET, here on SO: ASP.NET request validation causes: is there a list? ASP.NET上有一些允许和不允许的内容列表,这里是SO: ASP.NET请求验证原因:是否有列表?

On the specific case of # encoded characters, there is a complete reference of XSS attacks available here: XSS (Cross Site Scripting) Cheat Sheet that demonstrate how complex these attacks can be, and why encoded characters are forbidden. 关于#coding字符的具体情况,这里有一个完整的XSS攻击参考: XSS(跨站点脚本)备忘单 ,它说明了这些攻击的复杂程度,以及禁止编码字符的原因。

You can read the Script Exploits Overview in the msdn help. 您可以在msdn帮助中阅读Script Exploits Overview

If you are sure that you handle any possible malicious code input in your page then you can disable validation using the <%@ Page validateRequest="false" %> directive. 如果您确定在页面中处理任何可能的恶意代码输入,则可以使用<%@ Page validateRequest =“false”%>指令禁用验证

I'd suggest looking into doing limited html encoding on the client side, quite a breeze to do with jquery by binding processing to a form submit. 我建议在客户端进行有限的html编码,通过将处理绑定到表单提交,可以轻松地使用jquery。

What do I mean by "limited"? “有限”是什么意思? Ampersands, angled brackets and quotes should be be encoded but not the unicode symbols. 应该编码“&”字,角度括号和引号,但不能编码unicode符号。 You're pointing out that, in fact, numeric escape codes are evil and get declined, unlike their unescaped equivalents! 事实上,你指出,数字转义码是邪恶的并且被拒绝了,不像它们未转义的等价物!

You could run the string you're submitting through a javascript function similar to the following code, giving you a value that would pass request validation: 您可以通过类似于以下代码的javascript函数运行您提交的字符串,为您提供一个可以通过请求验证的值:

function safeString(s) {
    return s.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g, "&quot;");
}

This could cause you some grief if, after storing it or doing some server-side magic with the submitted value, you want to re-display it inside of an input. 如果在存储它或使用提交的值执行某些服务器端魔术之后,您希望在输入中重新显示它,这可能会让您感到悲伤。 Let me elaborate: if you simply put a string encoded that way into an empty paragraph, it will render perfectly; 让我详细说明一下:如果你只是将一个以这种方式编码的字符串放入一个空段落中,它就会完美呈现; however if you dump it into a textarea, you will see &lt; 但是如果你将它转储到textarea中,你会看到&lt; instead of < 而不是<

Ironically, when writing the last sentence I had to type &amp;lt; 具有讽刺意味的是,在写下最后一句话时,我必须输入&amp; lt; and &lt; &lt; respectively... 分别...

Just add in your page directive (first line of the page) this attribute: 只需添加您的页面指令(页面的第一行)此属性:

ValidateRequest="false" ValidateRequest = “假”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM