0

我正在我的应用程序控制器中创建一个日志记录方法。我有以下设置,但由于某种原因,我的参数过滤器没有过滤密码。什么不见​​了?如何确保我的应用程序是安全的并且所有密码参数都被过滤了?

配置/应用程序.rb

  # Configure sensitive parameters which will be filtered from the log file.
  config.filter_parameters += [:password]

应用控制器

  before_filter :record_activity

  def record_activity(note = nil)
      @activity={}
      @activity['user'] = current_user
      @activity['note'] = note
      @activity['browser'] = request.env['HTTP_USER_AGENT']
      @activity['ip_address'] = request.env['REMOTE_ADDR']
      @activity['controller'] = controller_name 
      @activity['action'] = action_name 
      @activity['params'] = log_filter(params.inspect)
      p @activity
  end

终端输出

    15:39:38 web.1    | 
{"user"=>nil, 
"note"=>nil, 
"browser"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:20.0) Gecko/20100101 Firefox/20.0",
 "ip_address"=>"127.0.0.1", 
"controller"=>"sessions", 
"action"=>"create", 
"params"=>"{\"utf8\"=>\"✓\", \"authenticity_token\"=>\"dYofOQ64sTajNVn2JiJWVM+E3kz5jCGazrYBObukBAQ=\", 
\"user\"=>{\"email\"=>\"user@domain.com\",
 \"password\"=>\"thepasswordexposed\",
 \"remember_me\"=>\"0\"}, 
\"commit\"=>\"Login\", 
\"action\"=>\"create\", 
\"controller\"=>\"sessions\"}"}

*编辑:* 我添加了以下内容,但它仍然不起作用,有什么建议吗?

  def log_filter(hash)
    filters = Rails.application.config.filter_parameters
    f = ActionDispatch::Http::ParameterFilter.new filters
    f.filter hash
  end

第 36 行出错...

NoMethodError at /users/sign_in    
undefined method `each' for #<String:0x007fa0280b3a68>

36     f.filter hash

回答:

我的解决方案如下,我需要删除 .inspect 并且它开始工作。

  def record_activity(note = nil)
      @activity={}
      @activity['user'] = current_user
      @activity['note'] = note
      @activity['browser'] = request.env['HTTP_USER_AGENT']
      @activity['ip_address'] = request.env['REMOTE_ADDR']
      @activity['controller'] = controller_name 
      @activity['action'] = action_name 
      @activity['params'] = params
      p @activity
  end
4

1 回答 1

3

那是因为config.filter_parameters仅适用于 http 参数(然后您在 中获得params)。它不适用于您自己的对象。

因此,如果您提出请求,那么在日志文件中您将获得一些标准信息

Started GET "/en/projects/1/edit" for 127.0.0.1 at 2013-04-28 04:13:11 +0700
Processing by ProjectsController#edit as HTML
  Parameters: {"locale"=>"en", "id"=>"1"}

在具有Parameters密码值的行中将被过滤它在那里。

但是如果你用了,p还是puts不行。

这里是手动过滤参数的方式

于 2013-04-27T21:15:55.293 回答