Skip to content

Commit 39b7bf7

Browse files
authored
Merge pull request rails#44844 from stefkin/acp-key-validation
Only allow String and Symbol keys in ActionController::Parameters
2 parents 8d79172 + 86980d1 commit 39b7bf7

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

actionpack/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Allow only String and Symbol keys in `ActionController::Parameters`.
2+
Raise `ActionController::InvalidParameterKey` when initializing Parameters
3+
with keys that aren't strings or symbols.
4+
5+
*Seva Stefkin*
6+
17
* Add the ability to use custom logic for storing and retrieving CSRF tokens.
28

39
By default, the token will be stored in the session. Custom classes can be

actionpack/lib/action_controller/metal/strong_parameters.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ def initialize # :nodoc:
6464
end
6565
end
6666

67+
# Raised when initializing Parameters with keys that aren't strings or symbols.
68+
#
69+
# ActionController::Parameters.new(123 => 456)
70+
# # => ActionController::InvalidParameterKey: all keys must be Strings or Symbols
71+
class InvalidParameterKey < ArgumentError
72+
def initialize # :nodoc:
73+
super("all keys must be Strings or Symbols")
74+
end
75+
end
76+
6777
# == Action Controller \Parameters
6878
#
6979
# Allows you to choose which attributes should be permitted for mass updating
@@ -259,6 +269,8 @@ def nested_attribute?(key, value) # :nodoc:
259269
# params.permitted? # => true
260270
# Person.new(params) # => #<Person id: nil, name: "Francesco">
261271
def initialize(parameters = {}, logging_context = {})
272+
raise InvalidParameterKey unless parameters.keys.all? { |key| key.is_a?(String) || key.is_a?(Symbol) }
273+
262274
@parameters = parameters.with_indifferent_access
263275
@logging_context = logging_context
264276
@permitted = self.class.permit_all_parameters

actionpack/test/controller/parameters/parameters_permit_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,10 @@ def dup; @dupped = true; end
519519

520520
assert_equal false, params.permitted?
521521
end
522+
523+
test "only String and Symbol keys are allowed" do
524+
assert_raises(ActionController::InvalidParameterKey) do
525+
ActionController::Parameters.new({ foo: 1 } => :bar)
526+
end
527+
end
522528
end

0 commit comments

Comments
 (0)