SYSTEM ARCHITECTURE & SECURITY GUARDRAILS • NODE.JS & MCP

Designing Sandboxed Model Context Protocol (MCP) Bridges for Android ADB & Headless Browser E2E Verification

Author: Fata Fatikhul Karim • Repository: android-browser-mcp • Verification: Automated Guardrail Unit Tests (test/guardrails.test.js)

1. Context & Threat Model

Connecting an LLM agent to physical Android hardware (via ADB) and headless Chromium (via Chrome DevTools Protocol) enables automated End-to-End (E2E) UI verification. However, bridging an LLM to OS-level tools introduces a critical threat vector: Indirect Prompt Injection leading to Command Injection when parsing untrusted DOM or UI accessibility text.

[LLM Client] ⇄ (JSON-RPC 2.0 stdio) ⇄ [Node.js MCP Server (android/guardrails.js)]
                                             ├──► Android ADB Module (21 Tools • XML UI Tree Parser)
                                             └──► Chromium CDP Module (23 Tools • Isolated Context)

2. Root Cause of Naive Bridge Vulnerabilities

Naive ADB wrappers concatenate LLM-supplied strings directly into shell commands (child_process.exec or unvalidated adb shell strings), allowing metacharacters (;, $(), backticks) in UI labels, package names, or form inputs to execute arbitrary shell commands:

// ❌ VULNERABLE: Unvalidated string interpolation in shell execution
function unsafeAdbInput(userText) {
  // If userText contains `"; rm -rf /sdcard/* #`, the shell executes it!
  exec(`adb shell input text "${userText}"`);
}

3. Architectural Trade-Offs & Security Guardrails

4. Production Guardrail Implementation (android/guardrails.js)

// ✅ SAFE: Parameterized execFile + strict package, URL & input sanitization (android/guardrails.js)
export const PACKAGE_REGEX = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)+$/;

export function assertValidPackage(pkg) {
  const clean = String(pkg || "").trim();
  if (!PACKAGE_REGEX.test(clean)) {
    throw new Error(`Invalid Android package identifier rejected by guardrail: "${clean}"`);
  }
  return clean;
}

export function sanitizeAdbInputText(rawText) {
  const singleLine = String(rawText ?? "").replace(/[\r\n\0]/g, " ");
  const escapedSpaces = singleLine.replace(/ /g, "%s");
  return `'${escapedSpaces.replace(/'/g, "'\\''")}'`;
}