Skip to content

Commit

Permalink
prevent object overwrite
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Sep 19, 2024
1 parent 14ee86c commit 99c7bae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/cache/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewFileCache(dir string) (cache.Cache, error) {
}

// Store stores the object and returns the metadata
// Fails if the object already exists
func (f *Cache) Store(_ context.Context, id string, content io.Reader) (cache.Object, error) {
if id == "" {
return cache.Object{}, fmt.Errorf("%w id cannot be empty", cache.ErrCreatingObject)
Expand All @@ -49,6 +50,11 @@ func (f *Cache) Store(_ context.Context, id string, content io.Reader) (cache.Ob
}

objectDir := filepath.Join(f.dir, id)

if _, err := os.Stat(objectDir); !errors.Is(err, os.ErrNotExist) {
return cache.Object{}, fmt.Errorf("%w: object already exists %q", cache.ErrCreatingObject, id)
}

// TODO: check permissions
err := os.MkdirAll(objectDir, 0o750)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/cache/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func TestFileCacheStoreObject(t *testing.T) {
content: []byte("content"),
},
},
id: "object",
content: []byte("new content"),
id: "object",
content: []byte("new content"),
expectErr: cache.ErrCreatingObject,
},
{
title: "store empty object",
Expand Down

0 comments on commit 99c7bae

Please sign in to comment.