Skip to content

Commit

Permalink
Reject bulk requests with invalid actions (#5302) (#5384)
Browse files Browse the repository at this point in the history
The existing bulk api silently ignores bulk item requests that have an invalid action. This change rejects those requests.

Signed-off-by: Rabi Panda <adnapibar@gmail.com>

Signed-off-by: Rabi Panda <adnapibar@gmail.com>
  • Loading branch information
adnapibar authored Nov 26, 2022
1 parent 9c68587 commit 452ed77
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Apply reproducible builds configuration for OpenSearch plugins through gradle plugin ([#4746](https://github.com/opensearch-project/OpenSearch/pull/4746))
- Prevent deletion of snapshots that are backing searchable snapshot indexes ([#5069](https://github.com/opensearch-project/OpenSearch/pull/5069))
- Update to Gradle 7.6 ([#5382](https://github.com/opensearch-project/OpenSearch/pull/5382))

- Reject bulk requests with invalid actions ([#5299](https://github.com/opensearch-project/OpenSearch/issues/5299))
### Dependencies
- Bump bcpg-fips from 1.0.5.1 to 1.0.7.1 ([#5148](https://github.com/opensearch-project/OpenSearch/pull/5148))
- Bumps `commons-compress` from 1.21 to 1.22 ([#5104](https://github.com/opensearch-project/OpenSearch/pull/5104))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

Expand All @@ -78,6 +79,8 @@ public final class BulkRequestParser {
private static final ParseField IF_PRIMARY_TERM = new ParseField("if_primary_term");
private static final ParseField REQUIRE_ALIAS = new ParseField(DocWriteRequest.REQUIRE_ALIAS);

private static final Set<String> VALID_ACTIONS = Set.of("create", "delete", "index", "update");

private static int findNextMarker(byte marker, int from, BytesReference data) {
final int res = data.indexOf(marker, from);
if (res != -1) {
Expand Down Expand Up @@ -177,6 +180,15 @@ public void parse(
);
}
String action = parser.currentName();
if (action == null || VALID_ACTIONS.contains(action) == false) {
throw new IllegalArgumentException(
"Malformed action/metadata line ["
+ line
+ "], expected one of [create, delete, index, update] but found ["
+ action
+ "]"
);
}

String index = defaultIndex;
String id = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,30 @@ public void testParseDeduplicatesParameterStrings() throws IOException {
assertSame(first.getPipeline(), second.getPipeline());
assertSame(first.routing(), second.routing());
}

public void testFailOnUnsupportedAction() {
BytesArray request = new BytesArray("{ \"baz\":{ \"_id\": \"bar\" } }\n{}\n");
BulkRequestParser parser = new BulkRequestParser();

IllegalArgumentException ex = expectThrows(
IllegalArgumentException.class,
() -> parser.parse(
request,
"foo",
null,
null,
null,
true,
false,
XContentType.JSON,
req -> fail(),
req -> fail(),
req -> fail()
)
);
assertEquals(
"Malformed action/metadata line [1], expected one of [create, delete, index, update] but found [baz]",
ex.getMessage()
);
}
}

0 comments on commit 452ed77

Please sign in to comment.