Skip to content

Personal AI, On Personal Devices

Python

security: harden network-exposed surface (#509)

rposulld7053c35d56e
diff --git a/configs/openjarvis/config.toml b/configs/openjarvis/config.toml
index 19091c53..943d31c4 100644
--- a/configs/openjarvis/config.toml
+++ b/configs/openjarvis/config.toml
@@ -106,6 +106,11 @@ enabled = true # Record traces for analysis
db_path = "~/.openjarvis/traces.db"
[server]
-host = "0.0.0.0"
+# Bind to loopback by default so the API is not exposed to the local network.
+# To serve other devices on your LAN, set host = "0.0.0.0" AND set an API key
+# (OPENJARVIS_API_KEY / `jarvis auth generate-key`) — startup refuses a
+# non-loopback bind without a key. The "server" security profile also flips
+# this to 0.0.0.0 intentionally.
+host = "127.0.0.1"
port = 8000
agent = "native_openhands"
diff --git a/src/openjarvis/server/api_routes.py b/src/openjarvis/server/api_routes.py
index 0d29a005..0344e1bb 100644
--- a/src/openjarvis/server/api_routes.py
+++ b/src/openjarvis/server/api_routes.py
@@ -285,14 +285,39 @@ async def memory_config(request: Request):
async def memory_index(req: MemoryIndexRequest, request: Request):
"""Index files from a path into memory."""
try:
+ import os
from pathlib import Path
+ from openjarvis.security.file_policy import is_sensitive_file
from openjarvis.tools.storage.ingest import ingest_path
target = Path(req.path).expanduser().resolve()
if not target.exists():
raise HTTPException(status_code=404, detail=f"Path not found: {req.path}")
+ # Sandbox: when workspace roots are configured via OPENJARVIS_WORKSPACE
+ # (os.pathsep-separated), only allow indexing inside them. This endpoint
+ # must not become an arbitrary-filesystem read primitive over the API.
+ workspace = os.environ.get("OPENJARVIS_WORKSPACE", "").strip()
+ if workspace:
+ roots = [
+ Path(d).expanduser().resolve()
+ for d in workspace.split(os.pathsep)
+ if d.strip()
+ ]
+ if not any(
+ target == root or root in target.parents for root in roots
+ ):
+ raise HTTPException(
+ status_code=403,
+ detail="Path is outside the allowed workspace directories.",
+ )
+ # Never ingest sensitive files (.env, private keys, credentials, ...).
+ if target.is_file() and is_sensitive_file(target):
+ raise HTTPException(
+ status_code=403, detail="Refusing to index a sensitive file."
+ )
+
backend = _get_memory_backend(request)
if backend is None:
raise HTTPException(status_code=503, detail="Memory is not configured")
diff --git a/src/openjarvis/server/auth_middleware.py b/src/openjarvis/server/auth_middleware.py
index a9390ee5..97d635bf 100644
--- a/src/openjarvis/server/auth_middleware.py
+++ b/src/openjarvis/server/auth_middleware.py
@@ -33,7 +33,10 @@ class AuthMiddleware(BaseHTTPMiddleware):
status_code=401,
)
scheme, _, token = auth.partition(" ")
- if scheme.lower() != "bearer" or token != self._api_key:
+ # Constant-time comparison to avoid leaking the key via timing.
+ if scheme.lower() != "bearer" or not secrets.compare_digest(
+ token, self._api_key
+ ):
return JSONResponse(
{"detail": "Invalid API key"},
status_code=401,
@@ -42,8 +45,18 @@ class AuthMiddleware(BaseHTTPMiddleware):
@staticmethod
def _requires_auth(path: str) -> bool:
- """Only protect API routes, not the frontend UI or static assets."""
- return path.startswith("/v1/") or path.startswith("/api/")
+ """Protect API routes and operational metrics; leave the UI/health open.
+
+ ``/metrics`` exposes request/token counters that should not be readable
+ by unauthenticated clients, so it is gated alongside ``/v1`` and
+ ``/api``. ``/health`` stays open for liveness probes.
+ """
+ return (
+ path.startswith("/v1/")
+ or path.startswith("/api/")
+ or path == "/metrics"
+ or path.startswith("/metrics/")
+ )
diff --git a/src/openjarvis/server/webhook_routes.py b/src/openjarvis/server/webhook_routes.py
index 3ea1ed1d..f1f7e7b6 100644
--- a/src/openjarvis/server/webhook_routes.py
+++ b/src/openjarvis/server/webhook_routes.py
@@ -113,7 +113,14 @@ def create_webhook_router(
signature = request.headers.get("X-Twilio-Signature", "")
url = str(request.url)
- if twilio_auth_token and not _validate_twilio_signature(
+ # Fail closed: an unconfigured token means we cannot verify the sender,
+ # so reject rather than trust unsigned input.
+ if not twilio_auth_token:
+ logger.error(
+ "Twilio webhook rejected: TWILIO_AUTH_TOKEN not configured."
+ )
+ return Response("Webhook signature verification not configured", 403)
+ if not _validate_twilio_signature(
twilio_auth_token, url, params, signature
):
return Response("Invalid signature", status_code=403)
@@ -257,7 +264,13 @@ def create_webhook_router(
request: Request,
) -> Response:
auth = request.headers.get("Authorization", "")
- if bluebubbles_password and auth != bluebubbles_password:
+ # Fail closed when no password is configured.
+ if not bluebubbles_password:
+ logger.error(
+ "BlueBubbles webhook rejected: password not configured."
+ )
+ return Response("Webhook authentication not configured", 403)
+ if not hmac.compare_digest(auth, bluebubbles_password):
return Response("Invalid password", status_code=403)
payload = await request.json()
@@ -292,7 +305,11 @@ def create_webhook_router(
token = request.query_params.get("hub.verify_token", "")
challenge = request.query_params.get("hub.challenge", "")
- if mode == "subscribe" and token == whatsapp_verify_token:
+ # Fail closed: never echo the challenge if no verify token is set,
+ # otherwise an empty token would match an empty query value.
+ if not whatsapp_verify_token:
+ return Response("Forbidden", status_code=403)
+ if mode == "subscribe" and hmac.compare_digest(token, whatsapp_verify_token):
return PlainTextResponse(challenge)
return Response("Forbidden", status_code=403)
@@ -302,19 +319,23 @@ def create_webhook_router(
) -> Response:
body_bytes = await request.body()
- # Verify signature
- if whatsapp_app_secret:
- signature = request.headers.get("X-Hub-Signature-256", "")
- expected = (
- "sha256="
- + hmac.new(
- whatsapp_app_secret.encode(),
- body_bytes,
- hashlib.sha256,
- ).hexdigest()
+ # Fail closed: reject when no app secret is configured to verify HMAC.
+ if not whatsapp_app_secret:
+ logger.error(
+ "WhatsApp webhook rejected: app secret not configured."
)
- if not hmac.compare_digest(signature, expected):
- return Response("Invalid signature", status_code=403)
+ return Response("Webhook signature verification not configured", 403)
+ signature = request.headers.get("X-Hub-Signature-256", "")
+ expected = (
+ "sha256="
+ + hmac.new(
+ whatsapp_app_secret.encode(),
+ body_bytes,
+ hashlib.sha256,
+ ).hexdigest()
+ )
+ if not hmac.compare_digest(signature, expected):
+ return Response("Invalid signature", status_code=403)
payload = json.loads(body_bytes)
for entry in payload.get("entry", []):
@@ -349,16 +370,16 @@ def create_webhook_router(
# Get the SendBlue channel — may be passed at init or set later
sb = sendblue_channel or getattr(request.app.state, "sendblue_channel", None)
- # Verify webhook secret if configured
- if sb and sb.webhook_secret:
- header_secret = request.headers.get("x-sendblue-secret", "")
- if header_secret != sb.webhook_secret:
- return Response("Invalid secret", status_code=403)
- elif sb:
- logger.warning(
- "SendBlue webhook received without secret verification. "
- "Set webhook_secret for HMAC validation."
+ # Fail closed: require a configured channel + webhook secret to verify
+ # the sender before processing any inbound message.
+ if sb is None or not getattr(sb, "webhook_secret", ""):
+ logger.error(
+ "SendBlue webhook rejected: webhook_secret not configured."
)
+ return Response("Webhook secret not configured", status_code=403)
+ header_secret = request.headers.get("x-sendblue-secret", "")
+ if not hmac.compare_digest(header_secret, sb.webhook_secret):
+ return Response("Invalid secret", status_code=403)
# Ignore outbound status callbacks
if payload.get("is_outbound", False):
diff --git a/src/openjarvis/tools/http_request.py b/src/openjarvis/tools/http_request.py
index 596945d1..a1d4b553 100644
--- a/src/openjarvis/tools/http_request.py
+++ b/src/openjarvis/tools/http_request.py
@@ -5,6 +5,7 @@ from __future__ import annotations
import logging
import os
import time
+import urllib.parse
from typing import Any
import httpx
@@ -21,6 +22,13 @@ _MAX_RESPONSE_BYTES = 1_048_576
_ALLOWED_METHODS = frozenset({"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"})
+# Cap redirect chains so a malicious server cannot loop us indefinitely.
+_MAX_REDIRECTS = 5
+
+
+class _SSRFRedirectError(Exception):
+ """Raised when a redirect target fails the SSRF check."""
+
@ToolRegistry.register("http_request")
class HttpRequestTool(BaseTool):
@@ -136,13 +144,11 @@ class HttpRequestTool(BaseTool):
try:
t0 = time.time()
- response = httpx.request(
- method,
- url,
- headers=headers,
- content=body,
- timeout=float(timeout),
- follow_redirects=True,
+ # Follow redirects manually so each hop is re-checked for SSRF — an
+ # allowed public URL must not be able to 30x-redirect us to an
+ # internal/metadata address.
+ response = self._request_following_redirects(
+ method, url, headers=headers, content=body, timeout=float(timeout)
)
elapsed_ms = (time.time() - t0) * 1000
@@ -178,6 +184,12 @@ class HttpRequestTool(BaseTool):
content=f"Request timed out after {timeout}s: {exc}",
success=False,
)
+ except _SSRFRedirectError as exc:
+ return ToolResult(
+ tool_name="http_request",
+ content=f"SSRF protection blocked redirect: {exc}",
+ success=False,
+ )
except httpx.RequestError as exc:
return ToolResult(
tool_name="http_request",
@@ -191,5 +203,52 @@ class HttpRequestTool(BaseTool):
success=False,
)
+ @staticmethod
+ def _request_following_redirects(
+ method: str,
+ url: str,
+ *,
+ headers: dict,
+ content: Any,
+ timeout: float,
+ ) -> httpx.Response:
+ """Issue the request, re-checking SSRF on every redirect hop.
+
+ httpx's built-in ``follow_redirects`` would chase a 30x ``Location``
+ without re-validating it, letting a public URL bounce us to an internal
+ host. We follow manually and run :func:`check_ssrf` on each target.
+ """
+ current_url = url
+ current_method = method
+ body = content
+ # Use module-level ``httpx.request`` (not a private Client) so the SSRF
+ # re-check seam stays patchable by callers' tests, with redirects
+ # disabled so we control every hop ourselves.
+ for _ in range(_MAX_REDIRECTS + 1):
+ response = httpx.request(
+ current_method,
+ current_url,
+ headers=headers,
+ content=body,
+ timeout=timeout,
+ follow_redirects=False,
+ )
+ if response.status_code not in (301, 302, 303, 307, 308):
+ return response
+ location = response.headers.get("location", "")
+ if not location:
+ return response
+ # Resolve relative redirects against the URL we just fetched.
+ current_url = urllib.parse.urljoin(str(response.url), location)
+ ssrf_error = check_ssrf(current_url)
+ if ssrf_error:
+ raise _SSRFRedirectError(ssrf_error)
+ # Per RFC 7231, 301/302/303 turn the method into GET and drop
+ # the body (except for HEAD).
+ if response.status_code in (301, 302, 303) and current_method != "HEAD":
+ current_method = "GET"
+ body = None
+ raise _SSRFRedirectError(f"Exceeded maximum of {_MAX_REDIRECTS} redirects.")
+
__all__ = ["HttpRequestTool"]
diff --git a/tests/server/test_auth_middleware.py b/tests/server/test_auth_middleware.py
index c6b4cb00..9c9bab53 100644
--- a/tests/server/test_auth_middleware.py
+++ b/tests/server/test_auth_middleware.py
@@ -28,6 +28,10 @@ def _make_app(api_key: str) -> FastAPI:
async def twilio_webhook():
return {"status": "received"}
+ @app.get("/metrics")
+ async def metrics():
+ return {"requests": 0}
+
return app
@@ -65,7 +69,18 @@ class TestAuthMiddleware:
resp = client.post("/webhooks/twilio")
assert resp.status_code == 200
+ def test_metrics_requires_auth(self, client):
+ resp = client.get("/metrics")
+ assert resp.status_code == 401
+
+ def test_metrics_accepts_valid_key(self, client):
+ resp = client.get(
+ "/metrics", headers={"Authorization": "Bearer oj_sk_test123"}
+ )
+ assert resp.status_code == 200
+
def test_no_key_configured_allows_all(self):
client = TestClient(_make_app(""))
resp = client.get("/v1/models")
assert resp.status_code == 200
+ assert client.get("/metrics").status_code == 200
diff --git a/tests/server/test_sendblue_webhook.py b/tests/server/test_sendblue_webhook.py
index ca979a15..54273481 100644
--- a/tests/server/test_sendblue_webhook.py
+++ b/tests/server/test_sendblue_webhook.py
@@ -41,6 +41,9 @@ def sendblue_channel():
api_key_id="test_key",
api_secret_key="test_secret",
from_number="+15551234567",
+ # Webhooks now fail closed without a secret, so configure one and have
+ # the test client send the matching header by default.
+ webhook_secret="testsecret",
)
ch.connect()
return ch
@@ -61,7 +64,9 @@ def webhook_app(mock_bridge, sendblue_channel):
@pytest.fixture
def client(webhook_app):
- return TestClient(webhook_app)
+ # Send the webhook secret by default so message-handling tests reach the
+ # bridge; fail-closed behavior is covered separately below.
+ return TestClient(webhook_app, headers={"x-sendblue-secret": "testsecret"})
# ---------------------------------------------------------------------------
@@ -169,7 +174,7 @@ class TestSendBlueWebhook:
app = FastAPI()
router = create_webhook_router(bridge=None, sendblue_channel=sendblue_channel)
app.include_router(router)
- c = TestClient(app)
+ c = TestClient(app, headers={"x-sendblue-secret": "testsecret"})
resp = c.post(
"/webhooks/sendblue",
@@ -181,6 +186,27 @@ class TestSendBlueWebhook:
)
assert resp.status_code == 200
+ def test_no_secret_configured_is_rejected(self, mock_bridge):
+ """Fail closed: a channel without a webhook_secret rejects all posts."""
+ from openjarvis.channels.sendblue import SendBlueChannel
+ from openjarvis.server.webhook_routes import create_webhook_router
+
+ ch = SendBlueChannel(
+ api_key_id="k", api_secret_key="s", from_number="+1555"
+ )
+ ch.connect()
+ app = FastAPI()
+ router = create_webhook_router(bridge=mock_bridge, sendblue_channel=ch)
+ app.include_router(router)
+ c = TestClient(app)
+
+ resp = c.post(
+ "/webhooks/sendblue",
+ json={"from_number": "+19127130720", "content": "Hi", "is_outbound": False},
+ )
+ assert resp.status_code == 403
+ mock_bridge.handle_incoming.assert_not_called()
+
# ---------------------------------------------------------------------------
# Health endpoint (requires agent_manager_routes)
diff --git a/tests/server/test_webhook_routes.py b/tests/server/test_webhook_routes.py
index 6493bda2..aafde890 100644
--- a/tests/server/test_webhook_routes.py
+++ b/tests/server/test_webhook_routes.py
@@ -213,3 +213,48 @@ class TestWhatsAppWebhook:
},
)
assert resp.status_code == 200
+
+
+class TestWebhooksFailClosed:
+ """When a channel's secret/token is unset, webhooks must reject (403)."""
+
+ def _client(self, mock_bridge, **kwargs):
+ app = FastAPI()
+ app.include_router(create_webhook_router(bridge=mock_bridge, **kwargs))
+ return TestClient(app)
+
+ def test_twilio_without_token_rejected(self, mock_bridge):
+ c = self._client(mock_bridge) # no twilio_auth_token
+ resp = c.post(
+ "/webhooks/twilio",
+ data={"From": "+15551234567", "Body": "hi", "MessageSid": "SM1"},
+ )
+ assert resp.status_code == 403
+ mock_bridge.handle_incoming.assert_not_called()
+
+ def test_bluebubbles_without_password_rejected(self, mock_bridge):
+ c = self._client(mock_bridge) # no bluebubbles_password
+ resp = c.post(
+ "/webhooks/bluebubbles",
+ json={"type": "new-message", "data": {}},
+ headers={"Authorization": "anything"},
+ )
+ assert resp.status_code == 403
+
+ def test_whatsapp_without_secret_rejected(self, mock_bridge):
+ c = self._client(mock_bridge) # no whatsapp_app_secret
+ resp = c.post(
+ "/webhooks/whatsapp",
+ content=b"{}",
+ headers={"Content-Type": "application/json"},
+ )
+ assert resp.status_code == 403
+
+ def test_whatsapp_verify_without_token_rejected(self, mock_bridge):
+ c = self._client(mock_bridge) # no whatsapp_verify_token
+ resp = c.get(
+ "/webhooks/whatsapp",
+ params={"hub.mode": "subscribe", "hub.verify_token": "",
+ "hub.challenge": "x"},
+ )
+ assert resp.status_code == 403
diff --git a/tests/tools/test_http_request.py b/tests/tools/test_http_request.py
index 04f68805..65e978cf 100644
--- a/tests/tools/test_http_request.py
+++ b/tests/tools/test_http_request.py
@@ -184,8 +184,9 @@ class TestHttpRequestTool:
"""Timeout should produce a clear error."""
tool = HttpRequestTool()
with patch("openjarvis.tools.http_request.check_ssrf", return_value=None):
- with patch(
- "openjarvis.tools.http_request.httpx.request",
+ with patch.object(
+ HttpRequestTool,
+ "_request_following_redirects",
side_effect=httpx.TimeoutException("timed out"),
):
result = tool.execute(url="https://slow.example.com", timeout=5)
@@ -196,14 +197,50 @@ class TestHttpRequestTool:
"""Connection error should produce a clear error."""
tool = HttpRequestTool()
with patch("openjarvis.tools.http_request.check_ssrf", return_value=None):
- with patch(
- "openjarvis.tools.http_request.httpx.request",
+ with patch.object(
+ HttpRequestTool,
+ "_request_following_redirects",
side_effect=httpx.ConnectError("Connection refused"),
):
result = tool.execute(url="https://down.example.com")
assert result.success is False
assert "Request error" in result.content
+ @respx.mock
+ def test_redirect_to_private_ip_blocked(self):
+ """A redirect to an internal/metadata host must be re-checked + blocked."""
+ respx.get("https://public.example.com/start").mock(
+ return_value=httpx.Response(
+ 302, headers={"location": "http://169.254.169.254/latest/"}
+ )
+ )
+ tool = HttpRequestTool()
+ # First check (initial URL) passes; the redirect target is blocked.
+ with patch(
+ "openjarvis.tools.http_request.check_ssrf",
+ side_effect=[None, "Blocked host: 169.254.169.254"],
+ ):
+ result = tool.execute(url="https://public.example.com/start")
+ assert result.success is False
+ assert "SSRF protection blocked redirect" in result.content
+
+ @respx.mock
+ def test_safe_redirect_is_followed(self):
+ """A redirect to another public URL is followed normally."""
+ respx.get("https://public.example.com/start").mock(
+ return_value=httpx.Response(
+ 302, headers={"location": "https://public.example.com/final"}
+ )
+ )
+ respx.get("https://public.example.com/final").mock(
+ return_value=httpx.Response(200, text="done")
+ )
+ tool = HttpRequestTool()
+ with patch("openjarvis.tools.http_request.check_ssrf", return_value=None):
+ result = tool.execute(url="https://public.example.com/start")
+ assert result.success is True
+ assert "done" in result.content
+
def test_method_validation(self):
"""Invalid HTTP method should be rejected."""
tool = HttpRequestTool()