【发布时间】:2016-11-11 16:39:25
【问题描述】:
Checkmarx 对我正在处理的代码库进行了分析,并返回了一份包含“Stored XSS”问题的报告。该问题指出:
方法 GetHomepageFilterByLocale HomepageRepo.cs 从数据库中获取 Select 元素的数据。然后,该元素的值在没有经过适当过滤或编码的情况下流经代码,并最终在 GetProductsByFilterType HomepageController.cs 方法中显示给用户。这可能会启用存储跨站点脚本攻击。
是否有解决此问题的标准推荐方法?
请参阅下面的代码 sn-ps 了解上述两种方法。
HomepageRepo.cs
public HomepageFilter GetHomepageFilterByLocale(int localeId)
{
return _context.HomepageFilter.SingleOrDefault(x => x.LocaleId == localeId);
}
HomepageController.cs
GetHomepageViewModel() 方法是调用存储库方法的地方。
[HttpGet]
public ActionResult GetProductsByFilterType(int locale, string filterType)
{
HomepageViewModel model = GetHomepageViewModel(locale, filterType);
if (model?.Products != null)
{
model.Products.ForEach(p => p.Name = HttpUtility.HtmlEncode(p.Name));
model.Products.ForEach(p => p.ImageUrl = HttpUtility.HtmlAttributeEncode(p.ImageUrl));
}
return Json(model, JsonRequestBehavior.AllowGet);
}
【问题讨论】: