Skip to content

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

fix: sanitize page-controlled strings in error details, not just the message

Luigi Agosti4d3550638e9d
diff --git a/internal/httpx/details_sanitize_test.go b/internal/httpx/details_sanitize_test.go
new file mode 100644
index 00000000..a5e37f01
--- /dev/null
+++ b/internal/httpx/details_sanitize_test.go
@@ -0,0 +1,70 @@
+package httpx
+
+import (
+ "encoding/json"
+ "net/http/httptest"
+ "strings"
+ "testing"
+)
+
+// Error details carry page-controlled data — dialog text, document titles,
+// URLs — and the CLI prints some of them straight to a terminal. They need the
+// same cleaning the top-level message already gets.
+func TestErrorCodeSanitizesDetailStrings(t *testing.T) {
+ rec := httptest.NewRecorder()
+ details := map[string]any{
+ "title": "Login\x1b[31m failed\x00",
+ "count": 3,
+ "retryable": true,
+ }
+ ErrorCode(rec, 400, "bad_input", "boom", false, details)
+
+ body := rec.Body.String()
+ if strings.Contains(body, "\\u001b") || strings.Contains(body, "\x1b") {
+ t.Errorf("ANSI escape survived into details: %s", body)
+ }
+ if strings.Contains(body, "\\u0000") {
+ t.Errorf("null byte survived into details: %s", body)
+ }
+
+ var payload map[string]any
+ if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
+ t.Fatalf("response is not valid JSON: %v", err)
+ }
+ got, _ := payload["details"].(map[string]any)
+ if got["count"] != float64(3) || got["retryable"] != true {
+ t.Errorf("non-string detail values must pass through unchanged, got %+v", got)
+ }
+
+ // The caller's map must not be rewritten underneath it.
+ if details["title"] != "Login\x1b[31m failed\x00" {
+ t.Errorf("caller's details map was mutated: %q", details["title"])
+ }
+}
+
+// An intentionally empty detail must stay empty rather than becoming the
+// message-level "error" placeholder.
+func TestErrorCodeKeepsEmptyDetailStringsEmpty(t *testing.T) {
+ rec := httptest.NewRecorder()
+ ErrorCode(rec, 400, "bad_input", "boom", false, map[string]any{"hint": ""})
+
+ var payload map[string]any
+ if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
+ t.Fatalf("response is not valid JSON: %v", err)
+ }
+ got, _ := payload["details"].(map[string]any)
+ if hint, ok := got["hint"].(string); !ok || hint != "" {
+ t.Errorf("empty hint = %q, want empty string", hint)
+ }
+}
+
+func TestProblemSanitizesDetailStrings(t *testing.T) {
+ rec := httptest.NewRecorder()
+ Problem(rec, 502, "backend_unavailable", "boom", true, map[string]any{
+ "target": "http://host\x1b[31m/path",
+ })
+
+ if body := rec.Body.String(); strings.Contains(body, "\\u001b") || strings.Contains(body, "\x1b") {
+ t.Errorf("ANSI escape survived into problem details: %s", body)
+ }
+}
diff --git a/internal/httpx/httpx.go b/internal/httpx/httpx.go
index 11d27547..13c18827 100644
--- a/internal/httpx/httpx.go
+++ b/internal/httpx/httpx.go
@@ -58,11 +58,62 @@ func ErrorCode(w http.ResponseWriter, status int, code, message string, retryabl
payload["retryable"] = true
}
if len(details) > 0 {
- payload["details"] = details
+ payload["details"] = sanitizeDetails(details)
}
JSON(w, status, payload)
}
+// sanitizeDetails returns a copy of details with every string cleaned the same
+// way as the message beside it. Details carry page-controlled data — dialog
+// text, document titles, navigated URLs — and the CLI prints some of them
+// straight to a terminal, so leaving them raw would let a visited page smuggle
+// ANSI escapes past the sanitizing the message already gets. The input is
+// copied rather than rewritten so callers may reuse their map.
+func sanitizeDetails(details map[string]any) map[string]any {
+ if len(details) == 0 {
+ return nil
+ }
+ out := make(map[string]any, len(details))
+ for key, value := range details {
+ out[key] = sanitizeDetailValue(value, 0)
+ }
+ return out
+}
+
+const maxDetailDepth = 4
+
+func sanitizeDetailValue(value any, depth int) any {
+ if depth > maxDetailDepth {
+ return nil
+ }
+ switch v := value.(type) {
+ case string:
+ // Not SanitizeErrorMessage: an intentionally empty detail must stay
+ // empty rather than becoming the message-level "error" placeholder.
+ return sanitize.CleanError(v, maxErrorMessageBytes)
+ case []string:
+ out := make([]string, len(v))
+ for i, s := range v {
+ out[i] = sanitize.CleanError(s, maxErrorMessageBytes)
+ }
+ return out
+ case []any:
+ out := make([]any, len(v))
+ for i, e := range v {
+ out[i] = sanitizeDetailValue(e, depth+1)
+ }
+ return out
+ case map[string]any:
+ out := make(map[string]any, len(v))
+ for k, e := range v {
+ out[k] = sanitizeDetailValue(e, depth+1)
+ }
+ return out
+ default:
+ return value
+ }
+}
+
func Problem(w http.ResponseWriter, status int, code, detail string, retryable bool, details map[string]any) {
title := http.StatusText(status)
if title == "" {
@@ -75,7 +126,7 @@ func Problem(w http.ResponseWriter, status int, code, detail string, retryable b
Status: status,
Detail: SanitizeErrorMessage(detail),
Code: code,
- Details: details,
+ Details: sanitizeDetails(details),
}
if retryable {
payload.Retryable = true