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

Commit

Permalink
Add possibility to trust a parameter branch
Browse files Browse the repository at this point in the history
This change introduces a way to mark a specific branch of the parameter
hash as trusted. Using the added `StrongParameters::ANY` or `:*` allows
to mark a value of the hash respectively.

This functionality can be handy when your controllers already rely on
strong_parameters and raising of errors is enabled. If the parameter
hash contains in such a case a parameter value which is completely
customizable by the consumer of the controller, it might be impossible
to predefine keys.
  • Loading branch information
sdepold committed Oct 13, 2015
1 parent 42397cc commit dcf02b8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
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

0 comments on commit dcf02b8

Please sign in to comment.