1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
require 'abstract_unit'
require 'action_dispatch/http/upload'
require 'action_controller/metal/strong_parameters'
class ParametersPermitTest < ActiveSupport::TestCase
def assert_filtered_out(params, key)
assert !params.has_key?(key), "key #{key.inspect} has not been filtered out"
end
setup do
@params = ActionController::Parameters.new({ person: {
age: "32", name: { first: "David", last: "Heinemeier Hansson" }
}})
@struct_fields = []
%w(0 1 12).each do |number|
['', 'i', 'f'].each do |suffix|
@struct_fields << "sf(#{number}#{suffix})"
end
end
end
test 'if nothing is permitted, the hash becomes empty' do
params = ActionController::Parameters.new(id: '1234')
permitted = params.permit
assert permitted.permitted?
assert permitted.empty?
end
test 'key: permitted scalar values' do
values = ['a', :a, nil]
values += [0, 1.0, 2**128, BigDecimal.new(1)]
values += [true, false]
values += [Date.today, Time.now, DateTime.now]
values += [STDOUT, StringIO.new, ActionDispatch::Http::UploadedFile.new(tempfile: __FILE__),
Rack::Test::UploadedFile.new(__FILE__)]
values.each do |value|
params = ActionController::Parameters.new(id: value)
permitted = params.permit(:id)
assert_equal value, permitted[:id]
@struct_fields.each do |sf|
params = ActionController::Parameters.new(sf => value)
permitted = params.permit(:sf)
assert_equal value, permitted[sf]
end
end
end
test 'key: unknown keys are filtered out' do
params = ActionController::Parameters.new(id: '1234', injected: 'injected')
permitted = params.permit(:id)
assert_equal '1234', permitted[:id]
assert_filtered_out permitted, :injected
end
test 'key: arrays are filtered out' do
[[], [1], ['1']].each do |array|
params = ActionController::Parameters.new(id: array)
permitted = params.permit(:id)
assert_filtered_out permitted, :id
@struct_fields.each do |sf|
params = ActionController::Parameters.new(sf => array)
permitted = params.permit(:sf)
assert_filtered_out permitted, sf
end
end
end
test 'key: hashes are filtered out' do
[{}, {foo: 1}, {foo: 'bar'}].each do |hash|
params = ActionController::Parameters.new(id: hash)
permitted = params.permit(:id)
assert_filtered_out permitted, :id
@struct_fields.each do |sf|
params = ActionController::Parameters.new(sf => hash)
permitted = params.permit(:sf)
assert_filtered_out permitted, sf
end
end
end
test 'key: non-permitted scalar values are filtered out' do
params = ActionController::Parameters.new(id: Object.new)
permitted = params.permit(:id)
assert_filtered_out permitted, :id
@struct_fields.each do |sf|
params = ActionController::Parameters.new(sf => Object.new)
permitted = params.permit(:sf)
assert_filtered_out permitted, sf
end
end
test 'key: it is not assigned if not present in params' do
params = ActionController::Parameters.new(name: 'Joe')
permitted = params.permit(:id)
assert !permitted.has_key?(:id)
end
test 'key to empty array: empty arrays pass' do
params = ActionController::Parameters.new(id: [])
permitted = params.permit(id: [])
assert_equal [], permitted[:id]
end
test 'do not break params filtering on nil values' do
params = ActionController::Parameters.new(a: 1, b: [1, 2, 3], c: nil)
permitted = params.permit(:a, c: [], b: [])
assert_equal 1, permitted[:a]
assert_equal [1, 2, 3], permitted[:b]
assert_equal nil, permitted[:c]
end
test 'key to empty array: arrays of permitted scalars pass' do
[['foo'], [1], ['foo', 'bar'], [1, 2, 3]].each do |array|
params = ActionController::Parameters.new(id: array)
permitted = params.permit(id: [])
assert_equal array, permitted[:id]
end
end
test 'key to empty array: permitted scalar values do not pass' do
['foo', 1].each do |permitted_scalar|
params = ActionController::Parameters.new(id: permitted_scalar)
permitted = params.permit(id: [])
assert_filtered_out permitted, :id
end
end
test 'key to empty array: arrays of non-permitted scalar do not pass' do
[[Object.new], [[]], [[1]], [{}], [{id: '1'}]].each do |non_permitted_scalar|
params = ActionController::Parameters.new(id: non_permitted_scalar)
permitted = params.permit(id: [])
assert_filtered_out permitted, :id
end
end
test "fetch raises ParameterMissing exception" do
e = assert_raises(ActionController::ParameterMissing) do
@params.fetch :foo
end
assert_equal :foo, e.param
end
test "fetch doesnt raise ParameterMissing exception if there is a default" do
assert_equal "monkey", @params.fetch(:foo, "monkey")
assert_equal "monkey", @params.fetch(:foo) { "monkey" }
end
test "not permitted is sticky on accessors" do
assert !@params.slice(:person).permitted?
assert !@params[:person][:name].permitted?
assert !@params[:person].except(:name).permitted?
@params.each { |key, value| assert(!value.permitted?) if key == "person" }
assert !@params.fetch(:person).permitted?
assert !@params.values_at(:person).first.permitted?
end
test "permitted is sticky on accessors" do
@params.permit!
assert @params.slice(:person).permitted?
assert @params[:person][:name].permitted?
assert @params[:person].except(:name).permitted?
@params.each { |key, value| assert(value.permitted?) if key == "person" }
assert @params.fetch(:person).permitted?
assert @params.values_at(:person).first.permitted?
end
test "not permitted is sticky on mutators" do
assert !@params.delete_if { |k| k == "person" }.permitted?
assert !@params.keep_if { |k,v| k == "person" }.permitted?
end
test "permitted is sticky on mutators" do
@params.permit!
assert @params.delete_if { |k| k == "person" }.permitted?
assert @params.keep_if { |k,v| k == "person" }.permitted?
end
test "not permitted is sticky beyond merges" do
assert !@params.merge(a: "b").permitted?
end
test "permitted is sticky beyond merges" do
@params.permit!
assert @params.merge(a: "b").permitted?
end
test "modifying the parameters" do
@params[:person][:hometown] = "Chicago"
@params[:person][:family] = { brother: "Jonas" }
assert_equal "Chicago", @params[:person][:hometown]
assert_equal "Jonas", @params[:person][:family][:brother]
end
test "permit state is kept on a dup" do
@params.permit!
assert_equal @params.permitted?, @params.dup.permitted?
end
test "permit is recursive" do
@params.permit!
assert @params.permitted?
assert @params[:person].permitted?
assert @params[:person][:name].permitted?
end
test "permitted takes a default value when Parameters.permit_all_parameters is set" do
begin
ActionController::Parameters.permit_all_parameters = true
params = ActionController::Parameters.new({ person: {
age: "32", name: { first: "David", last: "Heinemeier Hansson" }
}})
assert params.slice(:person).permitted?
assert params[:person][:name].permitted?
ensure
ActionController::Parameters.permit_all_parameters = false
end
end
test "permitting parameters as an array" do
assert_equal "32", @params[:person].permit([ :age ])[:age]
end
end
|