Update path validation error handling

This commit is contained in:
2024-11-28 21:18:30 +01:00
parent fbb8fa3a60
commit 91489ca633
4 changed files with 70 additions and 7 deletions

View File

@@ -0,0 +1,24 @@
// storage/errors.go
package storage
import (
"errors"
"fmt"
)
// PathValidationError represents a path validation error (e.g., path traversal attempt)
type PathValidationError struct {
Path string
Message string
}
func (e *PathValidationError) Error() string {
return fmt.Sprintf("%s: %s", e.Message, e.Path)
}
// IsPathValidationError checks if the error is a PathValidationError
func IsPathValidationError(err error) bool {
var pathErr *PathValidationError
return err != nil && errors.As(err, &pathErr)
}

View File

@@ -27,7 +27,7 @@ func (s *Service) ValidatePath(userID, workspaceID int, path string) (string, er
// First check if the path is absolute
if filepath.IsAbs(path) {
return "", fmt.Errorf("invalid path: absolute paths not allowed")
return "", &PathValidationError{Path: path, Message: "absolute paths not allowed"}
}
// Join and clean the path
@@ -36,7 +36,7 @@ func (s *Service) ValidatePath(userID, workspaceID int, path string) (string, er
// Verify the path is still within the workspace
if !strings.HasPrefix(cleanPath, workspacePath) {
return "", fmt.Errorf("invalid path: outside of workspace")
return "", &PathValidationError{Path: path, Message: "path traversal attempt"}
}
return cleanPath, nil

View File

@@ -48,7 +48,7 @@ func TestValidatePath(t *testing.T) {
path: "../../../etc/passwd",
want: "",
wantErr: true,
errContains: "outside of workspace",
errContains: "path traversal attempt",
},
{
name: "absolute path attempt",