diff --git a/change-notes/1.24/analysis-go.md b/change-notes/1.24/analysis-go.md index 2212c4d2e39..9b1c033d18d 100644 --- a/change-notes/1.24/analysis-go.md +++ b/change-notes/1.24/analysis-go.md @@ -19,6 +19,7 @@ The CodeQL library for Go now contains a folder of simple "cookbook" queries tha | Incomplete URL scheme check (`go/incomplete-url-scheme-check`) | correctness, security, external/cwe/cwe-020 | Highlights checks for `javascript` URLs that do not take `data` or `vbscript` URLs into account. Results are shown on LGTM by default. | | Potentially unsafe quoting (`go/unsafe-quoting`) | correctness, security, external/cwe/cwe-078, external/cwe/cwe-089, external/cwe/cwe-094 | Highlights code that constructs a quoted string literal containing data that may itself contain quotes. Results are shown on LGTM by default. | | Size computation for allocation may overflow (`go/allocation-size-overflow`) | correctness, security, external/cwe/cwe-190 | Highlights code that computes the size of an allocation based on the size of a potentially large object. Results are shown on LGTM by default. | +| XPath injection (`go/xml/xpath-injection`) | security, external/cwe/cwe-643 | Highlights code that uses remote input in an XPath expression. Results are shown on LGTM by default. | ## Changes to existing queries diff --git a/ql/src/experimental/Security/CWE-643/XPathInjection.go b/ql/src/Security/CWE-643/XPathInjection.go similarity index 73% rename from ql/src/experimental/Security/CWE-643/XPathInjection.go rename to ql/src/Security/CWE-643/XPathInjection.go index 869c98acb89..2758de5be25 100644 --- a/ql/src/experimental/Security/CWE-643/XPathInjection.go +++ b/ql/src/Security/CWE-643/XPathInjection.go @@ -13,20 +13,18 @@ func main() {} func processRequest(r *http.Request, doc tree.Node) { r.ParseForm() username := r.Form.Get("username") - password := r.Form.Get("password") // BAD: User input used directly in an XPath expression - xPath := goxpath.MustParse("//users/user[login/text()='" + username + "' and password/text() = '" + password + "']/home_dir/text()") + 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) - o.Vars["password"] = tree.String(password) } // GOOD: Uses parameters to avoid including user input directly in XPath expression - xPath = goxpath.MustParse("//users/user[login/text()=$username and password/text() = $password]/home_dir/text()") + xPath = goxpath.MustParse("//users/user[login/text()=$username]/home_dir/text()") safeRes, _ := xPath.ExecBool(doc, opt) fmt.Println(safeRes) } diff --git a/ql/src/experimental/Security/CWE-643/XPathInjection.qhelp b/ql/src/Security/CWE-643/XPathInjection.qhelp similarity index 86% rename from ql/src/experimental/Security/CWE-643/XPathInjection.qhelp rename to ql/src/Security/CWE-643/XPathInjection.qhelp index 23d693ac704..4c1d376202e 100644 --- a/ql/src/experimental/Security/CWE-643/XPathInjection.qhelp +++ b/ql/src/Security/CWE-643/XPathInjection.qhelp @@ -15,16 +15,16 @@ If user input must be included in an XPath expression, pre-compile the query and references to include the user input.
-For example, when using the github.com/ChrisTrenkamp/goxpath API, this can be done by creating a function that takes an *goxpath.Opts structure.
+For example, when using the github.com/ChrisTrenkamp/goxpath API, you can do this by creating a function that takes an *goxpath.Opts structure.
In this structure you can then set the values of the variable references.
-This function can then be specified when calling Exec(), Exec{Bool|Num|Node}(), ParseExec() or MustExec().
+This function can then be specified when calling Exec(), Exec{Bool|Num|Node}(), ParseExec(), or MustExec().
-In the first example, the code accepts a user name specified by the user, and uses this
+In the first example, the code accepts a username specified by the user, and uses this
unvalidated and unsanitized value in an XPath expression. This is vulnerable to the user providing
special characters or string sequences that change the meaning of the XPath expression to search
for different values.
diff --git a/ql/src/Security/CWE-643/XPathInjection.ql b/ql/src/Security/CWE-643/XPathInjection.ql
new file mode 100644
index 00000000000..ca6df0ff004
--- /dev/null
+++ b/ql/src/Security/CWE-643/XPathInjection.ql
@@ -0,0 +1,27 @@
+/**
+ * @name XPath injection
+ * @description Building an XPath expression from user-controlled sources is vulnerable to insertion of
+ * malicious code by the user.
+ * @kind path-problem
+ * @problem.severity error
+ * @precision high
+ * @id go/xml/xpath-injection
+ * @tags security
+ * external/cwe/cwe-643
+ */
+
+import go
+import semmle.go.security.XPathInjection::XPathInjection
+import DataFlow::PathGraph
+
+/** Holds if `node` is either a string or a byte slice */
+predicate isStringOrByte(DataFlow::PathNode node) {
+ exists(Type t | t = node.getNode().getType().getUnderlyingType() |
+ t instanceof StringType or t instanceof ByteSliceType
+ )
+}
+
+from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink
+where config.hasFlowPath(source, sink) and isStringOrByte(sink)
+select sink.getNode(), source, sink, "$@ flows here and is used in an XPath expression.",
+ source.getNode(), "A user-provided value"
diff --git a/ql/src/experimental/Security/CWE-643/XPathInjection.ql b/ql/src/experimental/Security/CWE-643/XPathInjection.ql
deleted file mode 100644
index 453710d71e0..00000000000
--- a/ql/src/experimental/Security/CWE-643/XPathInjection.ql
+++ /dev/null
@@ -1,188 +0,0 @@
-/**
- * @name XPath injection
- * @description Building an XPath expression from user-controlled sources is vulnerable to insertion of
- * malicious code by the user.
- * @kind path-problem
- * @problem.severity error
- * @id go/xml/xpath-injection
- * @tags security
- * external/cwe/cwe-643
- */
-
-import go
-import DataFlow::PathGraph
-
-class ByteSliceType extends SliceType {
- ByteSliceType() { this.getElementType() instanceof Uint8Type }
-}
-
-class XPathInjectionConfiguration extends TaintTracking::Configuration {
- XPathInjectionConfiguration() { this = "XPathInjectionConfiguration" }
-
- override predicate isSource(DataFlow::Node source) { source instanceof UntrustedFlowSource }
-
- override predicate isSink(DataFlow::Node sink) { sink instanceof XPathInjectionSink }
-
- override predicate isSanitizer(DataFlow::Node node) {
- exists(Type t | t = node.getType().getUnderlyingType() |
- not t instanceof StringType or not t instanceof ByteSliceType
- )
- }
-}
-
-abstract class XPathInjectionSink extends DataFlow::Node { }
-
-// https://github.com/antchfx/xpath
-class XPathSink extends XPathInjectionSink {
- XPathSink() {
- exists(Function f, string name | name.matches("Compile%") |
- f.hasQualifiedName("github.com/antchfx/xpath", name) and
- this = f.getACall().getArgument(0)
- )
- or
- exists(Function f, string name | name.matches("MustCompile%") |
- f.hasQualifiedName("github.com/antchfx/xpath", name) and
- this = f.getACall().getArgument(0)
- )
- or
- exists(Function f, string name | name.matches("Select%") |
- f.hasQualifiedName("github.com/antchfx/xpath", name) and
- this = f.getACall().getArgument(1)
- )
- }
-}
-
-// https://github.com/antchfx/htmlquery
-class HtmlQuerySink extends XPathInjectionSink {
- HtmlQuerySink() {
- exists(Function f, string name | name.matches("Find%") |
- f.hasQualifiedName("github.com/antchfx/htmlquery", name) and
- this = f.getACall().getArgument(1)
- )
- or
- exists(Function f, string name | name.matches("Query%") |
- f.hasQualifiedName("github.com/antchfx/htmlquery", name) and
- this = f.getACall().getArgument(1)
- )
- or
- exists(Function f |
- f.hasQualifiedName("github.com/antchfx/htmlquery", "getQuery") and
- this = f.getACall().getArgument(0)
- )
- }
-}
-
-// https://github.com/antchfx/xmlquery
-class XmlQuerySink extends XPathInjectionSink {
- XmlQuerySink() {
- exists(Function f, string name | name.matches("Find%") |
- f.hasQualifiedName("github.com/antchfx/xmlquery", name) and
- this = f.getACall().getArgument(1)
- )
- or
- exists(Function f, string name | name.matches("Query%") |
- f.hasQualifiedName("github.com/antchfx/xmlquery", name) and
- this = f.getACall().getArgument(1)
- )
- or
- exists(Function f, string name | name.matches("Select%") |
- f.hasQualifiedName("github.com/antchfx/xmlquery", name) and
- this = f.getACall().getArgument(0)
- )
- or
- exists(Function f |
- f.hasQualifiedName("github.com/antchfx/xmlquery", "getQuery") and
- this = f.getACall().getArgument(0)
- )
- }
-}
-
-// https://github.com/antchfx/jsonquery
-class JsonQuerySink extends XPathInjectionSink {
- JsonQuerySink() {
- exists(Function f, string name | name.matches("Find%") |
- f.hasQualifiedName("github.com/antchfx/jsonquery", name) and
- this = f.getACall().getArgument(1)
- )
- or
- exists(Function f, string name | name.matches("Query%") |
- f.hasQualifiedName("github.com/antchfx/jsonquery", name) and
- this = f.getACall().getArgument(1)
- )
- or
- exists(Function f |
- f.hasQualifiedName("github.com/antchfx/jsonquery", "getQuery") and
- this = f.getACall().getArgument(0)
- )
- }
-}
-
-// https://github.com/go-xmlpath/xmlpath
-class XmlPathSink extends XPathInjectionSink {
- XmlPathSink() {
- exists(Function f, string name | name.matches("Compile%") |
- f.hasQualifiedName("github.com/go-xmlpath/xmlpath", name) and
- this = f.getACall().getArgument(0)
- )
- or
- exists(Function f, string name | name.matches("MustCompile%") |
- f.hasQualifiedName("github.com/go-xmlpath/xmlpath", name) and
- this = f.getACall().getArgument(0)
- )
- }
-}
-
-// https://github.com/ChrisTrenkamp/goxpath
-class GoXPathSink extends XPathInjectionSink {
- GoXPathSink() {
- exists(Function f, string name | name.matches("Parse%") |
- f.hasQualifiedName("github.com/ChrisTrenkamp/goxpath", name) and
- this = f.getACall().getArgument(0)
- )
- or
- exists(Function f, string name | name.matches("MustParse%") |
- f.hasQualifiedName("github.com/ChrisTrenkamp/goxpath", name) and
- this = f.getACall().getArgument(0)
- )
- }
-}
-
-// https://github.com/santhosh-tekuri/xpathparser
-class XPathParserSink extends XPathInjectionSink {
- XPathParserSink() {
- exists(Function f, string name | name.matches("Parse%") |
- f.hasQualifiedName("github.com/santhosh-tekuri/xpathparser", name) and
- this = f.getACall().getArgument(0)
- )
- or
- exists(Function f, string name | name.matches("MustParse%") |
- f.hasQualifiedName("github.com/santhosh-tekuri/xpathparser", name) and
- this = f.getACall().getArgument(0)
- )
- }
-}
-
-// https://github.com/moovweb/gokogiri
-class GokogiriSink extends XPathInjectionSink {
- GokogiriSink() {
- exists(Function f, string name | name.matches("Compile%") |
- f.hasQualifiedName("github.com/moovweb/gokogiri/xpath", name) and
- this = f.getACall().getArgument(0)
- )
- or
- exists(Function f, string name | name.matches("Search%") |
- f.hasQualifiedName("github.com/moovweb/gokogiri/xml", name) and
- this = f.getACall().getArgument(0)
- )
- or
- exists(Function f, string name | name.matches("EvalXPath%") |
- f.hasQualifiedName("github.com/moovweb/gokogiri/xml", name) and
- this = f.getACall().getArgument(0)
- )
- }
-}
-
-from DataFlow::PathNode source, DataFlow::PathNode sink, XPathInjectionConfiguration c
-where c.hasFlowPath(source, sink)
-select sink.getNode(), source, sink, "$@ flows here and is used in an XPath expression.",
- source.getNode(), "A user-provided value"
diff --git a/ql/src/go.qll b/ql/src/go.qll
index dfdd54726cf..c176857a282 100644
--- a/ql/src/go.qll
+++ b/ql/src/go.qll
@@ -26,6 +26,7 @@ import semmle.go.dataflow.SSA
import semmle.go.frameworks.HTTP
import semmle.go.frameworks.SystemCommandExecutors
import semmle.go.frameworks.SQL
+import semmle.go.frameworks.XPath
import semmle.go.frameworks.Stdlib
import semmle.go.security.FlowSources
import semmle.go.Util
diff --git a/ql/src/semmle/go/Types.qll b/ql/src/semmle/go/Types.qll
index ad87b688fea..c68df092675 100644
--- a/ql/src/semmle/go/Types.qll
+++ b/ql/src/semmle/go/Types.qll
@@ -328,6 +328,11 @@ class SliceType extends @slicetype, CompositeType {
override string toString() { result = "slice type" }
}
+/** A byte slice type */
+class ByteSliceType extends SliceType {
+ ByteSliceType() { this.getElementType() instanceof Uint8Type }
+}
+
/** A struct type. */
class StructType extends @structtype, CompositeType {
/**
diff --git a/ql/src/semmle/go/frameworks/XPath.qll b/ql/src/semmle/go/frameworks/XPath.qll
new file mode 100644
index 00000000000..932d0b04cd5
--- /dev/null
+++ b/ql/src/semmle/go/frameworks/XPath.qll
@@ -0,0 +1,153 @@
+/**
+ * Provides classes for working with XPath-related concepts such as XPath expressions.
+ */
+
+import go
+
+/** Provides classes for working with XPath-related APIs. */
+module XPath {
+ /** Provides classes for working with XPath expression strings. */
+ module XPathExpressionString {
+ /**
+ * A data-flow node whose string value is interpreted as (part of) an XPath expression.
+ *
+ * Extend this class to model new APIs.
+ */
+ abstract class Range extends DataFlow::Node { }
+
+ /** An XPath expression string used in an API function of the https://github.com/antchfx/xpath package. */
+ private class AntchfxXpathXPathExpressionString extends Range {
+ AntchfxXpathXPathExpressionString() {
+ exists(Function f, string name | name.matches("Compile%") |
+ f.hasQualifiedName("github.com/antchfx/xpath", name) and
+ this = f.getACall().getArgument(0)
+ )
+ or
+ exists(Function f, string name | name.matches("MustCompile%") |
+ f.hasQualifiedName("github.com/antchfx/xpath", name) and
+ this = f.getACall().getArgument(0)
+ )
+ or
+ exists(Function f, string name | name.matches("Select%") |
+ f.hasQualifiedName("github.com/antchfx/xpath", name) and
+ this = f.getACall().getArgument(1)
+ )
+ }
+ }
+
+ /** An XPath expression string used in an API function of the https://github.com/antchfx/htmlquery package. */
+ private class AntchfxHtmlqueryXPathExpressionString extends Range {
+ AntchfxHtmlqueryXPathExpressionString() {
+ exists(Function f, string name | name.matches("Find%") |
+ f.hasQualifiedName("github.com/antchfx/htmlquery", name) and
+ this = f.getACall().getArgument(1)
+ )
+ or
+ exists(Function f, string name | name.matches("Query%") |
+ f.hasQualifiedName("github.com/antchfx/htmlquery", name) and
+ this = f.getACall().getArgument(1)
+ )
+ }
+ }
+
+ /** An XPath expression string used in an API function of the https://github.com/antchfx/xmlquery package. */
+ private class AntchfxXmlqueryXPathExpressionString extends Range {
+ AntchfxXmlqueryXPathExpressionString() {
+ exists(Function f, string name | name.matches("Find%") |
+ f.hasQualifiedName("github.com/antchfx/xmlquery", name) and
+ this = f.getACall().getArgument(1)
+ )
+ or
+ exists(Function f, string name | name.matches("Query%") |
+ f.hasQualifiedName("github.com/antchfx/xmlquery", name) and
+ this = f.getACall().getArgument(1)
+ )
+ or
+ exists(Method m, string name | name.matches("Select%") |
+ m.hasQualifiedName("github.com/antchfx/xmlquery", "Node", name) and
+ this = m.getACall().getArgument(0)
+ )
+ }
+ }
+
+ /** An XPath expression string used in an API function of the https://github.com/antchfx/jsonquery package. */
+ private class AntchfxJsonqueryXPathExpressionString extends Range {
+ AntchfxJsonqueryXPathExpressionString() {
+ exists(Function f, string name | name.matches("Find%") |
+ f.hasQualifiedName("github.com/antchfx/jsonquery", name) and
+ this = f.getACall().getArgument(1)
+ )
+ or
+ exists(Function f, string name | name.matches("Query%") |
+ f.hasQualifiedName("github.com/antchfx/jsonquery", name) and
+ this = f.getACall().getArgument(1)
+ )
+ }
+ }
+
+ /** An XPath expression string used in an API function of the https://github.com/go-xmlpath/xmlpath package. */
+ private class GoXmlpathXmlpathXPathExpressionString extends Range {
+ GoXmlpathXmlpathXPathExpressionString() {
+ exists(Function f, string name | name.matches("Compile%") |
+ f.hasQualifiedName("github.com/go-xmlpath/xmlpath", name) and
+ this = f.getACall().getArgument(0)
+ )
+ or
+ exists(Function f, string name | name.matches("MustCompile%") |
+ f.hasQualifiedName("github.com/go-xmlpath/xmlpath", name) and
+ this = f.getACall().getArgument(0)
+ )
+ }
+ }
+
+ /** An XPath expression string used in an API function of the https://github.com/ChrisTrenkamp/goxpath package. */
+ private class ChrisTrenkampGoxpathXPathExpressionString extends Range {
+ ChrisTrenkampGoxpathXPathExpressionString() {
+ exists(Function f, string name | name.matches("Parse%") |
+ f.hasQualifiedName("github.com/ChrisTrenkamp/goxpath", name) and
+ this = f.getACall().getArgument(0)
+ )
+ or
+ exists(Function f, string name | name.matches("MustParse%") |
+ f.hasQualifiedName("github.com/ChrisTrenkamp/goxpath", name) and
+ this = f.getACall().getArgument(0)
+ )
+ }
+ }
+
+ /** An XPath expression string used in an API function of the https://github.com/santhosh-tekuri/xpathparser package. */
+ private class SanthoshTekuriXpathparserXPathExpressionString extends Range {
+ SanthoshTekuriXpathparserXPathExpressionString() {
+ exists(Function f, string name | name.matches("Parse%") |
+ f.hasQualifiedName("github.com/santhosh-tekuri/xpathparser", name) and
+ this = f.getACall().getArgument(0)
+ )
+ or
+ exists(Function f, string name | name.matches("MustParse%") |
+ f.hasQualifiedName("github.com/santhosh-tekuri/xpathparser", name) and
+ this = f.getACall().getArgument(0)
+ )
+ }
+ }
+
+ /** An XPath expression string used in an API function of the https://github.com/jbowtie/gokogiri package. */
+ private class JbowtieGokogiriXPathExpressionString extends Range {
+ JbowtieGokogiriXPathExpressionString() {
+ exists(Function f, string name | name.matches("Compile%") |
+ f.hasQualifiedName("github.com/jbowtie/gokogiri/xpath", name) and
+ this = f.getACall().getArgument(0)
+ )
+ or
+ exists(Method m, string name | name.matches("Search%") |
+ m.hasQualifiedName("github.com/jbowtie/gokogiri/xml", "Node", name) and
+ this = m.getACall().getArgument(0)
+ )
+ or
+ exists(Method m, string name | name.matches("EvalXPath%") |
+ m.hasQualifiedName("github.com/jbowtie/gokogiri/xml", "Node", name) and
+ this = m.getACall().getArgument(0)
+ )
+ }
+ }
+ }
+}
diff --git a/ql/src/semmle/go/security/XPathInjection.qll b/ql/src/semmle/go/security/XPathInjection.qll
new file mode 100644
index 00000000000..dedaa16575f
--- /dev/null
+++ b/ql/src/semmle/go/security/XPathInjection.qll
@@ -0,0 +1,35 @@
+/**
+ * Provides a taint tracking configuration for reasoning about untrusted user input used in an XPath expression.
+ *
+ * Note: for performance reasons, only import this file if `XPathInjection::Configuration` is needed,
+ * otherwise `XPathInjectionCustomizations` should be imported instead.
+ */
+
+import go
+
+/**
+ * Provides a taint tracking configuration for reasoning about untrusted user input used in an XPath expression.
+ */
+module XPathInjection {
+ import XPathInjectionCustomizations::XPathInjection
+
+ /**
+ * A taint-tracking configuration for reasoning about untrusted user input used in an XPath expression.
+ */
+ class Configuration extends TaintTracking::Configuration {
+ Configuration() { this = "XPathInjection" }
+
+ override predicate isSource(DataFlow::Node source) { source instanceof Source }
+
+ override predicate isSink(DataFlow::Node sink) { sink instanceof Sink }
+
+ override predicate isSanitizer(DataFlow::Node node) {
+ super.isSanitizer(node) or
+ node instanceof Sanitizer
+ }
+
+ override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
+ guard instanceof SanitizerGuard
+ }
+ }
+}
diff --git a/ql/src/semmle/go/security/XPathInjectionCustomizations.qll b/ql/src/semmle/go/security/XPathInjectionCustomizations.qll
new file mode 100644
index 00000000000..b3a25fc2857
--- /dev/null
+++ b/ql/src/semmle/go/security/XPathInjectionCustomizations.qll
@@ -0,0 +1,41 @@
+/**
+ * Provides default sources, sinks and sanitizers for reasoning about untrusted user input used in an XPath expression,
+ * as well as extension points for adding your own.
+ */
+
+import go
+
+/**
+ * Provides extension points for reasoning about untrusted user input used in an XPath expression.
+ */
+module XPathInjection {
+ /**
+ * A data flow source for untrusted user input used in an XPath expression.
+ */
+ abstract class Source extends DataFlow::Node { }
+
+ /**
+ * A data flow sink for untrusted user input used in an XPath expression.
+ */
+ abstract class Sink extends DataFlow::ExprNode { }
+
+ /**
+ * A sanitizer for untrusted user input used in an XPath expression.
+ */
+ abstract class Sanitizer extends DataFlow::ExprNode { }
+
+ /**
+ * A sanitizer guard for untrusted user input used in an XPath expression.
+ */
+ abstract class SanitizerGuard extends DataFlow::BarrierGuard { }
+
+ /** A source of untrusted data, used in an XPath expression. */
+ class UntrustedFlowAsSource extends Source {
+ UntrustedFlowAsSource() { this instanceof UntrustedFlowSource }
+ }
+
+ /** An XPath expression string, considered as a taint sink for XPath injection. */
+ class XPathExpressionStringAsSink extends Sink {
+ XPathExpressionStringAsSink() { this instanceof XPath::XPathExpressionString::Range }
+ }
+}
diff --git a/ql/test/query-tests/Security/CWE-643/XPathInjection.expected b/ql/test/query-tests/Security/CWE-643/XPathInjection.expected
new file mode 100644
index 00000000000..c9ca557e59e
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/XPathInjection.expected
@@ -0,0 +1,122 @@
+edges
+| XPathInjection.go:21:14:21:19 | selection of Form : Values | XPathInjection.go:24:23:24:85 | ...+... |
+| XPathInjection.go:21:14:21:19 | selection of Form : Values | XPathInjection.go:27:24:27:86 | ...+... |
+| XPathInjection.go:21:14:21:19 | selection of Form : Values | XPathInjection.go:30:24:30:82 | ...+... |
+| XPathInjection.go:35:14:35:19 | selection of Form : Values | XPathInjection.go:38:26:38:84 | ...+... |
+| XPathInjection.go:35:14:35:19 | selection of Form : Values | XPathInjection.go:41:29:41:87 | ...+... |
+| XPathInjection.go:35:14:35:19 | selection of Form : Values | XPathInjection.go:44:33:44:91 | ...+... |
+| XPathInjection.go:35:14:35:19 | selection of Form : Values | XPathInjection.go:47:30:47:88 | ...+... |
+| XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:55:25:55:83 | ...+... |
+| XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:58:28:58:86 | ...+... |
+| XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:61:25:61:83 | ...+... |
+| XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:64:34:64:92 | ...+... |
+| XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:67:32:67:90 | ...+... |
+| XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:70:29:70:87 | ...+... |
+| XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:73:23:73:85 | ...+... |
+| XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:76:22:76:84 | ...+... |
+| XPathInjection.go:81:14:81:19 | selection of Form : Values | XPathInjection.go:84:26:84:84 | ...+... |
+| XPathInjection.go:81:14:81:19 | selection of Form : Values | XPathInjection.go:87:29:87:87 | ...+... |
+| XPathInjection.go:81:14:81:19 | selection of Form : Values | XPathInjection.go:90:33:90:91 | ...+... |
+| XPathInjection.go:81:14:81:19 | selection of Form : Values | XPathInjection.go:93:30:93:88 | ...+... |
+| XPathInjection.go:98:14:98:19 | selection of Form : Values | XPathInjection.go:101:25:101:87 | ...+... |
+| XPathInjection.go:98:14:98:19 | selection of Form : Values | XPathInjection.go:104:26:104:88 | ...+... |
+| XPathInjection.go:109:14:109:19 | selection of Form : Values | XPathInjection.go:113:23:113:126 | ...+... |
+| XPathInjection.go:109:14:109:19 | selection of Form : Values | XPathInjection.go:116:24:116:127 | ...+... |
+| XPathInjection.go:109:14:109:19 | selection of Form : Values | XPathInjection.go:119:27:119:122 | ...+... |
+| XPathInjection.go:110:14:110:19 | selection of Form : Values | XPathInjection.go:113:23:113:126 | ...+... |
+| XPathInjection.go:110:14:110:19 | selection of Form : Values | XPathInjection.go:116:24:116:127 | ...+... |
+| XPathInjection.go:110:14:110:19 | selection of Form : Values | XPathInjection.go:119:27:119:122 | ...+... |
+| XPathInjection.go:127:14:127:19 | selection of Form : Values | XPathInjection.go:130:27:130:89 | ...+... |
+| XPathInjection.go:127:14:127:19 | selection of Form : Values | XPathInjection.go:133:28:133:90 | ...+... |
+| XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:142:33:142:136 | ...+... |
+| XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:145:18:145:121 | ...+... |
+| XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:151:31:151:126 | ...+... |
+| XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:160:21:160:116 | ...+... |
+| XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:169:27:169:122 | ...+... |
+| XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:142:33:142:136 | ...+... |
+| XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:145:18:145:121 | ...+... |
+| XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:151:31:151:126 | ...+... |
+| XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:160:21:160:116 | ...+... |
+| XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:169:27:169:122 | ...+... |
+nodes
+| XPathInjection.go:21:14:21:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:24:23:24:85 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:27:24:27:86 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:30:24:30:82 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:35:14:35:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:38:26:38:84 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:41:29:41:87 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:44:33:44:91 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:47:30:47:88 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:52:14:52:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:55:25:55:83 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:58:28:58:86 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:61:25:61:83 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:64:34:64:92 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:67:32:67:90 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:70:29:70:87 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:73:23:73:85 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:76:22:76:84 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:81:14:81:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:84:26:84:84 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:87:29:87:87 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:90:33:90:91 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:93:30:93:88 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:98:14:98:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:101:25:101:87 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:104:26:104:88 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:109:14:109:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:110:14:110:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:113:23:113:126 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:116:24:116:127 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:119:27:119:122 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:127:14:127:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:130:27:130:89 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:133:28:133:90 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:138:14:138:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:139:14:139:19 | selection of Form : Values | semmle.label | selection of Form : Values |
+| XPathInjection.go:142:33:142:136 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:145:18:145:121 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:151:31:151:126 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:160:21:160:116 | ...+... | semmle.label | ...+... |
+| XPathInjection.go:169:27:169:122 | ...+... | semmle.label | ...+... |
+#select
+| XPathInjection.go:24:23:24:85 | ...+... | XPathInjection.go:21:14:21:19 | selection of Form : Values | XPathInjection.go:24:23:24:85 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:21:14:21:19 | selection of Form | A user-provided value |
+| XPathInjection.go:27:24:27:86 | ...+... | XPathInjection.go:21:14:21:19 | selection of Form : Values | XPathInjection.go:27:24:27:86 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:21:14:21:19 | selection of Form | A user-provided value |
+| XPathInjection.go:30:24:30:82 | ...+... | XPathInjection.go:21:14:21:19 | selection of Form : Values | XPathInjection.go:30:24:30:82 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:21:14:21:19 | selection of Form | A user-provided value |
+| XPathInjection.go:38:26:38:84 | ...+... | XPathInjection.go:35:14:35:19 | selection of Form : Values | XPathInjection.go:38:26:38:84 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:35:14:35:19 | selection of Form | A user-provided value |
+| XPathInjection.go:41:29:41:87 | ...+... | XPathInjection.go:35:14:35:19 | selection of Form : Values | XPathInjection.go:41:29:41:87 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:35:14:35:19 | selection of Form | A user-provided value |
+| XPathInjection.go:44:33:44:91 | ...+... | XPathInjection.go:35:14:35:19 | selection of Form : Values | XPathInjection.go:44:33:44:91 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:35:14:35:19 | selection of Form | A user-provided value |
+| XPathInjection.go:47:30:47:88 | ...+... | XPathInjection.go:35:14:35:19 | selection of Form : Values | XPathInjection.go:47:30:47:88 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:35:14:35:19 | selection of Form | A user-provided value |
+| XPathInjection.go:55:25:55:83 | ...+... | XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:55:25:55:83 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:52:14:52:19 | selection of Form | A user-provided value |
+| XPathInjection.go:58:28:58:86 | ...+... | XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:58:28:58:86 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:52:14:52:19 | selection of Form | A user-provided value |
+| XPathInjection.go:61:25:61:83 | ...+... | XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:61:25:61:83 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:52:14:52:19 | selection of Form | A user-provided value |
+| XPathInjection.go:64:34:64:92 | ...+... | XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:64:34:64:92 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:52:14:52:19 | selection of Form | A user-provided value |
+| XPathInjection.go:67:32:67:90 | ...+... | XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:67:32:67:90 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:52:14:52:19 | selection of Form | A user-provided value |
+| XPathInjection.go:70:29:70:87 | ...+... | XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:70:29:70:87 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:52:14:52:19 | selection of Form | A user-provided value |
+| XPathInjection.go:73:23:73:85 | ...+... | XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:73:23:73:85 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:52:14:52:19 | selection of Form | A user-provided value |
+| XPathInjection.go:76:22:76:84 | ...+... | XPathInjection.go:52:14:52:19 | selection of Form : Values | XPathInjection.go:76:22:76:84 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:52:14:52:19 | selection of Form | A user-provided value |
+| XPathInjection.go:84:26:84:84 | ...+... | XPathInjection.go:81:14:81:19 | selection of Form : Values | XPathInjection.go:84:26:84:84 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:81:14:81:19 | selection of Form | A user-provided value |
+| XPathInjection.go:87:29:87:87 | ...+... | XPathInjection.go:81:14:81:19 | selection of Form : Values | XPathInjection.go:87:29:87:87 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:81:14:81:19 | selection of Form | A user-provided value |
+| XPathInjection.go:90:33:90:91 | ...+... | XPathInjection.go:81:14:81:19 | selection of Form : Values | XPathInjection.go:90:33:90:91 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:81:14:81:19 | selection of Form | A user-provided value |
+| XPathInjection.go:93:30:93:88 | ...+... | XPathInjection.go:81:14:81:19 | selection of Form : Values | XPathInjection.go:93:30:93:88 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:81:14:81:19 | selection of Form | A user-provided value |
+| XPathInjection.go:101:25:101:87 | ...+... | XPathInjection.go:98:14:98:19 | selection of Form : Values | XPathInjection.go:101:25:101:87 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:98:14:98:19 | selection of Form | A user-provided value |
+| XPathInjection.go:104:26:104:88 | ...+... | XPathInjection.go:98:14:98:19 | selection of Form : Values | XPathInjection.go:104:26:104:88 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:98:14:98:19 | selection of Form | A user-provided value |
+| XPathInjection.go:113:23:113:126 | ...+... | XPathInjection.go:109:14:109:19 | selection of Form : Values | XPathInjection.go:113:23:113:126 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:109:14:109:19 | selection of Form | A user-provided value |
+| XPathInjection.go:113:23:113:126 | ...+... | XPathInjection.go:110:14:110:19 | selection of Form : Values | XPathInjection.go:113:23:113:126 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:110:14:110:19 | selection of Form | A user-provided value |
+| XPathInjection.go:116:24:116:127 | ...+... | XPathInjection.go:109:14:109:19 | selection of Form : Values | XPathInjection.go:116:24:116:127 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:109:14:109:19 | selection of Form | A user-provided value |
+| XPathInjection.go:116:24:116:127 | ...+... | XPathInjection.go:110:14:110:19 | selection of Form : Values | XPathInjection.go:116:24:116:127 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:110:14:110:19 | selection of Form | A user-provided value |
+| XPathInjection.go:119:27:119:122 | ...+... | XPathInjection.go:109:14:109:19 | selection of Form : Values | XPathInjection.go:119:27:119:122 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:109:14:109:19 | selection of Form | A user-provided value |
+| XPathInjection.go:119:27:119:122 | ...+... | XPathInjection.go:110:14:110:19 | selection of Form : Values | XPathInjection.go:119:27:119:122 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:110:14:110:19 | selection of Form | A user-provided value |
+| XPathInjection.go:130:27:130:89 | ...+... | XPathInjection.go:127:14:127:19 | selection of Form : Values | XPathInjection.go:130:27:130:89 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:127:14:127:19 | selection of Form | A user-provided value |
+| XPathInjection.go:133:28:133:90 | ...+... | XPathInjection.go:127:14:127:19 | selection of Form : Values | XPathInjection.go:133:28:133:90 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:127:14:127:19 | selection of Form | A user-provided value |
+| XPathInjection.go:142:33:142:136 | ...+... | XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:142:33:142:136 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:138:14:138:19 | selection of Form | A user-provided value |
+| XPathInjection.go:142:33:142:136 | ...+... | XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:142:33:142:136 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:139:14:139:19 | selection of Form | A user-provided value |
+| XPathInjection.go:145:18:145:121 | ...+... | XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:145:18:145:121 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:138:14:138:19 | selection of Form | A user-provided value |
+| XPathInjection.go:145:18:145:121 | ...+... | XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:145:18:145:121 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:139:14:139:19 | selection of Form | A user-provided value |
+| XPathInjection.go:151:31:151:126 | ...+... | XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:151:31:151:126 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:138:14:138:19 | selection of Form | A user-provided value |
+| XPathInjection.go:151:31:151:126 | ...+... | XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:151:31:151:126 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:139:14:139:19 | selection of Form | A user-provided value |
+| XPathInjection.go:160:21:160:116 | ...+... | XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:160:21:160:116 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:138:14:138:19 | selection of Form | A user-provided value |
+| XPathInjection.go:160:21:160:116 | ...+... | XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:160:21:160:116 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:139:14:139:19 | selection of Form | A user-provided value |
+| XPathInjection.go:169:27:169:122 | ...+... | XPathInjection.go:138:14:138:19 | selection of Form : Values | XPathInjection.go:169:27:169:122 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:138:14:138:19 | selection of Form | A user-provided value |
+| XPathInjection.go:169:27:169:122 | ...+... | XPathInjection.go:139:14:139:19 | selection of Form : Values | XPathInjection.go:169:27:169:122 | ...+... | $@ flows here and is used in an XPath expression. | XPathInjection.go:139:14:139:19 | selection of Form | A user-provided value |
diff --git a/ql/test/query-tests/Security/CWE-643/XPathInjection.go b/ql/test/query-tests/Security/CWE-643/XPathInjection.go
new file mode 100644
index 00000000000..75ca454bd5d
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/XPathInjection.go
@@ -0,0 +1,176 @@
+package main
+
+import (
+ "net/http"
+
+ "github.com/ChrisTrenkamp/goxpath"
+ "github.com/antchfx/htmlquery"
+ "github.com/antchfx/jsonquery"
+ "github.com/antchfx/xmlquery"
+ "github.com/antchfx/xpath"
+ "github.com/go-xmlpath/xmlpath"
+ gokogiriXml "github.com/jbowtie/gokogiri/xml"
+ gokogiriXpath "github.com/jbowtie/gokogiri/xpath"
+ "github.com/santhosh-tekuri/xpathparser"
+)
+
+func main() {}
+
+func testAntchfxXpath(r *http.Request) {
+ r.ParseForm()
+ username := r.Form.Get("username")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = xpath.Compile("//users/user[login/text()='" + username + "']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = xpath.MustCompile("//users/user[login/text()='" + username + "']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = xpath.Select(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+}
+
+func testAntchfxHtmlquery(r *http.Request) {
+ r.ParseForm()
+ username := r.Form.Get("username")
+
+ // BAD: User input used directly in an XPath expression
+ _ = htmlquery.Find(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = htmlquery.FindOne(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = htmlquery.QueryAll(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = htmlquery.Query(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+}
+
+func testAntchfxXmlquery(r *http.Request, n *xmlquery.Node) {
+ r.ParseForm()
+ username := r.Form.Get("username")
+
+ // BAD: User input used directly in an XPath expression
+ _ = xmlquery.Find(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = xmlquery.FindOne(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ xmlquery.FindEach(nil, "//users/user[login/text()='"+username+"']/home_dir/text()", nil)
+
+ // BAD: User input used directly in an XPath expression
+ xmlquery.FindEachWithBreak(nil, "//users/user[login/text()='"+username+"']/home_dir/text()", nil)
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = xmlquery.QueryAll(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = xmlquery.Query(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = n.SelectElements("//users/user[login/text()='" + username + "']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = n.SelectElement("//users/user[login/text()='" + username + "']/home_dir/text()")
+}
+
+func testAntchfxJsonquery(r *http.Request) {
+ r.ParseForm()
+ username := r.Form.Get("username")
+
+ // BAD: User input used directly in an XPath expression
+ _ = jsonquery.Find(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = jsonquery.FindOne(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = jsonquery.QueryAll(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = jsonquery.Query(nil, "//users/user[login/text()='"+username+"']/home_dir/text()")
+}
+
+func testGoXmlpathXmlpath(r *http.Request) {
+ r.ParseForm()
+ username := r.Form.Get("username")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = xmlpath.Compile("//users/user[login/text()='" + username + "']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = xmlpath.MustCompile("//users/user[login/text()='" + username + "']/home_dir/text()")
+}
+
+func testChrisTrenkampGoxpath(r *http.Request) {
+ r.ParseForm()
+ username := r.Form.Get("username")
+ password := r.Form.Get("password")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = goxpath.Parse("//users/user[login/text()='" + username + "' and password/text() = '" + password + "']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = goxpath.MustParse("//users/user[login/text()='" + username + "' and password/text() = '" + password + "']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = goxpath.ParseExec("//users/user[login/text()='"+username+"' and password/text() = '"+password+"']/home_dir/text()", nil)
+
+ // GOOD: Uses parameters to avoid including user input directly in XPath expression
+ _ = goxpath.MustParse("//users/user[login/text()=$username and password/text() = $password]/home_dir/text()")
+}
+
+func testSanthoshTekuriXpathparser(r *http.Request) {
+ r.ParseForm()
+ username := r.Form.Get("username")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = xpathparser.Parse("//users/user[login/text()='" + username + "']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _ = xpathparser.MustParse("//users/user[login/text()='" + username + "']/home_dir/text()")
+}
+
+func testJbowtieGokogiri(r *http.Request, n gokogiriXml.Node) {
+ r.ParseForm()
+ username := r.Form.Get("username")
+ password := r.Form.Get("password")
+
+ // BAD: User input used directly in an XPath expression
+ xpath := gokogiriXpath.Compile("//users/user[login/text()='" + username + "' and password/text() = '" + password + "']/home_dir/text()")
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = n.Search("//users/user[login/text()='" + username + "' and password/text() = '" + password + "']/home_dir/text()")
+
+ // OK: This is not flagged, since the creation of `xpath` is already flagged.
+ _, _ = n.Search(xpath)
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = n.SearchWithVariables("//users/user[login/text()='"+username+"' and password/text() = '"+password+"']/home_dir/text()", nil)
+
+ // GOOD: Uses parameters to avoid including user input directly in XPath expression
+ _, _ = n.SearchWithVariables("//users/user[login/text()=$username and password/text() = $password]/home_dir/text()", nil)
+
+ // OK: This is not flagged, since the creation of `xpath` is already flagged.
+ _, _ = n.SearchWithVariables(xpath, nil)
+
+ // BAD: User input used directly in an XPath expression
+ _, _ = n.EvalXPath("//users/user[login/text()='"+username+"' and password/text() = '"+password+"']/home_dir/text()", nil)
+
+ // GOOD: Uses parameters to avoid including user input directly in XPath expression
+ _, _ = n.EvalXPath("//users/user[login/text()=$username and password/text() = $password]/home_dir/text()", nil)
+
+ // OK: This is not flagged, since the creation of `xpath` is already flagged.
+ _, _ = n.EvalXPath(xpath, nil)
+
+ // BAD: User input used directly in an XPath expression
+ _ = n.EvalXPathAsBoolean("//users/user[login/text()='"+username+"' and password/text() = '"+password+"']/home_dir/text()", nil)
+
+ // GOOD: Uses parameters to avoid including user input directly in XPath expression
+ _ = n.EvalXPathAsBoolean("//users/user[login/text()=$username and password/text() = $password]/home_dir/text()", nil)
+
+ // OK: This is not flagged, since the creation of `xpath` is already flagged.
+ _ = n.EvalXPathAsBoolean(xpath, nil)
+}
diff --git a/ql/test/query-tests/Security/CWE-643/XPathInjection.qlref b/ql/test/query-tests/Security/CWE-643/XPathInjection.qlref
new file mode 100644
index 00000000000..c48e056141c
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/XPathInjection.qlref
@@ -0,0 +1 @@
+Security/CWE-643/XPathInjection.ql
diff --git a/ql/test/query-tests/Security/CWE-643/go.mod b/ql/test/query-tests/Security/CWE-643/go.mod
new file mode 100644
index 00000000000..f274ed45000
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/go.mod
@@ -0,0 +1,16 @@
+module main
+
+go 1.13
+
+require (
+ github.com/ChrisTrenkamp/goxpath v0.0.0-20190607011252-c5096ec8773d
+ github.com/antchfx/htmlquery v1.2.2
+ github.com/antchfx/jsonquery v1.1.2
+ github.com/antchfx/xmlquery v1.2.3
+ github.com/antchfx/xpath v1.1.4
+ github.com/go-xmlpath/xmlpath v0.0.0-20150820204837-860cbeca3ebc
+ github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e
+ github.com/jbowtie/gokogiri v0.0.0-20190301021639-37f655d3078f
+ github.com/santhosh-tekuri/xpathparser v1.0.0
+ golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
+)
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/ChrisTrenkamp/goxpath/LICENSE b/ql/test/query-tests/Security/CWE-643/vendor/github.com/ChrisTrenkamp/goxpath/LICENSE
new file mode 100644
index 00000000000..2afe88a6155
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/ChrisTrenkamp/goxpath/LICENSE
@@ -0,0 +1,22 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 ChrisTrenkamp
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/ChrisTrenkamp/goxpath/README.md b/ql/test/query-tests/Security/CWE-643/vendor/github.com/ChrisTrenkamp/goxpath/README.md
new file mode 100644
index 00000000000..4812afac396
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/ChrisTrenkamp/goxpath/README.md
@@ -0,0 +1,3 @@
+This is a simple stub for https://github.com/ChrisTrenkamp/goxpath, strictly for use in query testing.
+
+See the LICENSE file in this folder for information about the licensing of the original library.
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/ChrisTrenkamp/goxpath/goxpath.go b/ql/test/query-tests/Security/CWE-643/vendor/github.com/ChrisTrenkamp/goxpath/goxpath.go
new file mode 100644
index 00000000000..1867a462121
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/ChrisTrenkamp/goxpath/goxpath.go
@@ -0,0 +1,10 @@
+package goxpath
+
+type Opts struct{}
+type FuncOpts func(*Opts)
+
+type XPathExec struct{}
+
+func Parse(xp string) (XPathExec, error)
+func MustParse(xp string) XPathExec
+func ParseExec(xpstr string, t tree.Node, opts ...FuncOpts) (tree.Result, error)
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/htmlquery/LICENSE b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/htmlquery/LICENSE
new file mode 100644
index 00000000000..e14c37141c5
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/htmlquery/LICENSE
@@ -0,0 +1,17 @@
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
\ No newline at end of file
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/htmlquery/README.md b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/htmlquery/README.md
new file mode 100644
index 00000000000..34024e519bf
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/htmlquery/README.md
@@ -0,0 +1,3 @@
+This is a simple stub for https://github.com/antchfx/htmlquery, strictly for use in query testing.
+
+See the LICENSE file in this folder for information about the licensing of the original library.
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/htmlquery/query.go b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/htmlquery/query.go
new file mode 100644
index 00000000000..f3f88db24c5
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/htmlquery/query.go
@@ -0,0 +1,6 @@
+package htmlquery
+
+func Find(top *html.Node, expr string) []*html.Node
+func FindOne(top *html.Node, expr string) *html.Node
+func QueryAll(top *html.Node, expr string) ([]*html.Node, error)
+func Query(top *html.Node, expr string) (*html.Node, error)
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/LICENSE b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/LICENSE
new file mode 100644
index 00000000000..e14c37141c5
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/LICENSE
@@ -0,0 +1,17 @@
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
\ No newline at end of file
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/README.md b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/README.md
new file mode 100644
index 00000000000..533b3887b0a
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/README.md
@@ -0,0 +1,3 @@
+This is a simple stub for https://github.com/antchfx/jsonquery, strictly for use in query testing.
+
+See the LICENSE file in this folder for information about the licensing of the original library.
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/node.go b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/node.go
new file mode 100644
index 00000000000..b09fb54a83f
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/node.go
@@ -0,0 +1,3 @@
+package jsonquery
+
+type Node struct{}
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/query.go b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/query.go
new file mode 100644
index 00000000000..d3a43a46f77
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/jsonquery/query.go
@@ -0,0 +1,6 @@
+package jsonquery
+
+func Find(top *Node, expr string) []*Node
+func FindOne(top *Node, expr string) *Node
+func QueryAll(top *Node, expr string) ([]*Node, error)
+func Query(top *Node, expr string) (*Node, error)
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/LICENSE b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/LICENSE
new file mode 100644
index 00000000000..e14c37141c5
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/LICENSE
@@ -0,0 +1,17 @@
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
\ No newline at end of file
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/README.md b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/README.md
new file mode 100644
index 00000000000..da531d361a9
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/README.md
@@ -0,0 +1,3 @@
+This is a simple stub for https://github.com/antchfx/xmlquery, strictly for use in query testing.
+
+See the LICENSE file in this folder for information about the licensing of the original library.
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/node.go b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/node.go
new file mode 100644
index 00000000000..8bb284f174e
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/node.go
@@ -0,0 +1,4 @@
+package xmlquery
+
+type Node struct {
+}
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/query.go b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/query.go
new file mode 100644
index 00000000000..28e85185ed9
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xmlquery/query.go
@@ -0,0 +1,10 @@
+package xmlquery
+
+func (n *Node) SelectElements(name string) []*Node
+func (n *Node) SelectElement(name string) *Node
+func Find(top *Node, expr string) []*Node
+func FindOne(top *Node, expr string) *Node
+func QueryAll(top *Node, expr string) ([]*Node, error)
+func Query(top *Node, expr string) (*Node, error)
+func FindEach(top *Node, expr string, cb func(int, *Node))
+func FindEachWithBreak(top *Node, expr string, cb func(int, *Node) bool)
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xpath/LICENSE b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xpath/LICENSE
new file mode 100644
index 00000000000..e14c37141c5
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xpath/LICENSE
@@ -0,0 +1,17 @@
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
\ No newline at end of file
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xpath/README.md b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xpath/README.md
new file mode 100644
index 00000000000..4d741a91818
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xpath/README.md
@@ -0,0 +1,3 @@
+This is a simple stub for https://github.com/antchfx/xpath, strictly for use in query testing.
+
+See the LICENSE file in this folder for information about the licensing of the original library.
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xpath/xpath.go b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xpath/xpath.go
new file mode 100644
index 00000000000..538a3abb871
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/antchfx/xpath/xpath.go
@@ -0,0 +1,16 @@
+package xpath
+
+type NodeNavigator interface {
+}
+
+type NodeIterator struct {
+}
+
+func Select(root NodeNavigator, expr string) *NodeIterator
+
+type Expr struct {
+}
+
+func Compile(expr string) (*Expr, error)
+
+func MustCompile(expr string) *Expr
diff --git a/ql/test/query-tests/Security/CWE-643/vendor/github.com/go-xmlpath/xmlpath/LICENSE b/ql/test/query-tests/Security/CWE-643/vendor/github.com/go-xmlpath/xmlpath/LICENSE
new file mode 100644
index 00000000000..4ccd784ba7b
--- /dev/null
+++ b/ql/test/query-tests/Security/CWE-643/vendor/github.com/go-xmlpath/xmlpath/LICENSE
@@ -0,0 +1,187 @@
+Copyright (c) 2013-2014 Canonical Inc.
+
+This software is licensed under the LGPLv3, included below.
+
+As a special exception to the GNU Lesser General Public License version 3
+("LGPL3"), the copyright holders of this Library give you permission to
+convey to a third party a Combined Work that links statically or dynamically
+to this Library without providing any Minimal Corresponding Source or
+Minimal Application Code as set out in 4d or providing the installation
+information set out in section 4e, provided that you comply with the other
+provisions of LGPL3 and provided that you meet, for the Application the
+terms and conditions of the license(s) which apply to the Application.
+
+Except as stated in this special exception, the provisions of LGPL3 will
+continue to comply in full to this Library. If you modify this Library, you
+may apply this exception to your version of this Library, but you are not
+obliged to do so. If you do not wish to do so, delete this exception
+statement from your version. This exception does not (and cannot) modify any
+license terms which apply to the Application, with which you must still
+comply.
+
+
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc.