Skip to content

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

refactor(handlers): run the tab guard chain in one place behind a declared guard set

Luigi Agosti73e51f422ce2
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index 5f7be67e..8c686db2 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -540,13 +540,7 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
httpx.ErrorCode(w, 423, "tab_locked", err.Error(), false, nil)
return
}
- if h.refuseIfDialogBlocked(w, resolvedTabID) {
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
- return
- }
- if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDialogBlocked|guardDomainPolicy|guardHandoffPause); !ok {
return
}
defer h.armAutoCloseIfEnabled(resolvedTabID)
@@ -835,7 +829,7 @@ func (h *Handlers) handleActionsBatch(w http.ResponseWriter, r *http.Request, re
continue
}
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy); !ok {
return
}
if err := h.enforceTabNotPausedForHandoff(resolvedTabID); err != nil {
@@ -1055,7 +1049,7 @@ func (h *Handlers) HandleMacro(w http.ResponseWriter, r *http.Request) {
if step.TabID == "" {
step.TabID = resolvedTabID
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy); !ok {
return
}
if err := h.enforceTabNotPausedForHandoff(resolvedTabID); err != nil {
diff --git a/internal/handlers/console.go b/internal/handlers/console.go
index 1e4bd2ff..0f772551 100644
--- a/internal/handlers/console.go
+++ b/internal/handlers/console.go
@@ -54,7 +54,7 @@ func (h *Handlers) HandleGetConsoleLogs(w http.ResponseWriter, r *http.Request)
if !ok {
return
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, tabID); !ok {
+ if _, ok := h.applyTabGuards(w, r, ctx, tabID, guardDomainPolicy); !ok {
return
}
@@ -96,7 +96,7 @@ func (h *Handlers) HandleGetErrorLogs(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, tabID); !ok {
+ if _, ok := h.applyTabGuards(w, r, ctx, tabID, guardDomainPolicy); !ok {
return
}
diff --git a/internal/handlers/cookies.go b/internal/handlers/cookies.go
index f25439e7..c2ca833b 100644
--- a/internal/handlers/cookies.go
+++ b/internal/handlers/cookies.go
@@ -30,12 +30,8 @@ func (h *Handlers) HandleGetCookies(w http.ResponseWriter, r *http.Request) {
url := r.URL.Query().Get("url")
name := r.URL.Query().Get("name")
- ctx, resolvedTabID, err := h.tabContext(r, tabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, _, ok := h.guardedTabContext(w, r, tabID, guardDomainPolicy)
+ if !ok {
return
}
@@ -197,12 +193,8 @@ func (h *Handlers) HandleSetCookies(w http.ResponseWriter, r *http.Request) {
}
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, _, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/credentials.go b/internal/handlers/credentials.go
index f83daf6f..149bbf7a 100644
--- a/internal/handlers/credentials.go
+++ b/internal/handlers/credentials.go
@@ -145,12 +145,8 @@ func (h *Handlers) setCredentials(w http.ResponseWriter, r *http.Request, req cr
// Non-empty username requires password field (empty password is allowed).
// Empty username means "clear credentials".
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/dialog.go b/internal/handlers/dialog.go
index 247bb9bc..6df3fc9d 100644
--- a/internal/handlers/dialog.go
+++ b/internal/handlers/dialog.go
@@ -33,13 +33,8 @@ func (h *Handlers) HandleDialog(w http.ResponseWriter, r *http.Request) {
return
}
- ctx, resolvedID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
-
- if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedID) {
+ ctx, resolvedID, ok := h.guardedTabContext(w, r, req.TabID, guardHandoffPause)
+ if !ok {
return
}
diff --git a/internal/handlers/download.go b/internal/handlers/download.go
index 4c3cf78a..fcf34eb9 100644
--- a/internal/handlers/download.go
+++ b/internal/handlers/download.go
@@ -117,7 +117,7 @@ func (h *Handlers) enforceDownloadTabPolicy(w http.ResponseWriter, r *http.Reque
httpx.ErrorCode(w, http.StatusLocked, "tab_locked", err.Error(), false, nil)
return false
}
- currentURL, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID)
+ currentURL, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy)
if !ok {
return false
}
diff --git a/internal/handlers/evaluate.go b/internal/handlers/evaluate.go
index ec8182b5..871a9521 100644
--- a/internal/handlers/evaluate.go
+++ b/internal/handlers/evaluate.go
@@ -41,15 +41,8 @@ func (h *Handlers) HandleEvaluate(w http.ResponseWriter, r *http.Request) {
return
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
- return
- }
- if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {
+ ctx, _, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy|guardHandoffPause)
+ if !ok {
return
}
diff --git a/internal/handlers/find.go b/internal/handlers/find.go
index faaa85ce..1f41d7a2 100644
--- a/internal/handlers/find.go
+++ b/internal/handlers/find.go
@@ -68,14 +68,8 @@ func (h *Handlers) HandleFind(w http.ResponseWriter, r *http.Request) {
return
}
- // Resolve tab context to get the resolved ID for cache lookup.
- // Keep ctxTab so we can reuse it for CDP operations (e.g. auto-refresh).
- ctxTab, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctxTab, resolvedTabID); !ok {
+ ctxTab, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/geolocation.go b/internal/handlers/geolocation.go
index e51b3f3c..a096d45d 100644
--- a/internal/handlers/geolocation.go
+++ b/internal/handlers/geolocation.go
@@ -74,12 +74,8 @@ func (h *Handlers) setGeolocation(w http.ResponseWriter, r *http.Request, req ge
req.Accuracy = 1.0
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/handoff.go b/internal/handlers/handoff.go
index 550e835d..4cc2ed7b 100644
--- a/internal/handlers/handoff.go
+++ b/internal/handlers/handoff.go
@@ -68,12 +68,11 @@ func (h *Handlers) enforceTabNotPausedForHandoff(tabID string) error {
return fmt.Errorf("tab %s is paused for human handoff", tabID)
}
-// enforceTabNotPausedForHandoffOrRespond is the single writer of the RESPONSE-LEVEL
-// paused-tab refusal: an endpoint that answers with one status calls it, so a client
-// sees the same 409, code and remedy hint whichever it hit. POST /actions and POST /macro
-// answer 200 with a result list instead, so they carry the same condition per item through
-// handoffPausedActionResult — the two writers are siblings, same code and hint, different
-// envelope. Reports ok=false once the 409 has been written.
+// enforceTabNotPausedForHandoffOrRespond writes the RESPONSE-LEVEL paused-tab
+// refusal and is reached only through guardHandoffPause. POST /actions and POST
+// /macro answer 200 with a result list instead, so they carry the same condition
+// per step through handoffPausedActionResult — same code and hint, different
+// envelope. That per-step form is why the guard set cannot express them.
func (h *Handlers) enforceTabNotPausedForHandoffOrRespond(w http.ResponseWriter, tabID string) bool {
err := h.enforceTabNotPausedForHandoff(tabID)
if err == nil {
@@ -154,7 +153,7 @@ func (h *Handlers) HandleTabHandoff(w http.ResponseWriter, r *http.Request) {
httpx.ErrorCode(w, 423, "tab_locked", err.Error(), false, nil)
return
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy); !ok {
return
}
@@ -214,7 +213,7 @@ func (h *Handlers) HandleTabResume(w http.ResponseWriter, r *http.Request) {
httpx.ErrorCode(w, 423, "tab_locked", err.Error(), false, nil)
return
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy); !ok {
return
}
diff --git a/internal/handlers/headers.go b/internal/handlers/headers.go
index 9984a692..9fee730f 100644
--- a/internal/handlers/headers.go
+++ b/internal/handlers/headers.go
@@ -52,12 +52,8 @@ func (h *Handlers) setHeaders(w http.ResponseWriter, r *http.Request, req header
return
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/history.go b/internal/handlers/history.go
index d3ee1858..13783d03 100644
--- a/internal/handlers/history.go
+++ b/internal/handlers/history.go
@@ -33,12 +33,8 @@ func (h *Handlers) handleHistoryNav(w http.ResponseWriter, r *http.Request,
}
tabID := r.URL.Query().Get("tabId")
dismissBanners := historyDismissBannersFlag(r)
- ctx, resolvedID, err := h.tabContext(r, tabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedID) {
+ ctx, resolvedID, ok := h.guardedTabContext(w, r, tabID, guardHandoffPause)
+ if !ok {
return
}
if err := navigate(ctx, resolvedID, dismissBanners); err != nil {
diff --git a/internal/handlers/inspect_lifecycle.go b/internal/handlers/inspect_lifecycle.go
index 23ed18a0..dc5f6d65 100644
--- a/internal/handlers/inspect_lifecycle.go
+++ b/internal/handlers/inspect_lifecycle.go
@@ -91,12 +91,8 @@ func (h *Handlers) inspectElement(w http.ResponseWriter, r *http.Request, tabID
return
}
- ctx, resolvedTabID, err := h.tabContextWithHeader(w, r, tabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContextWithHeader(w, r, tabID, guardDomainPolicy)
+ if !ok {
return
}
defer h.armAutoCloseIfEnabled(resolvedTabID)
diff --git a/internal/handlers/media.go b/internal/handlers/media.go
index 60c99c7a..9dcb018e 100644
--- a/internal/handlers/media.go
+++ b/internal/handlers/media.go
@@ -63,12 +63,8 @@ func (h *Handlers) setMedia(w http.ResponseWriter, r *http.Request, req mediaReq
return
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index bfab84ab..1cf7db75 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -424,7 +424,7 @@ func (h *Handlers) executeNavigate(w http.ResponseWriter, r *http.Request, req n
WriteTabContextError(w, err, 404)
return
}
- if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardHandoffPause); !ok {
return
}
// Navigate signals fresh work on this tab — drop any pending auto-close
diff --git a/internal/handlers/network.go b/internal/handlers/network.go
index ec38831e..e898da03 100644
--- a/internal/handlers/network.go
+++ b/internal/handlers/network.go
@@ -53,19 +53,8 @@ func (h *Handlers) ensureBrowserReady(w http.ResponseWriter) bool {
return true
}
-// resolveNetworkTab resolves the tabId query param to a tab context and enforces
-// the current-tab domain policy, writing the 404/policy response on failure.
func (h *Handlers) resolveNetworkTab(w http.ResponseWriter, r *http.Request) (context.Context, string, bool) {
- tabID := r.URL.Query().Get("tabId")
- tabCtx, resolvedTabID, err := h.tabContext(r, tabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return nil, "", false
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, tabCtx, resolvedTabID); !ok {
- return nil, "", false
- }
- return tabCtx, resolvedTabID, true
+ return h.guardedTabContext(w, r, r.URL.Query().Get("tabId"), guardDomainPolicy)
}
// ensureCaptureBuffer returns the tab's network buffer, lazily starting capture
diff --git a/internal/handlers/network_route.go b/internal/handlers/network_route.go
index 8684a3d4..e6db348b 100644
--- a/internal/handlers/network_route.go
+++ b/internal/handlers/network_route.go
@@ -97,11 +97,9 @@ func (h *Handlers) HandleTabNetworkRouteList(w http.ResponseWriter, r *http.Requ
h.handleNetworkRouteListFor(w, r, tabID)
}
-// requireRouteContext is the shared prelude for the network/route handlers.
-// It runs the capability gate, ensures Chrome is up, and resolves the tab
-// context. On failure the response has been written and ok=false. On success
-// the caller has tabCtx + resolved tab ID; mutation handlers should follow up
-// with enforceCurrentTabDomainPolicy(tabCtx, resolvedID).
+// requireRouteContext is the shared prelude for the network/route handlers: the
+// capability gate, browser bringup and tab resolution. It applies no tab guards
+// — each caller declares its own.
func (h *Handlers) requireRouteContext(w http.ResponseWriter, r *http.Request, tabID string) (tabCtx context.Context, resolvedID string, ok bool) {
if !h.networkInterceptEnabled() {
h.writeCapabilityDisabled(w, routes.CapNetworkIntercept)
@@ -127,7 +125,7 @@ func (h *Handlers) handleNetworkRouteFor(w http.ResponseWriter, r *http.Request,
if !ok {
return
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, tabCtx, resolvedID); !ok {
+ if _, ok := h.applyTabGuards(w, r, tabCtx, resolvedID, guardDomainPolicy); !ok {
return
}
@@ -189,7 +187,7 @@ func (h *Handlers) handleNetworkUnrouteFor(w http.ResponseWriter, r *http.Reques
if !ok {
return
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, tabCtx, resolvedID); !ok {
+ if _, ok := h.applyTabGuards(w, r, tabCtx, resolvedID, guardDomainPolicy); !ok {
return
}
diff --git a/internal/handlers/offline.go b/internal/handlers/offline.go
index 96358f2f..4e9de7b8 100644
--- a/internal/handlers/offline.go
+++ b/internal/handlers/offline.go
@@ -65,12 +65,8 @@ func (h *Handlers) setOffline(w http.ResponseWriter, r *http.Request, req offlin
req.UploadThroughput = -1
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/read_prelude.go b/internal/handlers/read_prelude.go
index 578b9aea..5fae1bc1 100644
--- a/internal/handlers/read_prelude.go
+++ b/internal/handlers/read_prelude.go
@@ -34,25 +34,16 @@ func (h *Handlers) resolveReadRouting(w http.ResponseWriter, r *http.Request, ta
return routing.EffectiveCfg, route, true
}
-// resolveReadContext resolves the tab context, enforces the current-tab domain
-// policy, and wires the action-timeout context (launching the client-disconnect
-// canceller). ok=false means an error response was already written.
-//
-// When ok, the CALLER must defer in this registration order so the prior LIFO
-// semantics are preserved (cancel runs before auto-close arming):
+// resolveReadContext guards the tab and wires the action-timeout context.
+// ok=false means an error response was already written. When ok, the CALLER must
+// defer in this registration order so the LIFO semantics hold (cancel runs
+// before auto-close arming):
//
// defer h.armAutoCloseIfEnabled(resolvedTabID)
// defer cancel()
func (h *Handlers) resolveReadContext(w http.ResponseWriter, r *http.Request, tabID string, actionTimeout time.Duration) (resolvedTabID string, tCtx context.Context, cancel context.CancelFunc, ok bool) {
- ctx, resolvedTabID, err := h.tabContextWithHeader(w, r, tabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return "", nil, nil, false
- }
- if h.refuseIfDialogBlocked(w, resolvedTabID) {
- return "", nil, nil, false
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContextWithHeader(w, r, tabID, guardDialogBlocked|guardDomainPolicy)
+ if !ok {
return "", nil, nil, false
}
@@ -61,21 +52,12 @@ func (h *Handlers) resolveReadContext(w http.ResponseWriter, r *http.Request, ta
return resolvedTabID, tCtx, tCancel, true
}
-// resolveBinaryReadContext is the visual-export sibling of resolveReadContext: it
-// resolves the tab context (no request header), enforces the current-tab domain
-// policy, and wires the action-timeout/client-cancel — but does NOT arm
-// auto-close (screenshot/pdf are one-shot exports). ok=false means an error
-// response was already written. The caller must defer cancel().
+// resolveBinaryReadContext is the visual-export sibling of resolveReadContext:
+// no resolved-tab response header, and no auto-close arming (screenshot/pdf are
+// one-shot exports). The caller must defer cancel().
func (h *Handlers) resolveBinaryReadContext(w http.ResponseWriter, r *http.Request, tabID string, actionTimeout time.Duration) (resolvedTabID string, tCtx context.Context, cancel context.CancelFunc, ok bool) {
- ctx, resolvedTabID, err := h.tabContext(r, tabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return "", nil, nil, false
- }
- if h.refuseIfDialogBlocked(w, resolvedTabID) {
- return "", nil, nil, false
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, tabID, guardDialogBlocked|guardDomainPolicy)
+ if !ok {
return "", nil, nil, false
}
diff --git a/internal/handlers/screencast.go b/internal/handlers/screencast.go
index 010a3d85..2e4ad126 100644
--- a/internal/handlers/screencast.go
+++ b/internal/handlers/screencast.go
@@ -34,7 +34,7 @@ func (h *Handlers) HandleScreencast(w http.ResponseWriter, r *http.Request) {
httpx.Problem(w, http.StatusNotFound, "tab_not_found", "tab not found", false, nil)
return
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy); !ok {
return
}
diff --git a/internal/handlers/solver.go b/internal/handlers/solver.go
index b5ffab33..ec2325b2 100644
--- a/internal/handlers/solver.go
+++ b/internal/handlers/solver.go
@@ -79,10 +79,7 @@ func (h *Handlers) HandleSolve(w http.ResponseWriter, r *http.Request) {
return
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
- return
- }
- if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy|guardHandoffPause); !ok {
return
}
@@ -134,9 +131,7 @@ func (h *Handlers) HandleSolve(w http.ResponseWriter, r *http.Request) {
return
}
- // Re-check domain policy after solve — the page may have redirected
- // to a different domain once the challenge was resolved.
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy); !ok {
return
}
diff --git a/internal/handlers/state.go b/internal/handlers/state.go
index 3fd35a1d..d22a0ee5 100644
--- a/internal/handlers/state.go
+++ b/internal/handlers/state.go
@@ -84,12 +84,8 @@ func (h *Handlers) HandleStateSave(w http.ResponseWriter, r *http.Request) {
}
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/state_capture.go b/internal/handlers/state_capture.go
index 6180074c..272e317f 100644
--- a/internal/handlers/state_capture.go
+++ b/internal/handlers/state_capture.go
@@ -38,12 +38,8 @@ func (h *Handlers) HandleStateCurrent(w http.ResponseWriter, r *http.Request) {
}
tabID := r.URL.Query().Get("tabId")
- ctx, resolvedTabID, err := h.tabContext(r, tabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, tabID, guardDomainPolicy)
+ if !ok {
return
}
@@ -97,12 +93,8 @@ func (h *Handlers) HandleStateLoad(w http.ResponseWriter, r *http.Request) {
return
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/stealth.go b/internal/handlers/stealth.go
index 7a05d02d..3a302659 100644
--- a/internal/handlers/stealth.go
+++ b/internal/handlers/stealth.go
@@ -36,12 +36,8 @@ func (h *Handlers) HandleFingerprintRotate(w http.ResponseWriter, r *http.Reques
return
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/storage.go b/internal/handlers/storage.go
index 07bca4bd..6d6ea77d 100644
--- a/internal/handlers/storage.go
+++ b/internal/handlers/storage.go
@@ -47,12 +47,8 @@ func (h *Handlers) ensureStateExportEnabled(w http.ResponseWriter) bool {
// infix ("get"/"set"/"delete"); activityAction is the recordActivity action;
// logType/logKey are the structured-log values.
func (h *Handlers) runStorageOp(w http.ResponseWriter, r *http.Request, tabID, script, opLabel, activityAction, logType, logKey string) {
- ctx, resolvedTabID, err := h.tabContext(r, tabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, tabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/tab_policy.go b/internal/handlers/tab_policy.go
index 3e81fa93..74cade33 100644
--- a/internal/handlers/tab_policy.go
+++ b/internal/handlers/tab_policy.go
@@ -21,6 +21,54 @@ type tabPolicyStateSetter interface {
SetTabPolicyState(tabID string, state bridge.TabPolicyState)
}
+type tabGuards uint8
+
+const (
+ guardNone tabGuards = 1 << iota
+ guardDialogBlocked
+ guardDomainPolicy
+ guardHandoffPause
+)
+
+func (h *Handlers) applyTabGuards(w http.ResponseWriter, r *http.Request, ctx context.Context, tabID string, guards tabGuards) (string, bool) {
+ if guards&guardDialogBlocked != 0 && h.refuseIfDialogBlocked(w, tabID) {
+ return "", false
+ }
+ var currentURL string
+ if guards&guardDomainPolicy != 0 {
+ url, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, tabID)
+ if !ok {
+ return "", false
+ }
+ currentURL = url
+ }
+ if guards&guardHandoffPause != 0 && !h.enforceTabNotPausedForHandoffOrRespond(w, tabID) {
+ return "", false
+ }
+ return currentURL, true
+}
+
+func (h *Handlers) guardTabContext(w http.ResponseWriter, r *http.Request, ctx context.Context, tabID string, err error, guards tabGuards) (context.Context, string, bool) {
+ if err != nil {
+ WriteTabContextError(w, err, http.StatusNotFound)
+ return nil, "", false
+ }
+ if _, ok := h.applyTabGuards(w, r, ctx, tabID, guards); !ok {
+ return nil, "", false
+ }
+ return ctx, tabID, true
+}
+
+func (h *Handlers) guardedTabContext(w http.ResponseWriter, r *http.Request, tabID string, guards tabGuards) (context.Context, string, bool) {
+ ctx, resolvedTabID, err := h.tabContext(r, tabID)
+ return h.guardTabContext(w, r, ctx, resolvedTabID, err, guards)
+}
+
+func (h *Handlers) guardedTabContextWithHeader(w http.ResponseWriter, r *http.Request, tabID string, guards tabGuards) (context.Context, string, bool) {
+ ctx, resolvedTabID, err := h.tabContextWithHeader(w, r, tabID)
+ return h.guardTabContext(w, r, ctx, resolvedTabID, err, guards)
+}
+
func (h *Handlers) currentTabDomainPolicyEnabled() bool {
return h != nil &&
h.Config != nil &&
diff --git a/internal/handlers/upload.go b/internal/handlers/upload.go
index c7d8aa04..93ba10b0 100644
--- a/internal/handlers/upload.go
+++ b/internal/handlers/upload.go
@@ -194,10 +194,7 @@ func (h *Handlers) HandleUpload(w http.ResponseWriter, r *http.Request) {
httpx.ErrorCode(w, http.StatusLocked, "tab_locked", err.Error(), false, nil)
return
}
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
- return
- }
- if !h.enforceTabNotPausedForHandoffOrRespond(w, resolvedTabID) {
+ if _, ok := h.applyTabGuards(w, r, ctx, resolvedTabID, guardDomainPolicy|guardHandoffPause); !ok {
return
}
diff --git a/internal/handlers/viewport.go b/internal/handlers/viewport.go
index 640337b1..4dea2c28 100644
--- a/internal/handlers/viewport.go
+++ b/internal/handlers/viewport.go
@@ -66,12 +66,8 @@ func (h *Handlers) setViewport(w http.ResponseWriter, r *http.Request, req viewp
req.DeviceScaleFactor = 1.0
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
diff --git a/internal/handlers/wait.go b/internal/handlers/wait.go
index e5767c98..4dd9d0aa 100644
--- a/internal/handlers/wait.go
+++ b/internal/handlers/wait.go
@@ -173,12 +173,8 @@ func (h *Handlers) handleWaitCore(w http.ResponseWriter, r *http.Request, req wa
return
}
- ctx, resolvedTabID, err := h.tabContext(r, req.TabID)
- if err != nil {
- WriteTabContextError(w, err, 404)
- return
- }
- if _, ok := h.enforceCurrentTabDomainPolicy(w, r, ctx, resolvedTabID); !ok {
+ ctx, resolvedTabID, ok := h.guardedTabContext(w, r, req.TabID, guardDomainPolicy)
+ if !ok {
return
}
@@ -222,7 +218,7 @@ func (h *Handlers) handleWaitCore(w http.ResponseWriter, r *http.Request, req wa
matchLabel = "fn"
}
- err = pollUntil(tCtx, pollInterval, func() (bool, error) {
+ err := pollUntil(tCtx, pollInterval, func() (bool, error) {
if mode == "url" {
var href string
if evalErr := h.Bridge.Evaluate(tCtx, "window.location.href", &href, bridge.EvalOpts{}); evalErr != nil {