|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Regression tests: the CBF barrier must reject a non-finite or negative trade |
| 17 | +``amount`` instead of treating it as SAFE. |
| 18 | +
|
| 19 | +A negative ``amount`` makes ``next_cash = current - cost`` larger than the |
| 20 | +current balance, so the ``h_next >= (1-gamma)*h_t`` envelope check passes and |
| 21 | +``atomic_verify_and_commit`` writes the inflated balance back to Redis; a NaN |
| 22 | +``amount`` makes every comparison false, so the barrier also passes. Both let |
| 23 | +attacker-supplied trade parameters defeat the cash barrier. FiscalLimitGuard.reserve |
| 24 | +already applies the same finiteness/positive guard to reservations. |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +from unittest.mock import MagicMock |
| 30 | + |
| 31 | +import pytest |
| 32 | + |
| 33 | +pytest.importorskip("fakeredis", reason="fakeredis required for CBF state tests") |
| 34 | + |
| 35 | +import fakeredis.aioredis # type: ignore[import] |
| 36 | + |
| 37 | +from src.gateway.governance.cbf import ControlBarrierFunction |
| 38 | + |
| 39 | +_INVALID_AMOUNTS = [-1_000_000.0, float("nan"), float("inf"), float("-inf")] |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.local |
| 43 | +@pytest.mark.asyncio |
| 44 | +async def test_do_verify_action_rejects_invalid_amount() -> None: |
| 45 | + """The read-only barrier returns a non-SAFE result for negative / NaN / inf |
| 46 | + amounts, and still returns SAFE for a valid positive trade.""" |
| 47 | + cbf = ControlBarrierFunction() |
| 48 | + |
| 49 | + for amount in _INVALID_AMOUNTS: |
| 50 | + result = await cbf._do_verify_action( |
| 51 | + "execute_trade", |
| 52 | + {"amount": amount}, |
| 53 | + current_cash=100_000.0, |
| 54 | + balance_source="self_reported", |
| 55 | + span=None, |
| 56 | + ) |
| 57 | + assert result != "SAFE", ( |
| 58 | + f"amount={amount!r} must be rejected by the barrier; got SAFE " |
| 59 | + "(negative/NaN amount bypasses the CBF envelope check)" |
| 60 | + ) |
| 61 | + |
| 62 | + ok = await cbf._do_verify_action( |
| 63 | + "execute_trade", |
| 64 | + {"amount": 1_000.0}, |
| 65 | + current_cash=100_000.0, |
| 66 | + balance_source="self_reported", |
| 67 | + span=None, |
| 68 | + ) |
| 69 | + assert ok == "SAFE", f"a valid positive trade must still pass; got {ok!r}" |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.local |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_atomic_verify_rejects_invalid_amount_without_mutating_balance() -> None: |
| 75 | + """The atomic check+commit rejects an invalid amount and leaves the persisted |
| 76 | + cash balance untouched — a negative amount must not inflate it.""" |
| 77 | + fake_redis = fakeredis.aioredis.FakeRedis(decode_responses=False) |
| 78 | + await fake_redis.set("safety:current_cash", "100000.0") |
| 79 | + |
| 80 | + cbf = ControlBarrierFunction() |
| 81 | + cbf.tracer = None |
| 82 | + |
| 83 | + mock_redis_module = MagicMock() |
| 84 | + mock_redis_module.get_raw_client.return_value = fake_redis |
| 85 | + |
| 86 | + with pytest.MonkeyPatch().context() as mp: |
| 87 | + mp.setattr("src.gateway.governance.cbf.redis_client", mock_redis_module) |
| 88 | + |
| 89 | + for amount in _INVALID_AMOUNTS: |
| 90 | + committed, reason = await cbf.atomic_verify_and_commit( |
| 91 | + action_name="execute_trade", |
| 92 | + payload={"amount": amount}, |
| 93 | + ) |
| 94 | + assert committed is False, ( |
| 95 | + f"amount={amount!r} must not commit; got committed=True" |
| 96 | + ) |
| 97 | + assert "UNSAFE" in reason, f"reason must flag UNSAFE; got {reason!r}" |
| 98 | + balance = (await fake_redis.get("safety:current_cash")).decode() |
| 99 | + assert float(balance) == 100_000.0, ( |
| 100 | + f"balance must stay 100000.0 after rejecting amount={amount!r}; " |
| 101 | + f"got {balance} (negative amount inflated the persisted balance)" |
| 102 | + ) |
| 103 | + |
| 104 | + # A valid trade still commits and debits the balance. |
| 105 | + committed, reason = await cbf.atomic_verify_and_commit( |
| 106 | + action_name="execute_trade", |
| 107 | + payload={"amount": 1_000.0}, |
| 108 | + ) |
| 109 | + assert committed is True, f"valid trade must commit; got reason={reason!r}" |
| 110 | + balance = (await fake_redis.get("safety:current_cash")).decode() |
| 111 | + assert float(balance) == 99_000.0, ( |
| 112 | + f"balance must debit to 99000.0; got {balance}" |
| 113 | + ) |
0 commit comments