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

Commit 8df413f

Browse files
committed
plumbing/object: fix pgp signature encoder/decoder
The way of reading pgp signatures was searching for pgp begin line in the header. This caused problems when this string appeared and was not part of the signature. For example if it appears in the message as an example or is part of the author name the decoder starts treating it as a signature. In this state the code was not able to notice then the header ended so it entered in an infinite loop searching for pgp end string. Now it uses the same method as original git. Searches for gpgsig section in header and starts getting all lines until the next part. In encoder the string used to add signatures was incorrect. It is now changed to the proper "gpgsig" string instead of "pgpsig". Signed-off-by: Javi Fontan <[email protected]>
1 parent 9f00789 commit 8df413f

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

plumbing/object/commit.go

+13-18
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717
)
1818

1919
const (
20-
beginpgp string = "-----BEGIN PGP SIGNATURE-----"
21-
endpgp string = "-----END PGP SIGNATURE-----"
20+
beginpgp string = "-----BEGIN PGP SIGNATURE-----"
21+
endpgp string = "-----END PGP SIGNATURE-----"
22+
headerpgp string = "gpgsig"
2223
)
2324

2425
// Hash represents the hash of an object
@@ -181,23 +182,13 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
181182
}
182183

183184
if pgpsig {
184-
// Check if it's the end of a PGP signature.
185-
if bytes.Contains(line, []byte(endpgp)) {
186-
c.PGPSignature += endpgp + "\n"
187-
pgpsig = false
188-
} else {
189-
// Trim the left padding.
185+
if len(line) > 0 && line[0] == ' ' {
190186
line = bytes.TrimLeft(line, " ")
191187
c.PGPSignature += string(line)
188+
continue
189+
} else {
190+
pgpsig = false
192191
}
193-
continue
194-
}
195-
196-
// Check if it's the beginning of a PGP signature.
197-
if bytes.Contains(line, []byte(beginpgp)) {
198-
c.PGPSignature += beginpgp + "\n"
199-
pgpsig = true
200-
continue
201192
}
202193

203194
if !message {
@@ -217,6 +208,9 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
217208
c.Author.Decode(split[1])
218209
case "committer":
219210
c.Committer.Decode(split[1])
211+
case headerpgp:
212+
c.PGPSignature += string(split[1]) + "\n"
213+
pgpsig = true
220214
}
221215
} else {
222216
c.Message += string(line)
@@ -269,13 +263,14 @@ func (b *Commit) encode(o plumbing.EncodedObject, includeSig bool) (err error) {
269263
}
270264

271265
if b.PGPSignature != "" && includeSig {
272-
if _, err = fmt.Fprint(w, "pgpsig"); err != nil {
266+
if _, err = fmt.Fprint(w, "\n"+headerpgp); err != nil {
273267
return err
274268
}
275269

276270
// Split all the signature lines and write with a left padding and
277271
// newline at the end.
278-
lines := strings.Split(b.PGPSignature, "\n")
272+
signature := strings.TrimSuffix(b.PGPSignature, "\n")
273+
lines := strings.Split(signature, "\n")
279274
for _, line := range lines {
280275
if _, err = fmt.Fprintf(w, " %s\n", line); err != nil {
281276
return err

plumbing/object/commit_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,38 @@ RUysgqjcpT8+iQM1PblGfHR4XAhuOqN5Fx06PSaFZhqvWFezJ28/CLyX5q+oIVk=
324324
err = decoded.Decode(encoded)
325325
c.Assert(err, IsNil)
326326
c.Assert(decoded.PGPSignature, Equals, pgpsignature)
327+
328+
// signature in author name
329+
330+
commit.PGPSignature = ""
331+
commit.Author.Name = beginpgp
332+
encoded = &plumbing.MemoryObject{}
333+
decoded = &Commit{}
334+
335+
err = commit.Encode(encoded)
336+
c.Assert(err, IsNil)
337+
338+
err = decoded.Decode(encoded)
339+
c.Assert(err, IsNil)
340+
c.Assert(decoded.PGPSignature, Equals, "")
341+
c.Assert(decoded.Author.Name, Equals, beginpgp)
342+
343+
// broken signature
344+
345+
commit.PGPSignature = beginpgp + "\n" +
346+
"some\n" +
347+
"trash\n" +
348+
endpgp +
349+
"text\n"
350+
encoded = &plumbing.MemoryObject{}
351+
decoded = &Commit{}
352+
353+
err = commit.Encode(encoded)
354+
c.Assert(err, IsNil)
355+
356+
err = decoded.Decode(encoded)
357+
c.Assert(err, IsNil)
358+
c.Assert(decoded.PGPSignature, Equals, commit.PGPSignature)
327359
}
328360

329361
func (s *SuiteCommit) TestStat(c *C) {

0 commit comments

Comments
 (0)