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

Add possibility to trust a parameter branch #231

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ params.require(:token)
params.require(:post).permit(:title)
```

## Permitted parameter branches

In some scenarios it can be useful to mark a branch of the parameter hash as trusted so that a key
is just permitted independent from whether or not it's value is a scalar or matches a specific structure.
You can express that trust like this:

```
params = ActionController::Parameters.new({
:id => 'foo',
:custom_json => {
:bar => 'baz',
:very => 'customizable'
}
})
params.permit({:custom_json => StrongParameters::ANY})

# ==>
# {:custom_json => {:bar => 'baz', :very => 'customizable'}}
```

## Handling of Unpermitted Keys

By default parameter keys that are not explicitly permitted will be logged in the development and test environment. In other environments these parameters will simply be filtered out and ignored.
Expand Down
3 changes: 3 additions & 0 deletions lib/action_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ def hash_filter(params, filter)
if filter[key] == []
# Declaration {:comment_ids => []}.
array_of_permitted_scalars_filter(params, key)
elsif filter[key] == ::StrongParameters::ANY
# Declaration {:custom_json => :*} or {:custom_json => StrongParameters::ANY}
params[key] = value
else
# Declaration {:user => :name} or {:user => [:name, :age, {:adress => ...}]}.
params[key] = each_element(value) do |element, index|
Expand Down
1 change: 1 addition & 0 deletions lib/strong_parameters.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'action_controller/parameters'
require 'active_model/forbidden_attributes_protection'
require 'strong_parameters/any'
require 'strong_parameters/railtie'
require 'strong_parameters/log_subscriber'
3 changes: 3 additions & 0 deletions lib/strong_parameters/any.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module StrongParameters
ANY = :*
end
17 changes: 17 additions & 0 deletions test/parameters_permit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,21 @@ def assert_filtered_out(params, key)
assert !hash.permitted?
end
end

test "trusted values of nested parameters" do
params = ActionController::Parameters.new({
:resource => {
:id => 'foo',
:custom_json => {
:bar => 'baz',
:qux => {
:quux => 1
}
}
}
})
permitted = params.permit(:resource => [{ :custom_json => StrongParameters::ANY }])
assert_nil permitted[:resource][:id]
assert_not_nil permitted[:resource][:custom_json]
end
end