CodeQL 文档

XPath 注入

ID: js/xpath-injection
Kind: path-problem
Security severity: 9.8
Severity: error
Precision: high
Tags:
   - security
   - external/cwe/cwe-643
Query suites:
   - javascript-code-scanning.qls
   - javascript-security-extended.qls
   - javascript-security-and-quality.qls

点击查看 CodeQL 代码库中的查询

如果 XPath 表达式是使用字符串连接构建的,并且连接的组件包含用户输入,那么用户很容易创建恶意 XPath 表达式。

建议

如果用户输入必须包含在 XPath 表达式中,请清理数据或使用变量引用将其安全地嵌入,而不会更改表达式的结构。

示例

在此示例中,代码接受用户指定的用户名,并在使用 xpath 包构建的 XPath 表达式中使用此未验证和未清理的值。这容易受到用户提供特殊字符或字符串序列的攻击,这些字符或字符串序列会更改 XPath 表达式的含义以搜索不同的值。

const express = require('express');
const xpath = require('xpath');
const app = express();

app.get('/some/route', function(req, res) {
  let userName = req.param("userName");

  // BAD: Use user-provided data directly in an XPath expression
  let badXPathExpr = xpath.parse("//users/user[login/text()='" + userName + "']/home_dir/text()");
  badXPathExpr.select({
    node: root
  });
});

相反,请使用 xpath 提供的变量替换机制来嵌入用户输入

const express = require('express');
const xpath = require('xpath');
const app = express();

app.get('/some/route', function(req, res) {
  let userName = req.param("userName");

  // GOOD: Embed user-provided data using variables
  let goodXPathExpr = xpath.parse("//users/user[login/text()=$userName]/home_dir/text()");
  goodXPathExpr.select({
    node: root,
    variables: { userName: userName }
  });
});

参考

  • ©GitHub, Inc.
  • 条款
  • 隐私