Skip to content

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

fix(upload): keep decoded uploads until the browser reads them at submit

AriaShishegaran4aae5a82efa5
diff --git a/internal/handlers/upload.go b/internal/handlers/upload.go
index fc00146d..ed2df55b 100644
--- a/internal/handlers/upload.go
+++ b/internal/handlers/upload.go
@@ -91,15 +91,40 @@ func (h *Handlers) HandleUpload(w http.ResponseWriter, r *http.Request) {
req.Paths[i] = safe
}
- // Decode base64 files to temp dir.
+ // Decode base64 files into a staged dir that OUTLIVES this request. CDP
+ // SetFileInputFiles only records the path on the <input>; the browser reads the
+ // bytes LAZILY at form-submit time, which is typically a separate, later
+ // request. Deleting the decoded file when this handler returns (the previous
+ // `defer os.RemoveAll`) left the file gone by submit time, so multipart
+ // submissions failed with ERR_FILE_NOT_FOUND ("Your file couldn't be accessed.
+ // It may have been moved, edited, or deleted."). Keep the staged files on the
+ // success path (reclaimed with the session StateDir) and only remove them if
+ // the upload fails before attaching to a tab.
var tempFiles []string
+ var stagedDir string
+ uploadSucceeded := false
+ defer func() {
+ if !uploadSucceeded && stagedDir != "" {
+ _ = os.RemoveAll(stagedDir)
+ }
+ }()
if len(req.Files) > 0 {
- tmpDir, err := os.MkdirTemp("", "pinchtab-upload-*")
+ // Stage under StateDir/uploads when configured (persists for the session),
+ // else the OS temp dir — never the process working directory.
+ stageBase := os.TempDir()
+ if strings.TrimSpace(h.Config.StateDir) != "" {
+ if err := os.MkdirAll(uploadBase, 0o755); err != nil {
+ httpx.Error(w, 500, fmt.Errorf("create upload dir: %w", err))
+ return
+ }
+ stageBase = uploadBase
+ }
+ dir, err := os.MkdirTemp(stageBase, "pinchtab-upload-*")
if err != nil {
- httpx.Error(w, 500, fmt.Errorf("create temp dir: %w", err))
+ httpx.Error(w, 500, fmt.Errorf("create staged dir: %w", err))
return
}
- defer func() { _ = os.RemoveAll(tmpDir) }()
+ stagedDir = dir
for i, f := range req.Files {
data, ext, err := decodeFileData(f)
@@ -116,9 +141,9 @@ func (h *Handlers) HandleUpload(w http.ResponseWriter, r *http.Request) {
httpx.Error(w, 400, fmt.Errorf("upload payload too large: max %d bytes total", maxTotalBytes))
return
}
- path := fmt.Sprintf("%s/upload-%d%s", tmpDir, i, ext)
+ path := fmt.Sprintf("%s/upload-%d%s", stagedDir, i, ext)
if err := os.WriteFile(path, data, 0600); err != nil {
- httpx.Error(w, 500, fmt.Errorf("write temp file: %w", err))
+ httpx.Error(w, 500, fmt.Errorf("write staged file: %w", err))
return
}
tempFiles = append(tempFiles, path)
@@ -162,6 +187,10 @@ func (h *Handlers) HandleUpload(w http.ResponseWriter, r *http.Request) {
h.recordActivity(r, activity.Update{Action: "upload", TabID: resolvedTabID})
+ // Files attached successfully — keep the staged copies so the browser can read
+ // them at form-submit time (the deferred cleanup becomes a no-op).
+ uploadSucceeded = true
+
httpx.JSON(w, 200, map[string]any{
"status": "ok",
"files": len(allPaths),
diff --git a/internal/handlers/upload_test.go b/internal/handlers/upload_test.go
index d048f183..7a68b0ab 100644
--- a/internal/handlers/upload_test.go
+++ b/internal/handlers/upload_test.go
@@ -8,6 +8,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
+ "strings"
"testing"
"time"
@@ -331,3 +332,34 @@ func TestHandleUpload_Disabled(t *testing.T) {
t.Errorf("expected 403 when upload disabled, got %d", w.Code)
}
}
+
+// Regression: decoded base64 uploads must persist past the handler so the browser
+// can read them LAZILY at form-submit time (a separate later request). They are
+// kept on success and removed only on a pre-attach failure — never written into
+// the process working directory.
+func TestHandleUpload_StagedFilesCleanedOnFailureNotCWD(t *testing.T) {
+ tmpDir := t.TempDir()
+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{AllowUpload: true, StateDir: tmpDir}, nil, nil, nil)
+ png := "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=="
+ body := fmt.Sprintf(`{"selector":"input[type=file]","files":[%q]}`, png)
+ req := httptest.NewRequest("POST", "/upload?tabId=t1", bytes.NewReader([]byte(body)))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+ h.HandleUpload(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Fatalf("expected 404 (failTab) after decode, got %d", w.Code)
+ }
+ // The decode succeeded but the upload never attached to a tab → staged dir
+ // must be cleaned up.
+ entries, _ := os.ReadDir(filepath.Join(tmpDir, "uploads"))
+ for _, e := range entries {
+ if strings.HasPrefix(e.Name(), "pinchtab-upload-") {
+ t.Fatalf("staged upload dir leaked on failure: %s", e.Name())
+ }
+ }
+ // Must never create an "uploads" dir in the process working directory.
+ if _, err := os.Stat("uploads"); err == nil {
+ _ = os.RemoveAll("uploads")
+ t.Fatalf("upload handler created ./uploads in the working directory")
+ }
+}