test_cost_awareness_security.py
python
sha256:a78e7e5a8740e03315f325d19edeb3aa1b306b3337d04abbaa9a9e0f3bbeb7a1
docs: MuseHub-first before ISR #74 — staging solidify NEXT
Human
9 hours ago
| 1 | """Security tests for Track P / P-cost (§PC.9).""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import inspect |
| 6 | import json |
| 7 | import socket |
| 8 | from pathlib import Path |
| 9 | from unittest.mock import patch |
| 10 | |
| 11 | import pytest |
| 12 | |
| 13 | from cli.kit_root import kit_root |
| 14 | from tests.fixtures.cost_awareness import seed_cost_awareness_repo, seed_cost_e2e_repo |
| 15 | from tests.support import git_status_runner, run_cli |
| 16 | from tools import cost_awareness |
| 17 | |
| 18 | |
| 19 | class _SocketBlockedError(RuntimeError): |
| 20 | pass |
| 21 | |
| 22 | |
| 23 | @pytest.fixture |
| 24 | def block_network(): |
| 25 | real_socket = socket.socket |
| 26 | |
| 27 | def _guard(*args, **kwargs): |
| 28 | raise _SocketBlockedError("network connections forbidden in cost-awareness paths") |
| 29 | |
| 30 | with patch.object(socket, "socket", side_effect=_guard): |
| 31 | yield |
| 32 | |
| 33 | |
| 34 | def test_cost_paths_make_no_network_connections(tmp_path, block_network) -> None: |
| 35 | seed_cost_e2e_repo(tmp_path) |
| 36 | code = run_cli( |
| 37 | ["route", "--phase-tier", "auto", "--json"], |
| 38 | cwd=tmp_path, |
| 39 | runner=git_status_runner(), |
| 40 | kit=kit_root(), |
| 41 | json_mode=True, |
| 42 | ) |
| 43 | assert code == 0 |
| 44 | |
| 45 | |
| 46 | def test_cost_output_has_no_currency_or_price_markers(tmp_path, capsys) -> None: |
| 47 | seed_cost_e2e_repo(tmp_path) |
| 48 | run_cli( |
| 49 | ["route", "--phase-tier", "auto", "--json"], |
| 50 | cwd=tmp_path, |
| 51 | runner=git_status_runner(), |
| 52 | kit=kit_root(), |
| 53 | json_mode=True, |
| 54 | ) |
| 55 | text = capsys.readouterr().out.lower() |
| 56 | for marker in ("$", "usd", "eur", "price", "budget", "gpt-", "openrouter"): |
| 57 | assert marker not in text |
| 58 | |
| 59 | |
| 60 | def test_injection_shaped_cost_class_fail_closed(tmp_path, monkeypatch) -> None: |
| 61 | seed_cost_awareness_repo(tmp_path, enabled=False) |
| 62 | from tools.model_routing.labels import RoutingPolicyError |
| 63 | |
| 64 | def _raise(*args, **kwargs): |
| 65 | raise RoutingPolicyError("'; DROP TABLE tiers; --", exit_code=32) |
| 66 | |
| 67 | monkeypatch.setattr("cli.commands.route.load_model_tier_cost_bands", _raise) |
| 68 | assert ( |
| 69 | run_cli( |
| 70 | ["route"], |
| 71 | cwd=tmp_path, |
| 72 | runner=git_status_runner(), |
| 73 | kit=kit_root(), |
| 74 | ) |
| 75 | == 32 |
| 76 | ) |
| 77 | |
| 78 | |
| 79 | def test_cost_surface_reminder_only_never_blocks_success(tmp_path, capsys) -> None: |
| 80 | seed_cost_e2e_repo(tmp_path) |
| 81 | code = run_cli( |
| 82 | ["status", "--json"], |
| 83 | cwd=tmp_path, |
| 84 | runner=git_status_runner(), |
| 85 | kit=kit_root(), |
| 86 | json_mode=True, |
| 87 | ) |
| 88 | assert code == 0 |
| 89 | payload = json.loads(capsys.readouterr().out) |
| 90 | assert payload["cost_awareness"]["enabled"] is True |
| 91 | |
| 92 | |
| 93 | def test_cost_modules_do_not_import_network_clients() -> None: |
| 94 | forbidden = ("urllib", "http.client", "requests", "httpx") |
| 95 | for module in ( |
| 96 | cost_awareness.derive, |
| 97 | cost_awareness.surface, |
| 98 | cost_awareness.format, |
| 99 | cost_awareness.normalize, |
| 100 | ): |
| 101 | source = inspect.getsource(module) |
| 102 | for token in forbidden: |
| 103 | assert token not in source |
File History
1 commit
sha256:a78e7e5a8740e03315f325d19edeb3aa1b306b3337d04abbaa9a9e0f3bbeb7a1
docs: MuseHub-first before ISR #74 — staging solidify NEXT
Human
9 hours ago