带有富文本字段的代码流利潜在危险 Request.Form 值
Codefluent Potentially dangerous Request.Form value with richtext fields
添加来自
的多值(标志)枚举解决方案(效果很好,谢谢)后
http://blog.codefluententities.com/tag/multi-enumeration-values/
对于我们的 MVC 项目,我们现在正在使用 wysiwyg 编辑器(在本例中为 summernote)生成 html 全面的富文本字段,这是可怕的 "Potentially dangerous Request.Form value"。
如果我删除 summernote 并只提交纯文本,这些字段可以正常工作,但是将任何 html 代码放入文本输入会产生错误。
幸运的是,错误来自刚刚为第 246 行的多重枚举添加的代码(上面):
Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (Description="...rem ipsum <strong>dolor</stron...").
Source Error:
Line 244: continue;
Line 245:
Line 246: Add(name, nvc[name]);
Line 247:
Line 248: }
编辑:
为清楚起见,这里是有问题的整个方法:
private void AddRange(NameValueCollection nvc)
{
foreach (string name in nvc)
{
// handle MultiSelectList templates
const string listSelectedToken = ".list.item.Selected";
const string listValueToken = ".list.item.Value";
if (name.EndsWith(listSelectedToken))
{
List<bool> bools = CodeFluent.Runtime.Utilities.ConvertUtilities.SplitToList<bool>(nvc[name], ',');
string propertyName = name.Substring(0, name.Length - listSelectedToken.Length);
string valueKey = propertyName + listValueToken;
List<string> keys = CodeFluent.Runtime.Utilities.ConvertUtilities.SplitToList<string>(nvc[valueKey], ',');
int j = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keys.Count; i++)
{
if (bools[j])
{
if (sb.Length > 0)
{
sb.Append(CodeFluentConfiguration.EntityKeyListSeparator);
}
sb.Append(keys[i]);
j++;
}
j++;
}
Add(propertyName, sb.ToString());
continue;
}
if (name.EndsWith(listValueToken))
continue;
Add(name, nvc[name]);
}
}
我在多值实现中遗漏了什么吗?
谢谢,
拉斯
我认为这个错误与使用多值枚举无关。事实上,您 post 包含 HTML 标签的描述字段的值(强)。默认情况下 ASP.NET 阻止这种情况并抛出验证异常。
如果您希望您的用户输入 HTML,您必须指示 ASP.NET 授权 HTML。
更改EntityValueProvider
AddRange(context.HttpContext.Request.Unvalidated.Form); // Add Unvalidated
AddRange(context.HttpContext.Request.Unvalidated.QueryString);
或使用web.config:validateRequest
or requestValidationMode
<system.web>
<pages validateRequest="false" />
<httpRuntime requestValidationMode="2.0" />
</system.web>
public class Sample
{
[AllowHtml]
public string Description {get;set;}
}
[HttpPost, ValidateInput(true, Exclude = "Description")]
public ActionResult Edit(int id, FormCollection collection)
{
...
}
添加来自
的多值(标志)枚举解决方案(效果很好,谢谢)后http://blog.codefluententities.com/tag/multi-enumeration-values/
对于我们的 MVC 项目,我们现在正在使用 wysiwyg 编辑器(在本例中为 summernote)生成 html 全面的富文本字段,这是可怕的 "Potentially dangerous Request.Form value"。
如果我删除 summernote 并只提交纯文本,这些字段可以正常工作,但是将任何 html 代码放入文本输入会产生错误。
幸运的是,错误来自刚刚为第 246 行的多重枚举添加的代码(上面):
Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (Description="...rem ipsum <strong>dolor</stron...").
Source Error:
Line 244: continue;
Line 245:
Line 246: Add(name, nvc[name]);
Line 247:
Line 248: }
编辑:
为清楚起见,这里是有问题的整个方法:
private void AddRange(NameValueCollection nvc)
{
foreach (string name in nvc)
{
// handle MultiSelectList templates
const string listSelectedToken = ".list.item.Selected";
const string listValueToken = ".list.item.Value";
if (name.EndsWith(listSelectedToken))
{
List<bool> bools = CodeFluent.Runtime.Utilities.ConvertUtilities.SplitToList<bool>(nvc[name], ',');
string propertyName = name.Substring(0, name.Length - listSelectedToken.Length);
string valueKey = propertyName + listValueToken;
List<string> keys = CodeFluent.Runtime.Utilities.ConvertUtilities.SplitToList<string>(nvc[valueKey], ',');
int j = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < keys.Count; i++)
{
if (bools[j])
{
if (sb.Length > 0)
{
sb.Append(CodeFluentConfiguration.EntityKeyListSeparator);
}
sb.Append(keys[i]);
j++;
}
j++;
}
Add(propertyName, sb.ToString());
continue;
}
if (name.EndsWith(listValueToken))
continue;
Add(name, nvc[name]);
}
}
我在多值实现中遗漏了什么吗?
谢谢,
拉斯
我认为这个错误与使用多值枚举无关。事实上,您 post 包含 HTML 标签的描述字段的值(强)。默认情况下 ASP.NET 阻止这种情况并抛出验证异常。
如果您希望您的用户输入 HTML,您必须指示 ASP.NET 授权 HTML。
更改EntityValueProvider
AddRange(context.HttpContext.Request.Unvalidated.Form); // Add Unvalidated
AddRange(context.HttpContext.Request.Unvalidated.QueryString);
或使用web.config:validateRequest
or requestValidationMode
<system.web>
<pages validateRequest="false" />
<httpRuntime requestValidationMode="2.0" />
</system.web>
public class Sample
{
[AllowHtml]
public string Description {get;set;}
}
[HttpPost, ValidateInput(true, Exclude = "Description")]
public ActionResult Edit(int id, FormCollection collection)
{
...
}