Skip to content

Commit

Permalink
convert between url and path
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Oct 1, 2024
1 parent e832919 commit 225d8f6
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/cache/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"

"github.com/grafana/k6build/pkg/cache"
"github.com/grafana/k6build/pkg/util"
)

// Cache a Cache backed by a file system
Expand Down Expand Up @@ -83,10 +84,11 @@ func (f *Cache) Store(_ context.Context, id string, content io.Reader) (cache.Ob
return cache.Object{}, fmt.Errorf("%w: %w", cache.ErrCreatingObject, err)
}

objectURL, _ := util.URLFromFilePath(objectFile.Name())
return cache.Object{
ID: id,
Checksum: checksum,
URL: fmt.Sprintf("file://%s", objectFile.Name()),
URL: objectURL.String(),
}, nil
}

Expand All @@ -108,10 +110,11 @@ func (f *Cache) Get(_ context.Context, id string) (cache.Object, error) {
return cache.Object{}, fmt.Errorf("%w: %w", cache.ErrAccessingObject, err)
}

objectURL, _ := util.URLFromFilePath(filepath.Join(objectDir, "data"))
return cache.Object{
ID: id,
Checksum: string(checksum),
URL: fmt.Sprintf("file://%s", filepath.Join(objectDir, "data")),
URL: objectURL.String(),
}, nil
}

Expand All @@ -124,8 +127,13 @@ func (f *Cache) Download(_ context.Context, object cache.Object) (io.ReadCloser,

switch url.Scheme {
case "file":
objectPath, err := util.URLToFilePath(url)
if err != nil {
return nil, err
}

// prevent malicious path
objectPath, err := f.sanitizePath(url.Path)
objectPath, err = f.sanitizePath(objectPath)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 225d8f6

Please sign in to comment.