-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathXMLInputUnsafeValidationOffTestCase.java
More file actions
48 lines (39 loc) · 1.78 KB
/
XMLInputUnsafeValidationOffTestCase.java
File metadata and controls
48 lines (39 loc) · 1.78 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
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.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@WebServlet("/xmlinputunsafevalidationoff")
public class XMLInputUnsafeValidationOffTestCase extends XXETestCase {
/*
* XMLInputFactory: Unsafe when Disabling Validation Example
* Proves that disabling validation for the XMLInputFactory leaves the XMLStreamReader parsing entities
*/
protected void doTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
final boolean expectedSafe = false;
// parsing the XML
try {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
xmlInputFactory.setProperty("javax.xml.stream.isValidating", "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
}
}
}