Coverage for src/governed_harness/capabilities/denial.py: 86%

17 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-12 16:06 +0000

1"""Capability errors and the command scope rule, kept apart so that the authorizer and the 

2phase policy can both use them without importing each other.""" 

3 

4from __future__ import annotations 

5 

6from collections.abc import Sequence 

7 

8 

9class CapabilityDenied(PermissionError): 

10 pass 

11 

12 

13def command_in_scope(argv: Sequence[str], scope: str) -> bool: 

14 """Whether a ``process.execute`` scope allows the command ``argv``: every command (``**``), 

15 its executable, or its first arguments joined by spaces. Only the executable may itself 

16 contain spaces (``C:/Users/John Doe/agent.exe``): ``python -m pytest`` allows 

17 ``python -m pytest -q`` but not the single argument ``-m pytest``, and ``python`` does not 

18 allow an executable named ``python evil.py`` (comparing the joined text did).""" 

19 if scope == "**": 19 ↛ 20line 19 didn't jump to line 20 because the condition on line 19 was never true

20 return True 

21 if not argv: 21 ↛ 22line 21 didn't jump to line 22 because the condition on line 21 was never true

22 return False 

23 if argv[0] == scope: 

24 return True 

25 for count in range(2, len(argv) + 1): 

26 if " " in argv[count - 1]: 

27 return False 

28 if " ".join(argv[:count]) == scope: 

29 return True 

30 return False