+
+
+ Using user-supplied information to construct an XPath query for XML data can
+ result in an XPath injection flaw. By sending intentionally malformed information,
+ an attacker can access data that he may not normally have access to.
+ He/She may even be able to elevate his privileges on the web site if the XML data
+ is being used for authentication (such as an XML based user file).
+
+
+
+
+ XPath injection can be prevented using parameterized XPath interface or escaping the user input to make it safe to include in a dynamically constructed query.
+ If you are using quotes to terminate untrusted input in a dynamically constructed XPath query, then you need to escape that quote in the untrusted input to ensure the untrusted data can’t try to break out of that quoted context.
+
+
+ Another better mitigation option is to use a precompiled XPath query. Precompiled XPath queries are already preset before the program executes, rather than created on the fly after the user’s input has been added to the string. This is a better route because you don’t have to worry about missing a character that should have been escaped.
+
+
+
+ In the example below, the xpath query is controlled by the user and hence leads to a vulnerability.
+
+ This can be fixed by using a parameterized query as shown below.
+
+
+
+ OWASP XPath injection : />>
+
+
diff --git a/python/ql/src/experimental/CWE-643/xpath.ql b/python/ql/src/experimental/CWE-643/xpath.ql
new file mode 100644
index 000000000000..fbdf57d4f1ac
--- /dev/null
+++ b/python/ql/src/experimental/CWE-643/xpath.ql
@@ -0,0 +1,35 @@
+/**
+ * @name XPath query built from user-controlled sources
+ * @description Building a XPath query from user-controlled sources is vulnerable to insertion of
+ * malicious Xpath code by the user.
+ * @kind path-problem
+ * @problem.severity error
+ * @precision high
+ * @id py/xpath-injection
+ * @tags security
+ * external/cwe/cwe-643
+ */
+
+import python
+import semmle.python.security.Paths
+/* Sources */
+import semmle.python.web.HttpRequest
+/* Sinks */
+import experimental.semmle.python.security.injection.Xpath
+
+class XpathInjectionConfiguration extends TaintTracking::Configuration {
+ XpathInjectionConfiguration() { this = "Xpath injection configuration" }
+
+ override predicate isSource(TaintTracking::Source source) {
+ source instanceof HttpRequestTaintSource
+ }
+
+ override predicate isSink(TaintTracking::Sink sink) {
+ sink instanceof XpathInjection::XpathInjectionSink
+ }
+}
+
+from XpathInjectionConfiguration config, TaintedPathSource src, TaintedPathSink sink
+where config.hasFlowPath(src, sink)
+select sink.getSink(), src, sink, "This Xpath query depends on $@.", src.getSource(),
+ "a user-provided value"
diff --git a/python/ql/src/experimental/CWE-643/xpathBad.py b/python/ql/src/experimental/CWE-643/xpathBad.py
new file mode 100644
index 000000000000..ee836dd385eb
--- /dev/null
+++ b/python/ql/src/experimental/CWE-643/xpathBad.py
@@ -0,0 +1,18 @@
+from lxml import etree
+from io import StringIO
+
+from django.urls import path
+from django.http import HttpResponse
+from django.template import Template, Context, Engine, engines
+
+
+def a(request):
+ value = request.GET['xpath']
+ f = StringIO('