Skip to content

Commit

Permalink
Merge pull request #39 from kevans91/fixread
Browse files Browse the repository at this point in the history
storage/filesystem: dotgit, sanity check provided reference path
  • Loading branch information
mcuadros committed Apr 24, 2020
2 parents 218a744 + 63967ab commit c9533a6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions storage/filesystem/dotgit/dotgit.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ var (
// targeting a non-existing object. This usually means the repository
// is corrupt.
ErrSymRefTargetNotFound = errors.New("symbolic reference target not found")
// ErrIsDir is returned when a reference file is attempting to be read,
// but the path specified is a directory.
ErrIsDir = errors.New("reference path is a directory")
)

// Options holds configuration for the storage.
Expand Down Expand Up @@ -946,6 +949,14 @@ func (d *DotGit) addRefFromHEAD(refs *[]*plumbing.Reference) error {

func (d *DotGit) readReferenceFile(path, name string) (ref *plumbing.Reference, err error) {
path = d.fs.Join(path, d.fs.Join(strings.Split(name, "/")...))
st, err := d.fs.Stat(path)
if err != nil {
return nil, err
}
if st.IsDir() {
return nil, ErrIsDir
}

f, err := d.fs.Open(path)
if err != nil {
return nil, err
Expand Down
14 changes: 13 additions & 1 deletion storage/filesystem/dotgit/dotgit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,15 @@ func testSetRefs(c *C, dir *DotGit) {
), nil)
c.Assert(err, IsNil)

err = dir.SetRef(plumbing.NewReferenceFromStrings(
"refs/heads/feature/baz",
"e8d3ffab552895c19b9fcf7aa264d277cde33881",
), nil)
c.Assert(err, IsNil)

refs, err := dir.Refs()
c.Assert(err, IsNil)
c.Assert(refs, HasLen, 2)
c.Assert(refs, HasLen, 3)

ref := findReference(refs, "refs/heads/foo")
c.Assert(ref, NotNil)
Expand All @@ -108,6 +114,12 @@ func testSetRefs(c *C, dir *DotGit) {
ref = findReference(refs, "bar")
c.Assert(ref, IsNil)

_, err = dir.readReferenceFile(".", "refs/heads/feature/baz")
c.Assert(err, IsNil)

_, err = dir.readReferenceFile(".", "refs/heads/feature")
c.Assert(err, Equals, ErrIsDir)

ref, err = dir.Ref("refs/heads/foo")
c.Assert(err, IsNil)
c.Assert(ref, NotNil)
Expand Down

0 comments on commit c9533a6

Please sign in to comment.