Skip to content

Commit 7f0181c

Browse files
committed
C#: Add XPathNavigator test for cs/xml/xpath-injection
1 parent 2821b01 commit 7f0181c

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

csharp/ql/test/query-tests/Security Features/CWE-643/XPathInjection.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs /r:System.Collections.Specialized.dll ${testdir}/../../../resources/stubs/System.Data.cs /r:System.Private.Xml.dll /r:System.Xml.XPath.dll /r:System.Data.Common.dll
1+
// semmle-extractor-options: ${testdir}/../../../resources/stubs/System.Web.cs /r:System.Collections.Specialized.dll ${testdir}/../../../resources/stubs/System.Data.cs /r:System.Private.Xml.dll /r:System.Xml.XPath.dll /r:System.Data.Common.dll /r:System.Runtime.Extensions.dll
22

33
using System;
44
using System.Web;
@@ -19,7 +19,22 @@ public void ProcessRequest(HttpContext ctx)
1919
xmlNode.SelectNodes("//users/user[login/text()='" + userName + "' and password/text() = '" + password + "']/home_dir/text()");
2020

2121
// GOOD: Uses parameters to avoid including user input directly in XPath expression
22-
XPathExpression.Compile("//users/user[login/text()=$username]/home_dir/text()");
22+
var expr = XPathExpression.Compile("//users/user[login/text()=$username]/home_dir/text()");
23+
24+
var doc = new XPathDocument("");
25+
var nav = doc.CreateNavigator();
26+
27+
// BAD
28+
nav.Select("//users/user[login/text()='" + userName + "' and password/text() = '" + password + "']/home_dir/text()");
29+
30+
// BAD
31+
nav.SelectSingleNode("//users/user[login/text()='" + userName + "' and password/text() = '" + password + "']/home_dir/text()");
32+
33+
// GOOD
34+
nav.Select(expr);
35+
36+
// GOOD
37+
nav.SelectSingleNode(expr);
2338
}
2439

2540
public bool IsReusable

0 commit comments

Comments
 (0)