Skip to content

Commit 12d9214

Browse files
Merge pull request #59 from snyk/fix/file-upload-create-revision-from
fix(fileupload): add path validation to `CreateRevisionFrom` functions
2 parents 6bbb65c + 13ed8ff commit 12d9214

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

internal/fileupload/client.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (c *HTTPClient) CreateRevisionFromPaths(ctx context.Context, paths []string
222222
for _, pth := range paths {
223223
info, err := os.Stat(pth)
224224
if err != nil {
225-
return uuid.Nil, fmt.Errorf("failed to stat path %s: %w", pth, err)
225+
return uuid.Nil, uploadrevision.NewFileAccessError(pth, err)
226226
}
227227

228228
if info.IsDir() {
@@ -244,13 +244,31 @@ func (c *HTTPClient) CreateRevisionFromPaths(ctx context.Context, paths []string
244244
}
245245

246246
// CreateRevisionFromDir uploads a directory and all its contents, returning a revision ID.
247-
// This is a convenience method equivalent to CreateRevisionFromPaths with a single directory.
247+
// This is a convenience method for validating the directory path and calling CreateRevisionFromPaths with a single directory path.
248248
func (c *HTTPClient) CreateRevisionFromDir(ctx context.Context, dirPath string, opts UploadOptions) (RevisionID, error) {
249+
info, err := os.Stat(dirPath)
250+
if err != nil {
251+
return uuid.Nil, uploadrevision.NewFileAccessError(dirPath, err)
252+
}
253+
254+
if !info.IsDir() {
255+
return uuid.Nil, fmt.Errorf("the provided path is not a directory: %s", dirPath)
256+
}
257+
249258
return c.CreateRevisionFromPaths(ctx, []string{dirPath}, opts)
250259
}
251260

252261
// CreateRevisionFromFile uploads a single file, returning a revision ID.
253-
// This is a convenience method equivalent to CreateRevisionFromPaths with a single file.
262+
// This is a convenience method for validating the file path and calling CreateRevisionFromPaths with a single file path.
254263
func (c *HTTPClient) CreateRevisionFromFile(ctx context.Context, filePath string, opts UploadOptions) (RevisionID, error) {
264+
info, err := os.Stat(filePath)
265+
if err != nil {
266+
return uuid.Nil, uploadrevision.NewFileAccessError(filePath, err)
267+
}
268+
269+
if !info.Mode().IsRegular() {
270+
return uuid.Nil, fmt.Errorf("the provided path is not a regular file: %s", filePath)
271+
}
272+
255273
return c.CreateRevisionFromPaths(ctx, []string{filePath}, opts)
256274
}

internal/fileupload/client_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ func Test_CreateRevisionFromPaths(t *testing.T) {
9494

9595
_, err := client.CreateRevisionFromPaths(ctx, paths, fileupload.UploadOptions{})
9696
require.Error(t, err)
97-
assert.Contains(t, err.Error(), "failed to stat path")
98-
assert.Contains(t, err.Error(), "/nonexistent/file.go") // Should include the specific path
97+
var fileAccessErr *uploadrevision.FileAccessError
98+
assert.ErrorAs(t, err, &fileAccessErr)
99+
assert.Equal(t, "/nonexistent/file.go", fileAccessErr.FilePath)
100+
assert.ErrorContains(t, fileAccessErr.Err, "no such file or directory")
99101
})
100102
}
101103

0 commit comments

Comments
 (0)