High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
refactor(e2e): simplify 37-idpi.sh with extracted helpers (-83 lines)
Boschd1df78841faf
diff --git a/tests/e2e/scenarios/37-idpi.sh b/tests/e2e/scenarios/37-idpi.shindex a0ca5a21..ee36ae1f 100755--- a/tests/e2e/scenarios/37-idpi.sh+++ b/tests/e2e/scenarios/37-idpi.sh@@ -8,233 +8,163 @@source "$(dirname "$0")/common.sh"# ─────────────────────────────────────────────────────────────────-# Helper: curl with headers capture+# IDPI test helpers# ─────────────────────────────────────────────────────────────────-# POST and capture response body, HTTP status, and specific header-# Usage: pt_post_hdr <base_url> <path> <body> <header_name>-# Sets: RESULT, HTTP_STATUS, HDR_VALUE-pt_post_hdr() {- local base_url="$1"- local path="$2"- local body="$3"- local header_name="$4"+# Navigate to a page on a specific server, return tab ID+# Usage: TAB_ID=$(idpi_setup <base_url> <page_url>)+idpi_setup() {+ local base_url="$1" page_url="$2"+ local old_url="$PINCHTAB_URL"+ PINCHTAB_URL="$base_url"+ pt_post /navigate "{\"url\":\"$page_url\"}" >/dev/null+ PINCHTAB_URL="$old_url"+ sleep 1+ echo "$RESULT" | jq -r '.tabId'+}- echo -e "${BLUE}→ curl -X POST ${base_url}${path}${NC}" >&2- local tmpheaders=$(mktemp)- local response- response=$(curl -s -w "\n%{http_code}" \- -X POST \- "${base_url}${path}" \+# Close a tab on a specific server+idpi_cleanup() {+ local base_url="$1" tab_id="$2"+ curl -sf -X POST "${base_url}/tab" \-H "Content-Type: application/json" \- -D "$tmpheaders" \- -d "$body")- RESULT=$(echo "$response" | head -n -1)- HTTP_STATUS=$(echo "$response" | tail -n 1)- HDR_VALUE=$(grep -i "^${header_name}:" "$tmpheaders" | sed 's/^[^:]*: *//' | tr -d '\r' | head -1)- rm -f "$tmpheaders"+ -d "{\"tabId\":\"$tab_id\",\"action\":\"close\"}" >/dev/null 2>&1 || true}-# GET with headers capture-pt_get_hdr() {- local base_url="$1"- local path="$2"- local header_name="$3"-- echo -e "${BLUE}→ curl -X GET ${base_url}${path}${NC}" >&2+# Make a request and capture a response header+# Usage: idpi_request POST <base_url> <path> <body> <header_name>+# idpi_request GET <base_url> <path> "" <header_name>+# Sets: RESULT, HTTP_STATUS, HDR_VALUE+idpi_request() {+ local method="$1" base_url="$2" path="$3" body="$4" header_name="$5"+ echo -e "${BLUE}→ curl -X $method ${base_url}${path}${NC}" >&2local tmpheaders=$(mktemp)+ local curl_args=(-s -w "\n%{http_code}" -X "$method" "${base_url}${path}" -H "Content-Type: application/json" -D "$tmpheaders")+ [ -n "$body" ] && curl_args+=(-d "$body")local response- response=$(curl -s -w "\n%{http_code}" \- -X GET \- "${base_url}${path}" \- -H "Content-Type: application/json" \- -D "$tmpheaders")+ response=$(curl "${curl_args[@]}")RESULT=$(echo "$response" | head -n -1)HTTP_STATUS=$(echo "$response" | tail -n 1)HDR_VALUE=$(grep -i "^${header_name}:" "$tmpheaders" | sed 's/^[^:]*: *//' | tr -d '\r' | head -1)rm -f "$tmpheaders"}-# Helper: navigate and get tab ID-idpi_nav() {- local base_url="$1"- local page_url="$2"- local old_url="$PINCHTAB_URL"- PINCHTAB_URL="$base_url"- pt_post /navigate "{\"url\":\"$page_url\"}" >/dev/null- PINCHTAB_URL="$old_url"- echo "$RESULT" | jq -r '.tabId'+# Assert header is present or absent+assert_header_present() {+ local desc="$1"+ if [ -n "$HDR_VALUE" ]; then+ echo -e " ${GREEN}✓${NC} $desc: $HDR_VALUE"+ ((ASSERTIONS_PASSED++)) || true+ else+ echo -e " ${RED}✗${NC} $desc (header missing)"+ ((ASSERTIONS_FAILED++)) || true+ fi}-# Helper: close tab-idpi_close() {- local base_url="$1"- local tab_id="$2"- curl -sf -X POST "${base_url}/tab" \- -H "Content-Type: application/json" \- -d "{\"tabId\":\"$tab_id\",\"action\":\"close\"}" >/dev/null 2>&1 || true+assert_header_absent() {+ local desc="$1"+ if [ -z "$HDR_VALUE" ]; then+ echo -e " ${GREEN}✓${NC} $desc"+ ((ASSERTIONS_PASSED++)) || true+ else+ echo -e " ${RED}✗${NC} $desc (unexpected: $HDR_VALUE)"+ ((ASSERTIONS_FAILED++)) || true+ fi}+FIND_BODY='{"query":"continue button","threshold":0.1,"topK":5}'+FIND_CLEAN='{"query":"safe action button","threshold":0.1,"topK":5}'+# ═══════════════════════════════════════════════════════════════════-# /find WARN MODE (main instance)+# /find — WARN MODE (main instance)# ═══════════════════════════════════════════════════════════════════start_test "idpi: /find clean page — no warning (warn mode)"--TAB_ID=$(idpi_nav "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-clean.html")-sleep 1-pt_post_hdr "$PINCHTAB_URL" "/tabs/${TAB_ID}/find" '{"query":"safe action button","threshold":0.1,"topK":5}' "X-IDPI-Warning"+TAB_ID=$(idpi_setup "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-clean.html")+idpi_request POST "$PINCHTAB_URL" "/tabs/${TAB_ID}/find" "$FIND_CLEAN" "X-IDPI-Warning"assert_ok "/find clean page"-if [ -z "$HDR_VALUE" ]; then- echo -e " ${GREEN}✓${NC} no X-IDPI-Warning header (expected)"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} unexpected X-IDPI-Warning: $HDR_VALUE"- ((ASSERTIONS_FAILED++)) || true-fi-idpi_close "$PINCHTAB_URL" "$TAB_ID"-+assert_header_absent "no X-IDPI-Warning on clean page"+idpi_cleanup "$PINCHTAB_URL" "$TAB_ID"end_test# ─────────────────────────────────────────────────────────────────-start_test "idpi: /find injection page — warns (warn mode)"--TAB_ID=$(idpi_nav "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-inject.html")-sleep 1-pt_post_hdr "$PINCHTAB_URL" "/tabs/${TAB_ID}/find" '{"query":"continue button","threshold":0.1,"topK":5}' "X-IDPI-Warning"-assert_ok "/find injection page (warn mode returns 200)"-if [ -n "$HDR_VALUE" ]; then- echo -e " ${GREEN}✓${NC} X-IDPI-Warning header present: $HDR_VALUE"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} missing X-IDPI-Warning header"- ((ASSERTIONS_FAILED++)) || true-fi-# Check idpiWarning in JSON body-IDPI_BODY=$(echo "$RESULT" | jq -r '.idpiWarning // empty')-if [ -n "$IDPI_BODY" ]; then- echo -e " ${GREEN}✓${NC} idpiWarning field in response body"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} missing idpiWarning in response body"- ((ASSERTIONS_FAILED++)) || true-fi-idpi_close "$PINCHTAB_URL" "$TAB_ID"-+TAB_ID=$(idpi_setup "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-inject.html")+idpi_request POST "$PINCHTAB_URL" "/tabs/${TAB_ID}/find" "$FIND_BODY" "X-IDPI-Warning"+assert_ok "/find injection (warn mode returns 200)"+assert_header_present "X-IDPI-Warning header present"+assert_json_exists "$RESULT" ".idpiWarning" "idpiWarning field in body"+idpi_cleanup "$PINCHTAB_URL" "$TAB_ID"end_test# ─────────────────────────────────────────────────────────────────--start_test "idpi: POST /find injection page — warns (warn mode)"--TAB_ID=$(idpi_nav "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-inject.html")-sleep 1-pt_post_hdr "$PINCHTAB_URL" "/find" "{\"query\":\"malicious paragraph\",\"tabId\":\"$TAB_ID\",\"threshold\":0.1}" "X-IDPI-Warning"+start_test "idpi: POST /find injection — warns (warn mode)"+TAB_ID=$(idpi_setup "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-inject.html")+idpi_request POST "$PINCHTAB_URL" "/find" "{\"query\":\"malicious paragraph\",\"tabId\":\"$TAB_ID\",\"threshold\":0.1}" "X-IDPI-Warning"assert_ok "POST /find (warn mode)"-if [ -n "$HDR_VALUE" ]; then- echo -e " ${GREEN}✓${NC} X-IDPI-Warning on POST /find"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} missing X-IDPI-Warning on POST /find"- ((ASSERTIONS_FAILED++)) || true-fi-idpi_close "$PINCHTAB_URL" "$TAB_ID"-+assert_header_present "X-IDPI-Warning on POST /find"+idpi_cleanup "$PINCHTAB_URL" "$TAB_ID"end_test# ═══════════════════════════════════════════════════════════════════-# /find STRICT MODE (secure instance)+# /find — STRICT MODE (secure instance)# ═══════════════════════════════════════════════════════════════════start_test "idpi: /find clean page — allowed (strict mode)"--TAB_ID=$(idpi_nav "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-clean.html")-sleep 1-pt_post_hdr "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/find" '{"query":"safe action button","threshold":0.1,"topK":5}' "X-IDPI-Warning"+TAB_ID=$(idpi_setup "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-clean.html")+idpi_request POST "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/find" "$FIND_CLEAN" "X-IDPI-Warning"assert_ok "/find clean page (strict mode)"-idpi_close "$PINCHTAB_SECURE_URL" "$TAB_ID"-+idpi_cleanup "$PINCHTAB_SECURE_URL" "$TAB_ID"end_test# ─────────────────────────────────────────────────────────────────-start_test "idpi: /find injection page — blocked (strict mode)"--TAB_ID=$(idpi_nav "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-inject.html")-sleep 1-pt_post_hdr "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/find" '{"query":"continue button","threshold":0.1,"topK":5}' "X-IDPI-Warning"+TAB_ID=$(idpi_setup "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-inject.html")+idpi_request POST "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/find" "$FIND_BODY" "X-IDPI-Warning"assert_http_status 403 "/find blocked in strict mode"assert_contains "$RESULT" "idpi" "403 body mentions IDPI"-idpi_close "$PINCHTAB_SECURE_URL" "$TAB_ID"-+idpi_cleanup "$PINCHTAB_SECURE_URL" "$TAB_ID"end_test# ═══════════════════════════════════════════════════════════════════-# /pdf WARN MODE (main instance)+# /pdf — WARN MODE (main instance)# ═══════════════════════════════════════════════════════════════════start_test "idpi: /pdf clean page — no warning (warn mode)"--TAB_ID=$(idpi_nav "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-clean.html")-sleep 1-pt_get_hdr "$PINCHTAB_URL" "/tabs/${TAB_ID}/pdf" "X-IDPI-Warning"+TAB_ID=$(idpi_setup "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-clean.html")+idpi_request GET "$PINCHTAB_URL" "/tabs/${TAB_ID}/pdf" "" "X-IDPI-Warning"assert_ok "/pdf clean page"-if [ -z "$HDR_VALUE" ]; then- echo -e " ${GREEN}✓${NC} no X-IDPI-Warning on clean PDF"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} unexpected X-IDPI-Warning: $HDR_VALUE"- ((ASSERTIONS_FAILED++)) || true-fi-idpi_close "$PINCHTAB_URL" "$TAB_ID"-+assert_header_absent "no X-IDPI-Warning on clean PDF"+idpi_cleanup "$PINCHTAB_URL" "$TAB_ID"end_test# ─────────────────────────────────────────────────────────────────-start_test "idpi: /pdf injection page — warns (warn mode)"--TAB_ID=$(idpi_nav "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-inject.html")-sleep 1-pt_get_hdr "$PINCHTAB_URL" "/tabs/${TAB_ID}/pdf" "X-IDPI-Warning"-assert_ok "/pdf injection page (warn mode returns 200)"-if [ -n "$HDR_VALUE" ]; then- echo -e " ${GREEN}✓${NC} X-IDPI-Warning header on injection PDF"- ((ASSERTIONS_PASSED++)) || true-else- echo -e " ${RED}✗${NC} missing X-IDPI-Warning on injection PDF"- ((ASSERTIONS_FAILED++)) || true-fi-idpi_close "$PINCHTAB_URL" "$TAB_ID"-+TAB_ID=$(idpi_setup "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-inject.html")+idpi_request GET "$PINCHTAB_URL" "/tabs/${TAB_ID}/pdf" "" "X-IDPI-Warning"+assert_ok "/pdf injection (warn mode returns 200)"+assert_header_present "X-IDPI-Warning on injection PDF"+idpi_cleanup "$PINCHTAB_URL" "$TAB_ID"end_test# ═══════════════════════════════════════════════════════════════════-# /pdf STRICT MODE (secure instance)+# /pdf — STRICT MODE (secure instance)# ═══════════════════════════════════════════════════════════════════start_test "idpi: /pdf clean page — allowed (strict mode)"--TAB_ID=$(idpi_nav "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-clean.html")-sleep 1-pt_get_hdr "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/pdf" "X-IDPI-Warning"+TAB_ID=$(idpi_setup "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-clean.html")+idpi_request GET "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/pdf" "" "X-IDPI-Warning"assert_ok "/pdf clean page (strict mode)"-idpi_close "$PINCHTAB_SECURE_URL" "$TAB_ID"-+idpi_cleanup "$PINCHTAB_SECURE_URL" "$TAB_ID"end_test# ─────────────────────────────────────────────────────────────────-start_test "idpi: /pdf injection page — blocked (strict mode)"--TAB_ID=$(idpi_nav "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-inject.html")-sleep 1-pt_get_hdr "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/pdf" "X-IDPI-Warning"+TAB_ID=$(idpi_setup "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-inject.html")+idpi_request GET "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/pdf" "" "X-IDPI-Warning"assert_http_status 403 "/pdf blocked in strict mode"assert_contains "$RESULT" "idpi" "403 body mentions IDPI"-idpi_close "$PINCHTAB_SECURE_URL" "$TAB_ID"-+idpi_cleanup "$PINCHTAB_SECURE_URL" "$TAB_ID"end_test# ═══════════════════════════════════════════════════════════════════@@ -242,48 +172,28 @@ end_test# ═══════════════════════════════════════════════════════════════════start_test "idpi: multiple injection phrases — single warning header"--TAB_ID=$(idpi_nav "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-inject.html")-sleep 1-# Capture all X-IDPI-Warning headers+TAB_ID=$(idpi_setup "$PINCHTAB_URL" "${FIXTURES_URL}/idpi-inject.html")tmpheaders=$(mktemp)curl -s -X POST "${PINCHTAB_URL}/tabs/${TAB_ID}/find" \-H "Content-Type: application/json" \-D "$tmpheaders" \- -d '{"query":"continue button","threshold":0.1,"topK":5}' >/dev/null--HDR_COUNT=$(grep -ci "^X-IDPI-Warning:" "$tmpheaders" 2>/dev/null || true)-HDR_COUNT=${HDR_COUNT:-0}+ -d "$FIND_BODY" >/dev/null+HDR_COUNT=$(grep -ci "^X-IDPI-Warning:" "$tmpheaders" 2>/dev/null || echo "0")rm -f "$tmpheaders"-if [ "$HDR_COUNT" -eq 1 ]; thenecho -e " ${GREEN}✓${NC} exactly one X-IDPI-Warning header"((ASSERTIONS_PASSED++)) || true-elif [ "$HDR_COUNT" -eq 0 ]; then- echo -e " ${RED}✗${NC} no X-IDPI-Warning header found"- ((ASSERTIONS_FAILED++)) || trueelseecho -e " ${RED}✗${NC} expected 1 X-IDPI-Warning, got $HDR_COUNT"((ASSERTIONS_FAILED++)) || truefi-idpi_close "$PINCHTAB_URL" "$TAB_ID"-+idpi_cleanup "$PINCHTAB_URL" "$TAB_ID"end_test# ─────────────────────────────────────────────────────────────────-start_test "idpi: /pdf?raw=true blocked in strict mode"--TAB_ID=$(idpi_nav "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-inject.html")-sleep 1-pt_get_hdr "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/pdf?raw=true" "X-IDPI-Warning"+TAB_ID=$(idpi_setup "$PINCHTAB_SECURE_URL" "${FIXTURES_URL}/idpi-inject.html")+idpi_request GET "$PINCHTAB_SECURE_URL" "/tabs/${TAB_ID}/pdf?raw=true" "" "X-IDPI-Warning"assert_http_status 403 "raw PDF blocked in strict mode"-idpi_close "$PINCHTAB_SECURE_URL" "$TAB_ID"-+idpi_cleanup "$PINCHTAB_SECURE_URL" "$TAB_ID"end_test--# ─────────────────────────────────────────────────────────────────-# NOTE: wrapContent test skipped-# Feature check: WrapContent is only applied in /text handler, not in /find results.-# The /find handler does not wrap content, so this test would be redundant.-# Verify with: grep -rn "wrapContent" internal/handlers/find.go