Skip to content

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

fix: return 403 for IDPI-blocked requests instead of 500

Boschf5b06988ffe4
diff --git a/internal/engine/engine.go b/internal/engine/engine.go
index 3ac39d79..8cd0dc37 100644
--- a/internal/engine/engine.go
+++ b/internal/engine/engine.go
@@ -6,7 +6,26 @@
// or any other package.
package engine
-import "context"
+import (
+ "context"
+ "errors"
+ "fmt"
+)
+
+// IDPIBlockedError is returned when IDPI security checks block a request.
+type IDPIBlockedError struct {
+ Reason string
+}
+
+func (e *IDPIBlockedError) Error() string {
+ return fmt.Sprintf("blocked by IDPI: %s", e.Reason)
+}
+
+// IsIDPIBlocked reports whether err is an IDPI block.
+func IsIDPIBlocked(err error) bool {
+ var target *IDPIBlockedError
+ return errors.As(err, &target)
+}
// Capability identifies an operation the engine may handle.
type Capability string
diff --git a/internal/engine/safe.go b/internal/engine/safe.go
index 6e5303cb..a0d3d2fe 100644
--- a/internal/engine/safe.go
+++ b/internal/engine/safe.go
@@ -2,7 +2,6 @@ package engine
import (
"context"
- "fmt"
"log/slog"
"strings"
@@ -39,7 +38,7 @@ func (s *SafeEngine) Navigate(ctx context.Context, url string) (*NavigateResult,
// Pre-flight: IDPI domain check.
domainResult := s.guard.CheckDomain(url)
if domainResult.Blocked {
- return nil, fmt.Errorf("navigation blocked by IDPI: %s", domainResult.Reason)
+ return nil, &IDPIBlockedError{Reason: domainResult.Reason}
}
result, err := s.inner.Navigate(ctx, url)
@@ -75,7 +74,7 @@ func (s *SafeEngine) Snapshot(ctx context.Context, tabID, filter string) (*Snaps
scanResult := s.guard.ScanContent(sb.String())
if scanResult.Blocked {
- return nil, fmt.Errorf("snapshot blocked by IDPI scanner: %s", scanResult.Reason)
+ return nil, &IDPIBlockedError{Reason: scanResult.Reason}
}
if scanResult.Threat {
result.IDPIWarning = scanResult.Reason
@@ -94,7 +93,7 @@ func (s *SafeEngine) Text(ctx context.Context, tabID string) (*TextResult, error
// Post-flight: scan text for injection patterns.
scanResult := s.guard.ScanContent(result.Text)
if scanResult.Blocked {
- return nil, fmt.Errorf("content blocked by IDPI scanner: %s", scanResult.Reason)
+ return nil, &IDPIBlockedError{Reason: scanResult.Reason}
}
if scanResult.Threat {
slog.Warn("IDPI content warning on text", "engine", result.Engine, "reason", scanResult.Reason)
diff --git a/internal/engine/safe_test.go b/internal/engine/safe_test.go
index 81a55e88..a889c7d7 100644
--- a/internal/engine/safe_test.go
+++ b/internal/engine/safe_test.go
@@ -78,8 +78,8 @@ func TestSafeEngine_Navigate_BlockedDomain(t *testing.T) {
if err == nil {
t.Fatal("expected error for blocked domain")
}
- if !strings.Contains(err.Error(), "IDPI") {
- t.Errorf("error should mention IDPI: %v", err)
+ if !IsIDPIBlocked(err) {
+ t.Errorf("expected IDPIBlockedError, got: %T", err)
}
if inner.navigateCalled {
t.Error("inner Navigate should not be called when domain is blocked")
@@ -114,8 +114,8 @@ func TestSafeEngine_Snapshot_BlockedContent(t *testing.T) {
if err == nil {
t.Fatal("expected error for blocked content")
}
- if !strings.Contains(err.Error(), "IDPI") {
- t.Errorf("error should mention IDPI: %v", err)
+ if !IsIDPIBlocked(err) {
+ t.Errorf("expected IDPIBlockedError, got: %T", err)
}
}
@@ -150,6 +150,9 @@ func TestSafeEngine_Text_BlockedContent(t *testing.T) {
if err == nil {
t.Fatal("expected error for blocked content")
}
+ if !IsIDPIBlocked(err) {
+ t.Errorf("expected IDPIBlockedError, got: %T", err)
+ }
}
func TestSafeEngine_Text_WrapContent(t *testing.T) {
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index 41b028a5..70ff9ece 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -329,6 +329,10 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
httpx.ErrorCode(w, http.StatusNotImplemented, "not_supported", actionErr.Error(), false, nil)
return
}
+ if engine.IsIDPIBlocked(actionErr) {
+ httpx.ErrorCode(w, http.StatusForbidden, "idpi_blocked", actionErr.Error(), false, nil)
+ return
+ }
httpx.ErrorCode(w, 500, "action_failed", fmt.Sprintf("action %s: %v", req.Kind, actionErr), true, nil)
return
}
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 892d13f1..b72b7b68 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -115,7 +115,11 @@ func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
h.recordEngine(r, "lite")
result, err := h.Router.Lite().Navigate(r.Context(), req.URL)
if err != nil {
- httpx.Error(w, 502, fmt.Errorf("lite navigate: %w", err))
+ if engine.IsIDPIBlocked(err) {
+ httpx.Error(w, http.StatusForbidden, err)
+ } else {
+ httpx.Error(w, 502, fmt.Errorf("lite navigate: %w", err))
+ }
return
}
w.Header().Set("X-Engine", "lite")
diff --git a/internal/handlers/snapshot.go b/internal/handlers/snapshot.go
index 1917aa6c..e417ac0d 100644
--- a/internal/handlers/snapshot.go
+++ b/internal/handlers/snapshot.go
@@ -68,7 +68,11 @@ func (h *Handlers) HandleSnapshot(w http.ResponseWriter, r *http.Request) {
h.recordEngine(r, "lite")
result, err := h.Router.Lite().Snapshot(r.Context(), tabID, filter)
if err != nil {
- httpx.Error(w, 500, fmt.Errorf("lite snapshot: %w", err))
+ if engine.IsIDPIBlocked(err) {
+ httpx.Error(w, http.StatusForbidden, err)
+ } else {
+ httpx.Error(w, 500, fmt.Errorf("lite snapshot: %w", err))
+ }
return
}
// Convert to bridge.A11yNode for API compatibility.
@@ -77,7 +81,7 @@ func (h *Handlers) HandleSnapshot(w http.ResponseWriter, r *http.Request) {
flat[i] = bridge.A11yNode{Ref: n.Ref, Role: n.Role, Name: n.Name, Depth: n.Depth, Value: n.Value}
}
w.Header().Set("X-Engine", "lite")
- httpx.JSON(w, 200, map[string]any{"nodes": flat})
+ httpx.JSON(w, 200, map[string]any{"engine": "lite", "nodes": flat})
return
}
diff --git a/internal/handlers/text.go b/internal/handlers/text.go
index 4de2c9a7..b17337df 100644
--- a/internal/handlers/text.go
+++ b/internal/handlers/text.go
@@ -24,7 +24,11 @@ func (h *Handlers) HandleText(w http.ResponseWriter, r *http.Request) {
h.recordEngine(r, "lite")
result, err := h.Router.Lite().Text(r.Context(), tabID)
if err != nil {
- httpx.Error(w, 500, fmt.Errorf("lite text: %w", err))
+ if engine.IsIDPIBlocked(err) {
+ httpx.Error(w, http.StatusForbidden, err)
+ } else {
+ httpx.Error(w, 500, fmt.Errorf("lite text: %w", err))
+ }
return
}
w.Header().Set("X-Engine", "lite")