From 48f892bf4468451df9d50edad753062f61791323 Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Fri, 30 Sep 2022 04:19:14 +0200 Subject: [PATCH] [#34] Make `setFeature` calls optional IMHO a missing `setFeature()` method or support for disabling external entities should not cause a parsing error, just warnings. Signed-off-by: Piotr P. Karwasz --- .../org/apache/log4j/xml/DOMConfigurator.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/log4j/xml/DOMConfigurator.java b/src/main/java/org/apache/log4j/xml/DOMConfigurator.java index 8c72e93f..9d0b21a5 100644 --- a/src/main/java/org/apache/log4j/xml/DOMConfigurator.java +++ b/src/main/java/org/apache/log4j/xml/DOMConfigurator.java @@ -788,6 +788,16 @@ public String toString() { doConfigure(action, repository); } + private final void setFeature(final DocumentBuilderFactory dbf, final String feature, final boolean value) { + try { + dbf.setFeature(feature, value); + } catch (Exception e) { + LogLog.warn("Failed to set DocumentBuilderFactory feature " + feature + ".", e); + } catch (AbstractMethodError e) { + LogLog.warn("Failed to set DocumentBuilderFactory feature " + feature + ". Missing DocumentBuilderFactory.setFeature() method?", e); + } + } + private final void doConfigure(final ParseAction action, final LoggerRepository repository) throws FactoryConfigurationError { DocumentBuilderFactory dbf = null; @@ -806,8 +816,8 @@ private final void doConfigure(final ParseAction action, final LoggerRepository try { dbf.setValidating(true); // prevent XXE attacks - dbf.setFeature("http://xml.org/sax/features/external-general-entities", false); - dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + setFeature(dbf, "http://xml.org/sax/features/external-general-entities", false); + setFeature(dbf, "http://xml.org/sax/features/external-parameter-entities", false); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); @@ -821,9 +831,6 @@ private final void doConfigure(final ParseAction action, final LoggerRepository Thread.currentThread().interrupt(); } LogLog.error("Could not parse " + action.toString() + ".", e); - } catch (AbstractMethodError e) { - LogLog.error("Failed to parse XML file. Missing DocumentBuilderFactory.setFeature() method?", e); - throw e; } }