diff --git a/agentic_security/routes/scan.py b/agentic_security/routes/scan.py index 319b06a..a6460e0 100644 --- a/agentic_security/routes/scan.py +++ b/agentic_security/routes/scan.py @@ -30,8 +30,8 @@ async def verify( info: LLMInfo, secrets: InMemorySecrets = Depends(get_in_memory_secrets) ) -> dict[str, int | str | float]: logger.info("verify: checking LLM spec connectivity") - spec = LLMSpec.from_string(info.spec) try: + spec = LLMSpec.from_string(info.spec) r = await spec.verify() except InvalidHTTPSpecError as e: logger.warning("verify: invalid HTTP spec: %s", e) @@ -42,7 +42,10 @@ async def verify( if r.status_code >= 400: logger.warning("verify: upstream returned HTTP %s", r.status_code) - raise HTTPException(status_code=r.status_code, detail=r.text) + # Upstream failure is a bad target config, not a server error here. + raise HTTPException( + status_code=400, detail=f"Upstream returned HTTP {r.status_code}: {r.text}" + ) logger.info( "verify: success status=%s elapsed=%.2fs", r.status_code, diff --git a/tests/integration/routes/test_verify.py b/tests/integration/routes/test_verify.py new file mode 100644 index 0000000..34361f4 --- /dev/null +++ b/tests/integration/routes/test_verify.py @@ -0,0 +1,41 @@ +from datetime import timedelta + +import httpx +import pytest +from fastapi.testclient import TestClient + +from agentic_security.app import app +from agentic_security.http_spec import LLMSpec + +client = TestClient(app) + +SPEC = 'POST https://llm.example/v1\nContent-Type: application/json\n\n{"prompt": "<>"}' + + +# The UI shows any 5xx from /verify as "server crashed". A broken target or a +# bad spec is user input, so it must come back as 400 with a readable reason (#149). +@pytest.mark.parametrize( + "upstream_status,expected_status", + [(200, 200), (401, 400), (404, 400), (500, 400), (503, 400)], +) +def test_verify_maps_upstream_errors_to_400( + monkeypatch, upstream_status, expected_status +): + async def fake_verify(self): + r = httpx.Response(upstream_status, text="upstream body") + r.elapsed = timedelta(seconds=0.1) + return r + + monkeypatch.setattr(LLMSpec, "verify", fake_verify) + r = client.post("/verify", json={"spec": SPEC}) + assert r.status_code == expected_status + if expected_status == 400: + assert ( + r.json()["detail"] + == f"Upstream returned HTTP {upstream_status}: upstream body" + ) + + +@pytest.mark.parametrize("spec", ["POST /relative/path\n\n{}", "not a spec at all"]) +def test_verify_invalid_spec_is_400(spec): + assert client.post("/verify", json={"spec": spec}).status_code == 400