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

Prevent RequestEncoder#encode_params to parse falsey params #33392

Conversation

al3rez
Copy link

@al3rez al3rez commented Jul 19, 2018

Fixes #33388 - When a get method called with as: :json and params: nil
(explicitly or implicitly) RequestEncoder#encode_params converts it
into a null value which includes a unexpected null= query string
into request URL. From now on RequestEncoder#encode_params checks
whether params is nil or not otherwise returns.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@al3rez
Copy link
Author

al3rez commented Jul 19, 2018

@rafaelfranca

@@ -4,6 +4,7 @@
require "controller/fake_controllers"
require "rails/engine"


Copy link
Contributor

Choose a reason for hiding this comment

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

Could you remove this extra line?

Copy link
Author

Choose a reason for hiding this comment

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

done

@@ -34,7 +34,7 @@ def accept_header
end

def encode_params(params)
@param_encoder.call(params)
@param_encoder.call(params) unless params.nil?
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should be fixed in actionpack/lib/action_dispatch/testing/integration.rb See diff:

diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb
index 7637febd1c..9c6075ad0c 100644
--- a/actionpack/lib/action_dispatch/testing/integration.rb
+++ b/actionpack/lib/action_dispatch/testing/integration.rb
@@ -233,7 +233,7 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: n

         request_env = {
           :method => method,
-          :params => request_encoder.encode_params(params),
+          :params => params.nil? ? nil : request_encoder.encode_params(params),

Copy link
Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

Choose a reason for hiding this comment

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

@bogdanvlviv why shouldn't it be fixed in here? Isn't it a special case of the request encoder to handle?

I'd write it as @param_encoder.call(params) if params though.

Should we update some documentation too? E.g. to say that a request encoder will only be asked to encode params when they aren't nil or similar?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, probably would be better just to write @param_encoder.call(params) if params in ActionDispatch::RequestEncoder, Since it is private api we haven't written any documentation.
By the way if

get "/foos_json", as: :json, params: false
request.url # => http://www.example.com/foos_json?false

condition if params would fix it too.

@azbshiri My apologies for confusion. Could you apply @param_encoder.call(params) if params, and update the commit message accordance this change?

Copy link
Author

Choose a reason for hiding this comment

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

done

Copy link
Author

@al3rez al3rez Jul 20, 2018

Choose a reason for hiding this comment

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

I didn't commit and push the changes of Yesterday, it's ready to be merged. @bogdanvlviv

@al3rez al3rez force-pushed the actionpack/prevent-request-encoder-to-parse-nil-params branch from 385a14e to e38128c Compare July 19, 2018 12:33
@bogdanvlviv
Copy link
Contributor

@azbshiri Could you squash commits into one commit?

@al3rez al3rez force-pushed the actionpack/prevent-request-encoder-to-parse-nil-params branch from e38128c to c3ed3e2 Compare July 19, 2018 12:46
@al3rez
Copy link
Author

al3rez commented Jul 19, 2018

done

@al3rez al3rez force-pushed the actionpack/prevent-request-encoder-to-parse-nil-params branch 2 times, most recently from 4f420bf to 34c5472 Compare July 19, 2018 15:33
@al3rez al3rez changed the title Prevent RequestEncoder#encode_params to parse nil params Prevent RequestEncoder#encode_params to parse falsey params Jul 19, 2018
@@ -233,7 +233,7 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: n

request_env = {
:method => method,
:params => request_encoder.encode_params(params),
:params => params.nil? ? nil : request_encoder.encode_params(params),
Copy link
Contributor

@silva96 silva96 Jul 19, 2018

Choose a reason for hiding this comment

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

I would remove this change since this is a specific usage of the encode_params method which already handles the issue.

Copy link
Author

Choose a reason for hiding this comment

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

as i mentioned in tsolar reply that was the case, but i forgot to commit.

@@ -233,7 +233,7 @@ def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: n

request_env = {
:method => method,
:params => request_encoder.encode_params(params),
:params => params.nil? ? nil : request_encoder.encode_params(params),
Copy link

Choose a reason for hiding this comment

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

if encode_params returns nil if params not present, then this conditional is not necessary

Copy link
Author

Choose a reason for hiding this comment

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

@tsolar yeah it was the case, but i didn't commit yesterday.

@al3rez al3rez force-pushed the actionpack/prevent-request-encoder-to-parse-nil-params branch from 34c5472 to 0e59a32 Compare July 20, 2018 04:55
When a `get` method called with `as: :json` and `params: nil` or
`params: false` (explicitly or implicitly)
`RequestEncoder#encode_params` converts it into a `null` or `false`
value which includes a unexpected `null=` or `false` query string into
request URL. From now on `RequestEncoder#encode_params` checks whether
`params` is nil or not otherwise returns.

Move down `nil` conversion guard


Update CHANGELOG.md
@al3rez al3rez force-pushed the actionpack/prevent-request-encoder-to-parse-nil-params branch from 0e59a32 to 9b51ee9 Compare July 20, 2018 10:49
@kaspth kaspth merged commit e7fe07e into rails:master Jul 20, 2018
@kaspth
Copy link
Contributor

kaspth commented Jul 20, 2018

Thanks!

kaspth added a commit that referenced this pull request Jul 20, 2018
…coder-to-parse-nil-params

Prevent `RequestEncoder#encode_params` to parse falsey params
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.

None yet

6 participants