ASP.NET - 一些代码的安全反映 XSS 问题

ASP.NET - Security Reflected XSS problem with some codes

在我工作的地方,他们使用名为 checkmarx 的应用程序来分析应用程序的安全性

在其中一项分析中,应用程序检测到以下问题:

反映的 XSS 所有客户端:

The application's GetBarcosNaoVinculados embeds untrusted data in the generated output with Json, at line 1243 of .../Controllers/AdminUserController.cs. This untrusted data is embedded straight into the output without proper sanitization or encoding, enabling an attacker to inject malicious code into the output. The attacker would be able to alter the returned web page by simply providing modified data in the user inputusuarioId, which is read by the GetBarcosNaoVinculados method at line 1243 of .../Controllers/AdminUserController.cs. This input then flows through the code straight to the output web page, without sanitization.

public JsonResult GetBarcosNaoVinculados(string usuarioId)              
.....
.....
 return Json(barcosNaoVinculados, JsonRequestBehavior.AllowGet)

在系统的其他地方它给出了同样的问题但是使用这两种方法

The application's LoadCodeRve embeds untrusted data in the generated output with SerializeObject, at line 738 of .../BR.Rve.UI.Site/Controllers/InfoApontamentoController.cs. This untrusted data is embedded straight into the output without proper sanitization or encoding, enabling an attacker to inject malicious codeinto the output.The attacker would be able to alter the returned web page by saving malicious data in a data-store ahead oftime. The attacker's modified data is then read from the database by the Buscar method with Where, at line 78 of .../Repository/Repository.cs. This untrusted data then flows through the code straight tothe output web page, without sanitization.

public virtual IEnumerable<TEntity> Buscar(Expression<Func<TEntity, bool>>predicate)
   return Dbset.Where(predicate);

public string LoadCodeRve()
  return JsonConvert.SerializeObject(items);

好像跟JSON格式的处理有关,请问有谁知道这种问题怎么处理吗?

如警告消息所示,您需要执行某种形式的输入验证(或清理),以及作为安全编码最佳实践 - 在将输出呈现到页面之前进行输出编码。 Checkmarx 搜索是否存在这些“消毒剂”,这些“消毒剂”已在其 Checkmarx 查询中预定义。例如,一种是使用 AntiXSS 库(即 JavascriptEncode 函数)

Checkmarx 已经指出了需要注意的两条关键线:

return Json(barcosNaoVinculados, JsonRequestBehavior.AllowGet)

return JsonConvert.SerializeObject(items);

无论这些值(JSON 或字符串)将在哪个页面结束,都需要对其进行转义。现在,根据您使用的模板引擎,您可能已经获得了即时的 XSS 保护。例如,"The Razor engine used in MVC automatically encodes all output sourced from variables, unless you work really hard to prevent it doing so.",当然除非您使用了 Html.Raw 辅助方法。

作为应用程序安全的推动者,我们相信不信任输入并拥有多层防御,因此我的建议是通过传入 JsonSerializerSettings 参数明确指示您要对输出进行编码:

return JsonConvert.SerializeObject(items, new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.EscapeHtml });

这里唯一的难题是 Checkmarx 可能无法识别这是一种消毒剂,因为它可能不在他们预定义的消毒剂列表中。您始终可以将此解决方案作为参数提交给安全团队,即 运行 安全扫描

对于 JsonResult return 的情况,您可能想要 javascript 编码 barcosNaoVinculados 变量:

return Json(HttpUtility.JavaScriptStringEncode(barcosNaoVinculados), JsonRequestBehavior.AllowGet)

现在,这个Checkmarx可能也认不出来了。您可以尝试使用 Checkmarx 识别的那些(即 Encoder.JavascriptEncode 或 AntiXss.JavascriptEncode),但我认为这些 Nuget 包不会在您的项目类型中工作