Skip to content

Commit

Permalink
set transformer factory attributes to improve protection against XXE (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Mar 23, 2023
1 parent be7c8e1 commit dc717e9
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public DOMSerializer() {
try {
transformerFactory = TransformerFactory.newInstance();
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
setTransformerFactoryAttribute(transformerFactory, XMLConstants.ACCESS_EXTERNAL_DTD, "");
setTransformerFactoryAttribute(transformerFactory, XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
} catch (Exception e) {
throw new IllegalStateException("Could not instantiate `TransformerFactory`: "+e.getMessage(), e);
}
Expand Down Expand Up @@ -65,4 +67,13 @@ public JsonNode getSchema(SerializerProvider provider, java.lang.reflect.Type ty
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {
if (visitor != null) visitor.expectAnyFormat(typeHint);
}

private static void setTransformerFactoryAttribute(final TransformerFactory transformerFactory,
final String name, final Object value) {
try {
transformerFactory.setAttribute(name, value);
} catch (Exception e) {
System.err.println("[DOMSerializer] Failed to set TransformerFactory attribute: " + name);
}
}
}

0 comments on commit dc717e9

Please sign in to comment.