XPath 注入¶
ID: go/xml/xpath-injection
Kind: path-problem
Security severity: 9.8
Severity: error
Precision: high
Tags:
- security
- external/cwe/cwe-643
Query suites:
- go-code-scanning.qls
- go-security-extended.qls
- go-security-and-quality.qls
如果使用字符串连接构建 XPath 表达式,并且连接的组件包含用户输入,则用户很可能能够创建恶意的 XPath 表达式。
建议¶
如果必须在 XPath 表达式中包含用户输入,请预编译查询并使用变量引用来包含用户输入。
例如,当使用 github.com/ChrisTrenkamp/goxpath
API 时,您可以通过创建一个接受 *goxpath.Opts
结构的函数来实现此目的。然后,您可以在此结构中设置变量引用的值。然后,可以在调用 Exec()
、Exec{Bool|Num|Node}()
、ParseExec()
或 MustExec()
时指定此函数。
示例¶
在第一个示例中,代码接受用户指定的用户名,并在 XPath 表达式中使用此未经验证和未经净化的值。这很容易受到用户提供特殊字符或字符串序列的攻击,这些字符或字符串序列会更改 XPath 表达式的含义以搜索不同的值。
在第二个示例中,XPath 表达式是一个硬编码字符串,它指定了一些变量,这些变量在运行时使用 goxpath.Opts
结构安全地解析。
package main
import (
"fmt"
"net/http"
"github.com/ChrisTrenkamp/goxpath"
"github.com/ChrisTrenkamp/goxpath/tree"
)
func main() {}
func processRequest(r *http.Request, doc tree.Node) {
r.ParseForm()
username := r.Form.Get("username")
// BAD: User input used directly in an XPath expression
xPath := goxpath.MustParse("//users/user[login/text()='" + username + "']/home_dir/text()")
unsafeRes, _ := xPath.ExecBool(doc)
fmt.Println(unsafeRes)
// GOOD: Value of parameters is defined here instead of directly in the query
opt := func(o *goxpath.Opts) {
o.Vars["username"] = tree.String(username)
}
// GOOD: Uses parameters to avoid including user input directly in XPath expression
xPath = goxpath.MustParse("//users/user[login/text()=$username]/home_dir/text()")
safeRes, _ := xPath.ExecBool(doc, opt)
fmt.Println(safeRes)
}
参考¶
OWASP:测试 XPath 注入。
OWASP:XPath 注入。
常见弱点枚举:CWE-643。