-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invalid JSON request body caused endless loop (#26680)
Request bodys that only consists of a String value can lead to endless loops in the parser of several rest requests like e.g. `_count`. Up to 5.2 this seems to have been caught in the logic guessing the content type of the request, but since then it causes the node to block. This change introduces checks for receiving a valid xContent object before starting the parsing in RestActions#parseTopLevelQueryBuilder(). Closes #26083
- Loading branch information
1 parent
66f0fa8
commit e7509fa
Showing
2 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
core/src/test/java/org/elasticsearch/rest/action/RestActionsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.rest.action; | ||
|
||
import com.fasterxml.jackson.core.io.JsonEOFException; | ||
import java.util.Arrays; | ||
import org.elasticsearch.common.ParsingException; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.json.JsonXContent; | ||
import org.elasticsearch.index.query.MatchQueryBuilder; | ||
import org.elasticsearch.index.query.QueryBuilder; | ||
import org.elasticsearch.search.SearchModule; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
|
||
import java.io.IOException; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
public class RestActionsTests extends ESTestCase { | ||
|
||
private static NamedXContentRegistry xContentRegistry; | ||
|
||
@BeforeClass | ||
public static void init() { | ||
xContentRegistry = new NamedXContentRegistry(new SearchModule(Settings.EMPTY, false, emptyList()).getNamedXContents()); | ||
} | ||
|
||
@AfterClass | ||
public static void cleanup() { | ||
xContentRegistry = null; | ||
} | ||
|
||
public void testParseTopLevelBuilder() throws IOException { | ||
QueryBuilder query = new MatchQueryBuilder("foo", "bar"); | ||
String requestBody = "{ \"query\" : " + query.toString() + "}"; | ||
try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { | ||
QueryBuilder actual = RestActions.getQueryContent(parser); | ||
assertEquals(query, actual); | ||
} | ||
} | ||
|
||
public void testParseTopLevelBuilderEmptyObject() throws IOException { | ||
for (String requestBody : Arrays.asList("{}", "")) { | ||
try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { | ||
QueryBuilder query = RestActions.getQueryContent(parser); | ||
assertNull(query); | ||
} | ||
} | ||
} | ||
|
||
public void testParseTopLevelBuilderMalformedJson() throws IOException { | ||
for (String requestBody : Arrays.asList("\"\"", "\"someString\"", "\"{\"")) { | ||
try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { | ||
ParsingException exception = | ||
expectThrows(ParsingException.class, () -> RestActions.getQueryContent(parser)); | ||
assertEquals("Expected [START_OBJECT] but found [VALUE_STRING]", exception.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
public void testParseTopLevelBuilderIncompleteJson() throws IOException { | ||
for (String requestBody : Arrays.asList("{", "{ \"query\" :")) { | ||
try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { | ||
ParsingException exception = | ||
expectThrows(ParsingException.class, () -> RestActions.getQueryContent(parser)); | ||
assertEquals("Failed to parse", exception.getMessage()); | ||
assertEquals(JsonEOFException.class, exception.getRootCause().getClass()); | ||
} | ||
} | ||
} | ||
|
||
public void testParseTopLevelBuilderUnknownParameter() throws IOException { | ||
String requestBody = "{ \"foo\" : \"bar\"}"; | ||
try (XContentParser parser = createParser(JsonXContent.jsonXContent, requestBody)) { | ||
ParsingException exception = expectThrows(ParsingException.class, () -> RestActions.getQueryContent(parser)); | ||
assertEquals("request does not support [foo]", exception.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
protected NamedXContentRegistry xContentRegistry() { | ||
return xContentRegistry; | ||
} | ||
|
||
} |