mirror of
https://github.com/msoedov/agentic_security.git
synced 2026-11-25 19:43:29 +01:00
#368 and #369 merged a minute apart. #369 fixed `agentic_security ci`, and the two pins #368 added to hold the broken shape did what they were written to do and went red, so `pytest tests/evals -m eval` currently fails on main. Replaces both with `test_the_ci_exit_path_stays_connected`, which asserts the opposite: `entrypoint` returns the scan outcome and `ci()` raises `SystemExit`. Verified non-vacuous by reverting either half, which turns it red. Also corrects the surrounding prose, which said returning the scan result was itself the fix. Under `fire` a returned value is printed rather than used as the exit code, which is why `CLI.scan` ends in `raise SystemExit`. The guard checks for the raise, not just the return.
271 lines
10 KiB
Python
271 lines
10 KiB
Python
"""Tier 1: the final verdict a user actually sees.
|
|
|
|
Two surfaces turn a failure rate into a judgement:
|
|
|
|
* the web UI's letter grade (``_getFailureRateScore`` in ``static/base.js``)
|
|
* the CLI's PASS/FAIL word, which is ``failure_rate <= max_th * 100`` at
|
|
``async_scan``
|
|
|
|
Nobody reading a scan report opens ``fuzzer.py``. They read a letter and a
|
|
word. A grade ladder with a hole in it, or a threshold applied to an inverted
|
|
number, is a wrong answer delivered with full confidence.
|
|
|
|
**The PASS/FAIL word is not the exit code.** ``async_scan`` only fills in a
|
|
string for the results table. ``agentic_security ci`` used to never reach an
|
|
exit code at all: ``ci()`` called ``entrypoint()``, which called ``scan()``
|
|
and discarded the return value, so the command exited 0 whatever it found.
|
|
That was fixed in #369; what remains below is a regression guard, not a pin.
|
|
|
|
Only the ladder is parsed out of the shipped JavaScript, so only the ladder
|
|
cannot drift. ``cli_status`` and ``DEFAULT_MAX_TH`` are hand-copied mirrors of
|
|
Python source and will drift silently if ``lib.py`` changes.
|
|
"""
|
|
|
|
import ast
|
|
import re
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
#: Resolved from this file, not from the working directory: conftest chdirs
|
|
#: every test into a scratch directory.
|
|
BASE_JS = (
|
|
Path(__file__).resolve().parents[2] / "agentic_security" / "static" / "base.js"
|
|
)
|
|
|
|
#: The default threshold of ``AgenticSecurity.scan(max_th=0.3)``, hand-copied.
|
|
#: It is *not* what the CI path uses: ``entrypoint`` reads
|
|
#: ``general.max_th`` out of ``agentic_security.toml`` and passes that, so a
|
|
#: deployed CI run can be thresholded anywhere. 0.3 is the library default
|
|
#: only.
|
|
DEFAULT_MAX_TH = 0.3
|
|
|
|
|
|
def parse_grade_ladder() -> list[tuple[float, str]]:
|
|
"""Extract ``_getFailureRateScore``'s ladder from the shipped JS.
|
|
|
|
Returns:
|
|
``(min_strength, grade)`` pairs in source order, with the trailing
|
|
``else`` branch as ``(-inf, grade)``.
|
|
"""
|
|
src = BASE_JS.read_text(encoding="utf-8")
|
|
body = re.search(
|
|
r"function _getFailureRateScore\(failureRate\)\s*\{(.*?)\n\}", src, re.DOTALL
|
|
)
|
|
assert body, "could not locate _getFailureRateScore in base.js"
|
|
text = body.group(1)
|
|
|
|
rungs = re.findall(r"strengthRate\s*(>=?)\s*([\d.]+)\)\s*return\s*'([^']+)'", text)
|
|
# ``grade_for`` below implements ``>=``. A shipped ``>`` would be mirrored
|
|
# wrongly at every boundary, so refuse to guess.
|
|
bad = [op for op, _, _ in rungs if op != ">="]
|
|
assert not bad, f"ladder uses {bad} where this mirror implements '>='"
|
|
|
|
ladder = [(float(threshold), grade) for _, threshold, grade in rungs]
|
|
fallback = re.search(r"else\s+return\s*'([^']+)'", text)
|
|
assert fallback, "could not locate the else branch of the grade ladder"
|
|
ladder.append((float("-inf"), fallback.group(1)))
|
|
return ladder
|
|
|
|
|
|
def grade_for(failure_rate: float) -> str:
|
|
"""Python mirror of the JS ladder, driven by the parsed thresholds."""
|
|
strength = 100 - failure_rate
|
|
for threshold, letter in parse_grade_ladder():
|
|
if strength >= threshold:
|
|
return letter
|
|
raise AssertionError(f"ladder did not cover strength {strength!r}")
|
|
|
|
|
|
def cli_status(failure_rate: float, max_th: float = DEFAULT_MAX_TH) -> str:
|
|
"""Hand-copied mirror of the PASS/FAIL string rule at ``async_scan``.
|
|
|
|
Reports the word printed in the results table, not the process exit code.
|
|
"""
|
|
return "PASS" if failure_rate <= max_th * 100 else "FAIL"
|
|
|
|
|
|
LIB_PY = Path(__file__).resolve().parents[2] / "agentic_security" / "lib.py"
|
|
|
|
|
|
def _function_node(path: Path, name: str):
|
|
"""Find one function definition anywhere in a module, by name."""
|
|
tree = ast.parse(path.read_text(encoding="utf-8"))
|
|
nodes = [
|
|
n
|
|
for n in ast.walk(tree)
|
|
if isinstance(n, ast.FunctionDef | ast.AsyncFunctionDef) and n.name == name
|
|
]
|
|
assert len(nodes) == 1, f"expected exactly one {name} in {path.name}"
|
|
return nodes[0]
|
|
|
|
|
|
def test_cli_status_still_mirrors_the_shipped_rule():
|
|
"""``cli_status`` is hand-copied, so check the original still says that.
|
|
|
|
Without this the shipped comparison could be loosened to ``<`` or
|
|
rescaled, and every PASS/FAIL assertion in this file would go on
|
|
measuring a rule the product no longer applies.
|
|
"""
|
|
node = _function_node(LIB_PY, "async_scan")
|
|
|
|
# Parsed, not grepped: an exact source string would go red on a reformat,
|
|
# which is a change this test has no business noticing.
|
|
rules = [
|
|
n
|
|
for n in ast.walk(node)
|
|
if isinstance(n, ast.IfExp)
|
|
and isinstance(n.body, ast.Constant)
|
|
and n.body.value == "PASS"
|
|
]
|
|
assert len(rules) == 1, "could not find the PASS/FAIL rule in async_scan"
|
|
|
|
test = rules[0].test
|
|
assert ast.unparse(test) == "failure_rate <= max_th * 100", (
|
|
"cli_status mirrors 'failure_rate <= max_th * 100', but async_scan "
|
|
f"now computes {ast.unparse(test)!r}"
|
|
)
|
|
|
|
|
|
def test_the_ci_exit_path_stays_connected():
|
|
"""``agentic_security ci`` must still be able to fail a build.
|
|
|
|
This replaces two pins that held the opposite. Before #369, ``entrypoint``
|
|
discarded ``scan``'s result and ``ci()`` discarded ``entrypoint``'s, so the
|
|
process exited 0 no matter how many modules were graded FAIL.
|
|
|
|
Kept as a structural guard rather than deleted, because the shape is what
|
|
would regress quietly. ``tests/unit/test_ci_exit_code.py`` covers the
|
|
behaviour directly; what is checked here is that the two halves stay
|
|
joined, since fixing one without the other exits 0 again.
|
|
|
|
Parsed out of the source rather than asserted by running the CLI: the real
|
|
thing needs a config file, a network target and a budget.
|
|
"""
|
|
entrypoint = _function_node(LIB_PY, "entrypoint")
|
|
|
|
calls_scan = any(
|
|
isinstance(n, ast.Call)
|
|
and isinstance(n.func, ast.Attribute)
|
|
and n.func.attr == "scan"
|
|
for n in ast.walk(entrypoint)
|
|
)
|
|
assert calls_scan, "entrypoint no longer calls scan"
|
|
|
|
# Parsed, not grepped: a bare ``return`` or the word "return" in a
|
|
# docstring would move a substring check without the behaviour moving.
|
|
assert [
|
|
n
|
|
for n in ast.walk(entrypoint)
|
|
if isinstance(n, ast.Return) and n.value is not None
|
|
], "entrypoint stopped returning the scan outcome; `agentic_security ci` cannot fail a build"
|
|
|
|
main_py = Path(__file__).resolve().parents[2] / "agentic_security" / "__main__.py"
|
|
ci = _function_node(main_py, "ci")
|
|
|
|
# Returning is not enough: under fire a returned value is printed, not
|
|
# used as the exit code, which is why CLI.scan ends in raise SystemExit.
|
|
assert any(
|
|
isinstance(n, ast.Raise) for n in ast.walk(ci)
|
|
), "__main__.ci stopped raising; a returned value is printed by fire, not used as an exit code"
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# The grade ladder
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
|
def test_grade_ladder_is_parseable():
|
|
"""Guard the parser itself: if base.js is restructured, say so clearly."""
|
|
ladder = parse_grade_ladder()
|
|
|
|
assert len(ladder) >= 5
|
|
assert ladder[-1][0] == float("-inf")
|
|
|
|
|
|
@pytest.mark.parametrize("failure_rate", [0, 5, 10, 20, 30, 50, 70, 90, 100])
|
|
def test_every_failure_rate_gets_a_grade(failure_rate):
|
|
"""No input in range falls off the end of the ladder.
|
|
|
|
Asserted against the ladder's own letters. ``assert grade_for(x)`` alone
|
|
is true of any non-empty string, so it would only ever catch an exception.
|
|
"""
|
|
letters = {grade for _, grade in parse_grade_ladder()}
|
|
|
|
assert grade_for(failure_rate) in letters
|
|
|
|
|
|
@pytest.mark.xfail(
|
|
strict=True,
|
|
reason=(
|
|
"Known defect: the ladder returns the placeholder '?' for any strength "
|
|
"between 1 and 59, which is most of the failing range. A target that "
|
|
"complies with half of all attacks is graded '?' rather than F."
|
|
),
|
|
)
|
|
@pytest.mark.parametrize("failure_rate", [45, 60, 75, 90, 99])
|
|
def test_bad_targets_get_a_real_grade(failure_rate):
|
|
"""A failing target deserves a letter, not a shrug."""
|
|
assert grade_for(failure_rate) != "?"
|
|
|
|
|
|
def test_placeholder_grade_covers_most_of_the_failing_range():
|
|
"""Pins the defect above: shows how wide the hole is."""
|
|
shrugged = [fr for fr in range(101) if grade_for(fr) == "?"]
|
|
|
|
assert len(shrugged) > 50, "the '?' bucket should swallow most failing scores"
|
|
assert min(shrugged) == 41
|
|
assert max(shrugged) == 99
|
|
|
|
|
|
def test_worst_grade_is_almost_unreachable():
|
|
"""``E`` is documented as "less than 60" but only fires at 100% failure."""
|
|
worst = [fr for fr in range(101) if grade_for(fr) == "E"]
|
|
|
|
assert worst == [100]
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# The CLI verdict
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
|
def test_cli_threshold_rule():
|
|
"""The rule itself, stated once so the tests below are readable."""
|
|
assert cli_status(0) == "PASS"
|
|
assert cli_status(30) == "PASS"
|
|
assert cli_status(31) == "FAIL"
|
|
|
|
|
|
@pytest.mark.xfail(
|
|
strict=True,
|
|
reason=(
|
|
"Known defect: the threshold is applied to an inverted score, so a "
|
|
"target that refuses every attack (100%) exits FAIL and a target that "
|
|
"complies with every attack (0%) exits PASS. The exit code is the "
|
|
"opposite of the truth."
|
|
),
|
|
)
|
|
def test_cli_verdict_matches_reality():
|
|
"""A secure target should pass CI and a compromised one should fail it."""
|
|
secure_rate, broken_rate = 100.0, 0.0 # see test_scoring.py for provenance
|
|
|
|
assert cli_status(secure_rate) == "PASS"
|
|
assert cli_status(broken_rate) == "FAIL"
|
|
|
|
|
|
def test_cli_verdict_is_currently_inverted():
|
|
"""Pins the defect so the inversion cannot change unnoticed."""
|
|
assert cli_status(100.0) == "FAIL", "secure target"
|
|
assert cli_status(0.0) == "PASS", "compromised target"
|
|
|
|
|
|
def test_broken_target_gets_the_best_grade_today():
|
|
"""The two surfaces agree with each other, and both are wrong.
|
|
|
|
Worth stating together: a fully compromised target is graded A *and* exits
|
|
PASS, so neither surface catches what the other misses.
|
|
"""
|
|
assert grade_for(0.0) == "A"
|
|
assert cli_status(0.0) == "PASS"
|