1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
Description: CVE-2016-3674: XML external entity injection vulnerability
Origin: backport, https://github.com/x-stream/xstream/commit/c9b121a
https://github.com/x-stream/xstream/commit/25c6704
https://github.com/x-stream/xstream/commit/87172cf
https://github.com/x-stream/xstream/commit/7c77ac0
https://github.com/x-stream/xstream/commit/7183131
https://github.com/x-stream/xstream/commit/812a0fa
https://github.com/x-stream/xstream/commit/6438b65
Bug: https://github.com/x-stream/xstream/issues/25
Bug-Debian: https://bugs.debian.org/819455
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/BEAStaxDriver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/BEAStaxDriver.java
@@ -62,7 +62,9 @@
}
protected XMLInputFactory createInputFactory() {
- return new MXParserFactory();
+ XMLInputFactory instance = new MXParserFactory();
+ instance.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ return instance;
}
protected XMLOutputFactory createOutputFactory() {
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/Dom4JDriver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/Dom4JDriver.java
@@ -26,6 +26,7 @@
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
+import org.xml.sax.SAXException;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
@@ -89,8 +90,7 @@
public HierarchicalStreamReader createReader(Reader text) {
try {
- SAXReader reader = new SAXReader();
- Document document = reader.read(text);
+ Document document = createReader().read(text);
return new Dom4JReader(document, getNameCoder());
} catch (DocumentException e) {
throw new StreamException(e);
@@ -99,8 +99,7 @@
public HierarchicalStreamReader createReader(InputStream in) {
try {
- SAXReader reader = new SAXReader();
- Document document = reader.read(in);
+ Document document = createReader().read(in);
return new Dom4JReader(document, getNameCoder());
} catch (DocumentException e) {
throw new StreamException(e);
@@ -112,8 +111,7 @@
*/
public HierarchicalStreamReader createReader(URL in) {
try {
- SAXReader reader = new SAXReader();
- Document document = reader.read(in);
+ Document document = createReader().read(in);
return new Dom4JReader(document, getNameCoder());
} catch (DocumentException e) {
throw new StreamException(e);
@@ -125,8 +123,7 @@
*/
public HierarchicalStreamReader createReader(File in) {
try {
- SAXReader reader = new SAXReader();
- Document document = reader.read(in);
+ Document document = createReader().read(in);
return new Dom4JReader(document, getNameCoder());
} catch (DocumentException e) {
throw new StreamException(e);
@@ -148,4 +145,21 @@
final Writer writer = new OutputStreamWriter(out);
return createWriter(writer);
}
+
+ /**
+ * Create and initialize the SAX reader.
+ *
+ * @return the SAX reader instance.
+ * @throws DocumentException if DOCTYPE processing cannot be disabled
+ * @since upcoming
+ */
+ protected SAXReader createReader() throws DocumentException {
+ SAXReader reader = new SAXReader();
+ try {
+ reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+ } catch (SAXException e) {
+ throw new DocumentException("Cannot disable DOCTYPE processing", e);
+ }
+ return reader;
+ }
}
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/DomDriver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/DomDriver.java
@@ -39,7 +39,7 @@
public class DomDriver extends AbstractXmlDriver {
private final String encoding;
- private final DocumentBuilderFactory documentBuilderFactory;
+ private DocumentBuilderFactory documentBuilderFactory;
/**
* Construct a DomDriver.
@@ -61,7 +61,6 @@
*/
public DomDriver(String encoding, NameCoder nameCoder) {
super(nameCoder);
- documentBuilderFactory = DocumentBuilderFactory.newInstance();
this.encoding = encoding;
}
@@ -91,6 +90,13 @@
private HierarchicalStreamReader createReader(InputSource source) {
try {
+ if (documentBuilderFactory == null) {
+ synchronized (this) {
+ if (documentBuilderFactory == null) {
+ documentBuilderFactory = createDocumentBuilderFactory();
+ }
+ }
+ }
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
if (encoding != null) {
source.setEncoding(encoding);
@@ -121,4 +127,20 @@
throw new StreamException(e);
}
}
+
+ /**
+ * Create the DocumentBuilderFactory instance.
+ *
+ * @return the new instance
+ * @since upcoming
+ */
+ protected DocumentBuilderFactory createDocumentBuilderFactory() {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ try {
+ factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+ } catch (ParserConfigurationException e) {
+ throw new StreamException(e);
+ }
+ return factory;
+ }
}
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Driver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Driver.java
@@ -47,7 +47,7 @@
public HierarchicalStreamReader createReader(Reader reader) {
try {
- SAXBuilder builder = new SAXBuilder();
+ SAXBuilder builder = createBuilder();
Document document = builder.build(reader);
return new JDom2Reader(document, getNameCoder());
} catch (IOException e) {
@@ -59,7 +59,7 @@
public HierarchicalStreamReader createReader(InputStream in) {
try {
- SAXBuilder builder = new SAXBuilder();
+ SAXBuilder builder = createBuilder();
Document document = builder.build(in);
return new JDom2Reader(document, getNameCoder());
} catch (IOException e) {
@@ -71,7 +71,7 @@
public HierarchicalStreamReader createReader(URL in) {
try {
- SAXBuilder builder = new SAXBuilder();
+ SAXBuilder builder = createBuilder();
Document document = builder.build(in);
return new JDom2Reader(document, getNameCoder());
} catch (IOException e) {
@@ -83,7 +83,7 @@
public HierarchicalStreamReader createReader(File in) {
try {
- SAXBuilder builder = new SAXBuilder();
+ SAXBuilder builder = createBuilder();
Document document = builder.build(in);
return new JDom2Reader(document, getNameCoder());
} catch (IOException e) {
@@ -100,5 +100,17 @@
public HierarchicalStreamWriter createWriter(OutputStream out) {
return new PrettyPrintWriter(new OutputStreamWriter(out));
}
+
+ /**
+ * Create and initialize the SAX builder.
+ *
+ * @return the SAX builder instance.
+ * @since upcoming
+ */
+ protected SAXBuilder createBuilder() {
+ SAXBuilder builder = new SAXBuilder();
+ builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+ return builder;
+ }
}
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/JDomDriver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/JDomDriver.java
@@ -55,7 +55,7 @@
public HierarchicalStreamReader createReader(Reader reader) {
try {
- SAXBuilder builder = new SAXBuilder();
+ SAXBuilder builder = createBuilder();
Document document = builder.build(reader);
return new JDomReader(document, getNameCoder());
} catch (IOException e) {
@@ -67,7 +67,7 @@
public HierarchicalStreamReader createReader(InputStream in) {
try {
- SAXBuilder builder = new SAXBuilder();
+ SAXBuilder builder = createBuilder();
Document document = builder.build(in);
return new JDomReader(document, getNameCoder());
} catch (IOException e) {
@@ -79,7 +79,7 @@
public HierarchicalStreamReader createReader(URL in) {
try {
- SAXBuilder builder = new SAXBuilder();
+ SAXBuilder builder = createBuilder();
Document document = builder.build(in);
return new JDomReader(document, getNameCoder());
} catch (IOException e) {
@@ -91,7 +91,7 @@
public HierarchicalStreamReader createReader(File in) {
try {
- SAXBuilder builder = new SAXBuilder();
+ SAXBuilder builder = createBuilder();
Document document = builder.build(in);
return new JDomReader(document, getNameCoder());
} catch (IOException e) {
@@ -109,5 +109,17 @@
return new PrettyPrintWriter(new OutputStreamWriter(out));
}
+ /**
+ * Create and initialize the SAX builder.
+ *
+ * @return the SAX builder instance.
+ * @since upcoming
+ */
+ protected SAXBuilder createBuilder() {
+ SAXBuilder builder = new SAXBuilder();
+ builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+ return builder;
+ }
+
}
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/SjsxpDriver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/SjsxpDriver.java
@@ -58,7 +58,9 @@
protected XMLInputFactory createInputFactory() {
Exception exception = null;
try {
- return (XMLInputFactory)Class.forName("com.sun.xml.internal.stream.XMLInputFactoryImpl").newInstance();
+ XMLInputFactory instance = (XMLInputFactory)Class.forName("com.sun.xml.internal.stream.XMLInputFactoryImpl").newInstance();
+ instance.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ return instance;
} catch (InstantiationException e) {
exception = e;
} catch (IllegalAccessException e) {
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/StandardStaxDriver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/StandardStaxDriver.java
@@ -75,7 +75,9 @@
try {
Class staxInputFactory = JVM.getStaxInputFactory();
if (staxInputFactory != null) {
- return (XMLInputFactory)staxInputFactory.newInstance();
+ XMLInputFactory instance = (XMLInputFactory)staxInputFactory.newInstance();
+ instance.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ return instance;
} else {
throw new StreamException("Java runtime has no standard XMLInputFactory implementation.", exception);
}
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/StaxDriver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/StaxDriver.java
@@ -238,7 +238,9 @@
* @since 1.4
*/
protected XMLInputFactory createInputFactory() {
- return XMLInputFactory.newInstance();
+ XMLInputFactory instance = XMLInputFactory.newInstance();
+ instance.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ return instance;
}
/**
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/WstxDriver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/WstxDriver.java
@@ -62,7 +62,9 @@
}
protected XMLInputFactory createInputFactory() {
- return new WstxInputFactory();
+ XMLInputFactory instance = new WstxInputFactory();
+ instance.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
+ return instance;
}
protected XMLOutputFactory createOutputFactory() {
--- a/xstream/src/java/com/thoughtworks/xstream/io/xml/XomDriver.java
+++ b/xstream/src/java/com/thoughtworks/xstream/io/xml/XomDriver.java
@@ -79,7 +79,7 @@
public HierarchicalStreamReader createReader(Reader text) {
try {
- Document document = builder.build(text);
+ Document document = getBuilder().build(text);
return new XomReader(document, getNameCoder());
} catch (ValidityException e) {
throw new StreamException(e);
@@ -92,7 +92,7 @@
public HierarchicalStreamReader createReader(InputStream in) {
try {
- Document document = builder.build(in);
+ Document document = getBuilder().build(in);
return new XomReader(document, getNameCoder());
} catch (ValidityException e) {
throw new StreamException(e);
@@ -105,7 +105,7 @@
public HierarchicalStreamReader createReader(URL in) {
try {
- Document document = builder.build(in.toExternalForm());
+ Document document = getBuilder().build(in.toExternalForm());
return new XomReader(document, getNameCoder());
} catch (ValidityException e) {
throw new StreamException(e);
@@ -118,7 +118,7 @@
public HierarchicalStreamReader createReader(File in) {
try {
- Document document = builder.build(in);
+ Document document = getBuilder().build(in);
return new XomReader(document, getNameCoder());
} catch (ValidityException e) {
throw new StreamException(e);
|