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 java.util.Objects;
21
22 import javax.xml.parsers.SAXParser;
23 import javax.xml.validation.Schema;
24
25 import org.xml.sax.Parser;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.SAXNotRecognizedException;
28 import org.xml.sax.SAXNotSupportedException;
29 import org.xml.sax.XMLReader;
30 import org.xml.sax.helpers.XMLReaderAdapter;
31
32 /**
33 * {@link SAXParser} that exposes a secure {@link XMLReader} and a matching SAX 1 {@link Parser}.
34 *
35 * <p>Both views are produced from the same secure reader, so a caller reaching the parser through either the SAX 2 ({@link #getXMLReader()}) or the legacy
36 * SAX 1 ({@link #getParser()}) path gets the same securing. The SAX 1 view matters because some consumers, such as Xalan's identity transformer, still ask
37 * for a {@link Parser}.</p>
38 *
39 * <p>The secure reader is computed lazily on first access and cached: secure an {@link XMLReader} can install a new wrapper (Android's Expat path), so
40 * every parse must run through the same instance. The {@code parse(...)} overloads inherited from {@link SAXParser} dispatch virtually to {@link #getXMLReader()}
41 * and {@link #getParser()}, so they too run through the secure views without further overrides.</p>
42 */
43 final class SecureSAXParser extends SAXParser {
44
45 private final SAXParser delegate;
46
47 private XMLReader secureXMLReader;
48 private Parser secureParser;
49
50 /**
51 * Constructs a new instance.
52 *
53 * @param delegate the delegate to wrap; must not be {@code null}.
54 * @throws NullPointerException if {@code delegate} is {@code null}.
55 */
56 SecureSAXParser(final SAXParser delegate) {
57 this.delegate = Objects.requireNonNull(delegate, "delegate");
58 }
59
60 @Override
61 @SuppressWarnings("deprecation")
62 public Parser getParser() throws SAXException {
63 if (secureParser == null) {
64 final XMLReader reader = getXMLReader();
65 // Reuse the reader directly if it already is a SAX 1 parser; otherwise adapt it, so the SAX 1 path runs through the same secure reader.
66 secureParser = reader instanceof Parser ? (Parser) reader : new XMLReaderAdapter(reader);
67 }
68 return secureParser;
69 }
70
71 @Override
72 public Object getProperty(final String name) throws SAXNotRecognizedException, SAXNotSupportedException {
73 return delegate.getProperty(name);
74 }
75
76 @Override
77 public Schema getSchema() {
78 return delegate.getSchema();
79 }
80
81 @Override
82 public XMLReader getXMLReader() throws SAXException {
83 if (secureXMLReader == null) {
84 secureXMLReader = SecureSAXParserFactory.secure(delegate.getXMLReader());
85 }
86 return secureXMLReader;
87 }
88
89 @Override
90 public boolean isNamespaceAware() {
91 return delegate.isNamespaceAware();
92 }
93
94 @Override
95 public boolean isValidating() {
96 return delegate.isValidating();
97 }
98
99 @Override
100 public boolean isXIncludeAware() {
101 return delegate.isXIncludeAware();
102 }
103
104 @Override
105 public void reset() {
106 delegate.reset();
107 // The JAXP reset contract reverts the delegate to its just-created state, which strips the post-creation reader securing.
108 // We reset the cached readers, so securing can be applied again.
109 secureXMLReader = null;
110 secureParser = null;
111 }
112
113 @Override
114 public void setProperty(final String name, final Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
115 delegate.setProperty(name, value);
116 }
117
118 }