Securing Local Reverse Proxies for AI Agent Sidecars and MCP Tools
With the rapid adoption of Model Context Protocol (MCP) servers, local vector databases, and autonomous AI coding agents, developers are running dozens of backend services on localhost.
Whether it's an Ollama instance on port 11434, a PostgreSQL container on 5432, or a custom MCP server on 8080, developers routinely bind reverse proxies (Nginx, Caddy, or Traefik) to expose these tools to agents or internal web dashboards.
However, default reverse proxy configurations frequently expose developers to critical local network and loopback attacks. Here is an architectural blueprint for hardening your local agent proxy layer.
1. The Localhost Binding Trap
A common mistake in docker-compose.yml or container manifests:
# VULNERABLE: Binds to 0.0.0.0 on the host
ports:
- "8080:8080"
When you specify "8080:8080" without an explicit host IP, Docker maps the port to 0.0.0.0. If your laptop or workstation connects to public Wi-Fi, any device on the same local network subnet can send requests directly to your internal agent tooling.
Hardened Rule: Always explicitly bind to the loopback interface (127.0.0.1):
ports:
- "127.0.0.1:8080:8080"
2. DNS Rebinding Protection
If your local proxy serves a web UI or an HTTP MCP endpoint, it is vulnerable to DNS Rebinding Attacks.
An attacker can trick your browser into visiting an adversarial website (evil-domain.com). The attacker's DNS server initially returns a public IP with a short TTL (1 second), then immediately returns 127.0.0.1.
The victim's browser can now execute JavaScript making arbitrary HTTP calls to http://evil-domain.com:8080/, bypassing the Same-Origin Policy and interacting directly with your local LLM or MCP agent endpoints.
Nginx Defense: Enforce strict Host header whitelisting:
server {
listen 127.0.0.1:8080;
server_name localhost 127.0.0.1;
# Reject unexpected host headers immediately
if ($http_host !~* ^(localhost|127\.0\.0\.1)(:[0-9]+)?$) {
return 403 "Forbidden: Invalid Host Header";
}
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}
3. Loopback Path Normalization
Reverse proxies sitting in front of agent tool runners must prevent path traversal before requests reach internal subprocesses:
# Block dangerous dotfile or traversal patterns
location ~* /\.(git|env|aws|ssh|config) {
deny all;
return 404;
}
By enforcing strict host header validation, binding strictly to 127.0.0.1, and blocking traversal at the proxy boundary, you insulate your local AI workstations from external compromise.
Audit your MCP server tools for prompt injection & secret leaks free: https://mcpaudit.pages.dev/?src=devto
Comments
No comments yet. Start the discussion.