import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.avmfritz.internal.dto.DeviceListModel;
logger.trace("Received State response {}", response);
if (isValidRequest()) {
try {
+ XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_DEVICES.createUnmarshaller();
- DeviceListModel model = (DeviceListModel) unmarshaller.unmarshal(new StringReader(response));
+ DeviceListModel model = (DeviceListModel) unmarshaller.unmarshal(xsr);
if (model != null) {
handler.onDeviceListAdded(model.getDevicelist());
} else {
logger.debug("no model in response");
}
handler.setStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
- } catch (JAXBException e) {
+ } catch (JAXBException | XMLStreamException e) {
logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
handler.setStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
e.getLocalizedMessage());
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.avmfritz.internal.dto.templates.TemplateListModel;
logger.trace("Received response '{}'", response);
if (isValidRequest()) {
try {
+ XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY.createXMLStreamReader(new StringReader(response));
Unmarshaller unmarshaller = JAXBUtils.JAXBCONTEXT_TEMPLATES.createUnmarshaller();
- TemplateListModel model = (TemplateListModel) unmarshaller.unmarshal(new StringReader(response));
+ TemplateListModel model = (TemplateListModel) unmarshaller.unmarshal(xsr);
if (model != null) {
handler.addTemplateList(model.getTemplates());
} else {
logger.debug("no template in response");
}
- } catch (JAXBException e) {
+ } catch (JAXBException | XMLStreamException e) {
logger.error("Exception creating Unmarshaller: {}", e.getLocalizedMessage(), e);
}
} else {
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
+import javax.xml.stream.XMLInputFactory;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
public static final @Nullable JAXBContext JAXBCONTEXT_DEVICES = initJAXBContextDevices();
public static final @Nullable JAXBContext JAXBCONTEXT_TEMPLATES = initJAXBContextTemplates();
+ public static final XMLInputFactory XMLINPUTFACTORY = initXMLInputFactory();
private static @Nullable JAXBContext initJAXBContextDevices() {
try {
return null;
}
}
+
+ private static XMLInputFactory initXMLInputFactory() {
+ XMLInputFactory xif = XMLInputFactory.newInstance();
+ xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
+ return xif;
+ }
}
public void handleMessage(String msg) throws SAXException, IOException {
XMLReader reader = XMLReaderFactory.createXMLReader();
+ reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
reader.setContentHandler(new XMLResponseHandler(handler, stateSwitchingMap));
reader.parse(new InputSource(new StringReader(msg)));
}
if (StringUtils.isNotBlank(result)) {
JAXBContext jc = JAXBContext.newInstance(response);
XMLInputFactory xif = XMLInputFactory.newInstance();
+ xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xsr = xif.createXMLStreamReader(IOUtils.toInputStream(result));
xsr = new PropertyRenamerDelegate(xsr);
if (status == HttpURLConnection.HTTP_OK && response != null) {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder;
try {
+ // see
+ // https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ domFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ domFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ domFactory.setXIncludeAware(false);
+ domFactory.setExpandEntityReferences(false);
+ DocumentBuilder builder;
builder = domFactory.newDocumentBuilder();
Document dDoc = builder.parse(new InputSource(new StringReader(response.getContentAsString())));
XPath xPath = XPathFactory.newInstance().newXPath();
uri = new URI("http://" + ipAddress + "/HNAP1");
httpClient.start();
- parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbf.setXIncludeAware(false);
+ dbf.setExpandEntityReferences(false);
+ parser = dbf.newDocumentBuilder();
final MessageFactory messageFactory = MessageFactory.newInstance();
requestAction = messageFactory.createMessage();
private final Enigma2HttpClient enigma2HttpClient;
private final DocumentBuilderFactory factory;
- public Enigma2Client(String host, @Nullable String user, @Nullable String password, int requestTimeout) {
- this.enigma2HttpClient = new Enigma2HttpClient(requestTimeout);
- this.factory = DocumentBuilderFactory.newInstance();
+ public Enigma2Client(String host, @Nullable String user, @Nullable String password, int requestTimeout)
+ throws ParserConfigurationException {
+ enigma2HttpClient = new Enigma2HttpClient(requestTimeout);
+ factory = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ factory.setXIncludeAware(false);
+ factory.setExpandEntityReferences(false);
if (StringUtils.isNotEmpty(user) && StringUtils.isNotEmpty(password)) {
this.host = "http://" + user + ":" + password + "@" + host;
} else {
public Client() {
documentBuilderFactory.setNamespaceAware(true);
try {
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ documentBuilderFactory.setXIncludeAware(false);
+ documentBuilderFactory.setExpandEntityReferences(false);
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new IllegalStateException(e);
private Document getXmlDocFromString(String xmlString)
throws ParserConfigurationException, SAXException, IOException {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ factory.setXIncludeAware(false);
+ factory.setExpandEntityReferences(false);
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document xmlDocument = builder.parse(new InputSource(new StringReader(xmlString)));
return xmlDocument;
public void read() {
try {
- DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ factory.setXIncludeAware(false);
+ factory.setExpandEntityReferences(false);
+ DocumentBuilder builder = factory.newDocumentBuilder();
String statusPage = HttpUtil.executeUrl("GET", String.format(URL_TEMPLATE, hostname), 5000);
InputStream inputStream = new ByteArrayInputStream(statusPage.getBytes());
Document document = builder.parse(inputStream);
throws SAXException, ParserConfigurationException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
+ factory.setFeature("https://xml.org/sax/features/external-general-entities", false);
+ saxParser.getXMLReader().setFeature("https://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("https://apache.org/xml/features/disallow-doctype-decl", true);
InputSource inputSource = new InputSource(is);
inputSource.setEncoding(encoding);
saxParser.parse(inputSource, new XmlRpcHandler());
/**
* Creates a new HP Web Server Client object.
- *
+ *
* @param httpClient {HttpClient} The HttpClient to use for HTTP requests.
* @param address The address for the Embedded Web Server.
*/
/**
* Gets the Status information from the Embedded Web Server.
- *
+ *
* @return The status information.
*/
public HPServerResult<HPStatus> getStatus() {
/**
* Gets the Usage information from the Embedded Web Server.
- *
+ *
* @return The usage information.
*/
public HPServerResult<HPUsage> getUsage() {
private synchronized Document getDocument(String contentAsString)
throws ParserConfigurationException, SAXException, IOException {
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ factory.setXIncludeAware(false);
+ factory.setExpandEntityReferences(false);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource source = new InputSource(new StringReader(contentAsString));
return builder.parse(source);
File fXmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
try {
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbFactory.setXIncludeAware(false);
+ dbFactory.setExpandEntityReferences(false);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
return doc;
*/
public void loadDeviceTypesXML(InputStream in) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbFactory.setXIncludeAware(false);
+ dbFactory.setExpandEntityReferences(false);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(in);
doc.getDocumentElement().normalize();
List<FeatureTemplate> features = new ArrayList<>();
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbFactory.setXIncludeAware(false);
+ dbFactory.setExpandEntityReferences(false);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
// Parse it!
Document doc = dBuilder.parse(input);
Map<String, Msg> messageMap = new HashMap<>();
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbFactory.setXIncludeAware(false);
+ dbFactory.setExpandEntityReferences(false);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
// Parse it!
Document doc = dBuilder.parse(input);
private void processInfo(String infoXML) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ factory.setXIncludeAware(false);
+ factory.setExpandEntityReferences(false);
DocumentBuilder builder = factory.newDocumentBuilder();
try (StringReader sr = new StringReader(infoXML)) {
InputSource is = new InputSource(sr);
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
+import javax.xml.stream.XMLInputFactory;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
public static final @Nullable JAXBContext JAXBCONTEXT_APPS = initJAXBContextApps();
public static final @Nullable JAXBContext JAXBCONTEXT_DEVICE_INFO = initJAXBContextDeviceInfo();
public static final @Nullable JAXBContext JAXBCONTEXT_PLAYER = initJAXBContextPlayer();
+ public static final XMLInputFactory XMLINPUTFACTORY = initXMLInputFactory();
private static @Nullable JAXBContext initJAXBContextActiveApp() {
try {
return null;
}
}
+
+ private static XMLInputFactory initXMLInputFactory() {
+ XMLInputFactory xif = XMLInputFactory.newInstance();
+ xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
+ return xif;
+ }
}
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient;
if (ctx != null) {
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
- DeviceInfo device = (DeviceInfo) unmarshaller.unmarshal(new StringReader(getCommand(urlQryDevice)));
+ XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
+ .createXMLStreamReader(new StringReader(getCommand(urlQryDevice)));
+ DeviceInfo device = (DeviceInfo) unmarshaller.unmarshal(xsr);
if (device != null) {
return device;
}
}
}
throw new RokuHttpException("No DeviceInfo model in response");
- } catch (JAXBException e) {
+ } catch (JAXBException | XMLStreamException e) {
throw new RokuHttpException("Exception creating DeviceInfo Unmarshaller: " + e.getLocalizedMessage());
}
}
if (ctx != null) {
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
+ XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
+ .createXMLStreamReader(new StringReader(getCommand(urlQryActiveApp)));
ActiveApp activeApp = (ActiveApp) unmarshaller
- .unmarshal(new StringReader(getCommand(urlQryActiveApp)));
+ .unmarshal(xsr));
if (activeApp != null) {
return activeApp;
}
if (ctx != null) {
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
- Apps appList = (Apps) unmarshaller.unmarshal(new StringReader(getCommand(urlQryApps)));
+ XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
+ .createXMLStreamReader(new StringReader(getCommand(urlQryApps)));
+ Apps appList = (Apps) unmarshaller.unmarshal(xsr);
if (appList != null) {
return appList.getApp();
}
}
}
throw new RokuHttpException("No AppList model in response");
- } catch (JAXBException e) {
+ } catch (JAXBException | XMLStreamException e) {
throw new RokuHttpException("Exception creating AppList Unmarshaller: " + e.getLocalizedMessage());
}
}
if (ctx != null) {
Unmarshaller unmarshaller = ctx.createUnmarshaller();
if (unmarshaller != null) {
- Player playerInfo = (Player) unmarshaller.unmarshal(new StringReader(getCommand(urlQryPlayer)));
+ XMLStreamReader xsr = JAXBUtils.XMLINPUTFACTORY
+ .createXMLStreamReader(new StringReader(getCommand(urlQryPlayer)));
+ Player playerInfo = (Player) unmarshaller.unmarshal(xsr);
if (playerInfo != null) {
return playerInfo;
}
}
}
throw new RokuHttpException("No Player info model in response");
- } catch (JAXBException e) {
+ } catch (JAXBException | XMLStreamException e) {
throw new RokuHttpException("Exception creating Player info Unmarshaller: " + e.getLocalizedMessage());
}
}
public static @Nullable Document loadXMLFromString(String xml) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ factory.setXIncludeAware(false);
+ factory.setExpandEntityReferences(false);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
*/
public static @Nullable SonosResourceMetaData getResourceMetaData(String xml) throws SAXException {
XMLReader reader = XMLReaderFactory.createXMLReader();
+ reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
ResourceMetaDataHandler handler = new ResourceMetaDataHandler();
reader.setContentHandler(handler);
try {
// TelldusLiveHandler.logger.info("Devices" + resp.getResponseBody());
JAXBContext jc = JAXBContext.newInstance(response);
XMLInputFactory xif = XMLInputFactory.newInstance();
+ xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xsr = xif.createXMLStreamReader(resp.getResponseBodyAsStream());
// xsr = new PropertyRenamerDelegate(xsr);
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.jdt.annotation.NonNullByDefault;
InputStream xml = new ByteArrayInputStream(contentResponse.getContent());
JAXBContext context = JAXBContext.newInstance(PhonebooksType.class);
+ XMLInputFactory xif = XMLInputFactory.newFactory();
+ xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
+ XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(xml));
Unmarshaller um = context.createUnmarshaller();
- PhonebooksType phonebooksType = um.unmarshal(new StreamSource(xml), PhonebooksType.class).getValue();
+ PhonebooksType phonebooksType = um.unmarshal(xsr, PhonebooksType.class).getValue();
phonebookName = phonebooksType.getPhonebook().getName();
this::mergeSameContactNames));
}).collect(HashMap::new, HashMap::putAll, HashMap::putAll);
logger.debug("Downloaded phonebook {}: {}", phonebookName, phonebook);
- } catch (JAXBException | InterruptedException | ExecutionException | TimeoutException e) {
+ } catch (JAXBException | InterruptedException | ExecutionException | TimeoutException | XMLStreamException e) {
logger.warn("Failed to get phonebook with URL {}:", phonebookUrl, e);
}
}
import java.io.InputStream;
import java.lang.reflect.Field;
import java.time.Duration;
-import java.util.*;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.tr064.internal.dto.config.ChannelTypeDescriptions;
import org.openhab.binding.tr064.internal.dto.config.ParameterType;
import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDServiceType;
-import org.openhab.binding.tr064.internal.dto.scpd.service.*;
+import org.openhab.binding.tr064.internal.dto.scpd.service.SCPDActionType;
+import org.openhab.binding.tr064.internal.dto.scpd.service.SCPDArgumentType;
+import org.openhab.binding.tr064.internal.dto.scpd.service.SCPDDirection;
+import org.openhab.binding.tr064.internal.dto.scpd.service.SCPDScpdType;
+import org.openhab.binding.tr064.internal.dto.scpd.service.SCPDStateVariableType;
import org.openhab.core.cache.ExpiringCacheMap;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
/**
* read the channel config from the resource file (static initialization)
- *
+ *
* @return a list of all available channel configurations
*/
public static List<ChannelTypeDescription> readXMLChannelConfig() {
try {
InputStream resource = Thread.currentThread().getContextClassLoader().getResourceAsStream("channels.xml");
JAXBContext context = JAXBContext.newInstance(ChannelTypeDescriptions.class);
+ XMLInputFactory xif = XMLInputFactory.newFactory();
+ xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
+ XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(resource));
Unmarshaller um = context.createUnmarshaller();
- JAXBElement<ChannelTypeDescriptions> root = um.unmarshal(new StreamSource(resource),
- ChannelTypeDescriptions.class);
+ JAXBElement<ChannelTypeDescriptions> root = um.unmarshal(xsr, ChannelTypeDescriptions.class);
return root.getValue().getChannel();
- } catch (JAXBException e) {
+ } catch (JAXBException | XMLStreamException e) {
LOGGER.warn("Failed to read channel definitions", e);
return List.of();
}
/**
* Extract an argument from an SCPD action definition
- *
+ *
* @param scpdAction the action object
* @param argumentName the argument's name
* @param direction the direction (in or out)
/**
* Extract the related state variable from the service root for a given argument
- *
+ *
* @param serviceRoot the service root object
* @param scpdArgument the argument object
* @return the related state variable object for this argument
/**
* Extract an action from the service root
- *
+ *
* @param serviceRoot the service root object
* @param actionName the action name
* @param actionType "Get-Action" or "Set-Action" (for exception string only)
InputStream xml = new ByteArrayInputStream(response);
JAXBContext context = JAXBContext.newInstance(clazz);
+ XMLInputFactory xif = XMLInputFactory.newFactory();
+ xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
+ XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(xml));
Unmarshaller um = context.createUnmarshaller();
- T newValue = um.unmarshal(new StreamSource(xml), clazz).getValue();
+ T newValue = um.unmarshal(xsr, clazz).getValue();
LOGGER.trace("Storing in cache {}", newValue);
return newValue;
} catch (ExecutionException | InterruptedException | TimeoutException e) {
LOGGER.debug("HTTP Failed to GET uri '{}': {}", uri, e.getMessage());
throw new IllegalArgumentException();
- } catch (JAXBException e) {
+ } catch (JAXBException | XMLStreamException e) {
LOGGER.debug("Unmarshalling failed: {}", e.getMessage());
throw new IllegalArgumentException();
}
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
+ factory.setFeature("https://xml.org/sax/features/external-general-entities", false);
+ saxParser.getXMLReader().setFeature("https://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("https://apache.org/xml/features/disallow-doctype-decl", true);
saxParser.parse(new InputSource(new StringReader(xml)), handler);
} catch (IOException e) {
// This should never happen - we're not performing I/O!
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
+ factory.setFeature("https://xml.org/sax/features/external-general-entities", false);
+ saxParser.getXMLReader().setFeature("https://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("https://apache.org/xml/features/disallow-doctype-decl", true);
saxParser.parse(new InputSource(new StringReader(xml)), handler);
} catch (IOException e) {
// This should never happen - we're not performing I/O!
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
+ factory.setFeature("https://xml.org/sax/features/external-general-entities", false);
+ saxParser.getXMLReader().setFeature("https://xml.org/sax/features/external-general-entities", false);
+ factory.setFeature("https://apache.org/xml/features/disallow-doctype-decl", true);
saxParser.parse(new InputSource(new StringReader(xml)), handler);
} catch (IOException e) {
// This should never happen - we're not performing I/O!
logger.trace("Start Background Thread for recieving data from adapter");
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
+ xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
xmlReader.setContentHandler(new XmlHandler());
logger.trace("Start Parser for optolink adapter");
xmlReader.parse(new InputSource(inStream));
// Build parser for received <DeviceList>
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ // see
+ // https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbf.setXIncludeAware(false);
+ dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
stringParser = "<data>" + stringParser + "</data>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ // see
+ // https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbf.setXIncludeAware(false);
+ dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
stringParser = "<data>" + stringParser + "</data>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ // see
+ // https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbf.setXIncludeAware(false);
+ dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
stringParser = "<data>" + stringParser + "</data>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ // see
+ // https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbf.setXIncludeAware(false);
+ dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(stringParser));
: "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + message;
try {
- return XMLUtils.dbf.newDocumentBuilder().parse(new InputSource(new StringReader(response)));
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ dbf.setXIncludeAware(false);
+ dbf.setExpandEntityReferences(false);
+ return dbf.newDocumentBuilder().parse(new InputSource(new StringReader(response)));
} catch (SAXException | ParserConfigurationException e) {
throw new ReceivedMessageParseException(e);
}
try {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
+ // see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html
+ domFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+ domFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+ domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ domFactory.setXIncludeAware(false);
+ domFactory.setExpandEntityReferences(false);
domFactory.setNamespaceAware(true);
domFactory.setValidating(false);
DocumentBuilder builder = domFactory.newDocumentBuilder();