diff --git a/lib/gitlab/backend/shell.rb b/lib/gitlab/backend/shell.rb
index 14ee4701e7bb9faa44e664c9c36124a91efd4305..01b8bda05c6741cf7b914735722447388cec42f7 100644
--- a/lib/gitlab/backend/shell.rb
+++ b/lib/gitlab/backend/shell.rb
@@ -4,7 +4,8 @@ module Gitlab
 
     class KeyAdder < Struct.new(:io)
       def add_key(id, key)
-        io.puts("#{id}\t#{key.strip}")
+        key.gsub!(/[[:space:]]+/, ' ').strip!
+        io.puts("#{id}\t#{key}")
       end
     end
 
diff --git a/spec/lib/gitlab/backend/shell_spec.rb b/spec/lib/gitlab/backend/shell_spec.rb
index b6d04330599bd2988f13e0e06f828418fc8f7f07..b60e23454d60f875ca722fca949fd1c228e98ccf 100644
--- a/spec/lib/gitlab/backend/shell_spec.rb
+++ b/spec/lib/gitlab/backend/shell_spec.rb
@@ -15,4 +15,17 @@ describe Gitlab::Shell do
   it { is_expected.to respond_to :fork_repository }
 
   it { expect(gitlab_shell.url_to_repo('diaspora')).to eq(Gitlab.config.gitlab_shell.ssh_path_prefix + "diaspora.git") }
+
+  describe Gitlab::Shell::KeyAdder do
+    describe '#add_key' do
+      it 'normalizes space characters in the key' do
+        io = spy
+        adder = described_class.new(io)
+
+        adder.add_key('key-42', "sha-rsa foo\tbar\tbaz")
+
+        expect(io).to have_received(:puts).with("key-42\tsha-rsa foo bar baz")
+      end
+    end
+  end
 end