Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 779c88d

Browse files
committed
Return error when creating public keys from invalid PEM
* pem.Decode will return nil in this case, and passing that to x509.IsEncryptedBlock will cause it to panic Signed-off-by: Mark DeLillo <[email protected]>
1 parent 886dc83 commit 779c88d

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

plumbing/transport/ssh/auth_method.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ type PublicKeys struct {
124124
// (PKCS#1), DSA (OpenSSL), and ECDSA private keys.
125125
func NewPublicKeys(user string, pemBytes []byte, password string) (*PublicKeys, error) {
126126
block, _ := pem.Decode(pemBytes)
127+
if block == nil {
128+
return nil, errors.New("invalid PEM data")
129+
}
127130
if x509.IsEncryptedPEMBlock(block) {
128131
key, err := x509.DecryptPEMBlock(block, []byte(password))
129132
if err != nil {

plumbing/transport/ssh/auth_method_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,9 @@ func (*SuiteCommon) TestNewPublicKeysFromFile(c *C) {
143143
c.Assert(err, IsNil)
144144
c.Assert(auth, NotNil)
145145
}
146+
147+
func (*SuiteCommon) TestNewPublicKeysWithInvalidPEM(c *C) {
148+
auth, err := NewPublicKeys("foo", []byte("bar"), "")
149+
c.Assert(err, NotNil)
150+
c.Assert(auth, IsNil)
151+
}

0 commit comments

Comments
 (0)