-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathXalanSafeListTestCase.java
52 lines (43 loc) · 1.98 KB
/
XalanSafeListTestCase.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
package com.aspectsecurity.unittestsweb.xpathtestcases;
import com.aspectsecurity.unittestsweb.InvalidParameterException;
import com.aspectsecurity.unittestsweb.XPathTestCase;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/xalansafelist")
public class XalanSafeListTestCase extends XPathTestCase {
/*
* Xalan: Safe when Whitelisting on XPath Expression Example
* Proves that Xalan is safe from injection when whitelisting on the XPath expression
*/
protected void doTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
final boolean expectedSafe = true;
try {
//parsing the XML
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(getClass().getResourceAsStream("/students.xml"));
// querying the XML
String query;
if (request.getParameter("payload").contains("'")) {
printResults(expectedSafe, new ArrayList<String>(), response);
throw new InvalidParameterException("First Name parameter must not contain apostrophes");
}
else {
query = "/Students/Student[FirstName/text()='" + request.getParameter("payload") + "']"; // safe in here!
}
NodeList nodeList = XPathAPI.eval(document, query).nodelist();
// testing the result
printResults(expectedSafe, nodeList, response);
} catch (Exception ex) {
response.getWriter().write(ex.toString());
}
}
}