Skip to content

Commit 4ee5f60

Browse files
committed
Merge branch 'feature/permit-disable-namespace-awareness'
2 parents de0c78a + 6483a91 commit 4ee5f60

File tree

6 files changed

+162
-40
lines changed

6 files changed

+162
-40
lines changed

CHANGES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ Fixes:
1111
are now only parsed if this feature is explicitly enabled by passing a boolean
1212
flag value to the #create and #parse methods.
1313
WARNING: This will break code that expects external entities to be parsed.
14+
15+
Enhancements:
16+
17+
* Permit users to disable namespace-awareness in the underlying
18+
DocumentBuilderFactory when constructing the builder with extended `create()`
19+
and `parse()` methods. Namespace awareness is enabled by default unless you
20+
use the more explicit versions of these methods that take additional
21+
`enableExternalEntities` and `isNamespaceAware` parameters.
1422

1523
Version 1.1 - 22 July 2014
1624
--------------------------

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,22 @@ To produce:
351351
</Projects>
352352
```
353353

354+
### Configuring advanced features
355+
356+
When creating or parsing a document you can enable and disable advanced
357+
features by using the more explicit versions of the `parse()` and `create()`
358+
constructors.
359+
360+
You can:
361+
362+
* use the `enableExternalEntities` flag to enable or disable external entities.
363+
NOTE: you should leave these disabled, as they are by default, unless you
364+
really need them because they open you to XML External Entity (XXE) injection
365+
attacks.
366+
* use the `isNamespaceAware` flag to enable or disable namespace awareness in
367+
the underlying `DocumentBuilderFactory`.
368+
369+
354370
Release History
355371
---------------
356372

src/main/java/com/jamesmurty/utils/BaseXMLBuilder.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2014 James Murty (www.jamesmurty.com)
2+
* Copyright 2008-2017 James Murty (www.jamesmurty.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -74,8 +74,6 @@ public abstract class BaseXMLBuilder {
7474
*/
7575
private Node xmlNode = null;
7676

77-
private static boolean isNamespaceAware = true;
78-
7977
/**
8078
* If true, the builder will raise an {@link XMLBuilderRuntimeException}
8179
* if external general and parameter entities cannot be explicitly enabled
@@ -199,14 +197,20 @@ protected static void enableOrDisableExternalEntityParsing(
199197
* the name of the document's root element.
200198
* @param namespaceURI
201199
* default namespace URI for document, ignored if null or empty.
200+
* @param enableExternalEntities
201+
* enable external entities; beware of XML External Entity (XXE) injection.
202+
* @param isNamespaceAware
203+
* enable or disable namespace awareness in the underlying
204+
* {@link DocumentBuilderFactory}
202205
* @return
203206
* an XML Document.
204207
*
205208
* @throws FactoryConfigurationError
206209
* @throws ParserConfigurationException
207210
*/
208211
protected static Document createDocumentImpl(
209-
String name, String namespaceURI, boolean enableExternalEntities)
212+
String name, String namespaceURI, boolean enableExternalEntities,
213+
boolean isNamespaceAware)
210214
throws ParserConfigurationException, FactoryConfigurationError
211215
{
212216
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@@ -229,6 +233,11 @@ protected static Document createDocumentImpl(
229233
*
230234
* @param inputSource
231235
* an XML document input source that will be parsed into a DOM.
236+
* @param enableExternalEntities
237+
* enable external entities; beware of XML External Entity (XXE) injection.
238+
* @param isNamespaceAware
239+
* enable or disable namespace awareness in the underlying
240+
* {@link DocumentBuilderFactory}
232241
* @return
233242
* a builder node that can be used to add more nodes to the XML document.
234243
* @throws ParserConfigurationException
@@ -239,7 +248,8 @@ protected static Document createDocumentImpl(
239248
* @throws SAXException
240249
*/
241250
protected static Document parseDocumentImpl(
242-
InputSource inputSource, boolean enableExternalEntities)
251+
InputSource inputSource, boolean enableExternalEntities,
252+
boolean isNamespaceAware)
243253
throws ParserConfigurationException, SAXException, IOException
244254
{
245255
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

src/main/java/com/jamesmurty/utils/XMLBuilder.java

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2014 James Murty (www.jamesmurty.com)
2+
* Copyright 2008-2017 James Murty (www.jamesmurty.com)
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -101,18 +101,22 @@ protected XMLBuilder(Node myNode, Node parentNode) {
101101
* default namespace URI for document, ignored if null or empty.
102102
* @param enableExternalEntities
103103
* enable external entities; beware of XML External Entity (XXE) injection.
104+
* @param isNamespaceAware
105+
* enable or disable namespace awareness in the underlying
106+
* {@link DocumentBuilderFactory}
104107
* @return
105108
* a builder node that can be used to add more nodes to the XML document.
106109
*
107110
* @throws FactoryConfigurationError
108111
* @throws ParserConfigurationException
109112
*/
110113
public static XMLBuilder create(String name, String namespaceURI,
111-
boolean enableExternalEntities)
114+
boolean enableExternalEntities, boolean isNamespaceAware)
112115
throws ParserConfigurationException, FactoryConfigurationError
113116
{
114117
return new XMLBuilder(
115-
createDocumentImpl(name, namespaceURI, enableExternalEntities));
118+
createDocumentImpl(
119+
name, namespaceURI, enableExternalEntities, isNamespaceAware));
116120
}
117121

118122
/**
@@ -124,16 +128,20 @@ public static XMLBuilder create(String name, String namespaceURI,
124128
* the name of the document's root element.
125129
* @param enableExternalEntities
126130
* enable external entities; beware of XML External Entity (XXE) injection.
131+
* @param isNamespaceAware
132+
* enable or disable namespace awareness in the underlying
133+
* {@link DocumentBuilderFactory}
127134
* @return
128135
* a builder node that can be used to add more nodes to the XML document.
129136
*
130137
* @throws FactoryConfigurationError
131138
* @throws ParserConfigurationException
132139
*/
133-
public static XMLBuilder create(String name, boolean enableExternalEntities)
140+
public static XMLBuilder create(String name, boolean enableExternalEntities,
141+
boolean isNamespaceAware)
134142
throws ParserConfigurationException, FactoryConfigurationError
135143
{
136-
return create(name, null, enableExternalEntities);
144+
return create(name, null, enableExternalEntities, isNamespaceAware);
137145
}
138146

139147
/**
@@ -146,6 +154,7 @@ public static XMLBuilder create(String name, boolean enableExternalEntities)
146154
* the name of the document's root element.
147155
* @param namespaceURI
148156
* default namespace URI for document, ignored if null or empty.
157+
149158
* @return
150159
* a builder node that can be used to add more nodes to the XML document.
151160
*
@@ -155,7 +164,7 @@ public static XMLBuilder create(String name, boolean enableExternalEntities)
155164
public static XMLBuilder create(String name, String namespaceURI)
156165
throws ParserConfigurationException, FactoryConfigurationError
157166
{
158-
return create(name, namespaceURI, false);
167+
return create(name, namespaceURI, false, true);
159168
}
160169

161170
/**
@@ -186,6 +195,9 @@ public static XMLBuilder create(String name)
186195
* an XML document input source that will be parsed into a DOM.
187196
* @param enableExternalEntities
188197
* enable external entities; beware of XML External Entity (XXE) injection.
198+
* @param isNamespaceAware
199+
* enable or disable namespace awareness in the underlying
200+
* {@link DocumentBuilderFactory}
189201
* @return
190202
* a builder node that can be used to add more nodes to the XML document.
191203
* @throws ParserConfigurationException
@@ -196,11 +208,13 @@ public static XMLBuilder create(String name)
196208
* @throws SAXException
197209
*/
198210
public static XMLBuilder parse(
199-
InputSource inputSource, boolean enableExternalEntities)
211+
InputSource inputSource, boolean enableExternalEntities,
212+
boolean isNamespaceAware)
200213
throws ParserConfigurationException, SAXException, IOException
201214
{
202215
return new XMLBuilder(
203-
parseDocumentImpl(inputSource, enableExternalEntities));
216+
parseDocumentImpl(
217+
inputSource, enableExternalEntities, isNamespaceAware));
204218
}
205219

206220
/**
@@ -212,6 +226,9 @@ public static XMLBuilder parse(
212226
* an XML document string that will be parsed into a DOM.
213227
* @param enableExternalEntities
214228
* enable external entities; beware of XML External Entity (XXE) injection.
229+
* @param isNamespaceAware
230+
* enable or disable namespace awareness in the underlying
231+
* {@link DocumentBuilderFactory}
215232
* @return
216233
* a builder node that can be used to add more nodes to the XML document.
217234
*
@@ -222,12 +239,14 @@ public static XMLBuilder parse(
222239
* @throws SAXException
223240
*/
224241
public static XMLBuilder parse(
225-
String xmlString, boolean enableExternalEntities)
242+
String xmlString, boolean enableExternalEntities,
243+
boolean isNamespaceAware)
226244
throws ParserConfigurationException, SAXException, IOException
227245
{
228246
return XMLBuilder.parse(
229247
new InputSource(new StringReader(xmlString)),
230-
enableExternalEntities);
248+
enableExternalEntities,
249+
isNamespaceAware);
231250
}
232251

233252
/**
@@ -239,6 +258,9 @@ public static XMLBuilder parse(
239258
* an XML document file that will be parsed into a DOM.
240259
* @param enableExternalEntities
241260
* enable external entities; beware of XML External Entity (XXE) injection.
261+
* @param isNamespaceAware
262+
* enable or disable namespace awareness in the underlying
263+
* {@link DocumentBuilderFactory}
242264
* @return
243265
* a builder node that can be used to add more nodes to the XML document.
244266
*
@@ -248,11 +270,14 @@ public static XMLBuilder parse(
248270
* @throws IOException
249271
* @throws SAXException
250272
*/
251-
public static XMLBuilder parse(File xmlFile, boolean enableExternalEntities)
273+
public static XMLBuilder parse(File xmlFile, boolean enableExternalEntities,
274+
boolean isNamespaceAware)
252275
throws ParserConfigurationException, SAXException, IOException
253276
{
254277
return XMLBuilder.parse(
255-
new InputSource(new FileReader(xmlFile)), enableExternalEntities);
278+
new InputSource(new FileReader(xmlFile)),
279+
enableExternalEntities,
280+
isNamespaceAware);
256281
}
257282

258283
/**
@@ -274,7 +299,7 @@ public static XMLBuilder parse(File xmlFile, boolean enableExternalEntities)
274299
public static XMLBuilder parse(InputSource inputSource)
275300
throws ParserConfigurationException, SAXException, IOException
276301
{
277-
return XMLBuilder.parse(inputSource, false);
302+
return XMLBuilder.parse(inputSource, false, true);
278303
}
279304

280305
/**
@@ -296,7 +321,7 @@ public static XMLBuilder parse(InputSource inputSource)
296321
public static XMLBuilder parse(String xmlString)
297322
throws ParserConfigurationException, SAXException, IOException
298323
{
299-
return XMLBuilder.parse(xmlString, false);
324+
return XMLBuilder.parse(xmlString, false, true);
300325
}
301326

302327
/**
@@ -318,7 +343,7 @@ public static XMLBuilder parse(String xmlString)
318343
public static XMLBuilder parse(File xmlFile)
319344
throws ParserConfigurationException, SAXException, IOException
320345
{
321-
return XMLBuilder.parse(xmlFile, false);
346+
return XMLBuilder.parse(xmlFile, false, true);
322347
}
323348

324349
@Override

0 commit comments

Comments
 (0)