开发者

Manually filter parameters in Rails

开发者 https://www.devze.com 2023-03-09 05:13 出处:网络
How would I go about manually filtering a hash using my application\'s parameter filter? I imagine it\'d go like this:

How would I go about manually filtering a hash using my application's parameter filter?

I imagine it'd go like this:

Rails.application.fil开发者_如何学Goter :password => 'pass1234'
  # => {:password => '[FILTERED]'}

EDIT (clarification): I'm aware that Rails filters the params hash when writing to the logs. What I want to do is apply that same filter to a different hash at my prerogative before writing it to the logs with something like Rails.logger.info. I'm calling a remote HTTP query as a part of my application (since most of the backend operates through a remote API), and I'm logging the URL and parameters passed. I want to have the logs but also ensure that none of the sensitive params show up there.


After a few minutes of shotgunning it, I figured out this was the way to do it:

filters = Rails.application.config.filter_parameters
f = ActionDispatch::Http::ParameterFilter.new filters
f.filter :password => 'haha' # => {:password=>"[FILTERED]"}


See the config/application.rb file, towards the end there is a line:

config.filter_parameters += [:password]

This way the "password" param will not be shown in logs, but you can still access the value normally.

Edit

It seem that have misunderstood your meaning of "filter" originally. As for the clarified issue, I have no idea on how to handle it the truly Rails way.

Here is a brute force approach:

  1. Parse the query with CGI::parse(URI.parse(my_url_address_with_params).query) to get a hash of param/values (note: values are actually stored as an array; here is the discussion).
  2. Locate the parameters you want to filter out and replace values with literal *filtered*.
  3. Call Rails.logger.info (or debug) directly to log.

Here is what you should dig into when relying on Rails magical classes and methods:

In Rails 3 the code that does the trick seems to live in ActionDispatch::Http (ParameterFilter in particular, method `filtered_parameters'). The documentation is available at API Dock (or, to be honest, very little documentation). You can examine the sources to get an idea of how this works.

My knowledge of Rails internals is not good enough to suggest anything else. I believe that someone with a better understanding of it might be of more help.


Building on Steven Xu's answer above, I made this initializer in my rails app:

class ActionController::Parameters
  def filtered
    ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters).filter(self)
  end
end

Which let's me call params.filtered

[1] pry(#<LessonsController>)> params.filtered
{
  "controller" => "lessons",
  "action"     => "search",
  "locale"     => "en"
}
[2] pry(#<LessonsController>)> params[:password] = "bob"
"bob"
[3] pry(#<LessonsController>)> params.filtered
{
  "controller" => "lessons",
  "action"     => "search",
  "locale"     => "en",
  "password"   => "[FILTERED]"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消