From f3ce02b5c944e5956eb7208506ec513623d092d8 Mon Sep 17 00:00:00 2001 From: Sytse Sijbrandij Date: Fri, 21 Sep 2012 18:22:43 +0200 Subject: [PATCH] Reject ssh keys that break gitolite. Failing test. Working check. --- app/models/key.rb | 17 ++++++++++++++++- spec/factories.rb | 12 +++++++----- spec/models/key_spec.rb | 12 ++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/app/models/key.rb b/app/models/key.rb index a39a4a16c22..3982a94602d 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -18,7 +18,7 @@ class Key < ActiveRecord::Base before_save :set_identifier before_validation :strip_white_space delegate :name, :email, to: :user, prefix: true - validate :unique_key + validate :unique_key, :fingerprintable_key def strip_white_space self.key = self.key.strip unless self.key.blank? @@ -32,6 +32,21 @@ class Key < ActiveRecord::Base end end + def fingerprintable_key + return true unless key # Don't test if there is no key. + # `ssh-keygen -lf /dev/stdin <<< "#{key}"` errors with: redirection unexpected + file = Tempfile.new('key_file') + begin + file.puts key + file.rewind + fingerprint_output = `ssh-keygen -lf #{file.path} 2>&1` # Catch stderr. + ensure + file.close + file.unlink # deletes the temp file + end + errors.add(:key, "can't be fingerprinted") if fingerprint_output.match("failed") + end + def set_identifier if is_deploy_key self.identifier = "deploy_" + Digest::MD5.hexdigest(key) diff --git a/spec/factories.rb b/spec/factories.rb index 92790a3fdb7..848fc01ff1d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -83,11 +83,7 @@ FactoryGirl.define do factory :key do title key do - """ - ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4 - 596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4 - soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0= - """ + "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=" end factory :deploy_key do @@ -97,6 +93,12 @@ FactoryGirl.define do factory :personal_key do user end + + factory :key_with_a_space_in_the_middle do + key do + "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa ++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=" + end + end end factory :milestone do diff --git a/spec/models/key_spec.rb b/spec/models/key_spec.rb index 85cd291d681..9bb31a16483 100644 --- a/spec/models/key_spec.rb +++ b/spec/models/key_spec.rb @@ -46,4 +46,16 @@ describe Key do end end end + + context "validate it is a fingerprintable key" do + let(:user) { Factory.create(:user) } + + it "accepts the fingerprintable key" do + build(:key, user: user).should be_valid + end + + it "rejects the unfingerprintable key" do + build(:key_with_a_space_in_the_middle).should_not be_valid + end + end end