Commit 2209e35f authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '35133_strip_newlines_from_ssh_keys' into 'master'

fix #35133 strip new lines from ssh keys

Closes #35133

See merge request !13234
parents 118dcff0 ae99f05b
...@@ -16,8 +16,6 @@ class Key < ActiveRecord::Base ...@@ -16,8 +16,6 @@ class Key < ActiveRecord::Base
presence: true, presence: true,
length: { maximum: 5000 }, length: { maximum: 5000 },
format: { with: /\A(ssh|ecdsa)-.*\Z/ } format: { with: /\A(ssh|ecdsa)-.*\Z/ }
validates :key,
format: { without: /\n|\r/, message: 'should be a single line' }
validates :fingerprint, validates :fingerprint,
uniqueness: true, uniqueness: true,
presence: { message: 'cannot be generated' } presence: { message: 'cannot be generated' }
...@@ -31,6 +29,7 @@ class Key < ActiveRecord::Base ...@@ -31,6 +29,7 @@ class Key < ActiveRecord::Base
after_destroy :post_destroy_hook after_destroy :post_destroy_hook
def key=(value) def key=(value)
value&.delete!("\n\r")
value.strip! unless value.blank? value.strip! unless value.blank?
write_attribute(:key, value) write_attribute(:key, value)
end end
......
...@@ -94,15 +94,17 @@ describe Key do ...@@ -94,15 +94,17 @@ describe Key do
expect(key).not_to be_valid expect(key).not_to be_valid
end end
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 unfingerprintable key (not a key)' do it 'rejects the unfingerprintable key (not a key)' do
expect(build(:key, key: 'ssh-rsa an-invalid-key==')).not_to be_valid expect(build(:key, key: 'ssh-rsa an-invalid-key==')).not_to be_valid
end end
it 'rejects the multiple line key' do
key = build(:key)
key.key.tr!(' ', "\n")
expect(key).not_to be_valid
end
end end
context 'callbacks' do context 'callbacks' do
......
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