Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

[CHOO-3853] Stop 422s repeating what was sent (#1080)

Christian McDermottd3f9cf39b361
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 05241fef4..778f73d05 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -45,6 +45,18 @@ version of their own to them without also giving them a release of their own.
### [Unreleased]
#### Security
+- **A refused request is no longer repeated back.** When the gateway or the
+ agent API refused a request as invalid, the 422 repeated what was sent: a
+ password too short to accept, or the whole body, password included, when
+ another field was missing. Registering or editing a Slack, Discord or Teams
+ connection with a config that did not fit quoted part of its bot token or
+ secret, and on an edit that could be the stored token, which nothing else
+ returns. Anything that records error responses (APM, proxies, browser HAR
+ captures) could end up holding passwords and tokens. Each error now says
+ only where and what is wrong (`type`, `loc` and `msg`), and the agent API no
+ longer writes the request body to its log. A script that read `input` or
+ `ctx` from a 422 will find them gone. A validation rule that raised its own
+ error on the agent API now answers 422 rather than 500.
- **Agents are no longer handed a key that never expires.** Datadog connects
with an API key and an application key that do not expire and that only
Datadog can revoke, and an agent granted Datadog was handed both. Switch now
diff --git a/core/switch_core/bridges/agent/api/handlers.py b/core/switch_core/bridges/agent/api/handlers.py
index 61299f677..a4dcaf24f 100644
--- a/core/switch_core/bridges/agent/api/handlers.py
+++ b/core/switch_core/bridges/agent/api/handlers.py
@@ -120,6 +120,7 @@ from switch_core.db.stores.api_key_store import ApiKeyStore
from switch_core.gateway.known_agents import known_agent
from switch_core.observability.catalogue import AGENT_CONNECTIONS_REFUSED
from switch_core.observability.metrics import metrics
+from switch_core.request_validation import public_errors
from switch_core.trust.client import GuardrailBlockedError
from switch_core.version import switch_core_version
@@ -270,7 +271,7 @@ async def _register_known(
try:
options = spec.parse_options(options_raw)
except ValidationError as exc:
- raise HTTPException(status_code=400, detail=exc.errors()) from exc
+ raise HTTPException(status_code=400, detail=public_errors(exc)) from exc
integration_profile = spec.build_profile(options)
metadata = {
diff --git a/core/switch_core/bridges/agent/app.py b/core/switch_core/bridges/agent/app.py
index ce852fd6a..590fe0baf 100644
--- a/core/switch_core/bridges/agent/app.py
+++ b/core/switch_core/bridges/agent/app.py
@@ -41,6 +41,7 @@ from switch_core.logging_context import log_context
from switch_core.observability.http import MetricsMiddleware
from switch_core.refusal_breaker import RefusalBreaker
from switch_core.request_context import RequestContextMiddleware
+from switch_core.request_validation import public_errors
from switch_core.room_service import RoomService
from switch_core.session_activity.outcomes import ApprovalOutcomes
from switch_core.sessions.errors import SessionError
@@ -80,14 +81,16 @@ async def log_http_exceptions(request: Request, exc: HTTPException) -> JSONRespo
async def log_validation_errors(
request: Request, exc: RequestValidationError
) -> JSONResponse:
+ # The log gets what the response gets and no more: the body that was sent,
+ # or an error's `input`, would put message text and secrets in the logs.
+ errors = public_errors(exc)
logger.warning(
- "%s %s → 422 validation: errors=%s body=%r",
+ "%s %s → 422 validation: errors=%s",
request.method,
request.url.path,
- exc.errors(),
- exc.body,
+ errors,
)
- return JSONResponse(status_code=422, content={"detail": exc.errors()})
+ return JSONResponse(status_code=422, content={"detail": errors})
def create_agent_bridge_app(
diff --git a/core/switch_core/gateway/agents.py b/core/switch_core/gateway/agents.py
index d1d970b50..5ae1bc87a 100644
--- a/core/switch_core/gateway/agents.py
+++ b/core/switch_core/gateway/agents.py
@@ -70,6 +70,7 @@ from switch_core.gateway.schemas import (
UpdateAgentOptionsRequest,
)
from switch_core.gateway.subagent_registration import derive_subagent_registrations
+from switch_core.request_validation import public_errors
logger = logging.getLogger(__name__)
@@ -215,7 +216,7 @@ async def register_known_agent(
try:
options = spec.parse_options(req.options)
except ValidationError as exc:
- raise HTTPException(status_code=400, detail=exc.errors()) from exc
+ raise HTTPException(status_code=400, detail=public_errors(exc)) from exc
integration_profile = spec.build_profile(options)
metadata = {
@@ -329,7 +330,7 @@ async def register_known_subagents(
try:
options = spec.parse_options(d.options)
except ValidationError as exc:
- raise HTTPException(status_code=400, detail=exc.errors()) from exc
+ raise HTTPException(status_code=400, detail=public_errors(exc)) from exc
integration_profile = spec.build_profile(options)
metadata = {
"known_agent_type": req.agent_type,
@@ -405,7 +406,7 @@ async def update_agent_options(
except AgentOptionsNotEditable as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
except ValidationError as exc:
- raise HTTPException(status_code=400, detail=exc.errors()) from exc
+ raise HTTPException(status_code=400, detail=public_errors(exc)) from exc
await session.commit()
md = agent.metadata_ if isinstance(agent.metadata_, dict) else {}
diff --git a/core/switch_core/gateway/app.py b/core/switch_core/gateway/app.py
index 19a5fb317..eaf360fb1 100644
--- a/core/switch_core/gateway/app.py
+++ b/core/switch_core/gateway/app.py
@@ -3,6 +3,7 @@ from __future__ import annotations
from pathlib import Path
from fastapi import FastAPI
+from fastapi.exceptions import RequestValidationError
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from starlette.middleware.sessions import SessionMiddleware
@@ -76,6 +77,7 @@ from switch_core.gateway.workspace_services import (
)
from switch_core.keys import Purpose
from switch_core.providers.hosted import HostedControllerSettings
+from switch_core.request_validation import validation_error_response
from switch_core.room_service import RoomService
from switch_core.sessions.errors import SessionError
from switch_core.sessions.http import session_error_response
@@ -187,6 +189,7 @@ def create_gateway_app(
if config.gateway_oidc_enabled:
register_oidc_client(config)
+ app.add_exception_handler(RequestValidationError, validation_error_response)
app.add_exception_handler(SessionError, session_error_response)
app.add_exception_handler(ChatError, chat_error_response)
app.include_router(
diff --git a/core/switch_core/gateway/collaborations.py b/core/switch_core/gateway/collaborations.py
index eb7203135..cad9e017c 100644
--- a/core/switch_core/gateway/collaborations.py
+++ b/core/switch_core/gateway/collaborations.py
@@ -75,6 +75,7 @@ from switch_core.gateway.schemas import (
SlackTokensCheck,
SlackTokensChecked,
)
+from switch_core.request_validation import describe_errors
from switch_core.room_service import RoomService
logger = logging.getLogger(__name__)
@@ -358,7 +359,7 @@ async def create_bridge(
# credentials and we do not.
raise HTTPException(status_code=400, detail=str(exc)) from exc
except ValidationError as exc:
- raise HTTPException(status_code=422, detail=str(exc)) from exc
+ raise HTTPException(status_code=422, detail=describe_errors(exc)) from exc
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
@@ -520,7 +521,7 @@ async def update_bridge(
connection_config=merged,
)
except ValidationError as exc:
- raise HTTPException(status_code=422, detail=str(exc)) from exc
+ raise HTTPException(status_code=422, detail=describe_errors(exc)) from exc
except BridgeStartRefused as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
except (BridgeClaimConflict, BridgeCredentialError) as exc:
diff --git a/core/switch_core/request_validation.py b/core/switch_core/request_validation.py
new file mode 100644
index 000000000..9a616f847
--- /dev/null
+++ b/core/switch_core/request_validation.py
@@ -0,0 +1,49 @@
+"""What a refused request is told, without what it sent.
+
+A pydantic error's `input` is what was sent: the refused value, or for a
+missing field or a check across fields the entire object around it. FastAPI's
+default 422 repeats each error whole, and `str()` of a `ValidationError` quotes
+every `input`, cut to its first and last few characters, so a short secret
+whole. Either way a password, or a bridge's bot token, came back in the
+response and into anything that records error bodies. Only what says what is
+wrong goes back: `type`, `loc` and `msg`. `ctx` stays out too, since some error
+types carry part of the input there (a union tag, the character a UUID failed
+on), and no client reads it.
+"""
+
+from __future__ import annotations
+
+from typing import Any
+
+from fastapi import Request
+from fastapi.exceptions import RequestValidationError
+from fastapi.responses import JSONResponse
+from pydantic import ValidationError
+
+_PUBLIC_FIELDS = ("type", "loc", "msg")
+
+
+def public_errors(
+ exc: ValidationError | RequestValidationError,
+) -> list[dict[str, Any]]:
+ errors: list[dict[str, Any]] = [dict(error) for error in exc.errors()]
+ return [
+ {field: error[field] for field in _PUBLIC_FIELDS if field in error}
+ for error in errors
+ ]
+
+
+def describe_errors(exc: ValidationError) -> str:
+ """The errors as one sentence, for a route whose `detail` is text."""
+ return "; ".join(
+ f"{'.'.join(str(part) for part in error['loc'])}: {error['msg']}"
+ if error["loc"]
+ else error["msg"]
+ for error in public_errors(exc)
+ )
+
+
+async def validation_error_response(request: Request, error: Exception) -> JSONResponse:
+ if not isinstance(error, RequestValidationError):
+ raise error
+ return JSONResponse(status_code=422, content={"detail": public_errors(error)})
diff --git a/core/tests/switch_core/bridges/agent/test_http_error_logging.py b/core/tests/switch_core/bridges/agent/test_http_error_logging.py
index 4c5ad1449..c5e2f8f44 100644
--- a/core/tests/switch_core/bridges/agent/test_http_error_logging.py
+++ b/core/tests/switch_core/bridges/agent/test_http_error_logging.py
@@ -1,10 +1,11 @@
import logging
+from typing import Any
import pytest
from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
-from httpx import ASGITransport, AsyncClient
-from pydantic import BaseModel
+from httpx import ASGITransport, AsyncClient, Response
+from pydantic import BaseModel, field_validator
from switch_core.bridges.agent.app import log_http_exceptions, log_validation_errors
@@ -13,6 +14,15 @@ class _Body(BaseModel):
text: str
+class _Checked(BaseModel):
+ room: str
+
+ @field_validator("room")
+ @classmethod
+ def _refuse(cls, value: str) -> str:
+ raise ValueError("no such room")
+
+
def _app() -> FastAPI:
app = FastAPI()
app.add_exception_handler(HTTPException, log_http_exceptions) # type: ignore[arg-type]
@@ -28,19 +38,31 @@ def _app() -> FastAPI:
async def typed(body: _Body) -> None:
return None
+ @app.post("/checked")
+ async def checked(body: _Checked) -> None:
+ return None
+
return app
-async def _records(
- caplog: pytest.LogCaptureFixture, method: str, path: str
-) -> list[logging.LogRecord]:
+async def _send(
+ caplog: pytest.LogCaptureFixture, method: str, path: str, body: dict[str, Any]
+) -> tuple[Response, list[logging.LogRecord]]:
caplog.clear()
with caplog.at_level(logging.DEBUG, logger="switch_core.bridges.agent.app"):
async with AsyncClient(
transport=ASGITransport(app=_app()), base_url="http://test"
) as client:
- await client.request(method, path, json={})
- return [r for r in caplog.records if r.name == "switch_core.bridges.agent.app"]
+ response = await client.request(method, path, json=body)
+ records = [r for r in caplog.records if r.name == "switch_core.bridges.agent.app"]
+ return response, records
+
+
+async def _records(
+ caplog: pytest.LogCaptureFixture, method: str, path: str
+) -> list[logging.LogRecord]:
+ _, records = await _send(caplog, method, path, {})
+ return records
@pytest.mark.asyncio
@@ -70,3 +92,33 @@ async def test_a_request_that_fails_validation_logs_a_warning(
records = await _records(caplog, "POST", "/typed")
assert [r.levelno for r in records] == [logging.WARNING]
assert "→ 422 validation" in records[0].getMessage()
+
+
+async def test_a_request_that_fails_validation_is_not_repeated_in_the_answer_or_the_log(
+ caplog: pytest.LogCaptureFixture,
+) -> None:
+ """A missing field's error carries the whole body as its `input`, so a
+ secret sent beside it would come back and be logged."""
+ secret = "sk-test-not-a-real-key"
+ response, records = await _send(caplog, "POST", "/typed", {"token": secret})
+
+ assert response.status_code == 422
+ assert secret not in response.text
+ assert secret not in records[0].getMessage()
+ assert response.json() == {
+ "detail": [
+ {"type": "missing", "loc": ["body", "text"], "msg": "Field required"}
+ ]
+ }
+
+
+async def test_a_validator_that_raises_is_a_422_not_a_server_error(
+ caplog: pytest.LogCaptureFixture,
+) -> None:
+ """The error's `ctx` holds the ValueError itself, which is not JSON."""
+ response, _ = await _send(caplog, "POST", "/checked", {"room": "nowhere"})
+
+ assert response.status_code == 422
+ assert response.json()["detail"][0]["msg"] == "Value error, no such room"
diff --git a/core/tests/switch_core/gateway/test_validation_errors.py b/core/tests/switch_core/gateway/test_validation_errors.py
new file mode 100644
index 000000000..9cd84465c
--- /dev/null
+++ b/core/tests/switch_core/gateway/test_validation_errors.py
@@ -0,0 +1,240 @@
+"""A request the gateway refuses as invalid is not repeated back to the caller.
+
+FastAPI's default 422 carries each error's `input`: the refused value, or for a
+missing field the whole body around it. Every endpoint that takes a password
+is driven here through the app `create_gateway_app` builds, so the test covers
+the handler being registered as well as what it answers with. Validation fails
+before any route body runs, so those tests need no database.
+
+A bridge's connection config is validated inside the route instead, against
+the platform's config model, and refused as text. A check across fields there
+carries the whole config as its `input`, stored bot token included on an edit.
+"""
+
+from __future__ import annotations
+
+from collections.abc import AsyncIterator
+from types import SimpleNamespace
+from typing import Any
+from unittest.mock import MagicMock
+
+import httpx
+import pytest
+from fastapi import HTTPException
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.bridges.collaboration.slack.adapter import SlackConnectionConfig
+from switch_core.db.models import Client, CollaborationBridge, User
+from switch_core.db.stores.collaboration_bridge_store import CollaborationBridgeStore
+from switch_core.db.stores.room_store import RoomStore
+from switch_core.gateway.auth import get_current_user, require_admin
+from switch_core.gateway.collaborations import create_bridge, update_bridge
+from switch_core.gateway.dependencies import get_session, get_system_session
+from switch_core.gateway.schemas import BridgeCreateRequest, BridgeUpdateRequest
+from tests.switch_core.gateway.test_app_route_mounting import _app
+from tests.switch_core.gateway.test_shared_bridge_registration import _Lifecycle
+
+SECRET = "correct horse battery staple"
+SHORT_SECRET = "short7!"
+
+
+async def _no_session() -> AsyncIterator[MagicMock]:
+ yield MagicMock()
+
+
+async def client() -> AsyncIterator[httpx.AsyncClient]:
+ app: Any = _app()
+ admin = SimpleNamespace(id="admin-id", email="[email protected]", role="admin")
+ app.dependency_overrides[get_session] = _no_session
+ app.dependency_overrides[get_system_session] = _no_session
+ app.dependency_overrides[get_current_user] = lambda: admin
+ app.dependency_overrides[require_admin] = lambda: admin
+ async with httpx.AsyncClient(
+ transport=httpx.ASGITransport(app=app), base_url="http://test"
+ ) as client:
+ yield client
+
+
+ ("method", "path", "body", "secrets", "field"),
+ [
+ pytest.param(
+ "POST",
+ "/auth/signup",
+ {"email": "[email protected]", "password": SHORT_SECRET},
+ [SHORT_SECRET],
+ "password",
+ id="signup-short-password",
+ ),
+ pytest.param(
+ "POST",
+ "/auth/signup",
+ {"password": SECRET},
+ [SECRET],
+ "email",
+ id="signup-missing-email",
+ ),
+ pytest.param(
+ "POST",
+ "/auth/login",
+ {"password": SECRET},
+ [SECRET],
+ "email",
+ id="login-missing-email",
+ ),
+ pytest.param(
+ "PUT",
+ "/auth/me/password",
+ {"current_password": SECRET, "new_password": SHORT_SECRET},
+ [SECRET, SHORT_SECRET],
+ "new_password",
+ id="change-password-short-new-password",
+ ),
+ pytest.param(
+ "PUT",
+ "/auth/me/password",
+ {"current_password": SECRET},
+ [SECRET],
+ "new_password",
+ id="change-password-missing-new-password",
+ ),
+ pytest.param(
+ "POST",
+ "/users",
+ {"name": "Ada", "email": "[email protected]", "password": SHORT_SECRET},
+ [SHORT_SECRET],
+ "password",
+ id="create-user-short-password",
+ ),
+ pytest.param(
+ "POST",
+ "/users",
+ {"email": "[email protected]", "password": SECRET},
+ [SECRET],
+ "name",
+ id="create-user-missing-name",
+ ),
+ ],
+)
+async def test_a_refused_request_does_not_echo_its_password(
+ client: httpx.AsyncClient,
+ method: str,
+ path: str,
+ body: dict[str, str],
+ secrets: list[str],
+ field: str,
+) -> None:
+ response = await client.request(method, path, json=body)
+
+ assert response.status_code == 422, response.text
+ for secret in secrets:
+ assert secret not in response.text
+ [error] = response.json()["detail"]
+ assert set(error) == {"type", "loc", "msg"}
+ assert error["loc"] == ["body", field]
+
+
+async def test_the_answer_still_says_what_is_wrong(client: httpx.AsyncClient) -> None:
+ response = await client.post(
+ "/auth/signup", json={"email": "[email protected]", "password": "x" * 80}
+ )
+
+ assert response.status_code == 422, response.text
+ assert response.json() == {
+ "detail": [
+ {
+ "type": "value_error",
+ "loc": ["body", "password"],
+ "msg": "Value error, Password must be at most 72 bytes.",
+ }
+ ]
+ }
+
+
+BOT_TOKEN = "stored-bot-token-for-tests-only"
+_BRIDGE_STORE = CollaborationBridgeStore()
+_ADMIN = User(id="admin", name="admin", email="[email protected]", role="admin")
+
+
+class _SlackLifecycle(_Lifecycle):
+ """Validates a connection config the way the real lifecycle does: against
+ the platform's own config model."""
+
+ async def register(self, **kwargs: Any) -> None:
+ SlackConnectionConfig.model_validate(kwargs["connection_config"])
+
+ async def check_edited_connection_config(self, **kwargs: Any) -> None:
+ SlackConnectionConfig.model_validate(kwargs["connection_config"])
+
+
+def _assert_no_token(detail: object) -> None:
+ # `str(ValidationError)` keeps the first ten and last six characters.
+ assert BOT_TOKEN[:10] not in str(detail)
+ assert BOT_TOKEN[-6:] not in str(detail)
+
+
+async def test_a_refused_bridge_config_does_not_echo_its_token() -> None:
+ with pytest.raises(HTTPException) as refused:
+ await create_bridge(
+ req=BridgeCreateRequest(
+ bridge_type="slack",
+ display_name="Acme",
+ connection_config={"bot_token": BOT_TOKEN, "workspace_id": "T1"},
+ ),
+ session=None, # type: ignore[arg-type]
+ bridge_store=_BRIDGE_STORE,
+ collab_lifecycle=_SlackLifecycle(), # type: ignore[arg-type]
+ user=_ADMIN,
+ )
+
+ assert refused.value.status_code == 422
+ _assert_no_token(refused.value.detail)
+ assert refused.value.detail == (
+ "Value error, app_token is required: without it Switch opens no Socket "
+ "Mode connection and this bridge would receive no Slack events at all."
+ )
+
+
+async def test_a_refused_bridge_edit_does_not_echo_the_stored_token(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ """The edit is validated merged with what is stored, so clearing the app
+ token puts the stored bot token, which no response otherwise returns, in
+ the error's `input`."""
+ async with session_factory() as session:
+ client = Client(
+ transport_user_id="@bridge-validation-errors:test",
+ display_name="bridge client",
+ type="bridge",
+ )
+ session.add(client)
+ await session.flush()
+ bridge = CollaborationBridge(
+ type="slack",
+ display_name="Acme",
+ client_id=client.id,
+ status="active",
+ connection_config={
+ "bot_token": BOT_TOKEN,
+ "app_token": "stored-app-token",
+ "workspace_id": "T1",
+ },
+ )
+ session.add(bridge)
+ await session.commit()
+
+ with pytest.raises(HTTPException) as refused:
+ await update_bridge(
+ bridge_id=bridge.id,
+ payload=BridgeUpdateRequest(connection_config={"app_token": ""}),
+ session=session,
+ bridge_store=_BRIDGE_STORE,
+ room_store=RoomStore(),
+ collab_lifecycle=_SlackLifecycle(), # type: ignore[arg-type]
+ user=_ADMIN,
+ )
+
+ assert refused.value.status_code == 422
+ _assert_no_token(refused.value.detail)
+ assert "app_token is required" in str(refused.value.detail)