Implementation:0xSteph Pentest ai MCP Auth Local
| Knowledge Sources | |
|---|---|
| Domains | MCP, Security, DNS_Rebinding |
| Last Updated | 2026-10-23 00:00 GMT |
Overview
Concrete tool for local-only auth of ptai's MCP server over SSE/HTTP with per-install token and Host header allowlist, provided by the pentest-ai MCP server.
Description
The mcp_server.auth_local module provides local-only auth for ptai's MCP server over SSE/HTTP transports. Stdio transport stays auth-less by design. SSE/HTTP transports get a per-install token file (0600) plus a Host: header allowlist to defend against CVE-2025-49596-style DNS rebinding. The LocalAuthMiddleware is an ASGI middleware that enforces Bearer-token + Host-header allowlist on every incoming request.
Usage
Import this module when you need to mount auth middleware on the MCP server's SSE/HTTP transport. Used by the MCP server startup.
Code Reference
Source Location
- Repository: 0xSteph_Pentest_ai
- File: mcp_server/auth_local.py
- Lines: 1-86
Signature
TOKEN_PATH = Path.home() / ".pentest-ai" / "mcp-token"
def get_or_create_token(path: Path = TOKEN_PATH) -> str: ...
class LocalAuthMiddleware(BaseHTTPMiddleware):
"""ASGI middleware that enforces Bearer-token + Host-header allowlist."""
...
Import
from mcp_server.auth_local import get_or_create_token, LocalAuthMiddleware
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| Authorization | str (header) | Yes | Bearer token matching the per-install token file |
| Host | str (header) | Yes | Must be in allowlist (127.0.0.1, localhost, ::1) |
Outputs
| Name | Type | Description |
|---|---|---|
| token | str | Per-install token (0600 file) |
Usage Examples
Mount Auth Middleware
from mcp_server.auth_local import get_or_create_token, LocalAuthMiddleware
# Generate or read the per-install token
token = get_or_create_token()
print(f"MCP token: {token}")
# Mount on the Starlette/FastMCP app
app.add_middleware(LocalAuthMiddleware)