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 @@ -47,7 +47,7 @@ public final class JsonFormat implements EventFormat {
/**
* JSON Data Content Type Discriminator
*/
private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json(;.*)*$");
private static final Pattern JSON_CONTENT_TYPE_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?json(;.*)?$");
private final ObjectMapper mapper;
private final JsonFormatOptions options;

Expand Down
10 changes: 8 additions & 2 deletions formats/xml/src/main/java/io/cloudevents/xml/XMLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@

class XMLUtils {

private static final Pattern XML_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?xml(;.*)*$");
private static final Pattern TEXT_PATTERN = Pattern.compile("^application\\/([a-zA-Z]+\\+)?(xml|json)(;.*)*$");
private static final Pattern XML_PATTERN = Pattern.compile("^(application|text)\\/([a-zA-Z]+\\+)?xml(;.*)?$");
private static final Pattern TEXT_PATTERN = Pattern.compile("^application\\/([a-zA-Z]+\\+)?(xml|json)(;.*)?$");


// Prevent Construction
Expand All @@ -62,6 +62,12 @@ static Document parseIntoDocument(byte[] data) throws CloudEventRWException {
dbf.setNamespaceAware(true);

try {
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setExpandEntityReferences(false);
dbf.setXIncludeAware(false);
DocumentBuilder builder = dbf.newDocumentBuilder();
return builder.parse(new ByteArrayInputStream(data));
} catch (ParserConfigurationException | SAXException | IOException e) {
Expand Down
11 changes: 11 additions & 0 deletions formats/xml/src/test/java/io/cloudevents/xml/XMLUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package io.cloudevents.xml;

import io.cloudevents.rw.CloudEventRWException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -26,6 +27,7 @@

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -58,6 +60,15 @@ public void testChildCount() throws ParserConfigurationException {

}

@Test
public void testRejectsDoctype() {
String xml = "<!DOCTYPE root [<!ENTITY xxe SYSTEM \"file:///etc/passwd\">]><root>&xxe;</root>";
Assertions.assertThrows(
CloudEventRWException.class,
() -> XMLUtils.parseIntoDocument(xml.getBytes(StandardCharsets.UTF_8))
);
}

@ParameterizedTest
@MethodSource("xmlTestContentTypes")
public void testXmlContentType(String contentType, boolean expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<properties>
<module-name>cloudevents.http.restful.ws.jakarta.integration.tests.jersey</module-name>
<jersey.version>3.1.11</jersey.version>
<jersey.version>4.0.2</jersey.version>
</properties>

<dependencies>
Expand Down
Loading