fix(cli): reject non-finite or negative provider pricing (#5031 follow-up)
The landed fix caught a non-numeric price but still accepted nan, inf and negative values, which parse as floats and then flow into cost estimation as NaN/inf/negative. Validate each --pricing-input/--pricing-output through _valid_price (finite and >= 0) with the same clean error + exit(1), and test that nan/inf/-inf/1e999/-5 are rejected. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
6ee8e2258e
commit
cd9634832c
2 files changed
+47
-9
No files matched your search
+19
-8
@@ -7,6 +7,7 @@ import of main to avoid a cli<->__main__ import cycle.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
@@ -15,6 +16,16 @@ from graphify.paths import GRAPHIFY_OUT as _GRAPHIFY_OUT
|
||||
from pathlib import Path, PurePosixPath, PureWindowsPath
|
||||
|
||||
|
||||
def _valid_price(value: float) -> float:
|
||||
"""A provider price flag must be a finite, non-negative number. A NaN, inf
|
||||
or negative value parses as a float but flows into cost estimation (price x
|
||||
tokens), producing NaN/inf/negative cost reports (#5031), so reject it the
|
||||
same way a non-numeric value is rejected."""
|
||||
if not math.isfinite(value) or value < 0:
|
||||
raise ValueError("price must be finite and non-negative")
|
||||
return value
|
||||
|
||||
|
||||
# Hook reminders arrive inside the agent's tool-result stream, where commands
|
||||
# ("MANDATORY", "You MUST", "include this in every subagent prompt") read like a
|
||||
# prompt injection and get ignored or flagged (#2202). State facts instead; the
|
||||
@@ -1874,32 +1885,32 @@ def dispatch_command(cmd: str) -> None:
|
||||
env_key = a.split("=", 1)[1]; i += 1
|
||||
elif a == "--pricing-input" and i + 1 < len(args):
|
||||
try:
|
||||
pricing_input = float(args[i + 1])
|
||||
pricing_input = _valid_price(float(args[i + 1]))
|
||||
except ValueError:
|
||||
print(f"Error: invalid float for --pricing-input: '{args[i + 1]}'", file=sys.stderr)
|
||||
print(f"Error: invalid price for --pricing-input: '{args[i + 1]}' (expected a finite, non-negative number)", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
i += 2
|
||||
elif a.startswith("--pricing-input="):
|
||||
val = a.split("=", 1)[1]
|
||||
try:
|
||||
pricing_input = float(val)
|
||||
pricing_input = _valid_price(float(val))
|
||||
except ValueError:
|
||||
print(f"Error: invalid float for --pricing-input: '{val}'", file=sys.stderr)
|
||||
print(f"Error: invalid price for --pricing-input: '{val}' (expected a finite, non-negative number)", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
i += 1
|
||||
elif a == "--pricing-output" and i + 1 < len(args):
|
||||
try:
|
||||
pricing_output = float(args[i + 1])
|
||||
pricing_output = _valid_price(float(args[i + 1]))
|
||||
except ValueError:
|
||||
print(f"Error: invalid float for --pricing-output: '{args[i + 1]}'", file=sys.stderr)
|
||||
print(f"Error: invalid price for --pricing-output: '{args[i + 1]}' (expected a finite, non-negative number)", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
i += 2
|
||||
elif a.startswith("--pricing-output="):
|
||||
val = a.split("=", 1)[1]
|
||||
try:
|
||||
pricing_output = float(val)
|
||||
pricing_output = _valid_price(float(val))
|
||||
except ValueError:
|
||||
print(f"Error: invalid float for --pricing-output: '{val}'", file=sys.stderr)
|
||||
print(f"Error: invalid price for --pricing-output: '{val}' (expected a finite, non-negative number)", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
i += 1
|
||||
else:
|
||||
|
||||
@@ -209,4 +209,31 @@ def test_dispatch_command_provider_add_invalid_pricing_float(tmp_path, monkeypat
|
||||
cli.dispatch_command("provider")
|
||||
assert exc.value.code == 1
|
||||
err = capsys.readouterr().err
|
||||
assert "invalid float for --pricing-input" in err
|
||||
assert "invalid price for --pricing-input" in err
|
||||
|
||||
|
||||
@pytest.mark.parametrize("bad", ["nan", "inf", "-inf", "1e999", "-5"])
|
||||
def test_dispatch_command_provider_add_rejects_non_finite_or_negative_pricing(
|
||||
tmp_path, monkeypatch, capsys, bad
|
||||
):
|
||||
"""A price that parses as a float but is NaN/inf/negative is rejected: it
|
||||
would otherwise flow into cost estimation as NaN/inf/negative (#5031)."""
|
||||
import sys
|
||||
from graphify import cli, llm
|
||||
|
||||
providers_file = tmp_path / "providers.json"
|
||||
monkeypatch.setattr(llm, "_custom_providers_path", lambda global_=True: providers_file)
|
||||
|
||||
test_args = [
|
||||
"graphify", "provider", "add", "mycustom",
|
||||
"--base-url=http://localhost:8080/v1",
|
||||
"--default-model=model-x",
|
||||
"--env-key=MY_KEY",
|
||||
f"--pricing-input={bad}",
|
||||
]
|
||||
monkeypatch.setattr(sys, "argv", test_args)
|
||||
with pytest.raises(SystemExit) as exc:
|
||||
cli.dispatch_command("provider")
|
||||
assert exc.value.code == 1
|
||||
assert "invalid price for --pricing-input" in capsys.readouterr().err
|
||||
assert not providers_file.exists()
|
||||
Reference in new issue
Block a user