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 java.util.Objects;
21  import java.util.function.Supplier;
22  
23  import javax.xml.transform.Source;
24  import javax.xml.transform.Templates;
25  import javax.xml.transform.URIResolver;
26  import javax.xml.transform.sax.TemplatesHandler;
27  
28  import org.xml.sax.Attributes;
29  import org.xml.sax.Locator;
30  import org.xml.sax.SAXException;
31  
32  /**
33   * {@link TemplatesHandler} wrapper whose only purpose is to return a {@link SecureTemplates} from {@link TemplatesHandler#getTemplates()}.
34   *
35   * <p>The handler itself only compiles: the caller drives the stylesheet's SAX events, and {@code xsl:include}/{@code xsl:import} hrefs already resolve through
36   * the delegate factory's resolver, which is the secure floor. What the raw handler lacks is the runtime side: the {@link Templates} it compiles produce
37   * Transformers without a {@link URIResolver} floor. Wrapping {@code getTemplates()} closes that, exactly as
38   * {@link javax.xml.transform.TransformerFactory#newTemplates newTemplates} does.</p>
39   */
40  final class SecureTemplatesHandler implements TemplatesHandler {
41  
42      private final TemplatesHandler delegate;
43  
44      /**
45       * Compile-time URIResolver snapshot, restored onto Transformers produced from the compiled Templates.
46       */
47      private final URIResolver uriResolver;
48  
49      /**
50       * Empty-{@link Source} supplier for the produced Templates' floor; {@code null} means the default empty DOM.
51       */
52      private final Supplier<Source> emptySource;
53  
54      /**
55       * Snapshot of the factory's {@value SecureSAXParserFactory#OVERRIDE_DEFAULT_PARSER} outcome, carried onto the produced Templates.
56       */
57      private final boolean overrideDefaultParser;
58  
59      /**
60       * Constructs a new instance.
61       *
62       * @param delegate the delegate to wrap; must not be {@code null}.
63       * @param uriResolver the compile-time URIResolver snapshot to restore onto Transformers produced from the compiled Templates; may be {@code null}.
64       * @param emptySource the empty-{@link Source} supplier for the produced Templates; may be {@code null} for the default empty DOM document.
65       * @param overrideDefaultParser whether the produced Templates' source rewrites should use the pluggable parser lookup instead of the platform's built-in parser.
66       * @throws NullPointerException if {@code delegate} is {@code null}.
67       */
68      SecureTemplatesHandler(final TemplatesHandler delegate, final URIResolver uriResolver, final Supplier<Source> emptySource,
69              final boolean overrideDefaultParser) {
70          this.delegate = Objects.requireNonNull(delegate, "delegate");
71          this.uriResolver = uriResolver;
72          this.emptySource = emptySource;
73          this.overrideDefaultParser = overrideDefaultParser;
74      }
75  
76      @Override
77      public void characters(final char[] ch, final int start, final int length) throws SAXException {
78          delegate.characters(ch, start, length);
79      }
80  
81      @Override
82      public void endDocument() throws SAXException {
83          delegate.endDocument();
84      }
85  
86      @Override
87      public void endElement(final String uri, final String localName, final String qName) throws SAXException {
88          delegate.endElement(uri, localName, qName);
89      }
90  
91      @Override
92      public void endPrefixMapping(final String prefix) throws SAXException {
93          delegate.endPrefixMapping(prefix);
94      }
95  
96      @Override
97      public String getSystemId() {
98          return delegate.getSystemId();
99      }
100 
101     @Override
102     public Templates getTemplates() {
103         // Null before the stylesheet's endDocument (and on a failed compile in some implementations).
104         final Templates templates = delegate.getTemplates();
105         return templates == null ? null : new SecureTemplates(templates, uriResolver, emptySource, overrideDefaultParser);
106     }
107 
108     @Override
109     public void ignorableWhitespace(final char[] ch, final int start, final int length) throws SAXException {
110         delegate.ignorableWhitespace(ch, start, length);
111     }
112 
113     @Override
114     public void processingInstruction(final String target, final String data) throws SAXException {
115         delegate.processingInstruction(target, data);
116     }
117 
118     @Override
119     public void setDocumentLocator(final Locator locator) {
120         delegate.setDocumentLocator(locator);
121     }
122 
123     @Override
124     public void setSystemId(final String systemID) {
125         delegate.setSystemId(systemID);
126     }
127 
128     @Override
129     public void skippedEntity(final String name) throws SAXException {
130         delegate.skippedEntity(name);
131     }
132 
133 
134     @Override
135     public void startDocument() throws SAXException {
136         delegate.startDocument();
137     }
138 
139     @Override
140     public void startElement(final String uri, final String localName, final String qName, final Attributes atts) throws SAXException {
141         delegate.startElement(uri, localName, qName, atts);
142     }
143 
144     @Override
145     public void startPrefixMapping(final String prefix, final String uri) throws SAXException {
146         delegate.startPrefixMapping(prefix, uri);
147     }
148 }