apex-twin 0.4.0: findings get a lifecycle

A finding was a file that could only be created. It now starts 'open' and
finding_update moves it to confirmed, false-positive or fixed, changes severity,
confidence, remediation or tags, and records why in a note.

- one writer for the format (finding_json), shared by add and update, so a field
  cannot exist on one path and not the other;
- update reads through the validated finding_read path, changes only the fields
  that were passed, writes a temp file beside the old one and renames it: a
  reader sees one version or the other, never half of either;
- status and severity are validated and stored canonical (FIXED -> fixed);
  an unknown id is -1, a bad value -2, and the tool handler says which;
- report_markdown prints Status and Updated per finding, adds a lifecycle table,
  and filters by status; CSV carries status and updated columns.

Why it matters: 'confirmed findings only' is now an artefact, not a promise.
114 checks became 145; the status filter was seen red (disabled -> five checks
failed because filtered reports contained the full set). A missing .tmp file is
asserted after every update.
This commit is contained in:
lotherk committed 2026-11-21 06:45:14 +00:00
1 parent f5bbefc6aa
commit 854642559b
7 files changed
+469 -61

No files matched your search

+26 -10
View File
@@ -18,7 +18,7 @@ separate, explicitly armed switch.
```sh
make # apex-twin, ~170 kB static-ish binary, zero warnings under -Werror
make test # 116 end-to-end checks, no mocks
make test # 145 end-to-end checks, no mocks
make install # /usr/local/bin/apex-twin
```
@@ -93,6 +93,18 @@ It is a speed bump with an audit trail, not a sandbox. The tokenizer does not
expand variables or follow an interpreter, so a line can hide its targets; when
it sees none it says so. Use it on commands a human can read.
## Findings have a lifecycle
A finding starts `open`. `finding_update` moves it to `confirmed`,
`false-positive` or `fixed`, changes severity, confidence, remediation or tags,
and records a note on why. The file is written beside the old one and renamed,
so a reader sees the old version or the new one and never half of either, and
the update is audited like every other decision.
`report_markdown` prints the status of each finding, counts them in a lifecycle
table, and filters by status — which is what turns "confirmed findings only"
from a promise into an artefact you can send.
## Tools (32)
**Scope and bookkeeping** — `scope_show`, `scope_check` (ask the gate without
@@ -115,9 +127,11 @@ reflection context), `curl`
**Method** — `method_techniques`, `method_playbook`, `method_tool`
**Findings** — `finding_add`, `finding_list`, `finding_read`, `report_markdown`
(Markdown or CSV, severity-sorted, written to `$APEX_HOME/reports`),
`evidence_list`, `evidence_read` (text, or base64 when the bytes are not text)
**Findings** — `finding_add`, `finding_list`, `finding_read`, `finding_update`
(status open/confirmed/false-positive/fixed, severity, note), `report_markdown`
(Markdown or CSV, severity-sorted, filterable by target/severity/status, written
to `$APEX_HOME/reports`), `evidence_list`, `evidence_read` (text, or base64 when
the bytes are not text)
Method is not decoration. `method_playbook` returns the ordered steps for a
technique, the check that turns a lead into a finding, and the mistake that wastes
@@ -187,7 +201,7 @@ finding and the next report no longer mentions it.
make test
```
116 checks, real binary, real DNS, real files written; no mocks. The suite was
145 checks, real binary, real DNS, real files written; no mocks. The suite was
seen red on purpose: with the "no scope" branch disabled by `if (0 && ...)`,
three checks fail, because they assert the *reason* and not merely that an error
came back — a gate that falls through to the next deny branch looks identical
@@ -195,7 +209,9 @@ from `isError` alone. New guarantees are made to fail the same way: the report
JSON was missing its closing brace once (the suite refused to parse it), with the
slash guard in `evidence_read` disabled `sub/../<blob>` was returned, and with
the `rm` rule disabled the guard fell through to the scope and reported the URL
instead of the delete — the precedence check went red.
instead of the delete — the precedence check went red. Disabling the report's
status filter turned five checks red: every filtered report contained the
unfiltered set.
A port is never assumed: an earlier version of the HTTP test used 18797, which on
this host is an SSH forward to another machine, and a foreign server answered
@@ -203,9 +219,9 @@ this host is an SSH forward to another machine, and a foreign server answered
## Status
0.3.0. Built and run: 32 tools, 116 checks. The loop closes: a probe saves raw
evidence, a finding points at it, `report_markdown` turns the findings into one
Markdown or CSV file, and `evidence_read` gets the proof back. Not yet wired
into a fleet router.
0.4.0. Built and run: 33 tools, 145 checks. The loop closes: a probe saves raw
evidence, a finding points at it, `finding_update` gives it a state,
`report_markdown` turns the findings into one Markdown or CSV file, and
`evidence_read` gets the proof back. Not yet wired into a fleet router.
Licence: AGPL-3.0, same as ARTEX.
+3 -4
View File
@@ -16,7 +16,8 @@ module, deliberately left out, or still open.
| RoE / target scope (`intercept`, `server/target_gate*`) | `src/scope.c` | Ordered rules, deny-beats-allow, DNS-rebinding rule, expiry, `allow_private`, `serial_tx` |
| Guard audit trail (`guard/guard.go` audit ring) | `src/store.c` `apex_audit()` | Append-only JSON line written *before* the action |
| Command guard (`guard/guard.go` deny rules) | `src/guard.c`, `guard_check` | Deny rules first, then the scope of every target in the line; no execution |
| Findings (`server/findings*.go`, `db`) | `src/store.c` findings + `finding_*` | One JSON file per finding, no schema, no migration |
| Findings (`server/findings*.go`, `db`) | `src/store.c` findings + `finding_*` | One JSON file per finding, status lifecycle, atomic update |
| Finding lifecycle (`finding_workflow.go`, `finding_verification.go`) | `finding_update`, report status filter | open/confirmed/false-positive/fixed plus a note; no retest runner |
| Evidence (`evidence/store.go`) | `evidence/<ts>-<label>.bin` + `evidence_save`/`_list`/`_read` | 0600 blobs, base64 on read when not text |
| Report export (`report/findings.go`) | `tools_report.c` `report_markdown` | Markdown and CSV, severity-sorted, severity table |
| Budgets / timeouts (`agent/budget.go`) | `APEX_DEFAULT_TIMEOUT_MS`, `APEX_MAX_TIMEOUT_MS` | Per-tool timeout, process-group kill |
@@ -42,9 +43,7 @@ module, deliberately left out, or still open.
pieces (HTTP parser, evidence store, audit) but not the proxy loop.
2. **Asset inventory** (`server/assets*.go`): one record per host with the
services seen; findings currently carry a target string only.
3. **Finding lifecycle** (`finding_workflow.go`, `finding_verification.go`):
status pending/confirmed/false-positive, retest pointers.
4. **Notifications** (`notify/`): webhook on a critical finding.
3. **Notifications** (`notify/`): webhook on a critical finding.
The rule for every item: it may observe or prove, it may not add a shell, and it
must call `gated()`/`apex_scope_check()` before it opens a socket or spawns a
+13 -1
View File
@@ -15,7 +15,7 @@
#include "mcp.h"
#define APEX_VERSION "0.3.0"
#define APEX_VERSION "0.4.0"
#define APEX_MAX_OUT (1u << 20)
#define APEX_PATH_MAX 512
#define APEX_PATH_FULL 1024
@@ -112,15 +112,18 @@ char *apex_strdup(const char *s);
typedef struct {
char id[16];
char ts[40];
char updated[40]; /* empty until the first update */
char target[256];
char title[512];
char severity[16];
char confidence[16];
char status[24]; /* open | confirmed | false-positive | fixed */
char tool[64];
char cwe[32];
char remediation[1024];
char tags[256];
char evidence[512];
char note[1024]; /* why the status changed, latest only */
} apex_finding;
void apex_audit(const char *tool, const char *target, int port, int allowed,
@@ -128,6 +131,15 @@ void apex_audit(const char *tool, const char *target, int port, int allowed,
char *apex_finding_add(const char *target, const char *title, const char *severity,
const char *confidence, const char *tool, const char *evidence,
const char *cwe, const char *remediation, const char *tags);
/*
* Change one finding in place. NULL or "" leaves a field alone; status and
* severity are validated. Returns 0 on success, -1 when the finding does not
* exist, -2 when a value is not accepted.
*/
int apex_finding_update(const char *id, const char *status, const char *severity,
const char *confidence, const char *remediation,
const char *tags, const char *note);
int apex_finding_status_valid(const char *status);
int apex_finding_scan(apex_finding *out, int max);
int apex_finding_list(mcp_buf *b, const char *target_filter, int limit);
int apex_finding_read(const char *id, mcp_buf *b);
+221 -36
View File
@@ -18,6 +18,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
@@ -100,6 +101,70 @@ static int next_finding_id(void)
return -1;
}
static void field_copy(char *dst, size_t n, const char *src)
{
size_t len;
if (dst == NULL || n == 0)
return;
if (src == NULL) {
dst[0] = '\0';
return;
}
len = strlen(src);
if (len >= n)
len = n - 1;
memcpy(dst, src, len);
dst[len] = '\0';
}
/* The one writer of the finding format. add and update both go through it, so
* a field can never exist in one path and not the other. */
static void finding_json(mcp_buf *b, const apex_finding *f)
{
mcp_buf_puts(b, "{\"id\":");
apex_json_str(b, f->id);
mcp_buf_puts(b, ",\"ts\":");
apex_json_str(b, f->ts[0] != '\0' ? f->ts : "?");
mcp_buf_puts(b, ",\"updated\":");
apex_json_str(b, f->updated);
mcp_buf_puts(b, ",\"target\":");
apex_json_str(b, f->target);
mcp_buf_puts(b, ",\"title\":");
apex_json_str(b, f->title);
mcp_buf_puts(b, ",\"severity\":");
apex_json_str(b, f->severity[0] != '\0' ? f->severity : "info");
mcp_buf_puts(b, ",\"confidence\":");
apex_json_str(b, f->confidence[0] != '\0' ? f->confidence : "firm");
mcp_buf_puts(b, ",\"status\":");
apex_json_str(b, f->status[0] != '\0' ? f->status : "open");
mcp_buf_puts(b, ",\"tool\":");
apex_json_str(b, f->tool);
mcp_buf_puts(b, ",\"cwe\":");
apex_json_str(b, f->cwe);
mcp_buf_puts(b, ",\"remediation\":");
apex_json_str(b, f->remediation);
mcp_buf_puts(b, ",\"tags\":");
apex_json_str(b, f->tags);
mcp_buf_puts(b, ",\"evidence\":");
apex_json_str(b, f->evidence);
mcp_buf_puts(b, ",\"note\":");
apex_json_str(b, f->note);
mcp_buf_puts(b, "}\n");
}
static int finding_store(const apex_finding *f, const char *path)
{
mcp_buf b;
int rc;
mcp_buf_init(&b);
finding_json(&b, f);
rc = apex_write_file(path, b.p, b.len, 0600);
mcp_buf_free(&b);
return rc;
}
char *apex_finding_add(const char *target, const char *title, const char *severity,
const char *confidence, const char *tool, const char *evidence,
const char *cwe, const char *remediation, const char *tags)
@@ -107,50 +172,37 @@ char *apex_finding_add(const char *target, const char *title, const char *severi
int id = next_finding_id();
char path[APEX_PATH_FULL];
char fid[16];
char fname[32];
char *ts;
mcp_buf b;
apex_finding f;
if (id < 0)
return NULL;
snprintf(fid, sizeof(fid), "f%04d", id);
{
char fname[32];
snprintf(fname, sizeof(fname), "%s.json", fid);
if (apex_path_join(path, sizeof(path), g_cfg.finding_dir, fname) != 0)
return NULL;
}
snprintf(fname, sizeof(fname), "%s.json", fid);
if (apex_path_join(path, sizeof(path), g_cfg.finding_dir, fname) != 0)
return NULL;
ts = apex_now_iso();
mcp_buf_init(&b);
mcp_buf_printf(&b, "{\"id\":\"%s\",\"ts\":\"%s\",\"target\":", fid,
ts != NULL ? ts : "?");
apex_json_str(&b, target);
mcp_buf_puts(&b, ",\"title\":");
apex_json_str(&b, title);
mcp_buf_puts(&b, ",\"severity\":");
apex_json_str(&b, severity != NULL ? severity : "info");
mcp_buf_puts(&b, ",\"confidence\":");
apex_json_str(&b, confidence != NULL ? confidence : "firm");
mcp_buf_puts(&b, ",\"tool\":");
apex_json_str(&b, tool);
mcp_buf_puts(&b, ",\"cwe\":");
apex_json_str(&b, cwe);
mcp_buf_puts(&b, ",\"remediation\":");
apex_json_str(&b, remediation);
mcp_buf_puts(&b, ",\"tags\":");
apex_json_str(&b, tags);
mcp_buf_puts(&b, ",\"evidence\":");
apex_json_str(&b, evidence);
mcp_buf_puts(&b, "}\n");
if (apex_write_file(path, b.p, b.len, 0600) != 0) {
mcp_buf_free(&b);
free(ts);
return NULL;
}
mcp_buf_free(&b);
memset(&f, 0, sizeof(f));
field_copy(f.id, sizeof(f.id), fid);
field_copy(f.ts, sizeof(f.ts), ts != NULL ? ts : "?");
field_copy(f.target, sizeof(f.target), target);
field_copy(f.title, sizeof(f.title), title);
field_copy(f.severity, sizeof(f.severity),
severity != NULL && *severity != '\0' ? severity : "info");
field_copy(f.confidence, sizeof(f.confidence),
confidence != NULL && *confidence != '\0' ? confidence : "firm");
field_copy(f.status, sizeof(f.status), "open");
field_copy(f.tool, sizeof(f.tool), tool);
field_copy(f.cwe, sizeof(f.cwe), cwe);
field_copy(f.remediation, sizeof(f.remediation), remediation);
field_copy(f.tags, sizeof(f.tags), tags);
field_copy(f.evidence, sizeof(f.evidence), evidence);
free(ts);
if (finding_store(&f, path) != 0)
return NULL;
apex_audit("finding_add", target, 0, 1, fid, NULL);
return apex_strdup(fid);
}
@@ -460,16 +512,21 @@ int apex_finding_scan(apex_finding *out, int max)
memset(dst, 0, sizeof(*dst));
simple_field(text, "id", dst->id, sizeof(dst->id));
simple_field(text, "ts", dst->ts, sizeof(dst->ts));
simple_field(text, "updated", dst->updated, sizeof(dst->updated));
simple_field(text, "target", dst->target, sizeof(dst->target));
simple_field(text, "title", dst->title, sizeof(dst->title));
simple_field(text, "severity", dst->severity, sizeof(dst->severity));
simple_field(text, "confidence", dst->confidence, sizeof(dst->confidence));
simple_field(text, "status", dst->status, sizeof(dst->status));
simple_field(text, "tool", dst->tool, sizeof(dst->tool));
simple_field(text, "cwe", dst->cwe, sizeof(dst->cwe));
simple_field(text, "remediation", dst->remediation,
sizeof(dst->remediation));
simple_field(text, "tags", dst->tags, sizeof(dst->tags));
simple_field(text, "evidence", dst->evidence, sizeof(dst->evidence));
simple_field(text, "note", dst->note, sizeof(dst->note));
if (dst->status[0] == '\0')
snprintf(dst->status, sizeof(dst->status), "open");
free(text);
count++;
}
@@ -601,6 +658,134 @@ int apex_finding_read(const char *id, mcp_buf *b)
return 0;
}
static const char *canon_status(const char *status)
{
static const char *states[] = { "open", "confirmed", "false-positive",
"fixed", NULL };
int i;
if (status == NULL)
return NULL;
for (i = 0; states[i] != NULL; i++)
if (strcasecmp(status, states[i]) == 0)
return states[i];
return NULL;
}
int apex_finding_status_valid(const char *status)
{
return canon_status(status) != NULL;
}
static const char *canon_severity(const char *severity)
{
static const char *levels[] = { "info", "low", "medium", "high",
"critical", NULL };
int i;
if (severity == NULL)
return NULL;
for (i = 0; levels[i] != NULL; i++)
if (strcasecmp(severity, levels[i]) == 0)
return levels[i];
return NULL;
}
/* Read one finding through the same validated path as finding_read, then pull
* the fields out of it. */
static int load_finding(const char *id, apex_finding *out)
{
mcp_buf b;
int rc;
if (id == NULL || *id == '\0')
return -1;
mcp_buf_init(&b);
rc = apex_finding_read(id, &b);
if (rc != 0) {
mcp_buf_free(&b);
return -1;
}
memset(out, 0, sizeof(*out));
simple_field(b.p, "id", out->id, sizeof(out->id));
simple_field(b.p, "ts", out->ts, sizeof(out->ts));
simple_field(b.p, "updated", out->updated, sizeof(out->updated));
simple_field(b.p, "target", out->target, sizeof(out->target));
simple_field(b.p, "title", out->title, sizeof(out->title));
simple_field(b.p, "severity", out->severity, sizeof(out->severity));
simple_field(b.p, "confidence", out->confidence, sizeof(out->confidence));
simple_field(b.p, "status", out->status, sizeof(out->status));
simple_field(b.p, "tool", out->tool, sizeof(out->tool));
simple_field(b.p, "cwe", out->cwe, sizeof(out->cwe));
simple_field(b.p, "remediation", out->remediation, sizeof(out->remediation));
simple_field(b.p, "tags", out->tags, sizeof(out->tags));
simple_field(b.p, "evidence", out->evidence, sizeof(out->evidence));
simple_field(b.p, "note", out->note, sizeof(out->note));
mcp_buf_free(&b);
if (out->id[0] == '\0')
field_copy(out->id, sizeof(out->id), id);
if (out->status[0] == '\0')
field_copy(out->status, sizeof(out->status), "open");
return 0;
}
int apex_finding_update(const char *id, const char *status, const char *severity,
const char *confidence, const char *remediation,
const char *tags, const char *note)
{
apex_finding f;
char path[APEX_PATH_FULL];
char tmp[APEX_PATH_FULL];
char fname[64];
char tname[128];
const char *cs;
const char *cv;
char *now;
cs = status != NULL && *status != '\0' ? canon_status(status) : NULL;
if (status != NULL && *status != '\0' && cs == NULL)
return -2;
cv = severity != NULL && *severity != '\0' ? canon_severity(severity) : NULL;
if (severity != NULL && *severity != '\0' && cv == NULL)
return -2;
if (load_finding(id, &f) != 0)
return -1;
if (cs != NULL)
field_copy(f.status, sizeof(f.status), cs);
if (cv != NULL)
field_copy(f.severity, sizeof(f.severity), cv);
if (confidence != NULL && *confidence != '\0')
field_copy(f.confidence, sizeof(f.confidence), confidence);
if (remediation != NULL)
field_copy(f.remediation, sizeof(f.remediation), remediation);
if (tags != NULL)
field_copy(f.tags, sizeof(f.tags), tags);
if (note != NULL)
field_copy(f.note, sizeof(f.note), note);
now = apex_now_iso();
field_copy(f.updated, sizeof(f.updated), now != NULL ? now : "?");
free(now);
snprintf(fname, sizeof(fname), "%s.json", f.id);
snprintf(tname, sizeof(tname), "%s.tmp.%ld", fname, (long)getpid());
if (apex_path_join(path, sizeof(path), g_cfg.finding_dir, fname) != 0)
return -1;
if (apex_path_join(tmp, sizeof(tmp), g_cfg.finding_dir, tname) != 0)
return -1;
if (finding_store(&f, tmp) != 0)
return -1;
/* write beside the target and rename: a reader sees the old file or the
* new one, never half of either */
if (rename(tmp, path) != 0) {
unlink(tmp);
return -1;
}
apex_audit("finding_update", f.target, 0, 1,
cs != NULL ? cs : "updated", NULL);
return 0;
}
int apex_audit_tail(mcp_buf *b, int limit)
{
FILE *f;
+65
View File
@@ -798,6 +798,51 @@ static void t_finding_read(mcp_conn *c, const mcp_json *args, void *user)
mcp_buf_free(&b);
}
static void t_finding_update(mcp_conn *c, const mcp_json *args, void *user)
{
const char *id = mcp_arg_string(args, "id");
const char *status = mcp_arg_string(args, "status");
const char *severity = mcp_arg_string(args, "severity");
const char *confidence = mcp_arg_string(args, "confidence");
const char *remediation = mcp_arg_string(args, "remediation");
const char *tags = mcp_arg_string(args, "tags");
const char *note = mcp_arg_string(args, "note");
mcp_buf b;
int rc;
(void)user;
if (id == NULL || *id == '\0') {
mcp_reply_error(c, "id is required");
return;
}
if ((status == NULL || *status == '\0') &&
(severity == NULL || *severity == '\0') &&
(confidence == NULL || *confidence == '\0') &&
remediation == NULL && tags == NULL && note == NULL) {
mcp_reply_error(c, "nothing to update: pass at least one field");
return;
}
rc = apex_finding_update(id, status, severity, confidence, remediation, tags,
note);
if (rc == -1) {
mcp_reply_error(c, "no such finding");
return;
}
if (rc == -2) {
mcp_reply_error(c, "status must be open|confirmed|false-positive|fixed; "
"severity info|low|medium|high|critical");
return;
}
mcp_buf_init(&b);
if (apex_finding_read(id, &b) != 0) {
mcp_buf_free(&b);
mcp_reply_error(c, "the finding was updated but cannot be read back");
return;
}
mcp_reply_json(c, b.p);
mcp_buf_free(&b);
}
static void t_capabilities(mcp_conn *c, const mcp_json *args, void *user)
{
static const char *progs[] = { "nmap", "dig", "host", "openssl", "curl",
@@ -947,6 +992,26 @@ const mcp_tool TOOLS_RECON[] = {
"\"id\":{\"type\":\"string\"}},"
"\"required\":[\"id\"]}",
t_finding_read },
{ "finding_update",
"Change the lifecycle of a finding without rewriting the file by hand: "
"status (open, confirmed, false-positive, fixed), severity, confidence, "
"remediation, tags or a note. Only the fields you pass change; the file is "
"rewritten atomically and the update is audited. Use it to promote a lead "
"after a check, to mark a finding as a false positive, or to record a "
"retest result.",
"{\"type\":\"object\",\"properties\":{"
"\"id\":{\"type\":\"string\"},"
"\"status\":{\"type\":\"string\",\"enum\":[\"open\",\"confirmed\","
"\"false-positive\",\"fixed\"]},"
"\"severity\":{\"type\":\"string\",\"enum\":[\"info\",\"low\",\"medium\","
"\"high\",\"critical\"]},"
"\"confidence\":{\"type\":\"string\"},"
"\"remediation\":{\"type\":\"string\"},"
"\"tags\":{\"type\":\"string\"},"
"\"note\":{\"type\":\"string\",\"description\":\"why it changed, latest "
"only\"}},"
"\"required\":[\"id\"]}",
t_finding_update },
{ "evidence_save",
"Store raw bytes as evidence and get the path back. Use it for a full HTTP "
"response, a certificate dump or a scan output too long to paste into a "
+46 -7
View File
@@ -67,6 +67,11 @@ static const char *sev_canonical(const char *s, int *rank)
return "other";
}
static const char *status_of(const apex_finding *f)
{
return f->status[0] != '\0' ? f->status : "open";
}
static int cmp_finding(const void *a, const void *b)
{
const apex_finding *x = a;
@@ -107,10 +112,14 @@ static void md_field(mcp_buf *b, const char *label, const char *value)
}
static void render_markdown(mcp_buf *b, const apex_finding *rows, int n,
const char *target, const char *min_sev)
const char *target, const char *min_sev,
const char *status)
{
static const char *order[] = { "critical", "high", "medium", "low", "info" };
static const char *states[] = { "open", "confirmed", "false-positive",
"fixed" };
int counts[5] = { 0, 0, 0, 0, 0 };
int scounts[4] = { 0, 0, 0, 0 };
int other = 0;
char *now = apex_now_iso();
int i;
@@ -118,6 +127,7 @@ static void render_markdown(mcp_buf *b, const apex_finding *rows, int n,
for (i = 0; i < n; i++) {
int r;
const char *c = sev_canonical(rows[i].severity, &r);
const char *st = status_of(&rows[i]);
if (strcmp(c, "critical") == 0)
counts[0]++;
@@ -131,6 +141,14 @@ static void render_markdown(mcp_buf *b, const apex_finding *rows, int n,
counts[4]++;
else
other++;
if (strcmp(st, "confirmed") == 0)
scounts[1]++;
else if (strcmp(st, "false-positive") == 0)
scounts[2]++;
else if (strcmp(st, "fixed") == 0)
scounts[3]++;
else
scounts[0]++;
}
mcp_buf_puts(b, "# APEX TWIN findings report\n\n");
@@ -142,11 +160,16 @@ static void render_markdown(mcp_buf *b, const apex_finding *rows, int n,
mcp_buf_printf(b, "- **Target filter**: %s\n", target);
if (min_sev != NULL && *min_sev != '\0')
mcp_buf_printf(b, "- **Minimum severity**: %s\n", min_sev);
if (status != NULL && *status != '\0')
mcp_buf_printf(b, "- **Status filter**: %s\n", status);
mcp_buf_puts(b, "\n## Summary\n\n| Severity | Count |\n| --- | --- |\n");
for (i = 0; i < 5; i++)
mcp_buf_printf(b, "| %s | %d |\n", order[i], counts[i]);
if (other > 0)
mcp_buf_printf(b, "| other | %d |\n", other);
mcp_buf_puts(b, "\n| Status | Count |\n| --- | --- |\n");
for (i = 0; i < 4; i++)
mcp_buf_printf(b, "| %s | %d |\n", states[i], scounts[i]);
mcp_buf_puts(b, "\n");
if (n == 0) {
@@ -165,12 +188,15 @@ static void render_markdown(mcp_buf *b, const apex_finding *rows, int n,
md_field(b, "ID", f->id);
md_field(b, "Target", f->target);
md_field(b, "Found", f->ts);
md_field(b, "Updated", f->updated);
md_field(b, "Status", status_of(f));
md_field(b, "Confidence", f->confidence);
md_field(b, "Tool", f->tool);
md_field(b, "CWE", f->cwe);
md_field(b, "Evidence", f->evidence);
md_field(b, "Remediation", f->remediation);
md_field(b, "Tags", f->tags);
md_field(b, "Note", f->note);
mcp_buf_puts(b, "\n");
}
free(now);
@@ -181,7 +207,7 @@ static void render_csv(mcp_buf *b, const apex_finding *rows, int n)
int i;
mcp_buf_puts(b, "id,target,title,severity,confidence,tool,cwe,evidence,"
"remediation,tags,ts\n");
"remediation,tags,status,updated,ts\n");
for (i = 0; i < n; i++) {
const apex_finding *f = &rows[i];
@@ -205,6 +231,10 @@ static void render_csv(mcp_buf *b, const apex_finding *rows, int n)
mcp_buf_puts(b, ",");
csv_field(b, f->tags);
mcp_buf_puts(b, ",");
csv_field(b, status_of(f));
mcp_buf_puts(b, ",");
csv_field(b, f->updated);
mcp_buf_puts(b, ",");
csv_field(b, f->ts);
mcp_buf_puts(b, "\n");
}
@@ -267,6 +297,7 @@ static void t_report_markdown(mcp_conn *c, const mcp_json *args, void *user)
{
const char *target = mcp_arg_string(args, "target");
const char *min_sev = mcp_arg_string(args, "min_severity");
const char *status = mcp_arg_string(args, "status");
const char *format = mcp_arg_string(args, "format");
int save = mcp_arg_bool(args, "save", 1);
int is_csv = format != NULL && strcasecmp(format, "csv") == 0;
@@ -297,6 +328,9 @@ static void t_report_markdown(mcp_conn *c, const mcp_json *args, void *user)
continue;
if (sev_rank(all[i].severity) < min_rank)
continue;
if (status != NULL && *status != '\0' &&
strcasecmp(status_of(&all[i]), status) != 0)
continue;
rows[kept++] = all[i];
}
qsort(rows, (size_t)kept, sizeof(*rows), cmp_finding);
@@ -307,7 +341,7 @@ static void t_report_markdown(mcp_conn *c, const mcp_json *args, void *user)
if (is_csv)
render_csv(&body, rows, kept);
else
render_markdown(&body, rows, kept, target, min_sev);
render_markdown(&body, rows, kept, target, min_sev, status);
}
path[0] = '\0';
@@ -396,16 +430,21 @@ static void t_evidence_read(mcp_conn *c, const mcp_json *args, void *user)
const mcp_tool TOOLS_REPORT[] = {
{ "report_markdown",
"Render every recorded finding into one report that a human can send: "
"Markdown (default) or CSV, sorted critical-first, with a severity table. "
"Use it at the end of an engagement, and after a batch of findings, to "
"produce the artefact. The file is written to $APEX_HOME/reports and its "
"path is returned together with the rendered text.",
"Markdown (default) or CSV, sorted critical-first, with a severity table "
"and a lifecycle table. Use it at the end of an engagement, and after a "
"batch of findings, to produce the artefact. The file is written to "
"$APEX_HOME/reports and its path is returned together with the rendered "
"text. Filter by target, minimum severity and status (open, confirmed, "
"false-positive, fixed).",
"{\"type\":\"object\",\"properties\":{"
"\"target\":{\"type\":\"string\",\"description\":\"only findings for this "
"exact target\"},"
"\"min_severity\":{\"type\":\"string\",\"enum\":[\"info\",\"low\",\"medium\","
"\"high\",\"critical\"],\"description\":\"drop anything below this; "
"default info\"},"
"\"status\":{\"type\":\"string\",\"enum\":[\"open\",\"confirmed\","
"\"false-positive\",\"fixed\"],\"description\":\"only findings in this "
"lifecycle state\"},"
"\"format\":{\"type\":\"string\",\"enum\":[\"markdown\",\"csv\"],"
"\"description\":\"default markdown\"},"
"\"save\":{\"type\":\"boolean\",\"description\":\"write under "
+95 -3
View File
@@ -99,10 +99,10 @@ res, _ = run([
], home=empty)
check("tools/list answers", any(r.get("id") == 1 for r in res))
tools = [r for r in res if r.get("id") == 1][0]["result"]["tools"]
check("32 tools registered", len(tools) == 32, len(tools))
check("33 tools registered", len(tools) == 33, len(tools))
names = {t["name"] for t in tools}
for need in ("portscan", "nmap", "http_request", "marauder_scan",
"method_playbook", "finding_add", "scope_check",
"method_playbook", "finding_add", "finding_update", "scope_check",
"report_markdown", "evidence_list", "evidence_read",
"guard_check"):
check("tool present: " + need, need in names)
@@ -342,6 +342,11 @@ res, _ = run([
call(4, "finding_read", id="f0001"),
call(5, "finding_add", target="198.51.100.5", title="out of scope"),
call(6, "evidence_save", label="binary", data="caf\u00e9tail"),
call(7, "finding_update", id="f0001", status="confirmed",
note="verified twice"),
call(8, "finding_read", id="f0001"),
call(9, "finding_update", id="f0001", status="bogus"),
call(10, "finding_update", id="f9999", status="confirmed"),
])
by_id = {r.get("id"): r for r in res}
ev = json.loads(text(by_id[1]))
@@ -355,6 +360,20 @@ read = json.loads(text(by_id[4]))
check("finding_read returns the finding", read["id"] == "f0001", read)
check("finding for an out-of-scope target is refused", err(by_id[5]),
text(by_id[5]))
upd = json.loads(text(by_id[7]))
check("finding_update returns the updated finding",
upd["id"] == "f0001" and upd["status"] == "confirmed", upd)
check("finding_update stores the note", upd["note"] == "verified twice", upd)
check("finding_update set the updated timestamp", upd["updated"] != "", upd)
read = json.loads(text(by_id[8]))
check("finding_read sees the new status", read["status"] == "confirmed", read)
check("finding_update refuses an unknown status", err(by_id[9]), text(by_id[9]))
check("finding_update names the accepted statuses",
"status must be" in text(by_id[9]), text(by_id[9]))
check("finding_update refuses an unknown id", err(by_id[10]), text(by_id[10]))
check("finding_update leaves no temp file behind",
not [p for p in os.listdir(os.path.join(ROOT, "findings")) if ".tmp" in p],
os.listdir(os.path.join(ROOT, "findings")))
print("\n== evidence round trip and report ==")
ev_name = os.path.basename(ev["evidence"])
@@ -370,6 +389,8 @@ res, _ = run([
call(7, "report_markdown", format="csv", target="example.com"),
call(8, "report_markdown", target="no.example"),
call(9, "report_markdown", min_severity="high", save=False),
call(10, "report_markdown", status="confirmed", save=False),
call(11, "report_markdown", status="open", save=False),
])
by_id = {r.get("id"): r for r in res}
listed_ev = json.loads(text(by_id[1]))
@@ -419,6 +440,77 @@ check("min_severity drops a lower finding", unsaved["findings"] == 0, unsaved)
check("save=false writes no file", unsaved["saved"] is False and
unsaved["path"] is None, unsaved)
conf = json.loads(text(by_id[10]))
check("report filters by status=confirmed", conf["findings"] == 1, conf)
check("report counts the confirmed status", "| confirmed | 1 |" in conf["report"],
conf["report"][:400])
opened = json.loads(text(by_id[11]))
check("report filters by status=open and finds none", opened["findings"] == 0,
opened)
print("\n== finding lifecycle ==")
res, _ = run([
call(1, "finding_add", target="example.com", title="Second, with comma",
severity="low"),
call(2, "finding_update", id="f0002", severity="critical", tags="retest"),
call(3, "finding_read", id="f0002"),
call(4, "report_markdown", save=False),
call(5, "report_markdown", status="confirmed", save=False),
call(6, "report_markdown", status="open", save=False),
call(7, "finding_update", id="f0001", status="false-positive",
note="not reproducible"),
call(8, "report_markdown", status="open", save=False),
call(9, "report_markdown", status="false-positive", save=False),
call(10, "report_markdown", format="csv", save=False),
call(11, "finding_update", id="f0001"),
call(12, "audit_log", limit=100),
call(13, "finding_update", id="f0002", status="FIXED"),
call(14, "finding_read", id="f0002"),
])
by_id = {r.get("id"): r for r in res}
f2 = json.loads(text(by_id[3]))
check("severity can be raised", f2["severity"] == "critical", f2)
check("tags can be replaced", f2["tags"] == "retest", f2)
check("a new finding starts open", f2["status"] == "open", f2)
allr = json.loads(text(by_id[4]))
check("report includes both findings", allr["findings"] == 2, allr)
check("report sorts the critical finding first",
"### 1. [critical] Second" in allr["report"], allr["report"][:500])
check("report counts both severities",
"| critical | 1 |" in allr["report"] and "| medium | 1 |" in allr["report"],
allr["report"][:500])
check("report counts both statuses",
"| open | 1 |" in allr["report"] and "| confirmed | 1 |" in allr["report"],
allr["report"][:600])
check("status filter confirmed selects one", json.loads(text(by_id[5]))["findings"] == 1,
json.loads(text(by_id[5])))
check("status filter open selects one", json.loads(text(by_id[6]))["findings"] == 1,
json.loads(text(by_id[6])))
check("a finding can be marked false-positive",
not err(by_id[7]), text(by_id[7]))
open_after = json.loads(text(by_id[8]))
check("false-positive is no longer open", open_after["findings"] == 1, open_after)
fp = json.loads(text(by_id[9]))
check("report filters false-positive", fp["findings"] == 1 and
"not reproducible" in fp["report"], fp["report"][:400])
csv2 = json.loads(text(by_id[10]))
hdr = csv2["report"].splitlines()[0]
check("csv carries the lifecycle columns",
hdr.startswith("id,target,title,severity") and ",status,updated,ts" in hdr,
hdr)
check("csv writes the false-positive status",
any('"false-positive"' in line for line in csv2["report"].splitlines()),
csv2["report"][:300])
check("finding_update with no field is refused", err(by_id[11]),
text(by_id[11]))
audit2 = json.loads(text(by_id[12]))
check("finding_update is audited",
any(e["tool"] == "finding_update" for e in audit2),
[e for e in audit2 if e["tool"] == "finding_update"][:2])
fixed = json.loads(text(by_id[14]))
check("a status is accepted in any case and stored canonical",
fixed["status"] == "fixed", fixed)
# --------------------------------------------------------------- transport
print("\n== http transport ==")
@@ -463,7 +555,7 @@ try:
check("http initialize", init["result"]["serverInfo"]["name"] == "apex-twin",
init)
listing = post({"jsonrpc": "2.0", "id": 2, "method": "tools/list"})
check("http tools/list", len(listing["result"]["tools"]) == 32)
check("http tools/list", len(listing["result"]["tools"]) == 33)
shown = post(call(3, "scope_show"))
check("http scope_show", '"loaded":true' in shown["result"]["content"][0]["text"])
# the id must be the request's id, not one found inside the arguments