ppkarwasz commented on code in PR #103:
URL:
https://github.com/apache/commons-secure-xml/pull/103#discussion_r4093170504
##########
src/main/javadoc/overview.html:
##########
@@ -526,6 +526,69 @@ <h1>
href="../threat_model.html">Threat Model</a> documents the resulting
contract.
</p>
</section>
+ <section id="jaxb">
+ <h1>
+ <img src="org/apache/commons/xml/secure/doc-files/leaf.svg"
style="height: 1em; padding-right: 0.25em" alt="leaf">Migrating JAXB
Review Comment:
I would add this to the **website** documentation, not the Javadoc, since it
will evolve in time **independently** of the library.
On the same page we could add workarounds for other library limitations: for
example dom4j installs an allow-all resolver on each read and we need to
counter it with:
```java
SAXReader reader = new
SAXReader(SecureSAXParserFactory.newInstance().newSAXParser().getXMLReader());
reader.setEntityResolver((p, s) -> null);
```
##########
src/test/java/org/apache/commons/xml/secure/JaxbExamples.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.xml.secure;
+
+import java.io.InputStream;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.sax.SAXSource;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+
+/**
+ * Examples for the Javadoc {@code overview.html} file.
+ */
+public class JaxbExamples {
+
+ static class MyJaxbModel {
+ // JAXB model fields and methods
+ }
+
+ public MyJaxbModel unmarshalSecurelyWithSax(InputStream xmlStream) throws
Exception {
+ JAXBContext context = JAXBContext.newInstance(MyJaxbModel.class);
+ Unmarshaller unmarshaller = context.createUnmarshaller();
+
+ // Create a secure SAXParserFactory via Apache Commons Secure XML
+ SAXParserFactory spf = SecureSAXParserFactory.newDefaultNSInstance();
+
+ // Generate a hardened XMLReader and wrap the input source
+ XMLReader xmlReader = spf.newSAXParser().getXMLReader();
+ SAXSource source = new SAXSource(xmlReader, new
InputSource(xmlStream));
+
+ // Safe from XXE injection and entity-expansion DoS attacks
+ return (MyJaxbModel) unmarshaller.unmarshal(source);
Review Comment:
These would be ideal as copy-paste snippets from the website, so it is worth
naming what they replace. Every `Unmarshaller` overload that takes raw bytes or
a location builds the parser internally, so our hardening never applies:
- `unmarshal(File)`
- `unmarshal(InputStream)`
- `unmarshal(Reader)`
- `unmarshal(URL)`
- `unmarshal(InputSource)`
- `unmarshal(Source)` and `unmarshal(Source, Class)`, when given a
`StreamSource` or a `SAXSource` with a null `XMLReader`
We could offer helpers such as `unmarshalSecure(Unmarshaller, File)` that
wrap the input in a `SAXSource` from our factories, giving a mechanical
one-line replacement.
In our tests I would rather **test** that the examples resist entity
expansion and XXE.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]