Skip to content

Commit 97dbbcd

Browse files
committed
Check if private key is really a private key
1 parent 47019cc commit 97dbbcd

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/travis/model/repository/settings.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class EnvVar < Travis::Settings::Model
1717
end
1818

1919
class SshKey < Travis::Settings::Model
20+
class NotAPrivateKeyError < StandardError; end
21+
2022
attribute :description, String
2123
attribute :value, Travis::Settings::EncryptedValue
2224
attribute :repository_id, Integer
@@ -25,9 +27,10 @@ class SshKey < Travis::Settings::Model
2527
validate :validate_correctness
2628

2729
def validate_correctness
28-
OpenSSL::PKey::RSA.new(value.decrypt)
29-
rescue OpenSSL::PKey::RSAError
30-
errors.add(:value, :not_private_key)
30+
key = OpenSSL::PKey::RSA.new(value.decrypt)
31+
raise NotAPrivateKeyError unless key.private?
32+
rescue OpenSSL::PKey::RSAError, NotAPrivateKeyError
33+
errors.add(:value, :not_a_private_key)
3134
end
3235
end
3336

spec/travis/model/repository/settings/ssh_key_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
ssh_key.value = 'foo'
4141
ssh_key.should_not be_valid
4242

43-
ssh_key.errors[:value].should == [:not_private_key]
43+
ssh_key.errors[:value].should == [:not_a_private_key]
44+
end
45+
46+
it 'allows only private key' do
47+
public_key = OpenSSL::PKey::RSA.new(private_key).public_key.to_s
48+
ssh_key = described_class.new(value: public_key)
49+
50+
ssh_key.should_not be_valid
51+
ssh_key.errors[:value].should == [:not_a_private_key]
4452
end
4553
end

0 commit comments

Comments
 (0)