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

Commit dcf02b8

Browse files
committed
Add possibility to trust a parameter branch
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.
1 parent 42397cc commit dcf02b8

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ params.require(:token)
7979
params.require(:post).permit(:title)
8080
```
8181

82+
## Permitted parameter branches
83+
84+
In some scenarios it can be useful to mark a branch of the parameter hash as trusted so that a key
85+
is just permitted independent from whether or not it's value is a scalar or matches a specific structure.
86+
You can express that trust like this:
87+
88+
```
89+
params = ActionController::Parameters.new({
90+
:id => 'foo',
91+
:custom_json => {
92+
:bar => 'baz',
93+
:very => 'customizable'
94+
}
95+
})
96+
params.permit({:custom_json => StrongParameters::ANY})
97+
98+
# ==>
99+
# {:custom_json => {:bar => 'baz', :very => 'customizable'}}
100+
```
101+
82102
## Handling of Unpermitted Keys
83103

84104
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.

lib/action_controller/parameters.rb

+3
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ def hash_filter(params, filter)
194194
if filter[key] == []
195195
# Declaration {:comment_ids => []}.
196196
array_of_permitted_scalars_filter(params, key)
197+
elsif filter[key] == ::StrongParameters::ANY
198+
# Declaration {:custom_json => :*} or {:custom_json => StrongParameters::ANY}
199+
params[key] = value
197200
else
198201
# Declaration {:user => :name} or {:user => [:name, :age, {:adress => ...}]}.
199202
params[key] = each_element(value) do |element, index|

lib/strong_parameters.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'action_controller/parameters'
22
require 'active_model/forbidden_attributes_protection'
3+
require 'strong_parameters/any'
34
require 'strong_parameters/railtie'
45
require 'strong_parameters/log_subscriber'

lib/strong_parameters/any.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module StrongParameters
2+
ANY = :*
3+
end

test/parameters_permit_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,21 @@ def assert_filtered_out(params, key)
346346
assert !hash.permitted?
347347
end
348348
end
349+
350+
test "trusted values of nested parameters" do
351+
params = ActionController::Parameters.new({
352+
:resource => {
353+
:id => 'foo',
354+
:custom_json => {
355+
:bar => 'baz',
356+
:qux => {
357+
quux: 1
358+
}
359+
}
360+
}
361+
})
362+
permitted = params.permit(:resource => [{ :custom_json => StrongParameters::ANY }])
363+
assert_nil permitted[:resource][:id]
364+
assert_not_nil permitted[:resource][:custom_json]
365+
end
349366
end

0 commit comments

Comments
 (0)