Skip to content

Commit c49d959

Browse files
author
David Heinemeier Hansson
committed
Merge pull request #7251 from rails/integrate-strong_parameters
Integrate strong_parameters in Rails 4
2 parents ade7010 + 3919fcd commit c49d959

File tree

74 files changed

+662
-2232
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+662
-2232
lines changed

actionpack/lib/action_controller.rb

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'abstract_controller'
33
require 'action_dispatch'
44
require 'action_controller/metal/live'
5+
require 'action_controller/metal/strong_parameters'
56

67
module ActionController
78
extend ActiveSupport::Autoload
@@ -34,6 +35,7 @@ module ActionController
3435
autoload :Rescue
3536
autoload :Responder
3637
autoload :Streaming
38+
autoload :StrongParameters
3739
autoload :Testing
3840
autoload :UrlFor
3941
end

actionpack/lib/action_controller/base.rb

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ def self.without_modules(*modules)
214214
Caching,
215215
MimeResponds,
216216
ImplicitRender,
217+
StrongParameters,
217218

218219
Cookies,
219220
Flash,

actionpack/lib/action_controller/metal/params_wrapper.rb

+2-7
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ module ActionController
4242
# end
4343
#
4444
# On ActiveRecord models with no +:include+ or +:exclude+ option set,
45-
# if attr_accessible is set on that model, it will only wrap the accessible
46-
# parameters, else it will only wrap the parameters returned by the class
47-
# method attribute_names.
45+
# it will only wrap the parameters returned by the class method attribute_names.
4846
#
4947
# If you're going to pass the parameters to an +ActiveModel+ object (such as
5048
# <tt>User.new(params[:user])</tt>), you might consider passing the model class to
@@ -165,10 +163,7 @@ def _set_wrapper_defaults(options, model=nil)
165163

166164
unless options[:include] || options[:exclude]
167165
model ||= _default_wrap_model
168-
role = options.fetch(:as, :default)
169-
if model.respond_to?(:accessible_attributes) && model.accessible_attributes(role).present?
170-
options[:include] = model.accessible_attributes(role).to_a
171-
elsif model.respond_to?(:attribute_names) && model.attribute_names.present?
166+
if model.respond_to?(:attribute_names) && model.attribute_names.present?
172167
options[:include] = model.attribute_names
173168
end
174169
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
require 'active_support/concern'
2+
require 'active_support/core_ext/hash/indifferent_access'
3+
require 'active_support/rescuable'
4+
5+
module ActionController
6+
class ParameterMissing < KeyError
7+
attr_reader :param
8+
9+
def initialize(param)
10+
@param = param
11+
super("key not found: #{param}")
12+
end
13+
end
14+
15+
class Parameters < ActiveSupport::HashWithIndifferentAccess
16+
cattr_accessor :permit_all_parameters, instance_accessor: false
17+
attr_accessor :permitted
18+
alias :permitted? :permitted
19+
20+
def initialize(attributes = nil)
21+
super(attributes)
22+
@permitted = self.class.permit_all_parameters
23+
end
24+
25+
def permit!
26+
@permitted = true
27+
self
28+
end
29+
30+
def require(key)
31+
self[key].presence || raise(ParameterMissing.new(key))
32+
end
33+
34+
alias :required :require
35+
36+
def permit(*filters)
37+
params = self.class.new
38+
39+
filters.each do |filter|
40+
case filter
41+
when Symbol, String then
42+
params[filter] = self[filter] if has_key?(filter)
43+
when Hash then
44+
self.slice(*filter.keys).each do |key, value|
45+
return unless value
46+
47+
key = key.to_sym
48+
49+
params[key] = each_element(value) do |value|
50+
# filters are a Hash, so we expect value to be a Hash too
51+
next if filter.is_a?(Hash) && !value.is_a?(Hash)
52+
53+
value = self.class.new(value) if !value.respond_to?(:permit)
54+
55+
value.permit(*Array.wrap(filter[key]))
56+
end
57+
end
58+
end
59+
end
60+
61+
params.permit!
62+
end
63+
64+
def [](key)
65+
convert_hashes_to_parameters(key, super)
66+
end
67+
68+
def fetch(key, *args)
69+
convert_hashes_to_parameters(key, super)
70+
rescue KeyError
71+
raise ActionController::ParameterMissing.new(key)
72+
end
73+
74+
def slice(*keys)
75+
self.class.new(super)
76+
end
77+
78+
def dup
79+
super.tap do |duplicate|
80+
duplicate.instance_variable_set :@permitted, @permitted
81+
end
82+
end
83+
84+
private
85+
def convert_hashes_to_parameters(key, value)
86+
if value.is_a?(Parameters) || !value.is_a?(Hash)
87+
value
88+
else
89+
# Convert to Parameters on first access
90+
self[key] = self.class.new(value)
91+
end
92+
end
93+
94+
def each_element(object)
95+
if object.is_a?(Array)
96+
object.map { |el| yield el }.compact
97+
elsif object.is_a?(Hash) && object.keys.all? { |k| k =~ /\A-?\d+\z/ }
98+
hash = object.class.new
99+
object.each { |k,v| hash[k] = yield v }
100+
hash
101+
else
102+
yield object
103+
end
104+
end
105+
end
106+
107+
module StrongParameters
108+
extend ActiveSupport::Concern
109+
include ActiveSupport::Rescuable
110+
111+
included do
112+
rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
113+
render text: "Required parameter missing: #{parameter_missing_exception.param}", status: :bad_request
114+
end
115+
end
116+
117+
def params
118+
@_params ||= Parameters.new(request.parameters)
119+
end
120+
121+
def params=(val)
122+
@_params = val.is_a?(Hash) ? Parameters.new(val) : val
123+
end
124+
end
125+
end

actionpack/lib/action_controller/railtie.rb

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class Railtie < Rails::Railtie #:nodoc:
1919
ActionController::Helpers.helpers_path = app.helpers_paths
2020
end
2121

22+
initializer "action_controller.parameters_config" do |app|
23+
ActionController::Parameters.permit_all_parameters = app.config.action_controller.delete(:permit_all_parameters)
24+
end
25+
2226
initializer "action_controller.set_configs" do |app|
2327
paths = app.config.paths
2428
options = app.config.action_controller
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
require 'abstract_unit'
2+
require 'action_controller/metal/strong_parameters'
3+
4+
class NestedParametersTest < ActiveSupport::TestCase
5+
test "permitted nested parameters" do
6+
params = ActionController::Parameters.new({
7+
book: {
8+
title: "Romeo and Juliet",
9+
authors: [{
10+
name: "William Shakespeare",
11+
born: "1564-04-26"
12+
}, {
13+
name: "Christopher Marlowe"
14+
}],
15+
details: {
16+
pages: 200,
17+
genre: "Tragedy"
18+
}
19+
},
20+
magazine: "Mjallo!"
21+
})
22+
23+
permitted = params.permit book: [ :title, { authors: [ :name ] }, { details: :pages } ]
24+
25+
assert permitted.permitted?
26+
assert_equal "Romeo and Juliet", permitted[:book][:title]
27+
assert_equal "William Shakespeare", permitted[:book][:authors][0][:name]
28+
assert_equal "Christopher Marlowe", permitted[:book][:authors][1][:name]
29+
assert_equal 200, permitted[:book][:details][:pages]
30+
assert_nil permitted[:book][:details][:genre]
31+
assert_nil permitted[:book][:authors][1][:born]
32+
assert_nil permitted[:magazine]
33+
end
34+
35+
test "nested arrays with strings" do
36+
params = ActionController::Parameters.new({
37+
:book => {
38+
:genres => ["Tragedy"]
39+
}
40+
})
41+
42+
permitted = params.permit :book => :genres
43+
assert_equal ["Tragedy"], permitted[:book][:genres]
44+
end
45+
46+
test "permit may specify symbols or strings" do
47+
params = ActionController::Parameters.new({
48+
:book => {
49+
:title => "Romeo and Juliet",
50+
:author => "William Shakespeare"
51+
},
52+
:magazine => "Shakespeare Today"
53+
})
54+
55+
permitted = params.permit({:book => ["title", :author]}, "magazine")
56+
assert_equal "Romeo and Juliet", permitted[:book][:title]
57+
assert_equal "William Shakespeare", permitted[:book][:author]
58+
assert_equal "Shakespeare Today", permitted[:magazine]
59+
end
60+
61+
test "nested array with strings that should be hashes" do
62+
params = ActionController::Parameters.new({
63+
book: {
64+
genres: ["Tragedy"]
65+
}
66+
})
67+
68+
permitted = params.permit book: { genres: :type }
69+
assert_empty permitted[:book][:genres]
70+
end
71+
72+
test "nested array with strings that should be hashes and additional values" do
73+
params = ActionController::Parameters.new({
74+
book: {
75+
title: "Romeo and Juliet",
76+
genres: ["Tragedy"]
77+
}
78+
})
79+
80+
permitted = params.permit book: [ :title, { genres: :type } ]
81+
assert_equal "Romeo and Juliet", permitted[:book][:title]
82+
assert_empty permitted[:book][:genres]
83+
end
84+
85+
test "nested string that should be a hash" do
86+
params = ActionController::Parameters.new({
87+
book: {
88+
genre: "Tragedy"
89+
}
90+
})
91+
92+
permitted = params.permit book: { genre: :type }
93+
assert_nil permitted[:book][:genre]
94+
end
95+
96+
test "fields_for-style nested params" do
97+
params = ActionController::Parameters.new({
98+
book: {
99+
authors_attributes: {
100+
:'0' => { name: 'William Shakespeare', age_of_death: '52' },
101+
:'-1' => { name: 'Unattributed Assistant' }
102+
}
103+
}
104+
})
105+
permitted = params.permit book: { authors_attributes: [ :name ] }
106+
107+
assert_not_nil permitted[:book][:authors_attributes]['0']
108+
assert_not_nil permitted[:book][:authors_attributes]['-1']
109+
assert_nil permitted[:book][:authors_attributes]['0'][:age_of_death]
110+
assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['0'][:name]
111+
assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['-1'][:name]
112+
end
113+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
require 'abstract_unit'
2+
require 'action_controller/metal/strong_parameters'
3+
4+
class ParametersPermitTest < ActiveSupport::TestCase
5+
setup do
6+
@params = ActionController::Parameters.new({ person: {
7+
age: "32", name: { first: "David", last: "Heinemeier Hansson" }
8+
}})
9+
end
10+
11+
test "fetch raises ParameterMissing exception" do
12+
e = assert_raises(ActionController::ParameterMissing) do
13+
@params.fetch :foo
14+
end
15+
assert_equal :foo, e.param
16+
end
17+
18+
test "fetch doesnt raise ParameterMissing exception if there is a default" do
19+
assert_equal "monkey", @params.fetch(:foo, "monkey")
20+
assert_equal "monkey", @params.fetch(:foo) { "monkey" }
21+
end
22+
23+
test "permitted is sticky on accessors" do
24+
assert !@params.slice(:person).permitted?
25+
assert !@params[:person][:name].permitted?
26+
27+
@params.each { |key, value| assert(value.permitted?) if key == :person }
28+
29+
assert !@params.fetch(:person).permitted?
30+
31+
assert !@params.values_at(:person).first.permitted?
32+
end
33+
34+
test "permitted is sticky on mutators" do
35+
assert !@params.delete_if { |k| k == :person }.permitted?
36+
assert !@params.keep_if { |k,v| k == :person }.permitted?
37+
end
38+
39+
test "permitted is sticky beyond merges" do
40+
assert !@params.merge(a: "b").permitted?
41+
end
42+
43+
test "modifying the parameters" do
44+
@params[:person][:hometown] = "Chicago"
45+
@params[:person][:family] = { brother: "Jonas" }
46+
47+
assert_equal "Chicago", @params[:person][:hometown]
48+
assert_equal "Jonas", @params[:person][:family][:brother]
49+
end
50+
51+
test "permitting parameters that are not there should not include the keys" do
52+
assert !@params.permit(:person, :funky).has_key?(:funky)
53+
end
54+
55+
test "permit state is kept on a dup" do
56+
@params.permit!
57+
assert_equal @params.permitted?, @params.dup.permitted?
58+
end
59+
60+
test "permitted takes a default value when Parameters.permit_all_parameters is set" do
61+
begin
62+
ActionController::Parameters.permit_all_parameters = true
63+
params = ActionController::Parameters.new({ person: {
64+
age: "32", name: { first: "David", last: "Heinemeier Hansson" }
65+
}})
66+
67+
assert params.slice(:person).permitted?
68+
assert params[:person][:name].permitted?
69+
ensure
70+
ActionController::Parameters.permit_all_parameters = false
71+
end
72+
end
73+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'abstract_unit'
2+
require 'action_controller/metal/strong_parameters'
3+
4+
class ParametersRequireTest < ActiveSupport::TestCase
5+
test "required parameters must be present not merely not nil" do
6+
assert_raises(ActionController::ParameterMissing) do
7+
ActionController::Parameters.new(person: {}).require(:person)
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)