|
| 1 | +"""Negative amounts must not bypass spending ceilings or approval thresholds. |
| 2 | +
|
| 3 | +Verified bypass before this fix, against the exact policy the shipped pitch |
| 4 | +demo uses (max_amount=5000, require_approval_over=500): |
| 5 | +
|
| 6 | + amount= 9500 -> DENY (correct) |
| 7 | + amount=-9500 -> ALLOW (no ceiling, no human, no risk score) |
| 8 | +
|
| 9 | +Both bounds in _check_constraints are `amount > limit`, so any negative value |
| 10 | +cleared both. risk.py additionally gated its score on `amount > 0`, so the same |
| 11 | +request also scored zero risk. Some payment APIs treat a negative refund as a |
| 12 | +charge, which turns this from a nonsense value into a transfer the ceiling was |
| 13 | +meant to prevent. |
| 14 | +""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from agentguard.dlp.scanner import DLPResult |
| 21 | +from agentguard.models import Decision, Effect, Policy |
| 22 | +from agentguard.policy.engine import ActionRequest, PolicyEngine |
| 23 | + |
| 24 | +CEILING, REVIEW_OVER = 5000, 500 |
| 25 | + |
| 26 | + |
| 27 | +def _policy() -> Policy: |
| 28 | + return Policy( |
| 29 | + id=1, name="refunds", effect=Effect.ALLOW, resource="payment:**", |
| 30 | + actions=["payment.refund"], enabled=True, priority=0, |
| 31 | + conditions={"max_amount": CEILING, "require_approval_over": REVIEW_OVER}, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def _decide(amount): |
| 36 | + req = ActionRequest(action_type="payment.refund", |
| 37 | + resource="payment:stripe:refund", |
| 38 | + metadata={} if amount is None else {"amount": amount}) |
| 39 | + return PolicyEngine().evaluate(req, [_policy()], |
| 40 | + dlp=DLPResult(findings=[], redacted=None)) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize("amount", [-1, -0.01, -500, -9500, -1_000_000, "-9500"]) |
| 44 | +def test_negative_amounts_never_pass_silently(amount): |
| 45 | + """The bypass: any of these previously returned ALLOW.""" |
| 46 | + assert _decide(amount).decision == Decision.REQUIRE_APPROVAL |
| 47 | + |
| 48 | + |
| 49 | +def test_positive_amounts_are_unchanged(): |
| 50 | + """The fix must not alter correct existing behaviour.""" |
| 51 | + assert _decide(250).decision == Decision.ALLOW |
| 52 | + assert _decide(REVIEW_OVER + 1).decision == Decision.REQUIRE_APPROVAL |
| 53 | + assert _decide(CEILING + 1).decision == Decision.DENY |
| 54 | + |
| 55 | + |
| 56 | +def test_missing_amount_still_routes_to_human(): |
| 57 | + """Pre-existing fail-closed rule this fix is modelled on.""" |
| 58 | + assert _decide(None).decision == Decision.REQUIRE_APPROVAL |
| 59 | + |
| 60 | + |
| 61 | +def test_zero_is_allowed_not_treated_as_negative(): |
| 62 | + """0 is a bounded, verifiable amount — it must not be swept up by the fix.""" |
| 63 | + assert _decide(0).decision == Decision.ALLOW |
| 64 | + |
| 65 | + |
| 66 | +def test_unconstrained_policy_ignores_amount_sign(): |
| 67 | + """A policy with no amount condition must not suddenly demand approval.""" |
| 68 | + p = Policy(id=2, name="open", effect=Effect.ALLOW, resource="payment:**", |
| 69 | + actions=["payment.refund"], enabled=True, priority=0, conditions={}) |
| 70 | + req = ActionRequest(action_type="payment.refund", resource="payment:x", |
| 71 | + metadata={"amount": -9500}) |
| 72 | + assert PolicyEngine().evaluate( |
| 73 | + req, [p], dlp=DLPResult(findings=[], redacted=None)).decision == Decision.ALLOW |
| 74 | + |
| 75 | + |
| 76 | +def test_risk_scores_negative_amount_by_magnitude(): |
| 77 | + """risk.py gated on `amount > 0`, so -9500 scored as if there were no amount.""" |
| 78 | + from agentguard.risk import _coerce_amount |
| 79 | + |
| 80 | + assert _coerce_amount(-9500) == -9500.0 |
| 81 | + # The scoring path is exercised via assess(); here we assert the property |
| 82 | + # that motivated the change: exposure is magnitude, not signed value. |
| 83 | + assert abs(_coerce_amount(-9500)) == abs(_coerce_amount(9500)) |
0 commit comments