-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSchemaUnsafeTestCase.java
More file actions
51 lines (40 loc) · 1.8 KB
/
SchemaUnsafeTestCase.java
File metadata and controls
51 lines (40 loc) · 1.8 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
51
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.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@WebServlet("/schemaunsafe")
public class SchemaUnsafeTestCase extends XXETestCase {
/*
* Schema: Unsafe by Default Example
* Proves that SchemaFactory parses entities by default
*/
protected void doTest(HttpServletRequest request, HttpServletResponse response) throws IOException {
final boolean expectedSafe = false;
// parsing the XML
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(getClass().getResource("/test.xsd"));
Validator validator = schema.newValidator();
StreamSource input = new StreamSource(new ByteArrayInputStream(request.getParameter("payload").getBytes()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamResult output = new StreamResult(baos);
validator.validate(input, output); // unsafe!
String result = new String(baos.toByteArray());
// testing the result
printResults(expectedSafe, result, response);
}
catch (Exception ex) {
printResults(expectedSafe, ex, response); // safe: exception thrown when parsing XML
}
}
}