Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ public static XmlSerializer createSerializer() {

/** Returns a new XML pull parser. */
public static XmlPullParser createParser() throws XmlPullParserException {
return getParserFactory().newPullParser();
XmlPullParser parser = getParserFactory().newPullParser();
try {
parser.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, false);
} catch (XmlPullParserException e) {
// Ignore if the feature is not supported
}
return parser;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.client.util.ArrayMap;
import com.google.api.client.util.Key;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -31,6 +32,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

/**
Expand Down Expand Up @@ -621,4 +623,25 @@ private static class AllType {
@Key public String[] stringArray;
@Key public List<Integer> integerCollection;
}

@Test
public void testCreateParser_disablesDocDecl() throws Exception {
XmlPullParser parser = Xml.createParser();
String xmlWithDtd = "<?xml version=\"1.0\"?>\n"
+ "<!DOCTYPE any [\n"
+ " <!ENTITY xxe \"injected\">\n"
+ "]>\n"
+ "<any>&xxe;</any>";
parser.setInput(new StringReader(xmlWithDtd));
try {
SimpleTypeString xml = new SimpleTypeString();
XmlNamespaceDictionary namespaceDictionary = new XmlNamespaceDictionary().set("", "");
Xml.parseElement(parser, xml, namespaceDictionary, null);
if (xml.value != null) {
assertTrue(xml.value.isEmpty() || xml.value.equals("&xxe;"));
}
} catch (XmlPullParserException | IOException e) {
// Expected exception if the parser fails on DOCDECL
}
}
}
Loading