-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathXMLInputSafeAccessTestCase.java
More file actions
50 lines (41 loc) · 1.91 KB
/
XMLInputSafeAccessTestCase.java
File metadata and controls
50 lines (41 loc) · 1.91 KB
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
package com.aspectsecurity.unittestsweb.xxetestcases;
import com.aspectsecurity.unittestsweb.XXETestCase;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.XMLConstants;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@WebServlet("/xmlinputsafeaccess")
public class XMLInputSafeAccessTestCase extends XXETestCase {
/*
* XMLInputFactory: Safe when Disallowing External DTDs in Java 7u40 and up Example
* Proves that setting XMLInputFactory's ACCESS_EXTERNAL_DTD attribute to null makes the XMLStreamReader
* throw an exception when it sees an external entity reference (Java 7u40 and up only feature)
*/
protected void doTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
final boolean expectedSafe = true;
// parsing the XML
try {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(request.getParameter("payload").getBytes()));
String result = "";
while(xmlStreamReader.hasNext()) {
xmlStreamReader.next();
if (xmlStreamReader.isStartElement()) {
if (xmlStreamReader.getLocalName().equals("test")) {
result = xmlStreamReader.getElementText();
}
}
}
// testing the result
printResults(expectedSafe, result, response);
}
catch (Exception ex) {
printResults(expectedSafe, ex, response); // safe: exception thrown when parsing XML
}
}
}