Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Lock down JAXB. Don't load remote entities. #2735

Merged
merged 1 commit into from
Apr 17, 2018
Merged
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 @@ -33,6 +33,10 @@ final class JaxbResponseConverter<T> implements Converter<ResponseBody, T> {
JaxbResponseConverter(JAXBContext context, Class<T> type) {
this.context = context;
this.type = type;

// Prevent XML External Entity attacks (XXE).
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
}

@Override public T convert(ResponseBody value) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,55 @@ interface Service {
Response<Contact> response = call.execute();
assertThat(response.body().name).isEqualTo("Jenny");
}

@Test public void externalEntity() throws Exception {
server.enqueue(new MockResponse()
.setBody(""
+ "<?xml version=\"1.0\" ?>"
+ "<!DOCTYPE contact["
+ " <!ENTITY secret SYSTEM \"" + server.url("/secret.txt") + "\">"
+ "]>"
+ "<contact>"
+ "<name>&secret;</name>"
+ "</contact>"));
server.enqueue(new MockResponse()
.setBody("hello"));

Call<Contact> call = service.getXml();
try {
Response<Contact> response = call.execute();
response.body();
fail();
} catch (RuntimeException expected) {
assertThat(expected).hasMessageContaining("ParseError");
}

assertThat(server.getRequestCount()).isEqualTo(1);
}

@Test public void externalDtd() throws Exception {
server.enqueue(new MockResponse()
.setBody(""
+ "<?xml version=\"1.0\" ?>"
+ "<!DOCTYPE contact SYSTEM \"" + server.url("/contact.dtd") + "\">"
+ "<contact>"
+ "<name>&secret;</name>"
+ "</contact>"));
server.enqueue(new MockResponse()
.setBody(""
+ "<!ELEMENT contact (name)>\n"
+ "<!ELEMENT name (#PCDATA)>\n"
+ "<!ENTITY secret \"hello\">"));

Call<Contact> call = service.getXml();
try {
Response<Contact> response = call.execute();
response.body();
fail();
} catch (RuntimeException expected) {
assertThat(expected).hasMessageContaining("ParseError");
}

assertThat(server.getRequestCount()).isEqualTo(1);
}
}