Skip to content
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.

Stop fetch from mutating when default is a hash #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/action_controller/parameters.rb
Expand Up @@ -79,7 +79,14 @@ def [](key)
end

def fetch(key, *args)
convert_hashes_to_parameters(key, super)
value = super
# Don't rely on +convert_hashes_to_parameters+
# so as to not mutate via a +fetch+
if value.is_a?(Hash)
value = self.class.new(value)
value.permit! if permitted?
end
value
rescue KeyError, IndexError
raise ActionController::ParameterMissing.new(key)
end
Expand Down
7 changes: 7 additions & 0 deletions test/parameters_taint_test.rb
Expand Up @@ -22,6 +22,13 @@ class ParametersTaintTest < ActiveSupport::TestCase
end
end

test "fetch where the default is a hash will not mutate the instance" do
@params.fetch :foo, {}
assert !@params.has_key?(:foo)
@params.fetch :foo, {:fizz => :buzz}
assert !@params.has_key?(:foo)
end

test "not permitted is sticky on accessors" do
assert !@params.slice(:person).permitted?
assert !@params[:person][:name].permitted?
Expand Down