From 225d8f6c6f9a7b3534ad55406b04b9ee64e4f179 Mon Sep 17 00:00:00 2001 From: Pablo Chacin Date: Tue, 1 Oct 2024 17:18:25 +0200 Subject: [PATCH] convert between url and path Signed-off-by: Pablo Chacin --- pkg/cache/file/file.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/cache/file/file.go b/pkg/cache/file/file.go index d7f6022..e7cefed 100644 --- a/pkg/cache/file/file.go +++ b/pkg/cache/file/file.go @@ -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 @@ -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 } @@ -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 } @@ -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 }