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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/96406.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 96406
summary: Fix tchar pattern in `RestRequest`
area: Infra/REST API
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ParsedMediaType {
private final String subType;
private final Map<String, String> parameters;
// tchar pattern as defined by RFC7230 section 3.2.6
private static final Pattern TCHAR_PATTERN = Pattern.compile("[a-zA-z0-9!#$%&'*+\\-.\\^_`|~]+");
private static final Pattern TCHAR_PATTERN = Pattern.compile("[a-zA-Z0-9!#$%&'*+\\-.\\^_`|~]+");

private ParsedMediaType(String originalHeaderValue, String type, String subType, Map<String, String> parameters) {
this.originalHeaderValue = originalHeaderValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.test.ESTestCase;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -108,6 +109,14 @@ public void testEmptyParams() {
assertEquals(Collections.emptyMap(), parsedMediaType.getParameters());
}

public void testMalformedMediaType() {
List<String> headers = List.of("a/b[", "a/b]", "a/b\\");
for (String header : headers) {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ParsedMediaType.parseMediaType(header));
assertThat(e.getMessage(), equalTo("invalid media-type [" + header + "]"));
}
}

public void testMalformedParameters() {
String mediaType = "application/foo";
IllegalArgumentException exception = expectThrows(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
public class RestRequest implements ToXContent.Params {

// tchar pattern as defined by RFC7230 section 3.2.6
private static final Pattern TCHAR_PATTERN = Pattern.compile("[a-zA-z0-9!#$%&'*+\\-.\\^_`|~]+");
private static final Pattern TCHAR_PATTERN = Pattern.compile("[a-zA-Z0-9!#$%&'*+\\-.\\^_`|~]+");

private static final AtomicLong requestIdGenerator = new AtomicLong();

Expand Down
11 changes: 11 additions & 0 deletions server/src/test/java/org/elasticsearch/rest/RestRequestTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,17 @@ public void testMalformedContentTypeHeader() {
assertThat(e.getMessage(), equalTo("Invalid media-type value on headers [Content-Type]"));
}

public void testInvalidMediaTypeCharacter() {
List<String> headers = List.of("a/b[", "a/b]", "a/b\\");
for (String header : headers) {
IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> RestRequest.parseContentType(Collections.singletonList(header))
);
assertThat(e.getMessage(), equalTo("invalid Content-Type header [" + header + "]"));
}
}

public void testNoContentTypeHeader() {
RestRequest contentRestRequest = contentRestRequest("", Collections.emptyMap(), Collections.emptyMap());
assertNull(contentRestRequest.getXContentType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class ParsedMediaType {
// tchar pattern as defined by RFC7230 section 3.2.6
private static final Pattern TCHAR_PATTERN = Pattern.compile("[a-zA-z0-9!#$%&'*+\\-.\\^_`|~]+");
private static final Pattern TCHAR_PATTERN = Pattern.compile("[a-zA-Z0-9!#$%&'*+\\-.\\^_`|~]+");

private final String originalHeaderValue;
private final String type;
Expand Down