diff --git a/lib/gitlab/url_sanitizer.rb b/lib/gitlab/url_sanitizer.rb
index 86ed18fb50df72e83eeddabc4e0e00a22fb27668..19dad699edf4c50de81690add1b0e64dd790e6b7 100644
--- a/lib/gitlab/url_sanitizer.rb
+++ b/lib/gitlab/url_sanitizer.rb
@@ -4,6 +4,8 @@ module Gitlab
       regexp = URI::Parser.new.make_regexp(['http', 'https', 'ssh', 'git'])
 
       content.gsub(regexp) { |url| new(url).masked_url }
+    rescue Addressable::URI::InvalidURIError
+      content.gsub(regexp, '')
     end
 
     def self.valid?(url)
diff --git a/spec/lib/gitlab/url_sanitizer_spec.rb b/spec/lib/gitlab/url_sanitizer_spec.rb
index 59024d3290be9d1cbf20ae1d4ed94f6b36dee41c..2cb74629da861c380ef76a30625a6c7b0fd4fd74 100644
--- a/spec/lib/gitlab/url_sanitizer_spec.rb
+++ b/spec/lib/gitlab/url_sanitizer_spec.rb
@@ -45,6 +45,12 @@ describe Gitlab::UrlSanitizer, lib: true do
 
       expect(filtered_content).to include("user@server:project.git")
     end
+
+    it 'returns an empty string for invalid URLs' do
+      filtered_content = sanitize_url('ssh://')
+
+      expect(filtered_content).to include("repository '' not found")
+    end
   end
 
   describe '#sanitized_url' do