我正在处理的代码库已由 Checkmarx 进行了分析,并返回了包含“Stored XSS”问题的报告。该问题指出:
\n\n方法 GetHomepageFilterByLocale HomepageRepo.cs 从数据库中获取 Select 元素的数据。然后,此元素\xe2\x80\x99s 值会流经代码,而不会被正确过滤或编码,并最终在方法 GetProductsByFilterType HomepageController.cs 中显示给用户。这可能会引发存储型跨站点脚本攻击。
\n\n是否有解决此问题的标准推荐方法?
\n\n请参阅下面的代码片段了解上述两种方法。
\n\n主页Repo.cs
\n\npublic HomepageFilter GetHomepageFilterByLocale(int localeId)\n {\n return _context.HomepageFilter.SingleOrDefault(x => x.LocaleId == localeId);\n }\nRun Code Online (Sandbox Code Playgroud)\n\nHomepageController.cs
\n\nGetHomepageViewModel() 方法是调用存储库方法的地方。
\n\n[HttpGet]\n public ActionResult GetProductsByFilterType(int locale, string filterType)\n {\n HomepageViewModel model = GetHomepageViewModel(locale, filterType);\n\n if (model?.Products != null)\n {\n model.Products.ForEach(p => p.Name = HttpUtility.HtmlEncode(p.Name));\n model.Products.ForEach(p => p.ImageUrl = HttpUtility.HtmlAttributeEncode(p.ImageUrl));\n }\n\n return Json(model, JsonRequestBehavior.AllowGet);\n }\nRun Code Online (Sandbox Code Playgroud)\n Checkmarx 报告了一个关于密码属性字符串的堆检查漏洞。此属性是模型的一部分,在提交登录表单时绑定。在 ASP.NET MVC 中有没有办法使用除常规字符串以外的任何其他内容来绑定表单中的密码?
到目前为止,我已经尝试将属性类型更改为 char [] 或 SecureString,但在这种情况下,表单不会将数据绑定到它。
public class LoginModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
Run Code Online (Sandbox Code Playgroud)