apex-twin 0.21.0: a static audit that every target-taking tool is gated
tests/gate_audit.py parses every TOOLS_*[] table (45 tools, 8 tables) and the
handler bodies, with comments and string literals stripped before searching. It
enforces two rules:
A. a tool whose schema contains a target key must call gated(), gated_cmd()
or apex_scope_check() in its handler. Four record filters are named
exceptions: asset_list, finding_list, report_markdown, traffic_list.
B. a handler that opens a socket or spawns a process (http_once,
tcp_connect_host, tcp_probe, apex_run) must call one of the same functions.
It also pins the tool count at 45, so adding a tool forces a review here. The
audit runs first in tests/run.sh, so make test = gate audit + 298 checks; there
is also a make gate-audit target.
Red proof: renaming the gate call in portscan and finding_add produced three
violations (portscan twice: target and socket; finding_add once) and exit 1.
Restored, the audit is OK.
298 checks, 45 tools, analyze clean.
This commit is contained in:
1 parent
6b52bcc547
commit
9188dddfa5
5 files changed
+209
-5
No files matched your search
@@ -35,6 +35,9 @@ src/%.o: src/%.c src/apex.h $(LIBDIR)/mcp.h
|
||||
test: apex-twin
|
||||
./tests/run.sh
|
||||
|
||||
gate-audit:
|
||||
python3 tests/gate_audit.py
|
||||
|
||||
asan: $(ASAN_BIN)
|
||||
|
||||
test-asan: $(ASAN_BIN)
|
||||
@@ -173,4 +176,4 @@ clean:
|
||||
rm -f *.gcov
|
||||
rm -rf $(DISTNAME) $(DIST)
|
||||
|
||||
.PHONY: all test asan test-asan fuzz fuzz-selftest fuzz-red fuzz-proxy fuzz-proxy-red fuzz-http fuzz-http-red coverage analyze dist install clean
|
||||
.PHONY: all test gate-audit asan test-asan fuzz fuzz-selftest fuzz-red fuzz-proxy fuzz-proxy-red fuzz-http fuzz-http-red coverage analyze dist install clean
|
||||
@@ -28,9 +28,10 @@ separate, explicitly armed switch.
|
||||
|
||||
```sh
|
||||
make # apex-twin, ~239 kB static-ish binary, zero warnings under -Werror
|
||||
make test # 298 end-to-end checks, no mocks
|
||||
make test # gate audit + 298 end-to-end checks, no mocks
|
||||
make asan # apex-twin-asan with ASan + UBSan
|
||||
make test-asan # the whole suite against the sanitizer binary
|
||||
make gate-audit # static audit: every target-taking tool calls the gate
|
||||
make analyze # gcc -fanalyzer over every translation unit
|
||||
make coverage # -O0 --coverage build, suite, per-file gcov report
|
||||
make fuzz # mutated scope/rewrite/MCP inputs against the ASan binary
|
||||
@@ -422,6 +423,16 @@ and any ASan/UBSan report fails the run. A case also fails on a hang; 200/202/
|
||||
`make fuzz-http-red` (`APEX_FUZZ_BUG_HTTP`, a heap overflow after a complete
|
||||
head) is rejected 2/2 with the exact `apex-twin.c` UBSan line.
|
||||
|
||||
`make gate-audit` is a static check of the one invariant: it parses every
|
||||
`TOOLS_*[]` table and handler body (comments and strings stripped), and rejects
|
||||
any tool whose schema names a `target` without calling
|
||||
`gated()`/`gated_cmd()`/`apex_scope_check()`, and any handler that opens a socket
|
||||
or spawns a process without one of those calls. The only exceptions are the four
|
||||
record filters that read local state (`asset_list`, `finding_list`,
|
||||
`report_markdown`, `traffic_list`), named in the script. It also pins the tool
|
||||
count, so adding a tool forces a review. Renaming the gate call in `portscan`
|
||||
and `finding_add` produced three violations; the audit is part of `make test`.
|
||||
|
||||
`make coverage` builds `apex-twin-cov` with `-O0 --coverage`, runs the suite and
|
||||
prints per-file line coverage (`tests/coverage.py`, gcov only, no extra tools).
|
||||
The first measured pass was 55.9%; the gap tests brought it to **76.7%** —
|
||||
@@ -436,7 +447,7 @@ the copied scope reason is now bounded with a precision, not left to snprintf.
|
||||
|
||||
## Status
|
||||
|
||||
0.20.0. Built and run: 45 tools, 298 checks, 340 fuzz cases and 76.7% line
|
||||
0.21.0. Built and run: 45 tools, 298 checks, 340 fuzz cases and 76.7% line
|
||||
coverage. The loop closes: a probe captures the exchange as a pair, a finding
|
||||
points at it, `finding_update` gives it a state, an asset records the host it
|
||||
came from, `cors_check`/`cookie_audit`/`http_options` turn a web answer into a
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
|
||||
#include "mcp.h"
|
||||
|
||||
#define APEX_VERSION "0.20.0"
|
||||
#define APEX_VERSION "0.21.0"
|
||||
#define APEX_MAX_OUT (1u << 20)
|
||||
#define APEX_PATH_MAX 512
|
||||
#define APEX_PATH_FULL 1024
|
||||
|
||||
Executable
+187
@@ -0,0 +1,187 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Static audit: every tool that names a target must go through the gate.
|
||||
|
||||
Parses each TOOLS_*[] table in src/tools_*.c, extracts the handler bodies with
|
||||
brace matching (comments and string literals stripped), and checks two rules:
|
||||
|
||||
A. a tool whose input schema has a "target" key must call gated(),
|
||||
gated_cmd() or apex_scope_check() in its handler, unless it is a record
|
||||
filter that only reads local state (explicit allowlist below);
|
||||
B. a handler that opens a socket or spawns a process (http_once,
|
||||
tcp_connect_host, tcp_probe, apex_run) must call one of the same functions.
|
||||
|
||||
The tool count must match EXPECTED_TOOLS, so adding a tool forces a look here.
|
||||
|
||||
make gate-audit
|
||||
python3 tests/gate_audit.py -v
|
||||
"""
|
||||
import glob
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
EXPECTED_TOOLS = 45
|
||||
GATE = ("gated(", "gated_cmd(", "apex_scope_check(")
|
||||
NET = ("http_once(", "tcp_connect_host(", "tcp_probe(", "apex_run(")
|
||||
# Tools whose "target" argument filters local records; they never touch it.
|
||||
RECORD_FILTERS = {"asset_list", "finding_list", "report_markdown",
|
||||
"traffic_list"}
|
||||
|
||||
TABLE_RE = re.compile(r"const mcp_tool (TOOLS_[A-Z_]+)\[\] = \{(.*?)\n\};",
|
||||
re.S)
|
||||
ENTRY_RE = re.compile(r'\{\s*"([a-z0-9_]+)",(.*?)\n\t ([a-z_][a-z0-9_]*) \},',
|
||||
re.S)
|
||||
TARGET_KEY = re.compile(r'\\"target\\":')
|
||||
|
||||
|
||||
def extract_body(text, brace):
|
||||
"""Return (raw body, code-only body) for the brace block at `brace`."""
|
||||
code = []
|
||||
depth = 0
|
||||
i = brace
|
||||
state = "code"
|
||||
while i < len(text):
|
||||
ch = text[i]
|
||||
nxt = text[i + 1] if i + 1 < len(text) else ""
|
||||
if state == "code":
|
||||
if ch == "/" and nxt == "*":
|
||||
state = "block"
|
||||
code.append(" ")
|
||||
i += 2
|
||||
continue
|
||||
if ch == "/" and nxt == "/":
|
||||
state = "line"
|
||||
code.append(" ")
|
||||
i += 2
|
||||
continue
|
||||
if ch == '"':
|
||||
state = "string"
|
||||
code.append(" ")
|
||||
i += 1
|
||||
continue
|
||||
if ch == "'":
|
||||
state = "char"
|
||||
code.append(" ")
|
||||
i += 1
|
||||
continue
|
||||
code.append(ch)
|
||||
if ch == "{":
|
||||
depth += 1
|
||||
elif ch == "}":
|
||||
depth -= 1
|
||||
if depth == 0:
|
||||
return text[brace:i + 1], "".join(code)
|
||||
elif state == "block":
|
||||
if ch == "*" and nxt == "/":
|
||||
state = "code"
|
||||
code.append(" ")
|
||||
i += 2
|
||||
continue
|
||||
code.append("\n" if ch == "\n" else " ")
|
||||
elif state == "line":
|
||||
if ch == "\n":
|
||||
state = "code"
|
||||
code.append("\n")
|
||||
else:
|
||||
code.append(" ")
|
||||
elif state == "string":
|
||||
if ch == "\\":
|
||||
code.append(" ")
|
||||
i += 2
|
||||
continue
|
||||
if ch == '"':
|
||||
state = "code"
|
||||
code.append(" ")
|
||||
elif state == "char":
|
||||
if ch == "\\":
|
||||
code.append(" ")
|
||||
i += 2
|
||||
continue
|
||||
if ch == "'":
|
||||
state = "code"
|
||||
code.append(" ")
|
||||
i += 1
|
||||
return None, None
|
||||
|
||||
|
||||
def handler_code(sources, name):
|
||||
for path, text in sources:
|
||||
m = re.search(r"static void\s+" + re.escape(name) +
|
||||
r"\s*\(\s*mcp_conn\s*\*c\s*,[^)]*\)", text)
|
||||
if m is None:
|
||||
continue
|
||||
brace = text.find("{", m.end())
|
||||
if brace < 0:
|
||||
continue
|
||||
_, code = extract_body(text, brace)
|
||||
if code is not None:
|
||||
return code, path
|
||||
return None, None
|
||||
|
||||
|
||||
def main():
|
||||
verbose = "-v" in sys.argv
|
||||
sources = [(p, open(p).read())
|
||||
for p in sorted(glob.glob(os.path.join(HERE, "src", "tools_*.c")))]
|
||||
tools = []
|
||||
for path, text in sources:
|
||||
for tm in TABLE_RE.finditer(text):
|
||||
table = tm.group(1)
|
||||
body = tm.group(2)
|
||||
entries = list(ENTRY_RE.finditer(body))
|
||||
if not entries:
|
||||
print("gate-audit: %s has no entries" % table)
|
||||
return 1
|
||||
for em in entries:
|
||||
tools.append({"name": em.group(1), "table": table,
|
||||
"text": em.group(2),
|
||||
"handler": em.group(3), "file": path})
|
||||
print("gate-audit: parsed %d tools from %d tables" %
|
||||
(len(tools), len(set(t["table"] for t in tools))))
|
||||
|
||||
violations = []
|
||||
seen = set()
|
||||
for tool in tools:
|
||||
name = tool["name"]
|
||||
if name in seen:
|
||||
violations.append("%s: duplicate tool name" % name)
|
||||
seen.add(name)
|
||||
code, hpath = handler_code(sources, tool["handler"])
|
||||
if code is None:
|
||||
violations.append("%s: handler %s not found" %
|
||||
(name, tool["handler"]))
|
||||
continue
|
||||
gated = any(g in code for g in GATE)
|
||||
wants_target = TARGET_KEY.search(tool["text"]) is not None
|
||||
uses_net = any(n in code for n in NET)
|
||||
ok = True
|
||||
if wants_target and not gated and name not in RECORD_FILTERS:
|
||||
violations.append("%s: schema has target but %s has no gate call" %
|
||||
(name, tool["handler"]))
|
||||
ok = False
|
||||
if uses_net and not gated:
|
||||
violations.append("%s: %s uses a socket/process but has no gate call" %
|
||||
(name, tool["handler"]))
|
||||
ok = False
|
||||
if verbose:
|
||||
print(" %-4s %-22s %s" % ("ok" if ok else "FAIL", name,
|
||||
os.path.basename(hpath)))
|
||||
|
||||
if len(tools) != EXPECTED_TOOLS:
|
||||
violations.append("tool count is %d, expected %d (update EXPECTED_TOOLS "
|
||||
"and review the new tool)" % (len(tools),
|
||||
EXPECTED_TOOLS))
|
||||
if violations:
|
||||
for v in violations:
|
||||
print("FAIL " + v)
|
||||
print("gate-audit: %d violation(s) over %d tools" %
|
||||
(len(violations), len(tools)))
|
||||
return 1
|
||||
print("gate-audit: OK (%d tools, %d record-filter exception(s))" %
|
||||
(len(tools), len(RECORD_FILTERS)))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
+4
-1
@@ -1,2 +1,5 @@
|
||||
#!/bin/sh
|
||||
exec python3 "$(dirname "$0")/run.py"
|
||||
# The static gate audit runs first: it is the invariant the program exists for.
|
||||
dir=$(dirname "$0")
|
||||
python3 "$dir/gate_audit.py" || exit 1
|
||||
exec python3 "$dir/run.py"
|
||||
Reference in new issue
Block a user