diff options
author | Douwe Maan <douwe@gitlab.com> | 2018-02-19 11:47:14 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2018-02-19 11:47:14 +0300 |
commit | 804b38d5e63c7e0001e26741b99960e9553114b1 (patch) | |
tree | 04403bdc23bf311afea8f94b63eec29924ce9cec /lib | |
parent | 557db7e635c70bf68a15f7029014301013b30070 (diff) | |
parent | 7044a3a54a4ee4dd45af111727df2ff512db1a22 (diff) |
Merge branch 'rd-40552-gitlab-should-check-if-keys-are-valid-before-saving' into 'master'
Sanitize extra blank spaces used when uploading a SSH key
Closes #40552
See merge request gitlab-org/gitlab-ce!17138
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/ssh_public_key.rb | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/lib/gitlab/ssh_public_key.rb b/lib/gitlab/ssh_public_key.rb index 89ca1298120..6f63ea91ae8 100644 --- a/lib/gitlab/ssh_public_key.rb +++ b/lib/gitlab/ssh_public_key.rb @@ -21,6 +21,22 @@ module Gitlab technology(name)&.supported_sizes end + def self.sanitize(key_content) + ssh_type, *parts = key_content.strip.split + + return key_content if parts.empty? + + parts.each_with_object("#{ssh_type} ").with_index do |(part, content), index| + content << part + + if Gitlab::SSHPublicKey.new(content).valid? + break [content, parts[index + 1]].compact.join(' ') # Add the comment part if present + elsif parts.size == index + 1 # return original content if we've reached the last element + break key_content + end + end + end + attr_reader :key_text, :key # Unqualified MD5 fingerprint for compatibility @@ -37,23 +53,23 @@ module Gitlab end def valid? - key.present? + SSHKey.valid_ssh_public_key?(key_text) end def type - technology.name if valid? + technology.name if key.present? end def bits - return unless valid? + return if key.blank? case type when :rsa - key.n.num_bits + key.n&.num_bits when :dsa - key.p.num_bits + key.p&.num_bits when :ecdsa - key.group.order.num_bits + key.group.order&.num_bits when :ed25519 256 else |