Skip to content

add ability to set per param encoding #40457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/metal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def self.make_response!(request)
end
end

def self.binary_params_for?(action) # :nodoc:
def self.custom_encoding_for(action, param) # :nodoc:
false
end

Expand Down
36 changes: 32 additions & 4 deletions actionpack/lib/action_controller/metal/parameter_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def inherited(klass) # :nodoc:
end

def setup_param_encode # :nodoc:
@_parameter_encodings = {}
@_parameter_encodings = Hash.new { |h, k| h[k] = {} }
end

def binary_params_for?(action) # :nodoc:
@_parameter_encodings[action.to_s]
def custom_encoding_for(action, param) # :nodoc:
@_parameter_encodings[action.to_s][param.to_s]
end

# Specify that a given action's parameters should all be encoded as
Expand Down Expand Up @@ -44,7 +44,35 @@ def binary_params_for?(action) # :nodoc:
# encoded as ASCII-8BIT. This is useful in the case where an application
# must handle data but encoding of the data is unknown, like file system data.
def skip_parameter_encoding(action)
@_parameter_encodings[action.to_s] = true
@_parameter_encodings[action.to_s] = Hash.new { Encoding::ASCII_8BIT }
end

# Specify the encoding for a a parameter on an action
# ASCII-8BIT (it "skips" the encoding default of UTF-8).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also confusing. It seems it is not related with the previous sentence.

#
# For example, a controller would use it like this:
#
# class RepositoryController < ActionController::Base
# param_encoding :show, :file_path, Encoding::ASCII_8BIT
#
# def show
# @repo = Repository.find_by_filesystem_path params[:file_path]
#
# # `repo_name` is guaranteed to be UTF-8, but was ASCII-8BIT, so
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is confusing. If it is guaranteed to be UTF-8 why it was ASCII-8BIT?

# # tag it as such
# @repo_name = params[:repo_name].force_encoding 'UTF-8'
# end
#
# def index
# @repositories = Repository.all
# end
# end
#
# The file_path parameter on the show action would be encoded as ASCII-8BIT.
# This is useful in the case where an application must handle data
# but encoding of the data is unknown, like file system data.
def param_encoding(action, param, encoding)
@_parameter_encodings[action.to_s][param.to_s] = encoding
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def commit_cookie_jar! # :nodoc:
PASS_NOT_FOUND = Class.new { # :nodoc:
def self.action(_); self; end
def self.call(_); [404, { "X-Cascade" => "pass" }, []]; end
def self.binary_params_for?(action); false; end
def self.custom_encoding_for(action, param); nil; end
}

def controller_class
Expand Down
20 changes: 11 additions & 9 deletions actionpack/lib/action_dispatch/request/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def self.check_param_encoding(params)
end

def self.set_binary_encoding(request, params, controller, action)
BinaryParamEncoder.encode(request, params, controller, action)
CustomParamEncoder.encode(request, params, controller, action)
end

class ParamEncoder # :nodoc:
Expand Down Expand Up @@ -78,23 +78,25 @@ def self.handle_array(params)
end
end

class BinaryParamEncoder # :nodoc:
class CustomParamEncoder # :nodoc:
def self.encode(request, params, controller, action)
return params unless controller && controller.valid_encoding?

if binary_params_for?(request, controller, action)
ActionDispatch::Request::Utils.each_param_value(params.except(:controller, :action)) do |param|
param.force_encoding ::Encoding::ASCII_8BIT
params.except(:controller, :action).each do |key, value|
# next if key == :controller || key == :action
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this comment here?

ActionDispatch::Request::Utils.each_param_value(value) do |param|
if desired_encoding = custom_encoding_for(request, controller, action, key)
param.force_encoding(desired_encoding)
end
end
end

params
end

def self.binary_params_for?(request, controller, action)
request.controller_class_for(controller).binary_params_for?(action)
def self.custom_encoding_for(request, controller, action, param)
request.controller_class_for(controller).custom_encoding_for(action, param)
rescue MissingController
false
nil
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

class MultipartParamsParsingTest < ActionDispatch::IntegrationTest
class TestController < ActionController::Base
skip_parameter_encoding("parse_binary")

class << self
attr_accessor :last_request_parameters, :last_parameters

def binary_params_for?(action)
action == "parse_binary"
end
end

def parse_binary
Expand Down
6 changes: 2 additions & 4 deletions actionpack/test/dispatch/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4559,9 +4559,7 @@ def app; APP end

class TestInvalidUrls < ActionDispatch::IntegrationTest
class FooController < ActionController::Base
def self.binary_params_for?(action)
action == "show"
end
param_encoding :show, :id, Encoding::ASCII_8BIT

def show
render plain: "foo#show"
Expand Down Expand Up @@ -4595,7 +4593,7 @@ def show
end
end

test "params encoded with binary_params_for? are treated as ASCII 8bit" do
test "params param_encoding uses ASCII 8bit" do
with_routing do |set|
set.draw do
get "/foo/show(/:id)", to: "test_invalid_urls/foo#show"
Expand Down