密码未在日志中过滤

时间:2018-01-25 02:26:22

标签: ruby-on-rails ruby

根据Rails文档

  

config.filter_parameters用于过滤掉那些参数   您不希望在日志中显示,例如密码或信用卡   数字。默认情况下,Rails通过添加来过滤掉密码   Rails.application.config.filter_parameters += [:password] in   config/initializers/filter_parameter_logging.rb。参数过滤器   通过部分匹配正则表达式来工作。

那么,为什么我提交下面的表格

<%= form_with model: @user, url: admin_user_path, method: :delete do %>
    <%= label_tag :password, t('forms.password') %>
    <%= text_field_tag :password, nil %>
    <%= button_tag t('forms.save'), type: 'submit' %>
 <% end %>

我可以在日志中看到我的密码吗?

<ActionController::Parameters {"utf8"=>"✓", "_method"=>"delete", "authenticity_token"=>"r22P2Mi1xcWOjRHGogoFaDcOec9/FgkC9btCo66qmqaKG/zwzUkbUGtATsTKV19OOYK80VBf1h0CzFtoRltQOA==", "password"=>"x", "button"=>"", "controller"=>"admin/users", "action"=>"destroy", "id"=>"at-example-com"} permitted: false>

密码不应该是[过滤]吗?

2 个答案:

答案 0 :(得分:2)

您要显示的代码不是来自日志:

<ActionController::Parameters {"utf8"=>"✓", "_method"=>"delete", "authenticity_token"=>"r22P2Mi1xcWOjRHGogoFaDcOec9/FgkC9btCo66qmqaKG/zwzUkbUGtATsTKV19OOYK80VBf1h0CzFtoRltQOA==", "password"=>"x", "button"=>"", "controller"=>"admin/users", "action"=>"destroy", "id"=>"at-example-com"} permitted: false>

来自puts(params)之类命令的输出。选项filter_parameters是关于位于log目录下的日志文件。例如。 log/development.log

这是一段日志文件:

Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"mxQJeccoEATtyCFy1eV", "user"=>{"first_name"=>"Juggy Head", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create user"}

答案 1 :(得分:0)

您已经使用text_field_tag(它将输入类型文本)这就是为什么它在日志中显示您需要使用password_field_tag(它将输入类型密码)的值,如下所示

<%= form_with model: @user, url: admin_user_path, method: :delete do %>
    <%= label_tag :password, t('forms.password') %>
    <%= password_field_tag :password, nil %>
    <%= button_tag t('forms.save'), type: 'submit' %>
<% end %>

Rails API doc here

例如,密码过滤使用上面的代码

<强>更新

我完全同意这个answer Зелёный