Skip to content

Commit 72f8199

Browse files
adi-dhulipalaChristoph Büscher
authored andcommitted
Validate xContentType in PutWatchRequest. (#31088)
Trying to post a new watch without any body currently results in a NullPointerException. This change fixes that by validating that Post and Put requests always have a body. Closes #30057
1 parent 6030d4b commit 72f8199

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/watcher/transport/actions/put/PutWatchRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public ActionRequestValidationException validate() {
135135
if (source == null) {
136136
validationException = ValidateActions.addValidationError("watch source is missing", validationException);
137137
}
138+
if (xContentType == null) {
139+
validationException = ValidateActions.addValidationError("request body is missing", validationException);
140+
}
138141
return validationException;
139142
}
140143

x-pack/plugin/src/test/resources/rest-api-spec/api/xpack.watcher.put_watch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"body": {
2727
"description" : "The watch",
28-
"required" : true
28+
"required" : false
2929
}
3030
}
3131
}

x-pack/plugin/src/test/resources/rest-api-spec/test/watcher/put_watch/10_basic.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,16 @@
3636
}
3737
}
3838
- match: { _id: "my_watch" }
39+
40+
---
41+
"Test empty body is rejected by put watch":
42+
- do:
43+
cluster.health:
44+
wait_for_status: yellow
45+
46+
- do:
47+
catch: bad_request
48+
xpack.watcher.put_watch:
49+
id: "my_watch"
50+
- match: { error.root_cause.0.type: "action_request_validation_exception" }
51+
- match: { error.root_cause.0.reason: "Validation Failed: 1: request body is missing;" }

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/transport/action/WatchRequestValidationTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ public void testPutWatchSourceNull() {
8686
assertThat(e.validationErrors(), hasItem("watch source is missing"));
8787
}
8888

89+
public void testPutWatchContentNull() {
90+
ActionRequestValidationException e = new PutWatchRequest("foo", BytesArray.EMPTY, null).validate();
91+
assertThat(e, is(notNullValue()));
92+
assertThat(e.validationErrors(), hasItem("request body is missing"));
93+
}
94+
8995
public void testGetWatchInvalidWatchId() {
9096
ActionRequestValidationException e = new GetWatchRequest("id with whitespaces").validate();
9197
assertThat(e, is(notNullValue()));

0 commit comments

Comments
 (0)