Skip to content

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

fix: upload body limit and WaitForTitle/NavigatePage context handling

trs-8070e66e96054d
diff --git a/internal/bridge/cdp.go b/internal/bridge/cdp.go
index 856b871d..a4c69212 100644
--- a/internal/bridge/cdp.go
+++ b/internal/bridge/cdp.go
@@ -2,7 +2,6 @@ package bridge
import (
"context"
- "fmt"
"time"
"github.com/chromedp/cdproto/network"
@@ -24,14 +23,11 @@ func NavigatePage(ctx context.Context, url string) error {
return err
}
- timeout := time.After(30 * time.Second)
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
for {
select {
- case <-timeout:
- return fmt.Errorf("navigation timeout")
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
@@ -129,23 +125,37 @@ func ScrollByNodeID(ctx context.Context, nodeID int64) error {
)
}
-func WaitForTitle(ctx context.Context, timeout time.Duration) string {
+func WaitForTitle(ctx context.Context, timeout time.Duration) (string, error) {
if timeout <= 0 {
var title string
- _ = chromedp.Run(ctx, chromedp.Title(&title))
- return title
+ if err := chromedp.Run(ctx, chromedp.Title(&title)); err != nil {
+ return "", err
+ }
+ return title, nil
}
- start := time.Now()
- for time.Since(start) < timeout {
- var title string
- _ = chromedp.Run(ctx, chromedp.Title(&title))
- if title != "" && title != "about:blank" {
- return title
+ deadline := time.After(timeout)
+ ticker := time.NewTicker(200 * time.Millisecond)
+ defer ticker.Stop()
+
+ for {
+ select {
+ case <-ctx.Done():
+ return "", ctx.Err()
+ case <-deadline:
+ var title string
+ if err := chromedp.Run(ctx, chromedp.Title(&title)); err != nil {
+ return "", err
+ }
+ return title, nil
+ case <-ticker.C:
+ var title string
+ if err := chromedp.Run(ctx, chromedp.Title(&title)); err != nil {
+ continue
+ }
+ if title != "" && title != "about:blank" {
+ return title, nil
+ }
}
- time.Sleep(200 * time.Millisecond)
}
- var title string
- _ = chromedp.Run(ctx, chromedp.Title(&title))
- return title
}
diff --git a/internal/bridge/cdp_test.go b/internal/bridge/cdp_test.go
new file mode 100644
index 00000000..395688da
--- /dev/null
+++ b/internal/bridge/cdp_test.go
@@ -0,0 +1,39 @@
+package bridge
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ "github.com/chromedp/chromedp"
+)
+
+func TestWaitForTitle_ContextCancelled(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel() // cancel immediately
+
+ _, err := WaitForTitle(ctx, 5*time.Second)
+ if err == nil {
+ t.Error("expected error for cancelled context")
+ }
+}
+
+func TestWaitForTitle_NoTimeout(t *testing.T) {
+ ctx, _ := chromedp.NewContext(context.Background())
+
+ // With timeout <= 0, should return immediately
+ title, _ := WaitForTitle(ctx, 0)
+ if title != "" {
+ t.Errorf("expected empty title without browser, got %q", title)
+ }
+}
+
+func TestNavigatePage_ContextCancelled(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ err := NavigatePage(ctx, "https://example.com")
+ if err == nil {
+ t.Error("expected error for cancelled context")
+ }
+}
diff --git a/internal/handlers/handler_upload_test.go b/internal/handlers/handler_upload_test.go
index 45ab533e..b34201ce 100644
--- a/internal/handlers/handler_upload_test.go
+++ b/internal/handlers/handler_upload_test.go
@@ -66,3 +66,16 @@ func TestHandleTabUpload_NoTab(t *testing.T) {
t.Errorf("expected 404, got %d", w.Code)
}
}
+
+func TestHandleUpload_BodyTooLarge(t *testing.T) {
+ h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
+ // Create a body larger than 10MB
+ bigBody := make([]byte, 11<<20) // 11MB
+ req := httptest.NewRequest("POST", "/upload", bytes.NewReader(bigBody))
+ req.Header.Set("Content-Type", "application/json")
+ w := httptest.NewRecorder()
+ h.HandleUpload(w, req)
+ if w.Code != http.StatusBadRequest {
+ t.Errorf("expected 400 for oversized body, got %d", w.Code)
+ }
+}
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 74d80e9e..237c3653 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -161,7 +161,7 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
var url string
_ = chromedp.Run(tCtx, chromedp.Location(&url))
- title := bridge.WaitForTitle(tCtx, titleWait)
+ title, _ := bridge.WaitForTitle(tCtx, titleWait)
web.JSON(w, 200, map[string]any{"tabId": hashTabID, "url": url, "title": title})
return
@@ -198,7 +198,7 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
var url string
_ = chromedp.Run(tCtx, chromedp.Location(&url))
- title := bridge.WaitForTitle(tCtx, titleWait)
+ title, _ := bridge.WaitForTitle(tCtx, titleWait)
web.JSON(w, 200, map[string]any{"tabId": resolvedTabID, "url": url, "title": title})
}
diff --git a/internal/handlers/upload.go b/internal/handlers/upload.go
index 9b8726f0..d2871668 100644
--- a/internal/handlers/upload.go
+++ b/internal/handlers/upload.go
@@ -37,6 +37,8 @@ type uploadRequest struct {
func (h *Handlers) HandleUpload(w http.ResponseWriter, r *http.Request) {
tabID := r.URL.Query().Get("tabId")
+ r.Body = http.MaxBytesReader(w, r.Body, 10<<20) // 10MB limit
+
var req uploadRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
web.Error(w, 400, fmt.Errorf("invalid JSON body: %w", err))