Skip to content

Commit 424e88d

Browse files
author
Porcupiney Hairs
committed
include sugestions from review
1 parent 8c5a971 commit 424e88d

File tree

13 files changed

+115
-57
lines changed

13 files changed

+115
-57
lines changed
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
22
<qhelp>
33
<overview>
4-
Using user-supplied information to construct an XPath query for XML data can
4+
<p>
5+
Using user-supplied information to construct an XPath query for XML data can
56
result in an XPath injection flaw. By sending intentionally malformed information,
6-
an attacker can access data that he may not normally have access to.
7-
He/She may even be able to elevate his privileges on the web site if the XML data
7+
an attacker can access data that he may not normally have access to.
8+
He/She may even be able to elevate his privileges on the web site if the XML data
89
is being used for authentication (such as an XML based user file).
10+
</p>
911
</overview>
1012
<recommendation>
1113
<p>
12-
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.
13-
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.
14+
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.
15+
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.
1416
</p>
1517
<p>
1618
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.
1719
</p>
18-
<example>
19-
20-
<p>In the example below, the xpath query is controlled by the user and hence leads to a vulnerability.</p>
21-
22-
<sample src="xpath.py" />
23-
</example>
24-
<references>
25-
<li>OWASP XPath injection : <a href="https://owasp.org/www-community/attacks/XPATH_Injection"></a>/>> </li>
26-
</references>
27-
28-
2920
</recommendation>
30-
31-
21+
<example>
22+
<p>In the example below, the xpath query is controlled by the user and hence leads to a vulnerability.</p>
23+
<sample src="xpathBad.py" />
24+
</example>
25+
<p> This can be fixed by using a parameterized query as shown below.</p>
26+
<sample src="xpathGood.py" />
27+
<references>
28+
<li>OWASP XPath injection : <a href="https://owasp.org/www-community/attacks/XPATH_Injection"></a>/>> </li>
29+
</references>
3230
</qhelp>

python/ql/src/experimental/CWE-643/xpath.py renamed to python/ql/src/experimental/CWE-643/xpathBad.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88

99
def a(request):
10-
xpathQuery = request.GET['xpath']
10+
value = request.GET['xpath']
1111
f = StringIO('<foo><bar></bar></foo>')
1212
tree = etree.parse(f)
13-
r = tree.xpath(xpathQuery)
13+
r = tree.xpath("/tag[@id='%s']" % value)
1414

1515

1616
urlpatterns = [
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from lxml import etree
2+
from io import StringIO
3+
4+
from django.urls import path
5+
from django.http import HttpResponse
6+
from django.template import Template, Context, Engine, engines
7+
8+
9+
def a(request):
10+
value = request.GET['xpath']
11+
f = StringIO('<foo><bar></bar></foo>')
12+
tree = etree.parse(f)
13+
r = tree.xpath("/tag[@id=$tagid]", tagid=value)
14+
15+
16+
urlpatterns = [
17+
path('a', a)
18+
]

python/ql/src/experimental/semmle/python/security/injection/Xpath.qll

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import python
10-
import semmle.python.security.TaintTracking
10+
import semmle.python.dataflow.TaintTracking
1111
import semmle.python.web.HttpRequest
1212

1313
/** Models Xpath Injection related classes and functions */
@@ -29,11 +29,7 @@ module XpathInjection {
2929
override string toString() { result = "lxml.etree.Xpath" }
3030

3131
EtreeXpathArgument() {
32-
exists(CallNode call, AttrNode atr |
33-
atr = etree().getAReference().getASuccessor() and
34-
atr.getName() = "XPath" and
35-
atr = call.getFunction()
36-
|
32+
exists(CallNode call | call.getFunction().(AttrNode).getObject("XPath").pointsTo(etree()) |
3733
call.getArg(0) = this
3834
)
3935
}
@@ -52,11 +48,7 @@ module XpathInjection {
5248
override string toString() { result = "lxml.etree.ETXpath" }
5349

5450
EtreeETXpathArgument() {
55-
exists(CallNode call, AttrNode atr |
56-
atr = etree().getAReference().getASuccessor() and
57-
atr.getName() = "ETXPath" and
58-
atr = call.getFunction()
59-
|
51+
exists(CallNode call | call.getFunction().(AttrNode).getObject("ETXPath").pointsTo(etree()) |
6052
call.getArg(0) = this
6153
)
6254
}
@@ -77,17 +69,15 @@ module XpathInjection {
7769
override string toString() { result = "lxml.etree.parse.xpath" }
7870

7971
ParseXpathArgument() {
80-
exists(CallNode parseCall, AttrNode parse, string s |
81-
parse = etree().getAReference().getASuccessor() and
82-
parse.getName() = "parse" and
83-
parse = parseCall.getFunction() and
84-
exists(CallNode xpathCall, AttrNode xpath |
85-
xpath = parseCall.getASuccessor*() and
86-
xpath.getName() = "xpath" and
87-
xpath = xpathCall.getFunction() and
88-
s = xpath.getName() and
89-
this = xpathCall.getArg(0)
90-
)
72+
exists(
73+
CallNode parseCall, CallNode xpathCall, ControlFlowNode obj, Variable var, AssignStmt assign
74+
|
75+
parseCall.getFunction().(AttrNode).getObject("parse").pointsTo(etree()) and
76+
assign.getValue().(Call).getAFlowNode() = parseCall and
77+
xpathCall.getFunction().(AttrNode).getObject("xpath") = obj and
78+
var.getAUse() = obj and
79+
assign.getATarget() = var.getAStore() and
80+
xpathCall.getArg(0) = this
9181
)
9282
}
9383

python/ql/test/experimental/CWE-643/XpathLibTests/options

Lines changed: 0 additions & 1 deletion
This file was deleted.

python/ql/test/experimental/CWE-643/XpathLibTests/xpathSinks.expected

Lines changed: 0 additions & 4 deletions
This file was deleted.

python/ql/test/experimental/CWE-643/xpath.expected

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
edges
2+
| xpathBad.py:9:7:9:13 | django.request.HttpRequest | xpathBad.py:10:13:10:19 | django.request.HttpRequest |
3+
| xpathBad.py:9:7:9:13 | django.request.HttpRequest | xpathBad.py:10:13:10:19 | django.request.HttpRequest |
4+
| xpathBad.py:10:13:10:19 | django.request.HttpRequest | xpathBad.py:10:13:10:23 | django.http.request.QueryDict |
5+
| xpathBad.py:10:13:10:19 | django.request.HttpRequest | xpathBad.py:10:13:10:23 | django.http.request.QueryDict |
6+
| xpathBad.py:10:13:10:23 | django.http.request.QueryDict | xpathBad.py:10:13:10:32 | externally controlled string |
7+
| xpathBad.py:10:13:10:23 | django.http.request.QueryDict | xpathBad.py:10:13:10:32 | externally controlled string |
8+
| xpathBad.py:10:13:10:32 | externally controlled string | xpathBad.py:13:39:13:43 | externally controlled string |
9+
| xpathBad.py:10:13:10:32 | externally controlled string | xpathBad.py:13:39:13:43 | externally controlled string |
10+
| xpathBad.py:13:39:13:43 | externally controlled string | xpathBad.py:13:20:13:43 | externally controlled string |
11+
| xpathBad.py:13:39:13:43 | externally controlled string | xpathBad.py:13:20:13:43 | externally controlled string |
212
| xpathFlow.py:10:18:10:29 | dict of externally controlled string | xpathFlow.py:10:18:10:44 | externally controlled string |
313
| xpathFlow.py:10:18:10:29 | dict of externally controlled string | xpathFlow.py:10:18:10:44 | externally controlled string |
414
| xpathFlow.py:10:18:10:44 | externally controlled string | xpathFlow.py:13:20:13:29 | externally controlled string |
@@ -13,10 +23,11 @@ edges
1323
| xpathFlow.py:27:18:27:44 | externally controlled string | xpathFlow.py:29:29:29:38 | externally controlled string |
1424
| xpathFlow.py:35:18:35:29 | dict of externally controlled string | xpathFlow.py:35:18:35:44 | externally controlled string |
1525
| xpathFlow.py:35:18:35:29 | dict of externally controlled string | xpathFlow.py:35:18:35:44 | externally controlled string |
16-
| xpathFlow.py:35:18:35:44 | externally controlled string | xpathFlow.py:37:38:37:47 | externally controlled string |
17-
| xpathFlow.py:35:18:35:44 | externally controlled string | xpathFlow.py:37:38:37:47 | externally controlled string |
26+
| xpathFlow.py:35:18:35:44 | externally controlled string | xpathFlow.py:37:31:37:40 | externally controlled string |
27+
| xpathFlow.py:35:18:35:44 | externally controlled string | xpathFlow.py:37:31:37:40 | externally controlled string |
1828
#select
29+
| xpathBad.py:13:20:13:43 | BinaryExpr | xpathBad.py:9:7:9:13 | django.request.HttpRequest | xpathBad.py:13:20:13:43 | externally controlled string | This Xpath query depends on $@. | xpathBad.py:9:7:9:13 | request | a user-provided value |
1930
| xpathFlow.py:13:20:13:29 | xpathQuery | xpathFlow.py:10:18:10:29 | dict of externally controlled string | xpathFlow.py:13:20:13:29 | externally controlled string | This Xpath query depends on $@. | xpathFlow.py:10:18:10:29 | Attribute | a user-provided value |
2031
| xpathFlow.py:21:29:21:38 | xpathQuery | xpathFlow.py:18:18:18:29 | dict of externally controlled string | xpathFlow.py:21:29:21:38 | externally controlled string | This Xpath query depends on $@. | xpathFlow.py:18:18:18:29 | Attribute | a user-provided value |
2132
| xpathFlow.py:29:29:29:38 | xpathQuery | xpathFlow.py:27:18:27:29 | dict of externally controlled string | xpathFlow.py:29:29:29:38 | externally controlled string | This Xpath query depends on $@. | xpathFlow.py:27:18:27:29 | Attribute | a user-provided value |
22-
| xpathFlow.py:37:38:37:47 | xpathQuery | xpathFlow.py:35:18:35:29 | dict of externally controlled string | xpathFlow.py:37:38:37:47 | externally controlled string | This Xpath query depends on $@. | xpathFlow.py:35:18:35:29 | Attribute | a user-provided value |
33+
| xpathFlow.py:37:31:37:40 | xpathQuery | xpathFlow.py:35:18:35:29 | dict of externally controlled string | xpathFlow.py:37:31:37:40 | externally controlled string | This Xpath query depends on $@. | xpathFlow.py:35:18:35:29 | Attribute | a user-provided value |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from lxml import etree
2+
from io import StringIO
3+
4+
from django.urls import path
5+
from django.http import HttpResponse
6+
from django.template import Template, Context, Engine, engines
7+
8+
9+
def a(request):
10+
value = request.GET['xpath']
11+
f = StringIO('<foo><bar></bar></foo>')
12+
tree = etree.parse(f)
13+
r = tree.xpath("/tag[@id='%s']" % value)
14+
15+
16+
urlpatterns = [
17+
path('a', a)
18+
]

python/ql/test/experimental/CWE-643/xpathFlow.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77

88
@app.route("/xpath1")
9-
def a():
9+
def xpath1():
1010
xpathQuery = request.args.get('xml', '')
1111
f = StringIO('<foo><bar></bar></foo>')
1212
tree = etree.parse(f)
1313
r = tree.xpath(xpathQuery)
1414

1515

1616
@app.route("/xpath2")
17-
def b():
17+
def xpath2():
1818
xpathQuery = request.args.get('xml', '')
1919

2020
root = etree.XML("<root><a>TEXT</a></root>")
@@ -23,16 +23,16 @@ def b():
2323

2424

2525
@app.route("/xpath3")
26-
def c():
26+
def xpath3():
2727
xpathQuery = request.args.get('xml', '')
2828
root = etree.XML("<root><a>TEXT</a></root>")
2929
find_text = etree.XPath(xpathQuery, smart_strings=False)
3030
text = find_text(root)[0]
3131

3232

3333
@app.route("/xpath4")
34-
def d():
34+
def xpath4():
3535
xpathQuery = request.args.get('xml', '')
3636
root = etree.XML("<root><a>TEXT</a></root>")
37-
find_text = find = etree.ETXPath(xpathQuery)
38-
text = find_text(root)[0]
37+
find_text = etree.ETXPath(xpathQuery)
38+
text = find_text(root)[0]

0 commit comments

Comments
 (0)