Commit a559ded4 authored by Ruben Davila's avatar Ruben Davila

Bugfix: Error at parsing URL with special chars in credentials

parent 7a6520c8
...@@ -25,8 +25,8 @@ module Gitlab ...@@ -25,8 +25,8 @@ module Gitlab
end end
def initialize(url, credentials: nil) def initialize(url, credentials: nil)
@url = Addressable::URI.parse(url.strip)
@credentials = credentials @credentials = credentials
@url = parse_url(url)
end end
def sanitized_url def sanitized_url
...@@ -35,8 +35,8 @@ module Gitlab ...@@ -35,8 +35,8 @@ module Gitlab
def masked_url def masked_url
url = @url.dup url = @url.dup
url.password = "*****" unless url.password.nil? url.password = "*****" if url.password
url.user = "*****" unless url.user.nil? url.user = "*****" if url.user
url.to_s url.to_s
end end
...@@ -50,6 +50,24 @@ module Gitlab ...@@ -50,6 +50,24 @@ module Gitlab
private private
def parse_url(url)
url = url.strip
match = url.match(%r{\A(?:ssh|http(?:s?))\://(?:(.+)(?:@))?(.+)})
raw_credentials = match[1] if match
if raw_credentials.present?
url.sub!("#{raw_credentials}@", '')
user, password = raw_credentials.split(':')
@credentials ||= { user: user, password: password }
end
url = Addressable::URI.parse(url)
url.user = user
url.password = password
url
end
def generate_full_url def generate_full_url
return @url unless valid_credentials? return @url unless valid_credentials?
@full_url = @url.dup @full_url = @url.dup
......
...@@ -100,4 +100,13 @@ describe Gitlab::UrlSanitizer, lib: true do ...@@ -100,4 +100,13 @@ describe Gitlab::UrlSanitizer, lib: true do
it { expect(url_sanitizer.full_url).to eq("https://john.doe@github.com/me/project.git") } it { expect(url_sanitizer.full_url).to eq("https://john.doe@github.com/me/project.git") }
end end
end end
context 'when credentials contains special chars' do
it 'should parse the URL without errors' do
url_sanitizer = described_class.new("https://foo:b?r@github.com/me/project.git")
expect(url_sanitizer.sanitized_url).to eq("https://github.com/me/project.git")
expect(url_sanitizer.full_url).to eq("https://foo:b?r@github.com/me/project.git")
end
end
end end
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