Skip to content

Commit 340313b

Browse files
authored
RestAPI: Reject forcemerge requests with a body (#30792)
This commit adds validation to forcemerge rest requests which contain a body. All parameters to force merge must be part of http params. closes #29584
1 parent f3297ed commit 340313b

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestForceMergeAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public String getName() {
4747

4848
@Override
4949
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
50+
if (request.hasContent()) {
51+
throw new IllegalArgumentException("forcemerge takes arguments in query parameters, not in the request body");
52+
}
5053
ForceMergeRequest mergeRequest = new ForceMergeRequest(Strings.splitStringByCommaToArray(request.param("index")));
5154
mergeRequest.indicesOptions(IndicesOptions.fromRequest(request, mergeRequest.indicesOptions()));
5255
mergeRequest.maxNumSegments(request.paramAsInt("max_num_segments", mergeRequest.maxNumSegments()));
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.action.admin.indices.forcemerge;
21+
22+
import org.elasticsearch.client.node.NodeClient;
23+
import org.elasticsearch.common.bytes.BytesArray;
24+
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
26+
import org.elasticsearch.common.xcontent.XContentType;
27+
import org.elasticsearch.common.xcontent.json.JsonXContent;
28+
import org.elasticsearch.rest.RestController;
29+
import org.elasticsearch.rest.action.admin.indices.RestForceMergeAction;
30+
import org.elasticsearch.test.ESTestCase;
31+
import org.elasticsearch.test.rest.FakeRestRequest;
32+
33+
import static org.hamcrest.Matchers.equalTo;
34+
import static org.mockito.Mockito.mock;
35+
36+
public class RestForceMergeActionTests extends ESTestCase {
37+
38+
public void testBodyRejection() throws Exception {
39+
final RestForceMergeAction handler = new RestForceMergeAction(Settings.EMPTY, mock(RestController.class));
40+
String json = JsonXContent.contentBuilder().startObject().field("max_num_segments", 1).endObject().toString();
41+
final FakeRestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY)
42+
.withContent(new BytesArray(json), XContentType.JSON).build();
43+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
44+
() -> handler.prepareRequest(request, mock(NodeClient.class)));
45+
assertThat(e.getMessage(), equalTo("forcemerge takes arguments in query parameters, not in the request body"));
46+
}
47+
}

0 commit comments

Comments
 (0)