|
@@ -2,6 +2,7 @@ package egovframework.com.its.web.server.config;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.dom4j.io.SAXReader;
|
|
import org.dom4j.io.SAXReader;
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.xml.sax.SAXException;
|
|
import org.xml.sax.SAXException;
|
|
|
import org.xml.sax.SAXNotRecognizedException;
|
|
import org.xml.sax.SAXNotRecognizedException;
|
|
@@ -22,23 +23,16 @@ import javax.xml.validation.SchemaFactory;
|
|
|
public class XxeConfig {
|
|
public class XxeConfig {
|
|
|
|
|
|
|
|
private void documentBuilderFactory() {
|
|
private void documentBuilderFactory() {
|
|
|
- log.info("XxeConfig.documentBuilderFactory.");
|
|
|
|
|
|
|
+// log.info("XxeConfig.documentBuilderFactory.");
|
|
|
|
|
+
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
- //DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", null);
|
|
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- // to be compliant, completely disable DOCTYPE declaration:
|
|
|
|
|
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
|
- // or completely disable external entities declarations:
|
|
|
|
|
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
|
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
|
- // or prohibit the use of all protocols by external entities:
|
|
|
|
|
- //factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
|
|
|
|
|
- //factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
|
|
|
|
|
- // or disable entity expansion but keep in mind that this doesn't prevent fetching external entities
|
|
|
|
|
- // and this solution is not correct for OpenJDK < 13 due to a bug: https://bugs.openjdk.java.net/browse/JDK-8206132
|
|
|
|
|
|
|
+ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
|
|
factory.setExpandEntityReferences(false);
|
|
factory.setExpandEntityReferences(false);
|
|
|
- // and these as well, per Timothy Morgan's 2014 paper: "XML Schema, DTD, and Entity Attacks"
|
|
|
|
|
factory.setXIncludeAware(false);
|
|
factory.setXIncludeAware(false);
|
|
|
} catch (ParserConfigurationException e) {
|
|
} catch (ParserConfigurationException e) {
|
|
|
log.error("XxeConfig.documentBuilderFactory: ParserConfigurationException.");
|
|
log.error("XxeConfig.documentBuilderFactory: ParserConfigurationException.");
|
|
@@ -46,7 +40,7 @@ public class XxeConfig {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void saxParserFactory() {
|
|
private void saxParserFactory() {
|
|
|
- log.info("XxeConfig.saxParserFactory.");
|
|
|
|
|
|
|
+// log.info("XxeConfig.saxParserFactory.");
|
|
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
|
try {
|
|
try {
|
|
|
// to be compliant, completely disable DOCTYPE declaration:
|
|
// to be compliant, completely disable DOCTYPE declaration:
|
|
@@ -54,6 +48,7 @@ public class XxeConfig {
|
|
|
// or completely disable external entities declarations:
|
|
// or completely disable external entities declarations:
|
|
|
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
|
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
|
|
|
+ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
|
|
// or prohibit the use of all protocols by external entities:
|
|
// or prohibit the use of all protocols by external entities:
|
|
|
SAXParser parser = null; // Noncompliant
|
|
SAXParser parser = null; // Noncompliant
|
|
|
parser = factory.newSAXParser();
|
|
parser = factory.newSAXParser();
|
|
@@ -71,7 +66,7 @@ public class XxeConfig {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void xmlInputFactory() {
|
|
private void xmlInputFactory() {
|
|
|
- log.info("XxeConfig.xmlInputFactory.");
|
|
|
|
|
|
|
+// log.info("XxeConfig.xmlInputFactory.");
|
|
|
XMLInputFactory factory = XMLInputFactory.newInstance();
|
|
XMLInputFactory factory = XMLInputFactory.newInstance();
|
|
|
// to be compliant, completely disable DOCTYPE declaration:
|
|
// to be compliant, completely disable DOCTYPE declaration:
|
|
|
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
|
|
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
|
|
@@ -84,7 +79,7 @@ public class XxeConfig {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void transformerFactory() {
|
|
private void transformerFactory() {
|
|
|
- log.info("XxeConfig.transformerFactory.");
|
|
|
|
|
|
|
+// log.info("XxeConfig.transformerFactory.");
|
|
|
TransformerFactory factory = TransformerFactory.newInstance();
|
|
TransformerFactory factory = TransformerFactory.newInstance();
|
|
|
// to be compliant, prohibit the use of all protocols by external entities:
|
|
// to be compliant, prohibit the use of all protocols by external entities:
|
|
|
//factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
|
|
//factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
|
|
@@ -92,7 +87,7 @@ public class XxeConfig {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void schemaFactory() {
|
|
private void schemaFactory() {
|
|
|
- log.info("XxeConfig.schemaFactory.");
|
|
|
|
|
|
|
+// log.info("XxeConfig.schemaFactory.");
|
|
|
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
|
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
|
|
// to be compliant, completely disable DOCTYPE declaration:
|
|
// to be compliant, completely disable DOCTYPE declaration:
|
|
|
try {
|
|
try {
|
|
@@ -108,7 +103,7 @@ public class XxeConfig {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void saxReader() {
|
|
private void saxReader() {
|
|
|
- log.info("XxeConfig.saxReader.");
|
|
|
|
|
|
|
+// log.info("XxeConfig.saxReader.");
|
|
|
SAXReader xmlReader = new SAXReader();
|
|
SAXReader xmlReader = new SAXReader();
|
|
|
try {
|
|
try {
|
|
|
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
|
@@ -167,7 +162,7 @@ public class XxeConfig {
|
|
|
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
|
|
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
|
|
|
|
|
|
|
|
documentBuilderFactory();
|
|
documentBuilderFactory();
|
|
|
- //saxParserFactory();
|
|
|
|
|
|
|
+ saxParserFactory();
|
|
|
xmlInputFactory();
|
|
xmlInputFactory();
|
|
|
transformerFactory();
|
|
transformerFactory();
|
|
|
//schemaFactory();
|
|
//schemaFactory();
|