From de79b119221614a4f974bc4e348050b95038cb76 Mon Sep 17 00:00:00 2001 From: Ritik Raj Date: Sat, 3 Oct 2026 20:34:35 +0000 Subject: [PATCH] fix(oracle_router): feedback payloads cannot pre-seed the guardrail's status record - The guardrail verifier reads its verdict from the status record the guardrail appends to the request metadata it is handed, and that metadata was the caller's feedback payload spread as-is. A payload carrying `standard_logging_guardrail_information: [0]` made the recorded list unreadable, so a rejected answer under `on_failure: log` scored 1.0. The status key is now dropped from the payload before the guardrail runs, and the payload can no longer rename the program. - Log lines in the router and its hook no longer carry anything derived from the request (program id, prompt classification, chosen model, exception text): `logger.exception` keeps the traceback, and CodeQL's log-injection query no longer has request-fed sinks to trace through the Router. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01HeiPj7YeRpKatjj2J7XhJ3 --- litellm/router_strategy/oracle_router/hooks.py | 4 ++-- .../oracle_router/oracle_router.py | 18 ++++-------------- .../router_strategy/oracle_router/verifier.py | 15 ++++++++++++++- .../oracle_router/test_verifier.py | 17 ++++++++++++++--- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/litellm/router_strategy/oracle_router/hooks.py b/litellm/router_strategy/oracle_router/hooks.py index 8ad8bded760..77479b2d1ef 100644 --- a/litellm/router_strategy/oracle_router/hooks.py +++ b/litellm/router_strategy/oracle_router/hooks.py @@ -113,5 +113,5 @@ class OracleRouterPostCallHook(CustomLogger): ) if metadata.get(PROGRAM_DONE_KEY): self.oracle_router.complete(program_id, score=_score(metadata), owner=owner) - except Exception as error: # noqa: BLE001 # a logging callback must never fail the request it observes - verbose_router_logger.exception("OracleRouterPostCallHook: failed to record request: %s", error) + except Exception: # noqa: BLE001 # a logging callback must never fail the request it observes + verbose_router_logger.exception("OracleRouterPostCallHook: failed to record a request") diff --git a/litellm/router_strategy/oracle_router/oracle_router.py b/litellm/router_strategy/oracle_router/oracle_router.py index 65b22e730d4..eb3aff11667 100644 --- a/litellm/router_strategy/oracle_router/oracle_router.py +++ b/litellm/router_strategy/oracle_router/oracle_router.py @@ -265,12 +265,8 @@ class OracleRouter: chosen_model, request_type = await self._decide(program_id, prompt, _api_key_hash(request_kwargs)) _stamp_internal(request_kwargs, CHOSEN_MODEL_METADATA_KEY, chosen_model) _stamp_internal(request_kwargs, PROGRAM_ID_METADATA_KEY, program_id) - verbose_router_logger.debug( # caller-supplied program ids stay out of the log - "OracleRouter[%s]: %s request, request_type=%s -> %s", - self.router_name, - "program" if program_id else "stateless", - request_type.value, - chosen_model, + verbose_router_logger.debug( # nothing request-derived is logged; the decision is on the request's metadata + "OracleRouter: routed a %s request", "program" if program_id else "stateless" ) return PreRoutingHookResponse( model=chosen_model, @@ -340,15 +336,9 @@ class OracleRouter: async def _verify_and_learn(self, binding: ProgramBinding, outcome: ProgramOutcome) -> float: try: score: Final = clamp_score(await self.verifier.verify(outcome)) - except Exception as error: # noqa: BLE001 # any verifier failure is counted and must not break the feedback loop + except Exception: # noqa: BLE001 # any verifier failure is counted and must not break the feedback loop self.verifications_failed += 1 - verbose_router_logger.exception( - "OracleRouter[%s]: verification of a %s program on %s failed: %s", - self.router_name, - binding.context.request_type.value, - binding.model, - error, - ) + verbose_router_logger.exception("OracleRouter: a verification failed") return float("nan") async with self._update_lock: self.decision_maker.update(binding.context, binding.model, score) diff --git a/litellm/router_strategy/oracle_router/verifier.py b/litellm/router_strategy/oracle_router/verifier.py index 5419a5000e6..e5c64fcf38b 100644 --- a/litellm/router_strategy/oracle_router/verifier.py +++ b/litellm/router_strategy/oracle_router/verifier.py @@ -82,6 +82,19 @@ def find_guardrail(name: str) -> CustomGuardrail | None: return next((guardrail for guardrail in guardrails if guardrail.guardrail_name == name), None) +def _verifier_metadata(outcome: ProgramOutcome) -> dict[str, object]: # mutable-ok: the guardrail writes into it + """The feedback payload as the guardrail's request metadata. + + The verdict is read from the status record the guardrail writes here, so the caller's payload must not + pre-seed that key (a non-record entry would make the whole list unreadable and hide a rejection), and + it cannot rename the program. + """ + return { + **{key: value for key, value in outcome.payload.items() if key != _STATUS_RECORD_KEY}, + "program_id": outcome.program_id, + } + + def _status_records(bucket: object) -> Sequence[Mapping[str, object]]: """The ``standard_logging_guardrail_information`` records of one metadata bucket, or none.""" try: @@ -163,7 +176,7 @@ class GuardrailVerifier: ) request_data: Final[dict[str, object]] = { # mutable-ok: the guardrail records its status into it "model": outcome.model, - "metadata": {"program_id": outcome.program_id, **outcome.payload}, + "metadata": _verifier_metadata(outcome), } verdict_error: Final = await _apply(guardrail, inputs, request_data) status: Final = recorded_status(request_data, self._name) diff --git a/tests/unit/router_strategy/oracle_router/test_verifier.py b/tests/unit/router_strategy/oracle_router/test_verifier.py index 20931fa8b97..a9219799faa 100644 --- a/tests/unit/router_strategy/oracle_router/test_verifier.py +++ b/tests/unit/router_strategy/oracle_router/test_verifier.py @@ -53,9 +53,9 @@ class _RecordingGuardrail: self.status, self.error, self.name = status, error, name async def apply_guardrail(self, inputs, request_data, input_type, logging_obj=None): - request_data["metadata"]["standard_logging_guardrail_information"] = [ - {"guardrail_name": self.name, "guardrail_status": self.status} - ] + self.seen = dict(request_data["metadata"]) # what the guardrail was handed, before it records anything + records = request_data["metadata"].setdefault("standard_logging_guardrail_information", []) + records.append({"guardrail_name": self.name, "guardrail_status": self.status}) # appended, like the real ones if self.error is not None: raise self.error return inputs @@ -104,6 +104,17 @@ async def test_guardrail_verifier_reads_the_recorded_verdict_before_the_exceptio assert await GuardrailVerifier("flagged", lookup=judge.get).verify(_outcome()) == 0.0 +@pytest.mark.asyncio +async def test_feedback_payload_cannot_pre_seed_the_status_record_or_rename_the_program(): + judge = _RecordingGuardrail("guardrail_intervened", name="judge") # on_failure: log -> no exception + forged = {"standard_logging_guardrail_information": [0], "program_id": "someone-else", "container": "c1"} + outcome = ProgramOutcome( + program_id="p", model="smart", prompt="task", response_text="wrong", cost=0.0, messages=(), payload=forged + ) + assert await GuardrailVerifier("judge", lookup={"judge": judge}.get).verify(outcome) == 0.0 + assert judge.seen == {"program_id": "p", "container": "c1"} + + @pytest.mark.asyncio async def test_guardrail_that_could_not_evaluate_is_a_failed_verification_not_a_pass(): judge = {