BUG-2511: disable external entitiy resolution with EXI
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / handler / NetconfEXICodec.java
1 package org.opendaylight.controller.netconf.nettyutil.handler;
2
3 import com.google.common.base.Preconditions;
4 import org.openexi.proc.HeaderOptionsOutputType;
5 import org.openexi.proc.common.EXIOptions;
6 import org.openexi.proc.common.EXIOptionsException;
7 import org.openexi.proc.common.GrammarOptions;
8 import org.openexi.proc.grammars.GrammarCache;
9 import org.openexi.sax.EXIReader;
10 import org.openexi.sax.Transmogrifier;
11 import org.openexi.sax.TransmogrifierException;
12 import org.xml.sax.EntityResolver;
13 import org.xml.sax.InputSource;
14
15 public final class NetconfEXICodec {
16     /**
17      * NETCONF is XML environment, so the use of EXI cookie is not really needed. Adding it
18      * decreases efficiency of encoding by adding human-readable 4 bytes "EXI$" to the head
19      * of the stream. This is really useful, so let's output it now.
20      */
21     private static final boolean OUTPUT_EXI_COOKIE = true;
22     /**
23      * OpenEXI does not allow us to directly prevent resolution of external entities. In order
24      * to prevent XXE attacks, we reuse a single no-op entity resolver.
25      */
26     private static final EntityResolver ENTITY_RESOLVER = new EntityResolver() {
27         @Override
28         public InputSource resolveEntity(final String publicId, final String systemId) {
29             return new InputSource();
30         }
31     };
32
33     private final EXIOptions exiOptions;
34
35     public NetconfEXICodec(final EXIOptions exiOptions) {
36         this.exiOptions = Preconditions.checkNotNull(exiOptions);
37     }
38
39     private GrammarCache getGrammarCache() {
40         short go = GrammarOptions.DEFAULT_OPTIONS;
41         if (exiOptions.getPreserveComments()) {
42             go = GrammarOptions.addCM(go);
43         }
44         if (exiOptions.getPreserveDTD()) {
45             go = GrammarOptions.addDTD(go);
46         }
47         if (exiOptions.getPreserveNS()) {
48             go = GrammarOptions.addNS(go);
49         }
50         if (exiOptions.getPreservePIs()) {
51             go = GrammarOptions.addPI(go);
52         }
53
54         return new GrammarCache(null, go);
55     }
56
57     EXIReader getReader() throws EXIOptionsException {
58         final EXIReader r = new EXIReader();
59         r.setPreserveLexicalValues(exiOptions.getPreserveLexicalValues());
60         r.setGrammarCache(getGrammarCache());
61         r.setEntityResolver(ENTITY_RESOLVER);
62         return r;
63     }
64
65     Transmogrifier getTransmogrifier() throws EXIOptionsException, TransmogrifierException {
66         final Transmogrifier transmogrifier = new Transmogrifier();
67         transmogrifier.setAlignmentType(exiOptions.getAlignmentType());
68         transmogrifier.setBlockSize(exiOptions.getBlockSize());
69         transmogrifier.setGrammarCache(getGrammarCache());
70         transmogrifier.setOutputCookie(OUTPUT_EXI_COOKIE);
71         transmogrifier.setOutputOptions(HeaderOptionsOutputType.all);
72         transmogrifier.setResolveExternalGeneralEntities(false);
73         return transmogrifier;
74     }
75 }