2012-07-20 01:32:15 -04:00
|
|
|
require 'abstract_unit'
|
2013-01-23 17:14:47 -05:00
|
|
|
require 'action_dispatch/http/upload'
|
2012-07-12 01:50:42 -04:00
|
|
|
require 'action_controller/metal/strong_parameters'
|
|
|
|
|
2012-08-29 11:08:58 -04:00
|
|
|
class ParametersPermitTest < ActiveSupport::TestCase
|
2013-01-20 11:59:53 -05:00
|
|
|
def assert_filtered_out(params, key)
|
|
|
|
assert !params.has_key?(key), "key #{key.inspect} has not been filtered out"
|
|
|
|
end
|
|
|
|
|
2012-07-12 01:50:42 -04:00
|
|
|
setup do
|
2013-12-23 16:55:03 -05:00
|
|
|
@params = ActionController::Parameters.new(
|
|
|
|
person: {
|
|
|
|
age: '32',
|
|
|
|
name: {
|
|
|
|
first: 'David',
|
|
|
|
last: 'Heinemeier Hansson'
|
|
|
|
},
|
|
|
|
addresses: [{city: 'Chicago', state: 'Illinois'}]
|
|
|
|
}
|
|
|
|
)
|
2013-01-20 11:59:53 -05:00
|
|
|
|
|
|
|
@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
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: '1234')
|
2013-01-20 11:59:53 -05:00
|
|
|
permitted = params.permit
|
2013-01-20 19:01:53 -05:00
|
|
|
assert permitted.permitted?
|
|
|
|
assert permitted.empty?
|
2013-01-20 11:59:53 -05:00
|
|
|
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]
|
2013-02-21 09:00:26 -05:00
|
|
|
values += [STDOUT, StringIO.new, ActionDispatch::Http::UploadedFile.new(tempfile: __FILE__),
|
|
|
|
Rack::Test::UploadedFile.new(__FILE__)]
|
2013-01-20 11:59:53 -05:00
|
|
|
|
|
|
|
values.each do |value|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: value)
|
2013-01-20 11:59:53 -05:00
|
|
|
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
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: '1234', injected: 'injected')
|
2013-01-20 11:59:53 -05:00
|
|
|
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|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: array)
|
2013-01-20 11:59:53 -05:00
|
|
|
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
|
2013-01-22 07:40:33 -05:00
|
|
|
[{}, {foo: 1}, {foo: 'bar'}].each do |hash|
|
|
|
|
params = ActionController::Parameters.new(id: hash)
|
2013-01-20 11:59:53 -05:00
|
|
|
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
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: Object.new)
|
2013-01-20 11:59:53 -05:00
|
|
|
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
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(name: 'Joe')
|
2013-01-20 11:59:53 -05:00
|
|
|
permitted = params.permit(:id)
|
|
|
|
assert !permitted.has_key?(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'key to empty array: empty arrays pass' do
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: [])
|
|
|
|
permitted = params.permit(id: [])
|
2013-01-20 11:59:53 -05:00
|
|
|
assert_equal [], permitted[:id]
|
|
|
|
end
|
|
|
|
|
2013-09-07 08:16:45 -04:00
|
|
|
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
|
|
|
|
|
2013-01-20 11:59:53 -05:00
|
|
|
test 'key to empty array: arrays of permitted scalars pass' do
|
|
|
|
[['foo'], [1], ['foo', 'bar'], [1, 2, 3]].each do |array|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: array)
|
|
|
|
permitted = params.permit(id: [])
|
2013-01-20 11:59:53 -05:00
|
|
|
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|
|
2013-01-22 07:40:33 -05:00
|
|
|
params = ActionController::Parameters.new(id: permitted_scalar)
|
|
|
|
permitted = params.permit(id: [])
|
2013-01-20 11:59:53 -05:00
|
|
|
assert_filtered_out permitted, :id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'key to empty array: arrays of non-permitted scalar do not pass' do
|
2013-01-22 07:40:33 -05:00
|
|
|
[[Object.new], [[]], [[1]], [{}], [{id: '1'}]].each do |non_permitted_scalar|
|
|
|
|
params = ActionController::Parameters.new(id: non_permitted_scalar)
|
|
|
|
permitted = params.permit(id: [])
|
2013-01-20 11:59:53 -05:00
|
|
|
assert_filtered_out permitted, :id
|
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "fetch raises ParameterMissing exception" do
|
|
|
|
e = assert_raises(ActionController::ParameterMissing) do
|
|
|
|
@params.fetch :foo
|
|
|
|
end
|
|
|
|
assert_equal :foo, e.param
|
|
|
|
end
|
|
|
|
|
2013-10-26 22:22:31 -04:00
|
|
|
test "fetch with a default value of a hash does not mutate the object" do
|
|
|
|
params = ActionController::Parameters.new({})
|
|
|
|
params.fetch :foo, {}
|
|
|
|
assert_equal nil, params[:foo]
|
|
|
|
end
|
|
|
|
|
2013-12-20 19:11:47 -05:00
|
|
|
test 'hashes in array values get wrapped' do
|
|
|
|
params = ActionController::Parameters.new(foo: [{}, {}])
|
|
|
|
params[:foo].each do |hash|
|
|
|
|
assert !hash.permitted?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-07 07:17:50 -04:00
|
|
|
# Strong params has an optimization to avoid looping every time you read
|
|
|
|
# a key whose value is an array and building a new object. We check that
|
|
|
|
# optimization here.
|
2013-12-21 08:22:08 -05:00
|
|
|
test 'arrays are converted at most once' do
|
|
|
|
params = ActionController::Parameters.new(foo: [{}])
|
2014-06-07 07:17:50 -04:00
|
|
|
assert_same params[:foo], params[:foo]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Strong params has an internal cache to avoid duplicated loops in the most
|
2014-06-07 07:30:03 -04:00
|
|
|
# common usage pattern. See the docs of the method `converted_arrays`.
|
2014-06-07 07:17:50 -04:00
|
|
|
#
|
|
|
|
# This test checks that if we push a hash to an array (in-place modification)
|
|
|
|
# the cache does not get fooled, the hash is still wrapped as strong params,
|
|
|
|
# and not permitted.
|
|
|
|
test 'mutated arrays are detected' do
|
|
|
|
params = ActionController::Parameters.new(users: [{id: 1}])
|
|
|
|
|
|
|
|
permitted = params.permit(users: [:id])
|
|
|
|
permitted[:users] << {injected: 1}
|
|
|
|
assert_not permitted[:users].last.permitted?
|
2013-12-21 08:22:08 -05:00
|
|
|
end
|
|
|
|
|
2012-07-12 01:50:42 -04:00
|
|
|
test "fetch doesnt raise ParameterMissing exception if there is a default" do
|
2012-08-29 10:54:27 -04:00
|
|
|
assert_equal "monkey", @params.fetch(:foo, "monkey")
|
|
|
|
assert_equal "monkey", @params.fetch(:foo) { "monkey" }
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|
|
|
|
|
2015-07-18 18:40:47 -04:00
|
|
|
test "fetch doesnt raise ParameterMissing exception if there is a default that is nil" do
|
|
|
|
assert_equal nil, @params.fetch(:foo, nil)
|
|
|
|
assert_equal nil, @params.fetch(:foo) { nil }
|
|
|
|
end
|
|
|
|
|
2015-07-19 22:00:36 -04:00
|
|
|
test 'KeyError in fetch block should not be covered up' do
|
2015-07-18 18:40:47 -04:00
|
|
|
params = ActionController::Parameters.new
|
|
|
|
e = assert_raises(KeyError) do
|
|
|
|
params.fetch(:missing_key) { {}.fetch(:also_missing) }
|
|
|
|
end
|
|
|
|
assert_match(/:also_missing$/, e.message)
|
|
|
|
end
|
|
|
|
|
2012-11-06 15:23:24 -05:00
|
|
|
test "not permitted is sticky beyond merges" do
|
2012-07-12 01:50:42 -04:00
|
|
|
assert !@params.merge(a: "b").permitted?
|
|
|
|
end
|
|
|
|
|
2012-11-06 15:30:12 -05:00
|
|
|
test "permitted is sticky beyond merges" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.merge(a: "b").permitted?
|
|
|
|
end
|
|
|
|
|
2012-07-12 01:50:42 -04:00
|
|
|
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
|
2012-08-30 17:36:59 -04:00
|
|
|
|
2012-10-04 14:51:08 -04:00
|
|
|
test "permit is recursive" do
|
|
|
|
@params.permit!
|
|
|
|
assert @params.permitted?
|
|
|
|
assert @params[:person].permitted?
|
|
|
|
assert @params[:person][:name].permitted?
|
2013-12-23 16:55:03 -05:00
|
|
|
assert @params[:person][:addresses][0].permitted?
|
2012-10-04 14:51:08 -04:00
|
|
|
end
|
|
|
|
|
2012-08-30 17:36:59 -04:00
|
|
|
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
|
2012-11-06 15:22:57 -05:00
|
|
|
|
2012-10-31 11:32:24 -04:00
|
|
|
test "permitting parameters as an array" do
|
|
|
|
assert_equal "32", @params[:person].permit([ :age ])[:age]
|
|
|
|
end
|
2014-07-25 12:00:14 -04:00
|
|
|
|
|
|
|
test "to_h returns empty hash on unpermitted params" do
|
2015-12-14 09:52:52 -05:00
|
|
|
assert @params.to_h.is_a? ActiveSupport::HashWithIndifferentAccess
|
2014-07-25 12:00:14 -04:00
|
|
|
assert_not @params.to_h.is_a? ActionController::Parameters
|
|
|
|
assert @params.to_h.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "to_h returns converted hash on permitted params" do
|
|
|
|
@params.permit!
|
|
|
|
|
2015-12-14 09:52:52 -05:00
|
|
|
assert @params.to_h.is_a? ActiveSupport::HashWithIndifferentAccess
|
2014-07-25 12:00:14 -04:00
|
|
|
assert_not @params.to_h.is_a? ActionController::Parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
test "to_h returns converted hash when .permit_all_parameters is set" do
|
|
|
|
begin
|
|
|
|
ActionController::Parameters.permit_all_parameters = true
|
|
|
|
params = ActionController::Parameters.new(crab: "Senjougahara Hitagi")
|
|
|
|
|
2015-12-14 09:52:52 -05:00
|
|
|
assert params.to_h.is_a? ActiveSupport::HashWithIndifferentAccess
|
2014-07-25 12:00:14 -04:00
|
|
|
assert_not @params.to_h.is_a? ActionController::Parameters
|
|
|
|
assert_equal({ "crab" => "Senjougahara Hitagi" }, params.to_h)
|
|
|
|
ensure
|
|
|
|
ActionController::Parameters.permit_all_parameters = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "to_h returns always permitted parameter on unpermitted params" do
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
controller: "users",
|
|
|
|
action: "create",
|
|
|
|
user: {
|
|
|
|
name: "Sengoku Nadeko"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert_equal({ "controller" => "users", "action" => "create" }, params.to_h)
|
|
|
|
end
|
2014-12-12 06:48:42 -05:00
|
|
|
|
|
|
|
test "to_unsafe_h returns unfiltered params" do
|
2015-12-30 10:24:46 -05:00
|
|
|
assert @params.to_unsafe_h.is_a? ActiveSupport::HashWithIndifferentAccess
|
|
|
|
assert_not @params.to_unsafe_h.is_a? ActionController::Parameters
|
2014-12-12 06:48:42 -05:00
|
|
|
end
|
2015-12-17 15:21:12 -05:00
|
|
|
|
2015-12-30 09:20:25 -05:00
|
|
|
test "to_unsafe_h returns unfiltered params even after accessing few keys" do
|
|
|
|
params = ActionController::Parameters.new("f"=>{"language_facet"=>["Tibetan"]})
|
|
|
|
expected = {"f"=>{"language_facet"=>["Tibetan"]}}
|
|
|
|
|
|
|
|
assert params['f'].is_a? ActionController::Parameters
|
|
|
|
assert_equal expected, params.to_unsafe_h
|
|
|
|
end
|
|
|
|
|
2015-12-17 15:21:12 -05:00
|
|
|
test "to_h only deep dups Ruby collections" do
|
|
|
|
company = Class.new do
|
|
|
|
attr_reader :dupped
|
|
|
|
def dup; @dupped = true; end
|
|
|
|
end.new
|
|
|
|
|
|
|
|
params = ActionController::Parameters.new(prem: { likes: %i( dancing ) })
|
|
|
|
assert_equal({ 'prem' => { 'likes' => %i( dancing ) } }, params.permit!.to_h)
|
|
|
|
|
|
|
|
params = ActionController::Parameters.new(companies: [ company, :acme ])
|
|
|
|
assert_equal({ 'companies' => [ company, :acme ] }, params.permit!.to_h)
|
|
|
|
assert_not company.dupped
|
|
|
|
end
|
|
|
|
|
|
|
|
test "to_unsafe_h only deep dups Ruby collections" do
|
|
|
|
company = Class.new do
|
|
|
|
attr_reader :dupped
|
|
|
|
def dup; @dupped = true; end
|
|
|
|
end.new
|
|
|
|
|
|
|
|
params = ActionController::Parameters.new(prem: { likes: %i( dancing ) })
|
|
|
|
assert_equal({ 'prem' => { 'likes' => %i( dancing ) } }, params.to_unsafe_h)
|
|
|
|
|
|
|
|
params = ActionController::Parameters.new(companies: [ company, :acme ])
|
|
|
|
assert_equal({ 'companies' => [ company, :acme ] }, params.to_unsafe_h)
|
|
|
|
assert_not company.dupped
|
|
|
|
end
|
2015-12-29 11:10:04 -05:00
|
|
|
|
|
|
|
test "included? returns true when the key is present" do
|
|
|
|
assert @params.include? :person
|
|
|
|
assert @params.include? 'person'
|
|
|
|
assert_not @params.include? :gorilla
|
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|