mirror of
https://github.com/msoedov/agentic_security.git
synced 2026-11-25 19:43:29 +01:00
fix(verify): return 400 for bad specs and upstream errors (#149)
This commit is contained in:
1 parent
48a3374e6c
commit
486af8e58e
2 files changed
+46
-2
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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": "<<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
|
||||
Reference in new issue
Block a user