View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.xml.secure;
19  
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.InputStream;
24  import java.nio.charset.StandardCharsets;
25  
26  import javax.xml.parsers.SAXParserFactory;
27  import javax.xml.stream.XMLInputFactory;
28  import javax.xml.stream.XMLStreamReader;
29  import javax.xml.transform.sax.SAXSource;
30  
31  import org.junit.jupiter.api.Tag;
32  import org.junit.jupiter.api.Test;
33  import org.xml.sax.InputSource;
34  import org.xml.sax.XMLReader;
35  
36  import jakarta.xml.bind.JAXBContext;
37  import jakarta.xml.bind.Unmarshaller;
38  import jakarta.xml.bind.annotation.XmlRootElement;
39  
40  /**
41   * Tests JAXB integration.
42   * <p>
43   * This class' two public methods serve as examples for the Javadoc {@code overview.html} file.
44   * </p>
45   */
46  @Tag("jaxb-jakarta")
47  public class JaxbJakartaTest {
48  
49      @XmlRootElement
50      static class MyJaxbModel {
51          // JAXB model fields and methods
52      }
53  
54      @Test
55      void testUnmarshalSecurelyWithSax() throws Exception {
56          try (InputStream xmlStream = new ByteArrayInputStream("<myJaxbModel/>".getBytes(StandardCharsets.UTF_8))) {
57              assertNotNull(new JaxbJakartaTest().unmarshalSecurelyWithSax(xmlStream));
58          }
59      }
60  
61      @Test
62      void testUnmarshalSecurelyWithStax() throws Exception {
63          try (InputStream xmlStream = new ByteArrayInputStream("<myJaxbModel/>".getBytes(StandardCharsets.UTF_8))) {
64              assertNotNull(new JaxbJakartaTest().unmarshalSecurelyWithStax(xmlStream));
65          }
66      }
67  
68      public MyJaxbModel unmarshalSecurelyWithSax(final InputStream xmlStream) throws Exception {
69          final JAXBContext context = JAXBContext.newInstance(MyJaxbModel.class);
70          final Unmarshaller unmarshaller = context.createUnmarshaller();
71          // Create a secure SAXParserFactory via Apache Commons Secure XML
72          final SAXParserFactory spf = SecureSAXParserFactory.newDefaultNSInstance();
73          // Generate a hardened XMLReader and wrap the input source
74          final XMLReader xmlReader = spf.newSAXParser().getXMLReader();
75          final SAXSource source = new SAXSource(xmlReader, new InputSource(xmlStream));
76          // With the default resolver configuration, external DTDs and entities are prevented from being fetched or resolved, protecting against XXE attacks, protecting against XXE attacks.
77          // Entity-expansion limits use FEATURE_SECURE_PROCESSING on JDK parsers;
78          // Android support is implementation-dependent and best-effort.
79          return (MyJaxbModel) unmarshaller.unmarshal(source);
80      }
81  
82      public MyJaxbModel unmarshalSecurelyWithStax(final InputStream xmlStream) throws Exception {
83          final JAXBContext context = JAXBContext.newInstance(MyJaxbModel.class);
84          final Unmarshaller unmarshaller = context.createUnmarshaller();
85          // Create a secure XMLInputFactory via Apache Commons Secure XML
86          final XMLInputFactory xif = SecureXMLInputFactory.newDefaultFactory();
87          // Create a hardened cursor reader
88          final XMLStreamReader xmlReader = xif.createXMLStreamReader(xmlStream);
89          try {
90              // With the default resolver configuration, external DTDs and entities are prevented from being fetched or resolved, protecting against XXE attacks, protecting against XXE attacks.
91              // Entity-expansion protection is implementation-dependent and best-effort
92              // because StAX exposes no secure-processing feature.
93              return (MyJaxbModel) unmarshaller.unmarshal(xmlReader);
94          } finally {
95              xmlReader.close();
96          }
97      }
98  }