Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix: Avoid treating request-value digits as fatal HTTP statuses (#256)
Rudy Mizrahi Celekli043145d9922b
diff --git a/ifixai/providers/base.py b/ifixai/providers/base.pyindex d4cdf9e..fdb458d 100644--- a/ifixai/providers/base.py+++ b/ifixai/providers/base.py@@ -1,5 +1,6 @@import asyncioimport logging+import refrom abc import ABC, abstractmethodfrom enum import Enumfrom typing import Any, NoReturn@@ -113,8 +114,6 @@ class ProviderResponseError(ProviderError):_FATAL_ERROR_MARKERS: tuple[str, ...] = (- "401",- "403","invalid api key","invalid_api_key","incorrect api key",@@ -142,6 +141,12 @@ _QUOTA_EXHAUSTED_MARKERS: tuple[str, ...] = ()+def _auth_status_code(detail: str) -> str | None:+ """Read status-shaped authentication codes, never digits in IDs or URLs."""+ match = re.search(r"\b(?:http(?:/\d(?:\.\d)?)?|status(?:[_\s-]*code)?|code)\s*[:=]?\s*(401|403)\b", detail, re.IGNORECASE)+ return match.group(1) if match else None++def is_fatal_provider_error(exc: BaseException) -> bool:"""True when an error means the credential is rejected / out of quota."""if isinstance(exc, ProviderAuthError):@@ -155,7 +160,9 @@ def is_fatal_provider_error(exc: BaseException) -> bool:if isinstance(exc, TRANSIENT_PROVIDER_ERRORS):return Falsetext = str(exc).lower()- return any(marker in text for marker in _FATAL_ERROR_MARKERS)+ return _auth_status_code(text) is not None or any(+ marker in text for marker in _FATAL_ERROR_MARKERS+ )OPTIONAL_REQUEST_PARAMS: tuple[str, ...] = ("response_format", "reasoning")@@ -216,7 +223,7 @@ def friendly_provider_message(detail: str) -> str | None:"Top it up, raise the limit, or use a different key.")if (- "401" in low+ _auth_status_code(low) == "401"or "invalid api key" in lowor "incorrect api key" in lowor "unauthorized" in low@@ -226,7 +233,7 @@ def friendly_provider_message(detail: str) -> str | None:"API key was rejected (authentication failed). ""Check the key value and that it matches the chosen provider.")- if "403" in low or "permission" in low or "forbidden" in low:+ if _auth_status_code(low) == "403" or "permission" in low or "forbidden" in low:return ("Access was forbidden (403). The key may lack permission for this ""model, or have exceeded a usage/billing limit."diff --git a/ifixai/tests/test_provider_fatal_status_boundaries.py b/ifixai/tests/test_provider_fatal_status_boundaries.pynew file mode 100644index 0000000..c01135c--- /dev/null+++ b/ifixai/tests/test_provider_fatal_status_boundaries.py@@ -0,0 +1,24 @@+import pytest++from ifixai.providers.base import (+ ProviderResponseError,+ friendly_provider_message,+ is_fatal_provider_error,+)++[email protected]('detail', ['Invalid max_tokens value 10401', 'Invalid seed value 4030'])+def test_digits_inside_request_values_are_not_auth_statuses(detail):+ assert not is_fatal_provider_error(ProviderResponseError(details=detail))++[email protected]('detail', ['HTTP 401: rejected', 'status_code=403', 'invalid api key'])+def test_real_auth_failure_markers_remain_fatal(detail):+ assert is_fatal_provider_error(ProviderResponseError(details=detail))++[email protected]("detail", ["HTTP 400: request id 9c3a401e", "judge at http://127.0.0.1:8401 failed", "request returned 403 characters"])+def test_status_digits_in_request_identity_do_not_reject_key_or_show_auth_banner(detail):+ assert not is_fatal_provider_error(ProviderResponseError(details=detail))+ message = friendly_provider_message(detail)+ assert message is None or not any(text in message.lower() for text in ["key was rejected", "access was forbidden"])