我的一些模型屬性由AllowHtml屬性標記。有沒有辦法自動將AntiXss保護(即只篩選允許的標籤)應用於這些字段?AntiXss保護Html模型屬性
10
A
回答
4
沒有自動方式。您可以做的最接近的是獲得AntiXss Nuget包。然後你可以使用它下面喜歡在你的控制器:
Microsoft.Security.Application.Sanitizer.GetSafeHtml("YourHtml");
OR
Microsoft.Security.Application.Encoder.HtmlEncode("YourHtml");
如果你使用,你可以使用
Server.HtmlDecode("HtmlEncodedString");
希望這有助於解碼。
10
首先,afaik,沒有什麼內置的。 但MVC允許通過自定義ModelBinders容易做出這樣的事情,你可以定義你的
public class CustomAntiXssAttribute : Attribute { }
,並用它裝點您的屬性(如果你願意,甚至從AllowHtmlAttribute
繼承)。然後用模型綁定你可以添加你的具體的反XSS防護:
public class CutstomModelBinder : DefaultModelBinder
{
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.Attributes.OfType<CustomAntiXssAttribute>().Any())
{
var valueResult = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
var filteredValue = SOME_CUSTOM_FILTER_FUNCTION_HERE(valueResult.AttemptedValue);
propertyDescriptor.SetValue(bindingContext.Model, filteredValue);
}
else // revert to the default behavior.
{
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
}
那麼這裏面SOME_CUSTOM_FILTER_FUNCTION_HERE
你可以使用什麼@Yogiraj建議,或使用正則表達式,甚至申請HtmlAgilityPack,基於內容的過濾。
P.S.不要忘記添加ModelBinders.Binders.DefaultBinder = new CutstomModelBinder();
到的Application_Start(我忘了:))
0
未經測試的代碼,
public class ADefaultModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelMetadata.RequestValidationEnabled)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
value = value.Replace("&", "");// replace existing & from the value
var encodedValue = Microsoft.Security.Application.Encoder.HtmlEncode(value);
bindingContext.ModelMetadata.RequestValidationEnabled = encodedValue.Contains("&"); // Whether AntiXss encoded a char to &..
}
return base.BindModel(controllerContext, bindingContext);
}
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new ADefaultModelBinder();
1
我會去更換那些AllowHtml
與RegularExpression
數據註釋驗證屬性。優點是通過這種方式,您可以捕捉錯誤並向用戶顯示錯誤發生了什麼,而前者在全球範圍內觸發錯誤。
例如,
public class MyViewModel
{
[DataType(DataType.MultilineText)]
[RegularExpression(@"^[^\<\>]*$", ErrorMessage = "May not contain <,>")]
public string Text { get; set; }
}
相關問題
- 1. 如何保護Rails模型屬性?
- 2. 如何保護Laravel模型屬性
- 3. 保護div屬性
- 4. 保留模型屬性
- 5. 受保護的內部集屬性的模型綁定
- 6. ASP.NET模型保護
- 7. 嵌套屬性:無法大規模指派保護屬性
- 8. 內存保護屬性
- 9. 與保護的屬性setter
- 10. Cython,受保護的屬性
- 11. mongodb/mongoose保護屬性
- 12. Python「受保護」屬性
- 13. 使用AntiXss在mvc中編碼html屬性
- 14. RoR - 密碼保護模型
- 15. 受保護的內部屬性vs受保護的屬性和Resharper
- 16. 從模型屬性渲染HTML
- 17. Django模型 - html /渲染屬性?
- 18. 自定義HTML在MVC3模型屬性
- 19. Mongoose模型方法:屬性不保存?
- 20. Rails模型attr_accessor屬性沒有保存?
- 21. 更新屬性後保存模型
- 22. 「無法批量分配受保護的屬性」與嵌套的受保護模型
- 23. Backbone.js的模型:類型陣列的屬性維護實例
- 24. PHP接力保護屬性,保護方法,保護的構建體
- 25. 爲什麼我的Backbone.js模型屬性嵌入爲HTML屬性?
- 26. MVC自定義模型屬性爲HTML屬性
- 27. Rails 3嵌套模型表單:無法批量分配受保護的屬性
- 28. Rails - 嵌套模型:不能批量分配受保護的屬性
- 29. Rails 3.1不能爲非數據庫模型批量分配受保護屬性
- 30. 使用Devise時不能批量分配關聯模型的受保護屬性
你不能繼承所有ALLOWHTMLATTRIBUTE 「這是一個密封類」 – VJAI