From 9c7d576132a760645e3a6b3ebdf51c4aef532794 Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Thu, 23 Apr 2020 18:00:03 -0500 Subject: storage/filesystem: dotgit, add a test for reading refs from a directory Specifically, the problem demonstrated is that on some OS (e.g. FreeBSD), read() on a directory will succeed. The current implementation will accept that and return incorrect results, rather than rejecting the directory. --- storage/filesystem/dotgit/dotgit_test.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'storage/filesystem') diff --git a/storage/filesystem/dotgit/dotgit_test.go b/storage/filesystem/dotgit/dotgit_test.go index d57ff15..403f5ff 100644 --- a/storage/filesystem/dotgit/dotgit_test.go +++ b/storage/filesystem/dotgit/dotgit_test.go @@ -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) @@ -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, NotNil) + ref, err = dir.Ref("refs/heads/foo") c.Assert(err, IsNil) c.Assert(ref, NotNil) -- cgit From 63967abe62ecba22a792e728f1af509f3604881f Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Thu, 23 Apr 2020 17:47:05 -0500 Subject: storage/filesystem: dotgit, sanity check provided reference path On some operating systems, read() of a directory fd may actually succeed and return garbage resembling on-disk contents. As a result, dotgit may return successful from readReferenceFile() when it should in-fact not. This fixes readReferenceFile() on FreeBSD. Signed-off-by: Kyle Evans --- storage/filesystem/dotgit/dotgit.go | 11 +++++++++++ storage/filesystem/dotgit/dotgit_test.go | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'storage/filesystem') diff --git a/storage/filesystem/dotgit/dotgit.go b/storage/filesystem/dotgit/dotgit.go index 503ce18..8c3896b 100644 --- a/storage/filesystem/dotgit/dotgit.go +++ b/storage/filesystem/dotgit/dotgit.go @@ -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. @@ -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 diff --git a/storage/filesystem/dotgit/dotgit_test.go b/storage/filesystem/dotgit/dotgit_test.go index 403f5ff..519f601 100644 --- a/storage/filesystem/dotgit/dotgit_test.go +++ b/storage/filesystem/dotgit/dotgit_test.go @@ -118,7 +118,7 @@ func testSetRefs(c *C, dir *DotGit) { c.Assert(err, IsNil) _, err = dir.readReferenceFile(".", "refs/heads/feature") - c.Assert(err, NotNil) + c.Assert(err, Equals, ErrIsDir) ref, err = dir.Ref("refs/heads/foo") c.Assert(err, IsNil) -- cgit