-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathXMLInput2SafeDTDTestCase.java
More file actions
50 lines (41 loc) · 1.85 KB
/
XMLInput2SafeDTDTestCase.java
File metadata and controls
50 lines (41 loc) · 1.85 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 org.codehaus.stax2.XMLInputFactory2;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@WebServlet("/xmlinput2safedtd")
public class XMLInput2SafeDTDTestCase extends XXETestCase {
/*
* XMLInputFactory2: Safe when Disabling DTD Support Example
* Proves that disabling DTD support for the XMLInputFactory2 makes the XMLStreamReader throw an exception
* when it sees an external entity reference
*/
protected void doTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
final boolean expectedSafe = true;
// parsing the XML
try {
XMLInputFactory xmlInputFactory = XMLInputFactory2.newInstance();
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
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
}
}
}