-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSaxonUnsafeConcatXQueryTestCase.java
53 lines (43 loc) · 2.01 KB
/
SaxonUnsafeConcatXQueryTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.aspectsecurity.unittestsweb.xquerytestcases;
import com.aspectsecurity.unittestsweb.XQueryTestCase;
import net.sf.saxon.s9api.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/saxonunsafeconcatxquery")
public class SaxonUnsafeConcatXQueryTestCase extends XQueryTestCase {
/*
* Saxon: Unsafe when Using String Concatenation on XQuery Expression Example
* Proves that Saxon is vulnerable to injection when using string concatenation on the XQuery expression
*/
protected void doTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
final boolean expectedSafe = false;
try {
// parsing the XML
Processor processor = new Processor(false);
DocumentBuilder documentBuilder = processor.newDocumentBuilder();
XdmNode node = documentBuilder.build(new StreamSource(getClass().getResourceAsStream("/students.xml")));
// querying the XML
String query = "for $s in //Students/Student " +
"where $s/FirstName = \"" + request.getParameter("payload") + "\" " +
"return $s"; // unsafe!
XQueryCompiler xqComp = processor.newXQueryCompiler();
XQueryExecutable xqExec = xqComp.compile(query);
XQueryEvaluator xqEval = xqExec.load();
xqEval.setContextItem(node);
xqEval.evaluate();
// interpret the result of the query
ArrayList<String> resultList = new ArrayList<String>();
for (XdmValue value : xqEval) {
resultList.add(value.toString());
}
// testing the result
printResults(expectedSafe, resultList, response);
} catch (Exception ex) {
response.getWriter().write(ex.toString());
}
}
}