From ae99f05b059f613296c39dfa45c37dbcab40f4cd Mon Sep 17 00:00:00 2001
From: haseeb <haseebeqx@gmail.com>
Date: Thu, 3 Aug 2017 16:39:10 +0000
Subject: [PATCH] fix #35133 strip new lines from ssh keys

---
 app/models/key.rb       |  3 +--
 spec/models/key_spec.rb | 14 ++++++++------
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/app/models/key.rb b/app/models/key.rb
index cb8f10f6d55..49bc26122fa 100644
--- a/app/models/key.rb
+++ b/app/models/key.rb
@@ -16,8 +16,6 @@ class Key < ActiveRecord::Base
     presence: true,
     length: { maximum: 5000 },
     format: { with: /\A(ssh|ecdsa)-.*\Z/ }
-  validates :key,
-    format: { without: /\n|\r/, message: 'should be a single line' }
   validates :fingerprint,
     uniqueness: true,
     presence: { message: 'cannot be generated' }
@@ -31,6 +29,7 @@ class Key < ActiveRecord::Base
   after_destroy :post_destroy_hook
 
   def key=(value)
+    value&.delete!("\n\r")
     value.strip! unless value.blank?
     write_attribute(:key, value)
   end
diff --git a/spec/models/key_spec.rb b/spec/models/key_spec.rb
index d41717d0223..251d0cfd08c 100644
--- a/spec/models/key_spec.rb
+++ b/spec/models/key_spec.rb
@@ -94,15 +94,17 @@ describe Key do
       expect(key).not_to be_valid
     end
 
-    it 'rejects the unfingerprintable key (not a key)' do
-      expect(build(:key, key: 'ssh-rsa an-invalid-key==')).not_to be_valid
+    it 'accepts a key with newline charecters after stripping them' do
+      key = build(:key)
+      key.key = key.key.insert(100, "\n")
+      key.key = key.key.insert(40, "\r\n")
+      expect(key).to be_valid
     end
 
-    it 'rejects the multiple line key' do
-      key = build(:key)
-      key.key.tr!(' ', "\n")
-      expect(key).not_to be_valid
+    it 'rejects the unfingerprintable key (not a key)' do
+      expect(build(:key, key: 'ssh-rsa an-invalid-key==')).not_to be_valid
     end
+    
   end
 
   context 'callbacks' do
-- 
GitLab