Skip to content

Commit cd2b665

Browse files
authored
Fix XXE Vulnerability in Schema Loading
This PR addresses a critical security vulnerability in the [loadSchema] method related to XML External Entity (XXE) attacks. By restricting access to external DTDs and schemas, we prevent potential server-side exploits. This vulnerability was also found in 3dcitydb/importer-exporter@8ab7fb6, corresponding to CVE-2018-10054 and fixed. References: 1. https://nvd.nist.gov/vuln/detail/cve-2018-10054 2. 3dcitydb/importer-exporter@8ab7fb6
1 parent d30d626 commit cd2b665

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

engine/src/main/java/org/hibernate/validator/internal/xml/XmlParserHelper.java

+22-20
Original file line numberDiff line numberDiff line change
@@ -138,26 +138,28 @@ public Schema getSchema(String schemaResource) {
138138
}
139139

140140
private Schema loadSchema(String schemaResource) {
141-
ClassLoader loader = GetClassLoader.fromClass( XmlParserHelper.class );
142-
143-
URL schemaUrl = GetResource.action( loader, schemaResource );
144-
SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI );
145-
146-
try {
147-
sf.setFeature( javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true );
148-
}
149-
catch (SAXException e) {
150-
LOG.unableToEnableSecureFeatureProcessingSchemaXml( schemaResource, e.getMessage() );
151-
}
152-
153-
Schema schema = null;
154-
try {
155-
schema = NewSchema.action( sf, schemaUrl );
156-
}
157-
catch (Exception e) {
158-
LOG.unableToCreateSchema( schemaResource, e.getMessage() );
159-
}
160-
return schema;
141+
ClassLoader loader = GetClassLoader.fromClass(XmlParserHelper.class);
142+
143+
URL schemaUrl = GetResource.action(loader, schemaResource);
144+
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
145+
146+
// Security improvement: Restrict access to external DTDs and schemas
147+
try {
148+
sf.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
149+
sf.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
150+
} catch (SAXException e) {
151+
// Some older parsers might not support these properties
152+
LOG.debug("Unable to set external access restrictions on schema factory", e);
153+
}
154+
155+
Schema schema = null;
156+
try {
157+
schema = NewSchema.action(sf, schemaUrl);
158+
}
159+
catch (Exception e) {
160+
LOG.unableToCreateSchema(schemaResource, e.getMessage());
161+
}
162+
return schema;
161163
}
162164

163165
}

0 commit comments

Comments
 (0)