Commit 89f97222 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'issue_447' into 'master'

Mask credentials in URLs instead of remove them

REF: #447 

REF: https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/382#note_11713978

See merge request !385
parents 8c599cd0 15bb7e97
...@@ -519,12 +519,7 @@ class Project < ActiveRecord::Base ...@@ -519,12 +519,7 @@ class Project < ActiveRecord::Base
end end
def safe_import_url def safe_import_url
result = URI.parse(self.import_url) Gitlab::UrlSanitizer.new(import_url).masked_url
result.password = '*****' unless result.password.nil?
result.user = '*****' unless result.user.nil? || result.user == "git" #tokens or other data may be saved as user
result.to_s
rescue
self.import_url
end end
def mirror_updated? def mirror_updated?
......
...@@ -3,7 +3,7 @@ module Gitlab ...@@ -3,7 +3,7 @@ module Gitlab
def self.sanitize(content) def self.sanitize(content)
regexp = URI::Parser.new.make_regexp(['http', 'https', 'ssh', 'git']) regexp = URI::Parser.new.make_regexp(['http', 'https', 'ssh', 'git'])
content.gsub(regexp) { |url| new(url).sanitized_url } content.gsub(regexp) { |url| new(url).masked_url }
end end
def initialize(url, credentials: nil) def initialize(url, credentials: nil)
...@@ -15,6 +15,13 @@ module Gitlab ...@@ -15,6 +15,13 @@ module Gitlab
@sanitized_url ||= safe_url.to_s @sanitized_url ||= safe_url.to_s
end end
def masked_url
url = @url.dup
url.password = "*****" unless url.password.nil?
url.user = "*****" unless url.user.nil?
url.to_s
end
def credentials def credentials
@credentials ||= { user: @url.user, password: @url.password } @credentials ||= { user: @url.user, password: @url.password }
end end
......
...@@ -31,16 +31,16 @@ describe Gitlab::UrlSanitizer, lib: true do ...@@ -31,16 +31,16 @@ describe Gitlab::UrlSanitizer, lib: true do
}) })
end end
it 'remove credentials from HTTP URLs' do it 'mask the credentials from HTTP URLs' do
expect(filtered_content).to include("http://test.com/root/repoC.git/") expect(filtered_content).to include("http://*****:*****@test.com/root/repoC.git/")
end end
it 'remove credentials from HTTPS URLs' do it 'mask the credentials from HTTPS URLs' do
expect(filtered_content).to include("https://test.com/root/repoA.git/") expect(filtered_content).to include("https://*****:*****@test.com/root/repoA.git/")
end end
it 'remove credentials from SSH URLs' do it 'mask credentials from SSH URLs' do
expect(filtered_content).to include("ssh://host.test/path/to/repo.git") expect(filtered_content).to include("ssh://*****@host.test/path/to/repo.git")
end end
it 'does not modify Git URLs' do it 'does not modify Git URLs' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment