简体   繁体   English

正确编码/解码MVC3中潜在危险的表单值

[英]Properly encoding/decoding a potentially dangerous form value in MVC3

I have a 'Description' field in a form. 我在表单中有一个“描述”字段。 I would like the user to not be restricted from using special characters in this field. 我希望不限制用户在此字段中使用特殊字符。

As such, I would like to be able to POST the following data from my client to my server, save to the database, and be able to render properly again: 因此,我希望能够将以下数据从我的客户端发布到我的服务器,保存到数据库,并能够再次正确呈现:

Test Description ( & &%$^&^() ^&&$& 87566467679089765 ?<>?<>?<>?<>":";';';'][][][][{}{}{} 测试说明( && %$ ^&^() ^ && $& 87566467679089765?<>?<>?<>?<>“:”;';';'] [] [] [] [{} {} {}

I did some reading on the subject, but it seems like I have more options than I expected. 我做了一些关于这个主题的阅读,但似乎我有比我预期更多的选择。 I would like to make sure I do this properly for security's sake. 为了安全起见,我想确保我这样做。

In my model, I've tagged my Description property with the AllowHtml attribute: 在我的模型中,我使用AllowHtml属性标记了我的Description属性:

[DisplayFormat(ConvertEmptyStringToNull = false), AllowHtml]
public string Description { get; set; }

This allows the above data to POST to my Controller, but doesn't address the heart of the issue. 这允许上面的数据POST到我的控制器,但没有解决问题的核心。 I now believe I need to sanitize my input. 我现在相信我需要消毒我的意见。 I believe this means that I need to leverage the HttpUtility library. 我相信这意味着我需要利用HttpUtility库。

Looking at HttpUtility, I see a ton of methods: 看看HttpUtility,我看到了很多方法:

  • HtmlAttributeEncode HtmlAttributeEncode
  • HtmlEncode 的HTMLEncode
  • UrlEncode 以UrlEncode

According to this post there doesn't seem to be much difference between HtmlEncode and UrlEncode. 根据这篇文章 ,HtmlEncode和UrlEncode之间似乎没有太大区别。

I'm wondering a few things: 我想知道一些事情:

  • Is using HttpUtility the correct choice here? 在这里使用HttpUtility是正确的选择吗?
  • Is it fine to not do any processing of my input before POSTing? 在POST之前不对我的输入进行任何处理是没关系的吗? (Can I do all my encoding server-side?) (我可以在服务器端编码吗?)
  • Should I be using Microsoft anti-xss libraries instead of HttpUtility? 我应该使用Microsoft反xss库而不是HttpUtility吗?

and just generally any other pitfalls I might not have been made privvy. 而且通常我可能没有任何其他陷阱。

UPDATE: 更新:

Here's my model: 这是我的模特:

<div class="detailsRow optional">
    <%= Html.LabelFor(model => model.Description, new { @class = "descriptionLabel" }, Model.DescriptionLabel)%>
    <%= Html.TextAreaFor(model=> model.Description) %>
</div>

and my controller method: 和我的控制器方法:

public ActionResult SaveNewOrderDetails(NewOrderDetailsModel orderDetailsModel)
{
    string description = orderDetailsModel.Description;
    //Successfully got description
    //Example code:
    Order order = new Order(description);
    order.Save();

    return Json(new { id = order.ID, name = order.Name });
}

after my code passes the 'return Json' statement, I am greeted with another error: 在我的代码传递'return Json'语句后,我遇到了另一个错误:

A potentially dangerous Request.Form value was detected from the client (Description="...>?<>?<>?<><?>":";';';'][][][][..."). 从客户端检测到一个潜在危险的Request.Form值(Description =“...>?<>?<>?<> <?>”:“;';';'] [] [] [] [。 ..“)。

My understanding is that this should not be occurring. 我的理解是,这不应该发生。 Any places I should check? 我应该检查的任何地方?

UPDATE2 : Unable to get AllowHtml to work. UPDATE2 :无法让AllowHtml工作。 I'm going to revisit this when we upgrade to MVC4. 当我们升级到MVC4时,我将重新审视这个问题。

I now believe I need to sanitize my input. 我现在相信我需要消毒我的意见。

No, you are fine as far as receiving the user input and storing it into the database is concerned (assuming you have used parametrized queries which you should always do). 不,只要接收用户输入并将其存储到数据库中就可以了(假设您已经使用了参数化查询,您应该始终这样做)。 The [AllowHtml] attribute will give you the possibility to enter any characters. [AllowHtml]属性可让您输入任何字符。 If you wanted to sanitize the characters that the user could enter simply use a regular expression validator (I probably wouldn't bother with that): 如果你想清理用户可以输入的字符,只需使用正则表达式验证器(我可能不会打扰它):

[DisplayFormat(ConvertEmptyStringToNull = false)]
[AllowHtml]
[RegularExpression("PUT YOUR REGEX HERE")]
public string Description { get; set; }

You should be careful when you are rendering the value you have read from the database back to a view. 将从数据库中读取的值渲染回视图时应该小心。 You should make sure you always HTML encode it. 您应确保始终对其进行HTML编码。 For example the <%: function does that out of the box. 例如, <%:函数可以开箱即用。

So here's a safe way to display it: 所以这是一种安全的显示方式:

<div>
    <%: Model.Description  %>
</div>

Be careful to never do that: 小心永远不要那样做:

<div>
    <%= Model.Description  %>
</div>

because this will not HTML encode the output and your site becomes vulnerable to XSS. 因为这不会对输出进行HTML编码,并且您的网站容易受到XSS的攻击。

And here's another safe way using the DisplayFor helper: 这是使用DisplayFor帮助器的另一种安全方法:

<div>
    <%= Html.DisplayFor(x => x.Description) %>
</div>

Also if you want to render this in an editable field or text area simply use the corresponding helper: 此外,如果要在可编辑字段或文本区域中呈现此内容,只需使用相应的帮助程序:

<div>
    <%= Html.TextAreaFor(x => x.Description) %>
</div>

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

相关问题 如何使用原始的httppost表单抑制MVC中的“潜在危险的requestform值” - How to suppress the 'potentially dangerous requestform value' in MVC with a raw httppost form MVC潜在危险的Request.QueryString值 - MVC potentially dangerous Request.QueryString value ActionFilterAttribute-一个潜在危险的Request.Form值 - ActionFilterAttribute - A potentially dangerous Request.Form value “潜在危险的Request.Form值” - “potentially dangerous Request.Form value” 潜在危险的Request.Form值 - A potentially dangerous Request.Form value 检测到潜在危险的Request.Form值 - Potentially dangerous Request.Form value was detected MVC密码字段输入引发“从客户端检测到潜在危险的Request.Form值” - MVC password field input raises “A potentially dangerous Request.Form value was detected from the client” 针对潜在危险请求的JavaScript编码 - JavaScript encoding for potentially dangerous requests 如何获取导致MVC中“从客户端检测到潜在危险的Request.Form值”的表单值? - How do I get the form value that caused the “A potentially dangerous Request.Form value was detected from the client” in MVC? 从客户端检测到潜在危险的Request.Form值 - A potentially dangerous Request.Form value was detected from the client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM