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

Stop changes to a dupped `ActionController::Parameters` mutating the original #25735

Merged
merged 2 commits into from Jul 11, 2016

Conversation

Projects
None yet
7 participants
@timrogers
Contributor

timrogers commented Jul 7, 2016

When ActionController::Parameters is duplicated with #dup, it doesn't create a duplicate of the instance variables (e.g. @parameters) but rather maintains the reference (see http://ruby-doc.org/core-2.3.1/Object.html). Given that the parameters object is often manipulated as if it were a hash (e.g. with #delete and similar methods), this leads to unexpected behaviour, like the following:

params = ActionController::Parameters.new(foo: "bar")
duplicated_params = params.dup
duplicated_params.delete(:foo)

params == duplicated_params

This fixes the bug by defining a private #initialize_copy method, used internally by #dup, which makes a copy of @parameters.

Tim Rogers
Changes to a dupped `ActionController::Parameters` mutate the original
When `ActionController::Parameters` is duplicated with `#dup`, it doesn't create a duplicate of the instance variables (e.g. `@parameters`) but rather maintains the reference (see <http://ruby-doc.org/core-2.3.1/Object.html>). Given that the parameters object is often manipulated as if it were a hash (e.g. with `#delete` and similar methods), this leads to unexpected behaviour, like the following:

```
params = ActionController::Parameters.new(foo: "bar")
duplicated_params = params.dup
duplicated_params.delete(:foo)

params == duplicated_params
```

This fixes the bug by defining a private `#initialize_copy` method, used internally by `#dup`, which makes a copy of `@parameters`.
@rails-bot

This comment has been minimized.

rails-bot commented Jul 7, 2016

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @chancancode (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.

Please see the contribution instructions for more information.

@timrogers timrogers changed the title from Changes to a dupped `ActionController::Parameters` mutate the original to Stop changes to a dupped `ActionController::Parameters` mutating the original Jul 7, 2016

@maclover7 maclover7 added the actionpack label Jul 7, 2016

@bluehallu

This comment has been minimized.

bluehallu commented Jul 7, 2016

Isn't

new_params = params.except :foo

The better approach?

@timrogers

This comment has been minimized.

Contributor

timrogers commented Jul 8, 2016

@bluehallu Using #except is a partial solution, but to me the #dup behaviour seems pretty broken! Keen to see what @chancancode thinks though.

@timrogers

This comment has been minimized.

Contributor

timrogers commented Jul 8, 2016

I must say, I'm confused by the intention of the existing #dup method - wouldn't @permitted get copied anyway?

def initialize_copy(source)
super
@parameters = source.instance_variable_get(:@parameters).dup

This comment has been minimized.

@matthewd

matthewd Jul 8, 2016

Member

At this point, we can just use @parameters = @parameters.dup, I think?

@matthewd

This comment has been minimized.

Member

matthewd commented Jul 8, 2016

I must say, I'm confused by the intention of the existing #dup method - wouldn't @Permitted get copied anyway?

I imagine that may not have been true while this class inherited from Hash.

If you can confirm it works the same without it (and that we have test coverage to back that up), this seems like a good opportunity to clean up, before we make things look even more complicated, with a custom dup and an initialize_copy.

Tim Rogers
Trust `Object#dup` in `ActionController::Parameters`, using `#initial…
…ize_copy` to manually duplicate the underlying parameters hash

It looks like `ActionController::Parameters#dup` is leftover from when the class inherited from `Hash`. We can just trust `#dup`, which already copies the `@Permitted` instance variable (confirmed by tests). We still define a `#initialize_copy` to make `@parameters` a copy that can be mutated without affecting the original instance.
@timrogers

This comment has been minimized.

Contributor

timrogers commented Jul 8, 2016

@matthewd Agreed. Can you take another look? I've removed #dup altogether and replaced it with a simpler #initialize_copy. Some tests I've added confirm that @permitted is preserved.

@matthewd matthewd merged commit e6352db into rails:master Jul 11, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@matthewd

This comment has been minimized.

Member

matthewd commented Jul 11, 2016

Thanks! ❤️

prathamesh-sonpatki added a commit to prathamesh-sonpatki/rails that referenced this pull request Jul 12, 2016

Remove duplicate test and fix a typo in the test
- Tests for dup'ing params was separately added in a separate file in
  rails#25735.

rafaelfranca added a commit that referenced this pull request Aug 18, 2016

Merge pull request #25735 from timrogers/actioncontroller-parameters-dup
Stop changes to a dupped `ActionController::Parameters` mutating the original
@rafaelfranca

This comment has been minimized.

Member

rafaelfranca commented Aug 18, 2016

Backported in 702db0c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment