Skip to content

Commit 8da6a2c

Browse files
committed
Improve validation for ingest and search pipeline id length
Signed-off-by: Owais <[email protected]>
1 parent cf31931 commit 8da6a2c

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2424
### Changed
2525
- Migrate BC libs to their FIPS counterparts ([#14912](https://github.com/opensearch-project/OpenSearch/pull/14912))
2626
- Increase the floor segment size to 16MB ([#17699](https://github.com/opensearch-project/OpenSearch/pull/17699))
27+
- Add validation for Ingest and Search pipeline length ([])
2728

2829
### Dependencies
2930
- Bump `com.nimbusds:nimbus-jose-jwt` from 9.41.1 to 10.0.2 ([#17607](https://github.com/opensearch-project/OpenSearch/pull/17607), [#17669](https://github.com/opensearch-project/OpenSearch/pull/17669))

server/src/main/java/org/opensearch/ingest/IngestService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import java.util.HashSet;
8787
import java.util.Iterator;
8888
import java.util.List;
89+
import java.util.Locale;
8990
import java.util.Map;
9091
import java.util.Objects;
9192
import java.util.Set;
@@ -107,6 +108,7 @@ public class IngestService implements ClusterStateApplier, ReportingService<Inge
107108
public static final String NOOP_PIPELINE_NAME = "_none";
108109

109110
public static final String INGEST_ORIGIN = "ingest";
111+
private static final int MAX_PIPELINE_ID_LENGTH = 20;
110112

111113
/**
112114
* Defines the limit for the number of processors which can run on a given document during ingestion.
@@ -512,6 +514,17 @@ void validatePipeline(Map<DiscoveryNode, IngestInfo> ingestInfos, PutPipelineReq
512514
throw new IllegalStateException("Ingest info is empty");
513515
}
514516

517+
if (request.getId().length() > MAX_PIPELINE_ID_LENGTH) {
518+
throw new IllegalArgumentException(
519+
String.format(
520+
Locale.ROOT,
521+
"Pipeline id [%s] exceeds maximum length of %d characters",
522+
request.getId(),
523+
MAX_PIPELINE_ID_LENGTH
524+
)
525+
);
526+
}
527+
515528
Map<String, Object> pipelineConfig = XContentHelper.convertToMap(request.getSource(), false, request.getMediaType()).v2();
516529
Pipeline pipeline = Pipeline.create(request.getId(), pipelineConfig, processorFactories, scriptService);
517530

server/src/main/java/org/opensearch/search/pipeline/SearchPipelineService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.HashMap;
5555
import java.util.HashSet;
5656
import java.util.List;
57+
import java.util.Locale;
5758
import java.util.Map;
5859
import java.util.Objects;
5960
import java.util.Set;
@@ -73,6 +74,7 @@ public class SearchPipelineService implements ClusterStateApplier, ReportingServ
7374
public static final String SEARCH_PIPELINE_ORIGIN = "search_pipeline";
7475
public static final String AD_HOC_PIPELINE_ID = "_ad_hoc_pipeline";
7576
public static final String NOOP_PIPELINE_ID = "_none";
77+
private static final int MAX_PIPELINE_ID_LENGTH = 20;
7678
private static final Logger logger = LogManager.getLogger(SearchPipelineService.class);
7779
private final ClusterService clusterService;
7880
private final ScriptService scriptService;
@@ -278,6 +280,18 @@ void validatePipeline(Map<DiscoveryNode, SearchPipelineInfo> searchPipelineInfos
278280
if (searchPipelineInfos.isEmpty()) {
279281
throw new IllegalStateException("Search pipeline info is empty");
280282
}
283+
284+
if (request.getId().length() > MAX_PIPELINE_ID_LENGTH) {
285+
throw new IllegalArgumentException(
286+
String.format(
287+
Locale.ROOT,
288+
"Search pipeline id [%s] exceeds maximum length of %d characters",
289+
request.getId(),
290+
MAX_PIPELINE_ID_LENGTH
291+
)
292+
);
293+
}
294+
281295
Map<String, Object> pipelineConfig = XContentHelper.convertToMap(request.getSource(), false, request.getMediaType()).v2();
282296
Pipeline pipeline = PipelineWithMetrics.create(
283297
request.getId(),

server/src/test/java/org/opensearch/ingest/IngestServiceTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,32 @@ public void testValidateNoIngestInfo() throws Exception {
395395
ingestService.validatePipeline(Collections.singletonMap(discoveryNode, ingestInfo), putRequest);
396396
}
397397

398+
public void testValidatePipelineId_WithNotValidLength_ShouldThrowException() throws Exception {
399+
IngestService ingestService = createWithProcessors();
400+
PutPipelineRequest putRequest = new PutPipelineRequest(
401+
"_idwyebdjgeiwnddhekebddmd",
402+
new BytesArray(
403+
"{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}},"
404+
+ "{\"remove\" : {\"field\": \"_field\", \"tag\": \"tag2\"}}]}"
405+
),
406+
MediaTypeRegistry.JSON
407+
);
408+
DiscoveryNode discoveryNode = new DiscoveryNode(
409+
"_node_id",
410+
buildNewFakeTransportAddress(),
411+
emptyMap(),
412+
emptySet(),
413+
Version.CURRENT
414+
);
415+
IngestInfo ingestInfo = new IngestInfo(Collections.singletonList(new ProcessorInfo("set")));
416+
417+
Exception e = expectThrows(
418+
IllegalArgumentException.class,
419+
() -> ingestService.validatePipeline(Collections.singletonMap(discoveryNode, ingestInfo), putRequest)
420+
);
421+
assertEquals("Pipeline id [_idwyebdjgeiwnddhekebddmd] exceeds maximum length of 20 characters", e.getMessage());
422+
}
423+
398424
public void testGetProcessorsInPipeline() throws Exception {
399425
IngestService ingestService = createWithProcessors();
400426
String id = "_id";

server/src/test/java/org/opensearch/search/pipeline/SearchPipelineServiceTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,6 @@ public void testValidatePipeline() throws Exception {
878878

879879
ProcessorInfo reqProcessor = new ProcessorInfo("scale_request_size");
880880
ProcessorInfo rspProcessor = new ProcessorInfo("fixed_score");
881-
ProcessorInfo injProcessor = new ProcessorInfo("max_score");
882881
DiscoveryNode n1 = new DiscoveryNode("n1", buildNewFakeTransportAddress(), Version.CURRENT);
883882
DiscoveryNode n2 = new DiscoveryNode("n2", buildNewFakeTransportAddress(), Version.CURRENT);
884883
PutSearchPipelineRequest putRequest = new PutSearchPipelineRequest(
@@ -893,6 +892,12 @@ public void testValidatePipeline() throws Exception {
893892
MediaTypeRegistry.JSON
894893
);
895894

895+
PutSearchPipelineRequest maxLengthIdPutRequest = new PutSearchPipelineRequest(
896+
"_idwyebdjgeiwnddhekebddmd",
897+
new BytesArray("{\"request_processors\" : [ { \"scale_request_size\": { \"scale\" : \"foo\" } } ] }"),
898+
MediaTypeRegistry.JSON
899+
);
900+
896901
SearchPipelineInfo completePipelineInfo = new SearchPipelineInfo(
897902
Map.of(Pipeline.REQUEST_PROCESSORS_KEY, List.of(reqProcessor), Pipeline.RESPONSE_PROCESSORS_KEY, List.of(rspProcessor))
898903
);
@@ -906,6 +911,12 @@ public void testValidatePipeline() throws Exception {
906911
// Discovery failed, no infos passed.
907912
expectThrows(IllegalStateException.class, () -> searchPipelineService.validatePipeline(Collections.emptyMap(), putRequest));
908913

914+
// Max length of pipeline length
915+
expectThrows(
916+
IllegalStateException.class,
917+
() -> searchPipelineService.validatePipeline(Collections.emptyMap(), maxLengthIdPutRequest)
918+
);
919+
909920
// Invalid configuration in request
910921
PutSearchPipelineRequest badPutRequest = new PutSearchPipelineRequest(
911922
"p1",

0 commit comments

Comments
 (0)