Skip to content

Modern Docker Management, Designed for Everyone

fix(build): return 4xx for build workspace path errors (#4595)

José M. Requena Plensad0f7d8f8617
diff --git a/backend/internal/build/children/workspace/handler.go b/backend/internal/build/children/workspace/handler.go
index dbd073fc5..c9d10f101 100644
--- a/backend/internal/build/children/workspace/handler.go
+++ b/backend/internal/build/children/workspace/handler.go
@@ -9,6 +9,7 @@ import (
uploadtypes "github.com/getarcaneapp/arcane/types/v2/upload"
"github.com/getarcaneapp/arcane/types/v2/workspace"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/internal/upload"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
)
@@ -56,10 +57,15 @@ type DeleteBuildFileInput struct {
Path string `query:"path" doc:"File or directory path to delete"`
}
+func workspaceHTTPErrorInternal(err error) error {
+ apiErr := common.ToAPIError(err)
+ return huma.NewError(apiErr.HTTPStatus(), apiErr.Message)
+}
+
func (h *Handler) BrowseDirectory(ctx context.Context, input *BrowseBuildsInput) (*handlerutil.Out[[]workspace.FileEntry], error) {
entries, err := h.service.ListDirectory(ctx, input.Path)
if err != nil {
- return nil, huma.Error500InternalServerError(err.Error())
+ return nil, workspaceHTTPErrorInternal(err)
}
return &handlerutil.Out[[]workspace.FileEntry]{Body: base.ApiResponse[[]workspace.FileEntry]{Success: true, Data: entries}}, nil
}
@@ -67,7 +73,7 @@ func (h *Handler) BrowseDirectory(ctx context.Context, input *BrowseBuildsInput)
func (h *Handler) GetFileContent(ctx context.Context, input *GetBuildFileContentInput) (*handlerutil.Out[BuildFileContentResponse], error) {
content, mimeType, err := h.service.GetFileContent(ctx, input.Path, input.MaxBytes)
if err != nil {
- return nil, huma.Error500InternalServerError(err.Error())
+ return nil, workspaceHTTPErrorInternal(err)
}
return &handlerutil.Out[BuildFileContentResponse]{Body: base.ApiResponse[BuildFileContentResponse]{
Success: true,
@@ -78,7 +84,7 @@ func (h *Handler) GetFileContent(ctx context.Context, input *GetBuildFileContent
func (h *Handler) DownloadFile(ctx context.Context, input *DownloadBuildFileInput) (*huma.StreamResponse, error) {
reader, size, err := h.service.DownloadFile(ctx, input.Path)
if err != nil {
- return nil, huma.Error500InternalServerError(err.Error())
+ return nil, workspaceHTTPErrorInternal(err)
}
return handlerutil.DownloadResponse(reader, size, path.Base(input.Path)), nil
}
@@ -94,7 +100,7 @@ func (h *Handler) UploadFile(ctx context.Context, input *UploadBuildFileInput) (
defer cleanup()
if uploadFileErr := h.service.UploadFile(ctx, input.Path, file, session.Filename, session.Size); uploadFileErr != nil {
- return nil, huma.Error500InternalServerError(uploadFileErr.Error())
+ return nil, workspaceHTTPErrorInternal(uploadFileErr)
}
return &base.ApiResponse[base.MessageResponse]{
Success: true,
@@ -104,7 +110,7 @@ func (h *Handler) UploadFile(ctx context.Context, input *UploadBuildFileInput) (
func (h *Handler) CreateDirectory(ctx context.Context, input *CreateBuildDirectoryInput) (*base.ApiResponse[base.MessageResponse], error) {
if err := h.service.CreateDirectory(ctx, input.Path); err != nil {
- return nil, huma.Error500InternalServerError(err.Error())
+ return nil, workspaceHTTPErrorInternal(err)
}
return &base.ApiResponse[base.MessageResponse]{
Success: true,
@@ -114,7 +120,7 @@ func (h *Handler) CreateDirectory(ctx context.Context, input *CreateBuildDirecto
func (h *Handler) DeleteFile(ctx context.Context, input *DeleteBuildFileInput) (*base.ApiResponse[base.MessageResponse], error) {
if err := h.service.DeleteFile(ctx, input.Path); err != nil {
- return nil, huma.Error500InternalServerError(err.Error())
+ return nil, workspaceHTTPErrorInternal(err)
}
return &base.ApiResponse[base.MessageResponse]{
Success: true,
diff --git a/backend/internal/build/children/workspace/service.go b/backend/internal/build/children/workspace/service.go
index 093617212..242982ea5 100644
--- a/backend/internal/build/children/workspace/service.go
+++ b/backend/internal/build/children/workspace/service.go
@@ -6,17 +6,20 @@ import (
"errors"
"fmt"
"io"
+ "io/fs"
"log/slog"
"net/http"
"os"
"path"
"path/filepath"
"strings"
+ "syscall"
"github.com/getarcaneapp/arcane/types/v2/workspace"
"go.getarcane.app/acfs"
"go.getarcane.app/kit/pkg"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/common"
acfsutils "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/acfs"
)
@@ -41,12 +44,12 @@ func (s *Service) ListDirectory(ctx context.Context, dirPath string) ([]workspac
cleaned, err := kit.SanitizeBrowsePath(dirPath)
if err != nil {
- return nil, fmt.Errorf("invalid path: %w", err)
+ return nil, common.Classify(common.ErrForbidden, fmt.Errorf("invalid path: %w", err))
}
entries, err := acfs.List(ctx, root, cleaned)
if err != nil {
- return nil, fmt.Errorf("failed to list directory: %w", err)
+ return nil, classifyWorkspaceErrorInternal(fmt.Errorf("failed to list directory: %w", err))
}
results := make([]workspace.FileEntry, 0, len(entries))
@@ -66,7 +69,7 @@ func (s *Service) GetFileContent(ctx context.Context, filePath string, maxBytes
cleaned, err := kit.SanitizeBrowsePath(filePath)
if err != nil {
- return nil, "", fmt.Errorf("invalid path: %w", err)
+ return nil, "", common.Classify(common.ErrForbidden, fmt.Errorf("invalid path: %w", err))
}
if maxBytes <= 0 {
@@ -75,7 +78,7 @@ func (s *Service) GetFileContent(ctx context.Context, filePath string, maxBytes
file, _, err := acfs.OpenRead(ctx, root, cleaned, maxBytes)
if err != nil {
- return nil, "", fmt.Errorf("failed to open file: %w", err)
+ return nil, "", classifyWorkspaceErrorInternal(fmt.Errorf("failed to open file: %w", err))
}
defer func() { _ = file.Close() }()
@@ -97,12 +100,12 @@ func (s *Service) DownloadFile(ctx context.Context, filePath string) (io.ReadClo
cleaned, err := kit.SanitizeBrowsePath(filePath)
if err != nil {
- return nil, 0, fmt.Errorf("invalid path: %w", err)
+ return nil, 0, common.Classify(common.ErrForbidden, fmt.Errorf("invalid path: %w", err))
}
file, size, err := acfs.OpenRead(ctx, root, cleaned, 0)
if err != nil {
- return nil, 0, fmt.Errorf("failed to open file: %w", err)
+ return nil, 0, classifyWorkspaceErrorInternal(fmt.Errorf("failed to open file: %w", err))
}
return file, size, nil
@@ -117,21 +120,21 @@ func (s *Service) UploadFile(ctx context.Context, destPath string, content io.Re
safeFilename, err := sanitizeUploadFilenameInternal(filename)
if err != nil {
- return err
+ return common.Classify(common.ErrBadRequest, err)
}
cleaned, err := kit.SanitizeBrowsePath(destPath)
if err != nil {
- return fmt.Errorf("invalid path: %w", err)
+ return common.Classify(common.ErrForbidden, fmt.Errorf("invalid path: %w", err))
}
if mkdirAllErr := acfs.MkdirAll(ctx, root, cleaned, 0o755); mkdirAllErr != nil {
- return fmt.Errorf("failed to create directory: %w", mkdirAllErr)
+ return classifyWorkspaceErrorInternal(fmt.Errorf("failed to create directory: %w", mkdirAllErr))
}
targetFile := path.Join(cleaned, safeFilename)
if _, writeFromErr := acfs.WriteFrom(ctx, root, targetFile, content, size, 0o644); writeFromErr != nil {
- return fmt.Errorf("failed to write file: %w", writeFromErr)
+ return classifyWorkspaceErrorInternal(fmt.Errorf("failed to write file: %w", writeFromErr))
}
return nil
@@ -146,7 +149,7 @@ func (s *Service) CreateDirectory(ctx context.Context, dirPath string) error {
cleaned, err := kit.SanitizeBrowsePath(dirPath)
if err != nil {
- return fmt.Errorf("invalid path: %w", err)
+ return common.Classify(common.ErrForbidden, fmt.Errorf("invalid path: %w", err))
}
if cleaned == "/" {
@@ -154,7 +157,7 @@ func (s *Service) CreateDirectory(ctx context.Context, dirPath string) error {
}
if mkdirAllErr := acfs.MkdirAll(ctx, root, cleaned, 0o755); mkdirAllErr != nil {
- return fmt.Errorf("failed to create directory: %w", mkdirAllErr)
+ return classifyWorkspaceErrorInternal(fmt.Errorf("failed to create directory: %w", mkdirAllErr))
}
return nil
@@ -169,15 +172,15 @@ func (s *Service) DeleteFile(ctx context.Context, filePath string) error {
cleaned, err := kit.SanitizeBrowsePath(filePath)
if err != nil {
- return fmt.Errorf("invalid path: %w", err)
+ return common.Classify(common.ErrForbidden, fmt.Errorf("invalid path: %w", err))
}
if cleaned == "/" {
- return errors.New("cannot delete root directory")
+ return common.Classify(common.ErrBadRequest, errors.New("cannot delete root directory"))
}
if removeAllErr := acfs.RemoveAll(ctx, root, cleaned); removeAllErr != nil {
- return fmt.Errorf("failed to delete path: %w", removeAllErr)
+ return classifyWorkspaceErrorInternal(fmt.Errorf("failed to delete path: %w", removeAllErr))
}
return nil
@@ -202,6 +205,23 @@ func (s *Service) resolveRoot() (string, error) {
return cleaned, nil
}
+func classifyWorkspaceErrorInternal(err error) error {
+ switch {
+ case errors.Is(err, fs.ErrNotExist):
+ return common.Classify(common.ErrNotFound, err)
+ case errors.Is(err, fs.ErrExist), errors.Is(err, acfs.ErrAlreadyExists):
+ return common.Classify(common.ErrConflict, err)
+ case errors.Is(err, acfs.ErrInvalidPath), errors.Is(err, acfs.ErrOutsideRoot),
+ errors.Is(err, acfs.ErrSymlink), errors.Is(err, acfs.ErrSymlinkLoop):
+ return common.Classify(common.ErrForbidden, err)
+ case errors.Is(err, acfs.ErrIsDirectory), errors.Is(err, acfs.ErrNotDirectory), errors.Is(err, acfs.ErrNotFile),
+ errors.Is(err, syscall.ENOTDIR), errors.Is(err, acfs.ErrRootRemoval):
+ return common.Classify(common.ErrBadRequest, err)
+ default:
+ return err
+ }
+}
+
func sanitizeUploadFilenameInternal(filename string) (string, error) {
name := strings.TrimSpace(filename)
if name == "" {