Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestController should not consume request content #44902

Merged

Conversation

tlrx
Copy link
Member

@tlrx tlrx commented Jul 26, 2019

The change #37504 modifies the BaseRestHandler to make it reject all requests that have an unconsumed body. The notion of consumed or unconsumed body is carried by the RestRequest object and its contentConsumed attribute, which is set to true when the content() or content(true) methods are used.

In our REST layer, we usually expect the RestHandlers to consume the request content when needed, but it appears that the RestController always consumes the content upfront:

final int contentLength = request.hasContent() ? request.content().length() : 0;

making the check in BaseRestHandler useless:

if (request.hasContent() && request.isContentConsumed() == false) {
  throw new IllegalArgumentException("request [" + request.method() + " " + request.path() + "] does not support having a body");
}

This pull request changes the content() method used by the RestController to content(false) so that it does not mark the content as consumed. It also adds a test for this and changes the ForceMergeActionTests.testBodyRejection so that it uses the usual dispactchRequest() execution path (as a normal request would do).

Finally, it fixes some Rollup request objects that were generating a request body for APIs that do not require a body.

@tlrx tlrx requested a review from jasontedor July 26, 2019 12:48
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using this internal variant of content that takes in whether or not to mark the content as consumed, can we add a contentLength() method that will internally check this? I think then content(boolean) can be removed as the only user of content(false) is the check to content length in hasContent().

@tlrx tlrx requested a review from rjernst July 27, 2019 13:18
@tlrx
Copy link
Member Author

tlrx commented Jul 27, 2019

@rjernst Thanks for your feedback. I applied your suggestion, can you please have another look?

@tlrx
Copy link
Member Author

tlrx commented Jul 27, 2019

@elasticmachine update branch

Copy link
Member

@rjernst rjernst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tlrx
Copy link
Member Author

tlrx commented Jul 27, 2019

@elasticmachine run elasticsearch-ci/bwc

@tlrx tlrx merged commit 0f13a3a into elastic:master Jul 28, 2019
@tlrx tlrx deleted the rest-controller-should-not-consume-request-content branch July 28, 2019 08:55
@tlrx
Copy link
Member Author

tlrx commented Jul 28, 2019

Thanks @rjernst ! Because this fix might break existing APIs, I don't think it should be backported to 7.x. Thus I'm removing the 7.3/7.4 labels.

jkakavas pushed a commit that referenced this pull request Jul 31, 2019
The change #37504 modifies the BaseRestHandler to make it reject all requests 
that have an unconsumed body. The notion of consumed or unconsumed body
 is carried by the RestRequest object and its contentConsumed attribute, which
 is set to true when the content() or content(true) methods are used.

In our REST layer, we usually expect the RestHandlers to consume the request 
content when needed, but it appears that the RestController always consumes
 the content upfront.

This commit changes the content() method used by the RestController so that it 
does not mark the content as consumed.
Mpdreamz added a commit to elastic/elasticsearch-net that referenced this pull request Aug 6, 2019
…d any body if no body is documented we relied on serializing empty bodies but the server is now (rightfully) much stricter here.

elastic/elasticsearch#44902
Mpdreamz added a commit to elastic/elasticsearch-net that referenced this pull request Aug 7, 2019
* Update abstractions to version that prints JAVA_HOME just before starting elasticsearch and forces it on the Process instance

* update abstractions

* SkipVersion SNAPSHOT failing tests for now

* Make sure we take `body: null` into account from the spec and not send any body if no body is documented we relied on serializing empty bodies but the server is now (rightfully) much stricter here.

elastic/elasticsearch#44902

* Run codegen for SupportsBody

* Un skipped test failures due to sending bodies where non was expected

* Fix root node info tests when running against snapshots

* Audit trail test assumed timespan is never TimeSpan.Zero but of course can be (#3989)


(cherry picked from commit 79a71ac)
jaymode added a commit that referenced this pull request Dec 14, 2020
The change #37504 modifies the BaseRestHandler to make it reject all requests
that have an unconsumed body. The notion of consumed or unconsumed body
 is carried by the RestRequest object and its contentConsumed attribute, which
 is set to true when the content() or content(true) methods are used.

In our REST layer, we usually expect the RestHandlers to consume the request
content when needed, but it appears that the RestController always consumes
 the content upfront.

This commit changes the content() method used by the RestController so that it
does not mark the content as consumed.

Backport of #44902
Closes #65242

Co-authored-by: Tanguy Leroux <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>bug :Core/Infra/REST API REST infrastructure and utilities v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants