Skip to content

Commit

Permalink
Redact request password from logs.
Browse files Browse the repository at this point in the history
Fixes rest-client#349 and OSVDB-117461.
  • Loading branch information
xaviershay committed Jan 27, 2015
1 parent 08480eb commit 60ae4a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/restclient/request.rb
Expand Up @@ -536,7 +536,18 @@ def log_request
return unless RestClient.log

out = []
out << "RestClient.#{method} #{url.inspect}"
sanitized_url = begin
uri = URI.parse(url)
uri.password = "REDACTED" if uri.password
uri.to_s
rescue URI::InvalidURIError
# An attacker may be able to manipulate the URL to be
# invalid, which could force discloure of a password if
# we show any of the un-parsed URL here.
"[invalid uri]"
end

out << "RestClient.#{method} #{sanitized_url.inspect}"
out << payload.short_inspect if payload
out << processed_headers.to_a.sort.map { |(k, v)| [k.inspect, v.inspect].join("=>") }.join(", ")
RestClient.log << out.join(', ') + "\n"
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/request_spec.rb
Expand Up @@ -456,6 +456,18 @@
@request.log_response res
log[0].should eq "# => 200 OK | text/html 0 bytes\n"
end

it 'does not log request password' do
log = RestClient.log = []
RestClient::Request.new(:method => :get, :url => 'http://user:password@url', :headers => {:user_agent => 'rest-client'}).log_request
log[0].should eq %Q{RestClient.get "http://user:REDACTED@url", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n}
end

it 'logs invalid URIs, even though they will fail elsewhere' do
log = RestClient.log = []
RestClient::Request.new(:method => :get, :url => 'http://a@b:c', :headers => {:user_agent => 'rest-client'}).log_request
log[0].should eq %Q{RestClient.get "[invalid uri]", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n}
end
end

it "strips the charset from the response content type" do
Expand Down

0 comments on commit 60ae4a5

Please sign in to comment.