Skip to content

Commit

Permalink
Fix error on Rails filter_parameters with regex keys. Closes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
abevoelker committed Sep 18, 2023
1 parent 71ac882 commit b7ef992
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
16 changes: 16 additions & 0 deletions lib/devise/passwordless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,21 @@ def self.secret_key
Devise.secret_key
end
end

FILTER_PARAMS_WARNING = "[DEVISE-PASSWORDLESS] We have detected that your Rails configuration does not " \
"filter :token parameters out of your logs. You should append :token to your " \
"config.filter_parameters Rails setting so that magic link tokens don't " \
"leak out of your logs."

def self.check_filter_parameters(params)
begin
unless params.find{|p| p.to_sym == :token}
warn FILTER_PARAMS_WARNING
end
# Cancel the check if filter_parameters contains regular expressions or other exotic values
rescue NoMethodError
return
end
end
end
end
7 changes: 1 addition & 6 deletions lib/devise/passwordless/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ class Engine < Rails::Engine
initializer "devise_passwordless.log_filter_check" do
params = Rails.try(:application).try(:config).try(:filter_parameters) || []

unless params.map(&:to_sym).include?(:token)
warn "[DEVISE-PASSWORDLESS] We have detected that your Rails configuration does not " \
"filter :token parameters out of your logs. You should append :token to your " \
"config.filter_parameters Rails setting so that magic link tokens don't " \
"leak out of your logs."
end
::Devise::Passwordless.check_filter_parameters(params)
end
end
end
40 changes: 40 additions & 0 deletions spec/devise/passwordless_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,44 @@
it "has a version number" do
expect(Devise::Passwordless::VERSION).not_to be nil
end

context "check_filter_parameters" do
let(:warn_msg) { Devise::Passwordless::FILTER_PARAMS_WARNING + "\n" }

context "symbol keys" do
it "warns if :token is not filtered" do
params = [:password, :password_confirmation]
expect { Devise::Passwordless.check_filter_parameters(params) }.to output(warn_msg).to_stderr
end

it "doesn't warn if :token is filtered" do
params = [:token, :password, :password_confirmation]
expect { Devise::Passwordless.check_filter_parameters(params) }.not_to output(warn_msg).to_stderr
end
end

context "string keys" do
it "warns if :token is not filtered" do
params = ["password", "password_confirmation"]
expect { Devise::Passwordless.check_filter_parameters(params) }.to output(warn_msg).to_stderr
end

it "doesn't warn if :token is filtered" do
params = ["token", "password", "password_confirmation"]
expect { Devise::Passwordless.check_filter_parameters(params) }.not_to output(warn_msg).to_stderr
end
end

context "regex keys" do
it "doesn't warn if :token is not filtered" do
params = [:password, :password_confirmation, /foo/]
expect { Devise::Passwordless.check_filter_parameters(params) }.not_to output(warn_msg).to_stderr
end

it "doesn't warn if :token is filtered" do
params = [:token, "token", :password, :password_confirmation, /foo/]
expect { Devise::Passwordless.check_filter_parameters(params) }.not_to output(warn_msg).to_stderr
end
end
end
end

0 comments on commit b7ef992

Please sign in to comment.