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

Commit 1666236

Browse files
committed
object: fix panic when reading object header
When the first line of the pgp signature is an empty line or some header is malformed it crashes as there's no data for the header element. For example, if author name is "\n". Signed-off-by: Javi Fontan <jfontan@gmail.com>
1 parent 7b6c126 commit 1666236

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

plumbing/object/commit.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,23 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
199199
}
200200

201201
split := bytes.SplitN(line, []byte{' '}, 2)
202+
203+
var data []byte
204+
if len(split) == 2 {
205+
data = split[1]
206+
}
207+
202208
switch string(split[0]) {
203209
case "tree":
204-
c.TreeHash = plumbing.NewHash(string(split[1]))
210+
c.TreeHash = plumbing.NewHash(string(data))
205211
case "parent":
206-
c.ParentHashes = append(c.ParentHashes, plumbing.NewHash(string(split[1])))
212+
c.ParentHashes = append(c.ParentHashes, plumbing.NewHash(string(data)))
207213
case "author":
208-
c.Author.Decode(split[1])
214+
c.Author.Decode(data)
209215
case "committer":
210-
c.Committer.Decode(split[1])
216+
c.Committer.Decode(data)
211217
case headerpgp:
212-
c.PGPSignature += string(split[1]) + "\n"
218+
c.PGPSignature += string(data) + "\n"
213219
pgpsig = true
214220
}
215221
} else {

plumbing/object/commit_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,22 @@ RUysgqjcpT8+iQM1PblGfHR4XAhuOqN5Fx06PSaFZhqvWFezJ28/CLyX5q+oIVk=
325325
c.Assert(err, IsNil)
326326
c.Assert(decoded.PGPSignature, Equals, pgpsignature)
327327

328+
// signature with extra empty line, it caused "index out of range" when
329+
// parsing it
330+
331+
pgpsignature2 := "\n" + pgpsignature
332+
333+
commit.PGPSignature = pgpsignature2
334+
encoded = &plumbing.MemoryObject{}
335+
decoded = &Commit{}
336+
337+
err = commit.Encode(encoded)
338+
c.Assert(err, IsNil)
339+
340+
err = decoded.Decode(encoded)
341+
c.Assert(err, IsNil)
342+
c.Assert(decoded.PGPSignature, Equals, pgpsignature2)
343+
328344
// signature in author name
329345

330346
commit.PGPSignature = ""
@@ -461,3 +477,21 @@ func (s *SuiteCommit) TestPatchCancel(c *C) {
461477
c.Assert(err, ErrorMatches, "operation canceled")
462478

463479
}
480+
481+
func (s *SuiteCommit) TestMalformedHeader(c *C) {
482+
encoded := &plumbing.MemoryObject{}
483+
decoded := &Commit{}
484+
commit := *s.Commit
485+
486+
commit.PGPSignature = "\n"
487+
commit.Author.Name = "\n"
488+
commit.Author.Email = "\n"
489+
commit.Committer.Name = "\n"
490+
commit.Committer.Email = "\n"
491+
492+
err := commit.Encode(encoded)
493+
c.Assert(err, IsNil)
494+
495+
err = decoded.Decode(encoded)
496+
c.Assert(err, IsNil)
497+
}

0 commit comments

Comments
 (0)