SV.XPATH

当使用未经检查的用户输入作为 XPath 表达式时,将检测到该错误。

从 2023.2 版本开始,此检查器将支持 Jakarta EE。

缓解与预防

可以通过验证所有来自应用程序外部的输入(用户输入、文件输入、系统参数等等)实现对 XPath 注入攻击的阻止。筛选用户数据的最佳方式是使用仅包含允许的字符类型的黑名单正则表达式,所有其他字符都应被转义。

Klocwork 安全漏洞 (SV) 检查器可识别可能创建危险数据的调用;这些调用被视为不安全的来源。用户所提供的任何数据都可能是不安全的来源,因为用户可能是攻击者,或者可能引入人为错误。

示例 1

复制
  protected void doGet(HttpServletRequest httpServletRequest,
          HttpServletResponse httpServletResponse) throws ServletException, IOException {
     super.doGet(httpServletRequest, httpServletResponse);
     String userName = httpServletRequest.getParameter("user.name");
     String userDescr = httpServletRequest.getParameter("user.descr");
     CompiledExpression compiledExpression = JXPathContext.compile("string(//user[name/text()='" + userName + "']/account/text())");
     compiledExpression.setValue(context, userDescr);
     httpServletResponse.getOutputStream().println("User info updated");
  }

针对代码段报告两次 SV.XPATH:

  • 在第 27 行,将用户数据存储在 userName 中,并在之后的第 29 行用于构造 XPath 表达式
  • 在第 28 行,将用户数据存储在 userDescr 中,并在之后的第 30 行传递到 setValue(...)

扩展

此检查器可通过 Klocwork 知识库进行扩展。有关详情,请参阅调整 Java 分析。