这是个安全问题,当我们在系统注册页面输入密码等敏感数据时,我们可以看到,密码以明文的形式显示在日志文件里:
Processing UsersController#create (for 127.0.0.1 at 2007-02-23 19:11:20) [POST]
Session ID: 4047778b64af62d387f7e860e51cce20
[color=red]Parameters[/color]: {"user" => {"name" => "Ryan", "password_confirmation" => "abc123", "password" => "abc123"}, "commit" =>
"Register", "action" => "create", "controller" => "users"}
SQL (0.000108) BEGIN
[color=red]SQL[/color] (0.000238) INSERT INTO users ('name', 'password') VALUES('Ryan', 'abc123')
SQL (0.000395) COMMIT
Redirected to http://localhost:3000/users/5
...
我们看到Parameters一行密码是明文显示的,这样当然不安全。
解决方案也十分简单:
class ApplicationController < ActionController::Base
filter_parameter_logging "password"
end
这样在Parameters那行就会显示"password_confirmation" => "[FILTERED]","password" => "[FILTERED]"
filter_parameter_logging的方法定义位于action_pack/lib/action_controller/base.rb。
但是我们也看到SQL一行也是明文密码,我们的filter不会过滤SQL语句,怎么办?
解决方案是,部署production环境时,日志不会再打印SQL语句,这样就避免了此问题。