Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

Add SafePath validation to upload handler

Roopesh39c8d8f946f4
diff --git a/internal/handlers/upload.go b/internal/handlers/upload.go
index d2871668..2eb09330 100644
--- a/internal/handlers/upload.go
+++ b/internal/handlers/upload.go
@@ -54,12 +54,18 @@ func (h *Handlers) HandleUpload(w http.ResponseWriter, r *http.Request) {
return
}
- // Validate local paths exist.
- for _, p := range req.Paths {
- if _, err := os.Stat(p); err != nil {
- web.Error(w, 400, fmt.Errorf("file not found: %s", p))
+ // Validate local paths stay within the allowed StateDir.
+ for i, p := range req.Paths {
+ safe, err := web.SafePath(h.Config.StateDir, p)
+ if err != nil {
+ web.Error(w, 400, fmt.Errorf("invalid path: %w", err))
+ return
+ }
+ if _, err := os.Stat(safe); err != nil {
+ web.Error(w, 400, fmt.Errorf("file not found: %s", safe))
return
}
+ req.Paths[i] = safe
}
// Decode base64 files to temp dir.
diff --git a/internal/handlers/upload_test.go b/internal/handlers/upload_test.go
index b1a2d387..335733d6 100644
--- a/internal/handlers/upload_test.go
+++ b/internal/handlers/upload_test.go
@@ -2,6 +2,7 @@ package handlers
import (
"bytes"
+ "fmt"
"net/http"
"net/http/httptest"
"testing"
@@ -44,6 +45,33 @@ func TestHandleUpload_NonexistentPath(t *testing.T) {
}
}
+func TestHandleUpload_PathTraversal(t *testing.T) {
+ tmpDir := t.TempDir()
+ h := New(&mockBridge{}, &config.RuntimeConfig{StateDir: tmpDir}, nil, nil, nil)
+
+ tests := []struct {
+ name string
+ path string
+ }{
+ {"dotdot traversal", "../etc/passwd"},
+ {"absolute outside", "/etc/passwd"},
+ {"hidden traversal", "uploads/../../etc/passwd"},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ body := fmt.Sprintf(`{"selector": "input[type=file]", "paths": [%q]}`, tt.path)
+ req := httptest.NewRequest("POST", "/upload", bytes.NewReader([]byte(body)))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+ h.HandleUpload(w, req)
+ if w.Code != http.StatusBadRequest {
+ t.Errorf("expected 400 for traversal path %q, got %d", tt.path, w.Code)
+ }
+ })
+ }
+}
+
func TestHandleTabUpload_MissingTabID(t *testing.T) {
h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
req := httptest.NewRequest("POST", "/tabs//upload", bytes.NewReader([]byte(`{"selector":"input[type=file]"}`)))