Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4998140

Browse files
committed
git: worktree_commit, sanitize author and commiter name and email before creating the commit object. Fixes #680
1 parent 9049625 commit 4998140

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

worktree_commit.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"io"
77
"path"
8+
"regexp"
89
"sort"
910
"strings"
1011

@@ -23,6 +24,10 @@ var (
2324
// ErrEmptyCommit occurs when a commit is attempted using a clean
2425
// working tree, with no changes to be committed.
2526
ErrEmptyCommit = errors.New("cannot create empty commit: clean working tree")
27+
28+
// characters to be removed from user name and/or email before using them to build a commit object
29+
// See https://git-scm.com/docs/git-commit#_commit_information
30+
invalidCharactersRe = regexp.MustCompile(`[<>\n]`)
2631
)
2732

2833
// Commit stores the current contents of the index in a new commit along with
@@ -137,8 +142,8 @@ func (w *Worktree) updateHEAD(commit plumbing.Hash) error {
137142

138143
func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumbing.Hash) (plumbing.Hash, error) {
139144
commit := &object.Commit{
140-
Author: *opts.Author,
141-
Committer: *opts.Committer,
145+
Author: w.sanitize(*opts.Author),
146+
Committer: w.sanitize(*opts.Committer),
142147
Message: msg,
143148
TreeHash: tree,
144149
ParentHashes: opts.Parents,
@@ -164,6 +169,14 @@ func (w *Worktree) buildCommitObject(msg string, opts *CommitOptions, tree plumb
164169
return w.r.Storer.SetEncodedObject(obj)
165170
}
166171

172+
func (w *Worktree) sanitize(signature object.Signature) object.Signature {
173+
return object.Signature{
174+
Name: invalidCharactersRe.ReplaceAllString(signature.Name, ""),
175+
Email: invalidCharactersRe.ReplaceAllString(signature.Email, ""),
176+
When: signature.When,
177+
}
178+
}
179+
167180
type gpgSigner struct {
168181
key *openpgp.Entity
169182
cfg *packet.Config

worktree_commit_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
fixtures "github.com/go-git/go-git-fixtures/v4"
1314
"github.com/go-git/go-git/v5/plumbing"
1415
"github.com/go-git/go-git/v5/plumbing/cache"
1516
"github.com/go-git/go-git/v5/plumbing/object"
@@ -618,6 +619,44 @@ func (s *WorktreeSuite) TestJustStoreObjectsNotAlreadyStored(c *C) {
618619
c.Assert(infoLicenseSecond.ModTime(), Equals, infoLicense.ModTime()) // object of LICENSE should have the same timestamp because no additional write operation was performed
619620
}
620621

622+
func (s *WorktreeSuite) TestCommitInvalidCharactersInAuthorInfos(c *C) {
623+
f := fixtures.Basic().One()
624+
s.Repository = s.NewRepositoryWithEmptyWorktree(f)
625+
626+
expected := plumbing.NewHash("e8eecef2524c3a37cf0f0996603162f81e0373f1")
627+
628+
fs := memfs.New()
629+
storage := memory.NewStorage()
630+
631+
r, err := Init(storage, fs)
632+
c.Assert(err, IsNil)
633+
634+
w, err := r.Worktree()
635+
c.Assert(err, IsNil)
636+
637+
util.WriteFile(fs, "foo", []byte("foo"), 0644)
638+
639+
_, err = w.Add("foo")
640+
c.Assert(err, IsNil)
641+
642+
hash, err := w.Commit("foo\n", &CommitOptions{Author: invalidSignature()})
643+
c.Assert(hash, Equals, expected)
644+
c.Assert(err, IsNil)
645+
646+
assertStorageStatus(c, r, 1, 1, 1, expected)
647+
648+
// Check HEAD commit contains author informations with '<', '>' and '\n' stripped
649+
lr, err := r.Log(&LogOptions{})
650+
c.Assert(err, IsNil)
651+
652+
commit, err := lr.Next()
653+
c.Assert(err, IsNil)
654+
655+
c.Assert(commit.Author.Name, Equals, "foo bad")
656+
c.Assert(commit.Author.Email, Equals, "[email protected]")
657+
658+
}
659+
621660
func assertStorageStatus(
622661
c *C, r *Repository,
623662
treesCount, blobCount, commitCount int, head plumbing.Hash,
@@ -657,6 +696,15 @@ func defaultSignature() *object.Signature {
657696
}
658697
}
659698

699+
func invalidSignature() *object.Signature {
700+
when, _ := time.Parse(object.DateFormat, "Thu May 04 00:03:43 2017 +0200")
701+
return &object.Signature{
702+
Name: "foo <bad>\n",
703+
Email: "<bad>\n[email protected]",
704+
When: when,
705+
}
706+
}
707+
660708
func commitSignKey(c *C, decrypt bool) *openpgp.Entity {
661709
s := strings.NewReader(armoredKeyRing)
662710
es, err := openpgp.ReadArmoredKeyRing(s)

0 commit comments

Comments
 (0)