Skip to content

Commit

Permalink
Only allow String and Symbol keys in ActionController::Parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Seva Stefkin committed Apr 6, 2022
1 parent 8d79172 commit 86980d1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,3 +1,9 @@
* Allow only String and Symbol keys in `ActionController::Parameters`.
Raise `ActionController::InvalidParameterKey` when initializing Parameters
with keys that aren't strings or symbols.

*Seva Stefkin*

* Add the ability to use custom logic for storing and retrieving CSRF tokens.

By default, the token will be stored in the session. Custom classes can be
Expand Down
12 changes: 12 additions & 0 deletions actionpack/lib/action_controller/metal/strong_parameters.rb
Expand Up @@ -64,6 +64,16 @@ def initialize # :nodoc:
end
end

# Raised when initializing Parameters with keys that aren't strings or symbols.
#
# ActionController::Parameters.new(123 => 456)
# # => ActionController::InvalidParameterKey: all keys must be Strings or Symbols
class InvalidParameterKey < ArgumentError
def initialize # :nodoc:
super("all keys must be Strings or Symbols")
end
end

# == Action Controller \Parameters
#
# Allows you to choose which attributes should be permitted for mass updating
Expand Down Expand Up @@ -259,6 +269,8 @@ def nested_attribute?(key, value) # :nodoc:
# params.permitted? # => true
# Person.new(params) # => #<Person id: nil, name: "Francesco">
def initialize(parameters = {}, logging_context = {})
raise InvalidParameterKey unless parameters.keys.all? { |key| key.is_a?(String) || key.is_a?(Symbol) }

@parameters = parameters.with_indifferent_access
@logging_context = logging_context
@permitted = self.class.permit_all_parameters
Expand Down
Expand Up @@ -519,4 +519,10 @@ def dup; @dupped = true; end

assert_equal false, params.permitted?
end

test "only String and Symbol keys are allowed" do
assert_raises(ActionController::InvalidParameterKey) do
ActionController::Parameters.new({ foo: 1 } => :bar)
end
end
end

0 comments on commit 86980d1

Please sign in to comment.