Skip to content

Commit 888ed14

Browse files
Address review comments
1 parent 0c7ee5d commit 888ed14

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

server/src/main/java/org/elasticsearch/rest/action/search/RestSearchAction.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,18 @@ public static void parseSearchRequest(
200200
searchRequest.source(new SearchSourceBuilder());
201201
}
202202
searchRequest.indices(Strings.splitStringByCommaToArray(request.param("index")));
203+
/*
204+
* We pass this object to the request body parser so that we can extract info such as project_routing.
205+
* We only do it if in a Cross Project Environment, though, because outside it, such details are not
206+
* expected and valid.
207+
*/
208+
SearchRequest searchRequestForParsing = crossProjectEnabled ? searchRequest : null;
203209
if (requestContentParser != null) {
204210
if (searchUsageHolder == null) {
205-
searchRequest.source().parseXContent(searchRequest, requestContentParser, true, clusterSupportsFeature);
211+
searchRequest.source().parseXContent(searchRequestForParsing, requestContentParser, true, clusterSupportsFeature);
206212
} else {
207-
searchRequest.source().parseXContent(searchRequest, requestContentParser, true, searchUsageHolder, clusterSupportsFeature);
213+
searchRequest.source()
214+
.parseXContent(searchRequestForParsing, requestContentParser, true, searchUsageHolder, clusterSupportsFeature);
208215
}
209216
}
210217

server/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,9 @@ private SearchSourceBuilder shallowCopy(
13311331
* Parse some xContent into this SearchSourceBuilder, overwriting any values specified in the xContent.
13321332
*
13331333
* @param searchRequest The SearchRequest object that's representing the request we're parsing which shall receive
1334-
* the parsed info.
1334+
* the parsed info. Currently, this is non-null only if we expect project_routing to appear in
1335+
* the request body, and we allow it to appear because we're in a Cross Project Search
1336+
* environment and require this info.
13351337
* @param parser The xContent parser.
13361338
* @param checkTrailingTokens If true throws a parsing exception when extra tokens are found after the main object.
13371339
* @param searchUsageHolder holder for the search usage statistics
@@ -1370,7 +1372,9 @@ public SearchSourceBuilder parseXContent(
13701372
* usage stats into it is the one to use.
13711373
*
13721374
* @param searchRequest The SearchRequest object that's representing the request we're parsing which shall receive
1373-
* the parsed info.
1375+
* the parsed info. Currently, this is non-null only if we expect project_routing to appear in
1376+
* the request body, and we allow it to appear because we're in a Cross Project Search
1377+
* environment and require this info.
13741378
* @param parser The xContent parser.
13751379
* @param checkTrailingTokens If true throws a parsing exception when extra tokens are found after the main object.
13761380
* @param clusterSupportsFeature used to check if certain features are available on this cluster
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.xpack.search;
11+
12+
import org.elasticsearch.client.Request;
13+
import org.elasticsearch.client.ResponseException;
14+
import org.elasticsearch.common.util.CollectionUtils;
15+
import org.elasticsearch.plugins.Plugin;
16+
import org.elasticsearch.test.ESIntegTestCase;
17+
import org.hamcrest.Matchers;
18+
19+
import java.io.IOException;
20+
import java.util.Collection;
21+
22+
public class ProjectRoutingDisallowedIT extends ESIntegTestCase {
23+
@Override
24+
protected boolean addMockHttpTransport() {
25+
return false;
26+
}
27+
28+
@Override
29+
protected Collection<Class<? extends Plugin>> nodePlugins() {
30+
return CollectionUtils.appendToCopyNoNullElements(super.nodePlugins(), AsyncSearch.class);
31+
}
32+
33+
public void testDisallowProjectRouting() throws IOException {
34+
Request createAsyncRequest = new Request("POST", "/*,*:*/" + randomFrom("_async_search", "_search"));
35+
createAsyncRequest.setJsonEntity("""
36+
{
37+
"project_routing": "_alias:_origin"
38+
}
39+
""");
40+
41+
ResponseException err = expectThrows(ResponseException.class, () -> getRestClient().performRequest(createAsyncRequest));
42+
assertThat(err.toString(), Matchers.containsString("Unknown key for a VALUE_STRING in [project_routing]"));
43+
}
44+
}

0 commit comments

Comments
 (0)