Files
hermes-agent/tests/gateway/test_api_server_redact_error_text.py
yoniebans b7f590ed56 test(gateway): gateway cases over the persistence interface, part 1 of 6
The gateway tests seed and read the store through the storage fixtures.
2026-11-17 17:51:21 +01:00

31 lines
1.3 KiB
Python

"""Tests for the API server's redaction of exception text sent to HTTP clients."""
from unittest.mock import patch
from gateway.platforms.api_server import _redact_api_error_text
# ---------------------------------------------------------------------------
# _redact_api_error_text — guards every outward error site (envelopes, SSE
# error events, cron-endpoint 500 bodies) that routes raw exception text to
# authenticated HTTP clients. #37733
# ---------------------------------------------------------------------------
class TestRedactApiErrorText:
def test_masks_secret_value_but_preserves_structure(self):
secret = "sk-api-server-leak-1234567890"
out = _redact_api_error_text(Exception(f"auth failed OPENAI_API_KEY={secret}"))
assert secret not in out
assert "OPENAI_API_KEY=" in out
def test_redacts_regardless_of_global_redaction_setting(self):
# force=True must mask even when global redaction is disabled.
secret = "sk-forced-redaction-0987654321"
with patch("agent.redact._REDACT_ENABLED", False):
out = _redact_api_error_text(Exception(f"boom AWS_SECRET_ACCESS_KEY={secret}"))
assert secret not in out
def test_limit_truncates_after_redaction(self):
assert len(_redact_api_error_text("x" * 500, limit=50)) == 50