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

Make changes feed return bad request for invalid heartbeat values #2270

Merged
merged 1 commit into from Oct 23, 2019

Conversation

bessbd
Copy link
Member

@bessbd bessbd commented Oct 23, 2019

Overview

Using a negative heartbeat value does not return a 400 bad request, instead getting just an empty response with no status code at all.

This commit adds extra checks so that negative and non-integer heartbeat values return 400 bad request responses.

Testing recommendations

The changes has been tested manually:

[donat:~/couchdb] changes-feed-input-validation 1 ± curl -XPUT http://127.0.0.1:15984/asd
{"ok":true}
[donat:~/couchdb] changes-feed-input-validation ± curl -v http://127.0.0.1:15984/asd/_changes?feed=continuous\&heartbeat=-1000
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 15984 (#0)
> GET /asd/_changes?feed=continuous&heartbeat=-1000 HTTP/1.1
> Host: 127.0.0.1:15984
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Cache-Control: must-revalidate
< Content-Length: 60
< Content-Type: application/json
< Date: Wed, 23 Oct 2019 15:10:52 GMT
< Server: CouchDB/3.0.0-1dd00d6ac (Erlang OTP/22)
< X-Couch-Request-ID: 61d1b854bd
< X-CouchDB-Body-Time: 0
<
{"error":"bad_request","reason":"Negative heartbeat value"}
* Connection #0 to host 127.0.0.1 left intact
* Closing connection 0
[donat:~/couchdb] changes-feed-input-validation ± curl -v http://127.0.0.1:15984/asd/_changes?feed=continuous\&heartbeat=a1000
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 15984 (#0)
> GET /asd/_changes?feed=continuous&heartbeat=a1000 HTTP/1.1
> Host: 127.0.0.1:15984
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Cache-Control: must-revalidate
< Content-Length: 59
< Content-Type: application/json
< Date: Wed, 23 Oct 2019 15:11:01 GMT
< Server: CouchDB/3.0.0-1dd00d6ac (Erlang OTP/22)
< X-Couch-Request-ID: 4e9ae14ede
< X-CouchDB-Body-Time: 0
<
{"error":"bad_request","reason":"Invalid heartbeat value"}
* Connection #0 to host 127.0.0.1 left intact
* Closing connection 0

Also, there are new test cases added for the new behavior.

Related Issues or Pull Requests

This fixes #2234

Checklist

  • Code is written and works correctly
  • Changes are covered by tests
  • Any new configurable parameters are documented in rel/overlay/etc/default.ini
  • A PR for documentation changes has been made in https://github.com/apache/couchdb-documentation

Copy link
Contributor

@iilyak iilyak left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution @bessbd.
Would you mind writing a test case for it.
You can use any of the available test suites:

Args#changes_args{heartbeat=list_to_integer(Value)};
try list_to_integer(Value) of
V when V > 0 ->
Args#changes_args{heartbeat=heartbeat_integer};
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think heartbeat_integer is correct.

Args#changes_args{heartbeat=heartbeat_integer};

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call! I've just pushed a commit to fix this.

@iilyak
Copy link
Contributor

iilyak commented Oct 23, 2019

@iilyak Would you mind writing a test case for it.

I am so sorry I missed the fact that you have some tests

HeartbeatInteger when HeartbeatInteger > 0 ->
Args#changes_args{heartbeat=HeartbeatInteger};
_ ->
throw({bad_request, <<"Negative heartbeat value">>})
Copy link
Contributor

@iilyak iilyak Oct 23, 2019

Choose a reason for hiding this comment

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

The "Negative heartbeat value" doesn't sound correct if user pass 0. Should we change the wording to something like:

  • The heartbeat value should be positive integer (in milliseconds)

_ ->
throw({bad_request, <<"Negative heartbeat value">>})
catch error:badarg ->
throw({bad_request, <<"Invalid heartbeat value">>})
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to give user a hint. Something like:

<<"Invalid heartbeat value. Expecting positive integer value (in milliseconds).">>

@bessbd
Copy link
Member Author

bessbd commented Oct 23, 2019

@iilyak : thank you for the review! I've just pushed a commit (cabe4c4) to make the changes you've suggested.

Copy link
Contributor

@iilyak iilyak left a comment

Choose a reason for hiding this comment

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

+1 conditional to CI pass

@iilyak
Copy link
Contributor

iilyak commented Oct 23, 2019

@bessbd Could you squash commits.

Using a negative heartbeat value does not return a 400 bad request, instead getting just an empty response with no status code at all.

This commit adds extra checks so that negative and non-integer heartbeat values return 400 bad request responses.

This fixes apache#2234
@bessbd bessbd force-pushed the changes-feed-input-validation branch from cabe4c4 to a47f0fa Compare October 23, 2019 16:35
@bessbd
Copy link
Member Author

bessbd commented Oct 23, 2019

@bessbd Could you squash commits.

Done!

@iilyak iilyak merged commit be2364d into apache:master Oct 23, 2019
@iilyak
Copy link
Contributor

iilyak commented Oct 23, 2019

Thank you for your contribution.

@bessbd
Copy link
Member Author

bessbd commented Oct 24, 2019

Thank you for the review and the merge, @iilyak !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

/db/_changes feed continuous mode does not return bad request for negative heartbeat values
2 participants