Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redact password from query_options #1334

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Metrics/BlockNesting:
# Offense count: 1
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 135
Max: 141

# Offense count: 3
# Configuration parameters: IgnoredMethods.
Expand Down
22 changes: 16 additions & 6 deletions lib/mysql2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ def initialize(opts = {})
# SSL verify is a connection flag rather than a mysql_ssl_set option
flags |= SSL_VERIFY_SERVER_CERT if opts[:sslverify]

if %i[user pass hostname dbname db sock].any? { |k| @query_options.key?(k) }
warn "============= WARNING FROM mysql2 ============="
warn "The options :user, :pass, :hostname, :dbname, :db, and :sock are deprecated and will be removed at some point in the future."
warn "Instead, please use :username, :password, :host, :port, :database, :socket, :flags for the options."
warn "============= END WARNING FROM mysql2 ========="
end
check_and_clean_query_options

user = opts[:username] || opts[:user]
pass = opts[:password] || opts[:pass]
Expand Down Expand Up @@ -165,6 +160,21 @@ def info
self.class.info
end

private

def check_and_clean_query_options
if %i[user pass hostname dbname db sock].any? { |k| @query_options.key?(k) }
warn "============= WARNING FROM mysql2 ============="
warn "The options :user, :pass, :hostname, :dbname, :db, and :sock are deprecated and will be removed at some point in the future."
warn "Instead, please use :username, :password, :host, :port, :database, :socket, :flags for the options."
warn "============= END WARNING FROM mysql2 ========="
end

# avoid logging sensitive data via #inspect
@query_options.delete(:password)
@query_options.delete(:pass)
end

class << self
private

Expand Down
18 changes: 18 additions & 0 deletions spec/mysql2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1173,4 +1173,22 @@ def run_gc
it "should respond to #encoding" do
expect(@client).to respond_to(:encoding)
end

it "should not include the password in the output of #inspect" do
client_class = Class.new(Mysql2::Client) do
def connect(*args); end
end

client = client_class.new(password: "secretsecret")

expect(client.inspect).not_to include("password")
expect(client.inspect).not_to include("secretsecret")

expect do
client = client_class.new(pass: "secretsecret")
end.to output(/WARNING/).to_stderr

expect(client.inspect).not_to include("pass")
expect(client.inspect).not_to include("secretsecret")
end
end
2 changes: 1 addition & 1 deletion spec/mysql2/statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def stmt_count
result = @client.prepare("SELECT 1 UNION SELECT 2").execute(stream: true, cache_rows: false)
expect do
result.each {}
result.each {} # rubocop:disable Style/CombinableLoops
result.each {}
end.to raise_exception(Mysql2::Error)
end
end
Expand Down