Copilot commented on code in PR #103:
URL: 
https://github.com/apache/commons-secure-xml/pull/103#discussion_r4093148837


##########
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
+    </h1>
+    This section shows you how to secure XML parsing and JAXB unmarshalling.
+    <p>
+      By default, JAXB unmarshallers do not disable External Entity resolution 
(XXE) or Billion Laughs denial-of-service vulnerabilities. Below are the two
+      recommended approaches for integrating <strong>Apache Commons Secure 
XML</strong> to harden your unmarshalling pipeline.
+    </p>
+    <h2>Option 1: SAX-based Secure Unmarshalling (Recommended)</h2>
+    <p>
+      This approach uses
+      <code>SecureSAXParserFactory</code>
+      to construct a hardened, secure
+      <code>XMLReader</code>
+      which is then wrapped inside a
+      <code>SAXSource</code>
+      .
+    </p>
+    <pre>
+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.newDefaultInstance();

Review Comment:
   This documented SAX recipe uses the non-namespace-aware factory, so it is 
not a generally valid JAXB example: namespaced JAXB models can fail to bind 
because the `XMLReader` emits empty namespace URIs. Use 
`newDefaultNSInstance()` here (or call `setNamespaceAware(true)`) before 
handing the reader to JAXB.



##########
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.newDefaultInstance();

Review Comment:
   The plain `newDefaultInstance()` preserves the parser's default 
namespace-awareness setting, which is false for the JAXP SAX factory. JAXB 
bindings for namespaced XML therefore receive no namespace URIs and can fail or 
bind incorrectly; use the namespace-aware secure factory (or explicitly enable 
namespace awareness) for this example.
   
   This issue also appears on line 53 of the same file.



-- 
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]

Reply via email to