CVE-2020-36640: Improper Restriction of XML External Entity Reference (CWE-611)
value: A vulnerability, which was classified as problematic, was found in bonitasoft bonita-connector-webservice up to 1.3.0. This affects the function TransformerConfigurationException of the file src/main/java/org/bonitasoft/connectors/ws/SecureWSConnector.java. The manipulation leads to xml external entity reference. Upgrading to version 1.3.1 is able to address this issue. The patch is named a12ad691c05af19e9061d7949b6b828ce48815d5. It is recommended to upgrade the affected component. The associated identifier of this vulnerability is VDB-217443.
CWE description: The product processes an XML document that can contain XML entities with URIs that resolve to documents outside of the intended sphere of control, causing the product to embed incorrect documents into its output.
CVSS v3 base score: 9.8
Published: 2023-01-05T10:15Z
Repository: https://github.com/bonitasoft/bonita-connector-webservice
Language: Java
Files changed: src/main/java/org/bonitasoft/connectors/ws/SecureWSConnector.java
Commit date: 2020-10-12 14:22:59 +0200
Commit message: fix(vulnerabilities): fix XXE attacks vulnerabilities and other code smell (#17)

* Access to external entities and network access should always be
disable to avoid XXS attacks vulnerabilities.
* Log error properly
* refactor logger name to be compliant with java naming conventions
Diff: diff --git a/src/main/java/org/bonitasoft/connectors/ws/SecureWSConnector.java b/src/main/java/org/bonitasoft/connectors/ws/SecureWSConnector.java
index 7215800..1fc63ac 100644
--- a/src/main/java/org/bonitasoft/connectors/ws/SecureWSConnector.java
+++ b/src/main/java/org/bonitasoft/connectors/ws/SecureWSConnector.java
@@ -27,10 +27,11 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.logging.Logger;
 
+import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Source;
@@ -107,11 +108,11 @@ public class SecureWSConnector extends AbstractConnector {
 
     private static final String PROXY_USER = "proxyUser";
 
     private static final String PROXY_PASSWORD = "proxyPassword";
 
-    private final Logger LOGGER = Logger.getLogger(this.getClass().getName());
+    private final Logger logger = Logger.getLogger(this.getClass().getName());
 
     private Transformer transformer;
 
     private LookupTranslator lookupTranslator;
 
@@ -179,43 +180,42 @@ public class SecureWSConnector extends AbstractConnector {
         if (binding == null) {
             throw new ConnectorValidationException("binding is required");
         }
     }
 
-    @SuppressWarnings("unchecked")
     @Override
     protected void executeBusinessLogic() throws ConnectorException {
         configureProxy();
         final String serviceNS = (String) getInputParameter(SERVICE_NS);
-        LOGGER.info(SERVICE_NS + " " + serviceNS);
+        logger.info(SERVICE_NS + " " + serviceNS);
         final String serviceName = (String) getInputParameter(SERVICE_NAME);
-        LOGGER.info(SERVICE_NAME + " " + serviceName);
+        logger.info(SERVICE_NAME + " " + serviceName);
         final String portName = (String) getInputParameter(PORT_NAME);
-        LOGGER.info(PORT_NAME + " " + portName);
+        logger.info(PORT_NAME + " " + portName);
         final String binding = (String) getInputParameter(BINDING);
-        LOGGER.info(BINDING + " " + binding);
+        logger.info(BINDING + " " + binding);
         final String endpointAddress = (String) getInputParameter(ENDPOINT_ADDRESS);
-        LOGGER.info(ENDPOINT_ADDRESS + " " + endpointAddress);
+        logger.info(ENDPOINT_ADDRESS + " " + endpointAddress);
 
         final QName serviceQName = new QName(serviceNS, serviceName);
         final QName portQName = new QName(serviceNS, portName);
         final Service service = Service.create(serviceQName);
         service.addPort(portQName, binding, endpointAddress);
 
         final Dispatch<Source> dispatch = service.createDispatch(portQName, Source.class, Service.Mode.MESSAGE);
         dispatch.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointAddress);
         final Object authUserName = getInputParameter(USER_NAME);
         if (authUserName != null) {
-            LOGGER.info(USER_NAME + " " + authUserName);
+            logger.info(USER_NAME + " " + authUserName);
             dispatch.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, authUserName);
             final Object authPassword = getInputParameter(PASSWORD);
-            LOGGER.info(PASSWORD + " ********");
+            logger.info(PASSWORD + " ********");
             dispatch.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, authPassword);
         }
 
         final String soapAction = (String) getInputParameter(SOAP_ACTION);
-        LOGGER.info(SOAP_ACTION + " " + soapAction);
+        logger.info(SOAP_ACTION + " " + soapAction);
 
         if (soapAction != null) {
             dispatch.getRequestContext().put(BindingProvider.SOAPACTION_USE_PROPERTY, true);
             dispatch.getRequestContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, soapAction);
         }
@@ -243,13 +243,13 @@ public class SecureWSConnector extends AbstractConnector {
         }
 
         String initialEnvelope = (String) getInputParameter(ENVELOPE);
         String sanitizedEnvelope = sanitizeString(initialEnvelope);
         if (!Objects.equals(initialEnvelope, sanitizedEnvelope)) {
-            LOGGER.warning("Invalid XML characters have been detected in the envelope, they will be removed.");
+            logger.warning("Invalid XML characters have been detected in the envelope, they will be removed.");
         }
-        LOGGER.info(ENVELOPE + " " + sanitizedEnvelope);
+        logger.info(ENVELOPE + " " + sanitizedEnvelope);
 
         Boolean oneWayInvoke = (Boolean) getInputParameter(ONE_WAY_INVOKE);
         if (oneWayInvoke == null) {
             oneWayInvoke = false;
         }
@@ -266,13 +266,13 @@ public class SecureWSConnector extends AbstractConnector {
         }
 
         restoreConfiguration();
 
         Boolean buildResponseDocumentEnvelope = (Boolean) getInputParameter(BUILD_RESPONSE_DOCUMENT_ENVELOPE);
-        LOGGER.info(BUILD_RESPONSE_DOCUMENT_ENVELOPE + " " + buildResponseDocumentEnvelope);
+        logger.info(BUILD_RESPONSE_DOCUMENT_ENVELOPE + " " + buildResponseDocumentEnvelope);
         Boolean buildResponseDocumentBody = (Boolean) getInputParameter(BUILD_RESPONSE_DOCUMENT_BODY);
-        LOGGER.info(BUILD_RESPONSE_DOCUMENT_BODY + " " + buildResponseDocumentBody);
+        logger.info(BUILD_RESPONSE_DOCUMENT_BODY + " " + buildResponseDocumentBody);
         if (buildResponseDocumentEnvelope == null) {
             buildResponseDocumentEnvelope = false;
         }
         if (buildResponseDocumentBody == null) {
             buildResponseDocumentBody = false;
@@ -286,11 +286,11 @@ public class SecureWSConnector extends AbstractConnector {
         if (buildResponseDocumentBody) {
             responseDocumentBody = buildResponseDocumentBody(responseDocumentEnvelope);
         }
 
         Boolean printRequestAndResponse = (Boolean) getInputParameter(PRINT_REQUEST_AND_RESPONSE);
-        LOGGER.info(PRINT_REQUEST_AND_RESPONSE + " " + printRequestAndResponse);
+        logger.info(PRINT_REQUEST_AND_RESPONSE + " " + printRequestAndResponse);
         if (printRequestAndResponse == null) {
             printRequestAndResponse = false;
         }
         if (printRequestAndResponse) {
             printRequestAndResponse(sourceResponse, buildResponseDocumentEnvelope, buildResponseDocumentBody,
@@ -321,34 +321,34 @@ public class SecureWSConnector extends AbstractConnector {
         saveProxyConfiguration = saveProxyConfiguration();
         final String host = (String) getInputParameter(PROXY_HOST);
         if (host == null || host.isEmpty()) {
             return;
         }
-        LOGGER.info(PROXY_HOST + " " + host);
+        logger.info(PROXY_HOST + " " + host);
         final String protocol = (String) getInputParameter(PROXY_PROTOCOL);
-        LOGGER.info(PROXY_PROTOCOL + " " + protocol);
+        logger.info(PROXY_PROTOCOL + " " + protocol);
         final String port = (String) getInputParameter(PROXY_PORT);
-        LOGGER.info(PROXY_PORT + " " + port);
+        logger.info(PROXY_PORT + " " + port);
 
         if (SOCKS.equals(protocol)) {
             System.setProperty("socksProxyHost", host);
-            LOGGER.info("Setting environment variable: socksProxyHost=" + host);
+            logger.info("Setting environment variable: socksProxyHost=" + host);
             System.setProperty("socksProxyPort", port);
-            LOGGER.info("Setting environment variable: socksProxyPort=" + port);
+            logger.info("Setting environment variable: socksProxyPort=" + port);
         } else {
             final String hostKey = String.format("%s.proxyHost", protocol.toLowerCase());
             System.setProperty(hostKey, host);
-            LOGGER.info("Setting environment variable: " + hostKey + "=" + host);
+            logger.info("Setting environment variable: " + hostKey + "=" + host);
             final String portKey = String.format("%s.proxyPort", protocol.toLowerCase());
             System.setProperty(portKey, port);
-            LOGGER.info("Setting environment variable: " + portKey + "=" + port);
+            logger.info("Setting environment variable: " + portKey + "=" + port);
         }
 
         final String user = (String) getInputParameter(PROXY_USER);
-        LOGGER.info(PROXY_USER + " " + user);
+        logger.info(PROXY_USER + " " + user);
         final String password = (String) getInputParameter(PROXY_PASSWORD);
-        LOGGER.info(PROXY_PASSWORD + " ********");
+        logger.info(PROXY_PASSWORD + " ********");
         if (user != null && !user.isEmpty()) {
             Authenticator.setDefault(new Authenticator() {
 
                 @Override
                 protected PasswordAuthentication getPasswordAuthentication() {
@@ -390,11 +390,14 @@ public class SecureWSConnector extends AbstractConnector {
 
     private Document buildResponseDocumentBody(Document responseDocumentEnvelope) throws ConnectorException {
         Document responseDocumentBody = null;
         if (responseDocumentEnvelope != null) {
             try {
-                responseDocumentBody = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
+                documentBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
+                documentBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
+                responseDocumentBody = documentBuilderFactory.newDocumentBuilder().newDocument();
             } catch (final ParserConfigurationException pce) {
                 throw new ConnectorException(pce);
             }
             final Node bodyContent = getEnvelopeBodyContent(responseDocumentEnvelope);
             final Node clonedBodyContent = bodyContent.cloneNode(true);
@@ -415,11 +418,11 @@ public class SecureWSConnector extends AbstractConnector {
             } else if (buildResponseDocumentBody) {
                 getTransformer().transform(new DOMSource(responseDocumentEnvelope), new StreamResult(System.err));
                 getTransformer().transform(new DOMSource(responseDocumentBody), new StreamResult(System.err));
             }
         } catch (final TransformerException e) {
-            e.printStackTrace();
+            logger.severe(e.getMessage());
         }
     }
 
     Node getEnvelopeBodyContent(final Document envelope) {
         final Node envelopeNode = envelope.getFirstChild();
@@ -450,11 +453,14 @@ public class SecureWSConnector extends AbstractConnector {
         return envelopeBody;
     }
 
     Transformer getTransformer() throws TransformerConfigurationException {
         if (transformer == null) {
-            transformer = TransformerFactory.newInstance().newTransformer();
+            TransformerFactory transformerFactory = TransformerFactory.newInstance();
+            transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
+            transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
+            transformer = transformerFactory.newTransformer();
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
         }
         return transformer;
     }

Vulnerable code:     private final Logger LOGGER = Logger.getLogger(this.getClass().getName());
    @SuppressWarnings("unchecked")
        LOGGER.info(SERVICE_NS + " " + serviceNS);
        LOGGER.info(SERVICE_NAME + " " + serviceName);
        LOGGER.info(PORT_NAME + " " + portName);
        LOGGER.info(BINDING + " " + binding);
        LOGGER.info(ENDPOINT_ADDRESS + " " + endpointAddress);
            LOGGER.info(USER_NAME + " " + authUserName);
            LOGGER.info(PASSWORD + " ********");
        LOGGER.info(SOAP_ACTION + " " + soapAction);
            LOGGER.warning("Invalid XML characters have been detected in the envelope, they will be removed.");
        LOGGER.info(ENVELOPE + " " + sanitizedEnvelope);
        LOGGER.info(BUILD_RESPONSE_DOCUMENT_ENVELOPE + " " + buildResponseDocumentEnvelope);
        LOGGER.info(BUILD_RESPONSE_DOCUMENT_BODY + " " + buildResponseDocumentBody);
        LOGGER.info(PRINT_REQUEST_AND_RESPONSE + " " + printRequestAndResponse);
        LOGGER.info(PROXY_HOST + " " + host);
        LOGGER.info(PROXY_PROTOCOL + " " + protocol);
        LOGGER.info(PROXY_PORT + " " + port);
            LOGGER.info("Setting environment variable: socksProxyHost=" + host);
            LOGGER.info("Setting environment variable: socksProxyPort=" + port);
            LOGGER.info("Setting environment variable: " + hostKey + "=" + host);
            LOGGER.info("Setting environment variable: " + portKey + "=" + port);
        LOGGER.info(PROXY_USER + " " + user);
        LOGGER.info(PROXY_PASSWORD + " ********");
                responseDocumentBody = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            e.printStackTrace();
            transformer = TransformerFactory.newInstance().newTransformer();
Fixed code: import javax.xml.XMLConstants;
    private final Logger logger = Logger.getLogger(this.getClass().getName());
        logger.info(SERVICE_NS + " " + serviceNS);
        logger.info(SERVICE_NAME + " " + serviceName);
        logger.info(PORT_NAME + " " + portName);
        logger.info(BINDING + " " + binding);
        logger.info(ENDPOINT_ADDRESS + " " + endpointAddress);
            logger.info(USER_NAME + " " + authUserName);
            logger.info(PASSWORD + " ********");
        logger.info(SOAP_ACTION + " " + soapAction);
            logger.warning("Invalid XML characters have been detected in the envelope, they will be removed.");
        logger.info(ENVELOPE + " " + sanitizedEnvelope);
        logger.info(BUILD_RESPONSE_DOCUMENT_ENVELOPE + " " + buildResponseDocumentEnvelope);
        logger.info(BUILD_RESPONSE_DOCUMENT_BODY + " " + buildResponseDocumentBody);
        logger.info(PRINT_REQUEST_AND_RESPONSE + " " + printRequestAndResponse);
        logger.info(PROXY_HOST + " " + host);
        logger.info(PROXY_PROTOCOL + " " + protocol);
        logger.info(PROXY_PORT + " " + port);
            logger.info("Setting environment variable: socksProxyHost=" + host);
            logger.info("Setting environment variable: socksProxyPort=" + port);
            logger.info("Setting environment variable: " + hostKey + "=" + host);
            logger.info("Setting environment variable: " + portKey + "=" + port);
        logger.info(PROXY_USER + " " + user);
        logger.info(PROXY_PASSWORD + " ********");
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                documentBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
                documentBuilderFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
                responseDocumentBody = documentBuilderFactory.newDocumentBuilder().newDocument();
            logger.severe(e.getMessage());
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
            transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
            transformer = transformerFactory.newTransformer();