【问题标题】:Password not filtered in log密码未在日志中过滤
【发布时间】:2018-07-04 05:15:17
【问题描述】:

根据 Rails 文档

config.filter_parameters 用于过滤掉参数 您不希望在日志中显示,例如密码或信用卡 数字。默认情况下,Rails 通过添加 Rails.application.config.filter_parameters += [:password]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>

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

【问题讨论】:

标签: ruby-on-rails ruby


【解决方案1】:

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

<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"}

【讨论】:

    【解决方案2】:

    您已经使用了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 文档here

    例如,使用上述代码进行密码过滤

    更新

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

    【讨论】:

    • 恐怕这不是答案。据我所知,您不必使用password_field_tag 来过滤这些值。例如,您可以过滤信用卡号码,但我不知道有哪个应用程序使用password_field_tag 输入信用卡号码。还是谢谢。
    • 我搜索并找到了一些有用的链接和其中一个blog.bigbinary.com/2016/03/07/…@macsig
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 2018-10-15
    相关资源
    最近更新 更多