apex-twin 0.26.0: deduplicate findings on add

finding_add is idempotent for the same target and title. apex_finding_find()
scans the store with case-insensitive comparison where whitespace runs are
collapsed and ends trimmed, so 'Dup Test', '  dUp   tEsT ' and 'dup test' are
one finding. The second call returns the existing id with duplicate:true,
writes no second file, leaves the stored fields alone (use finding_update) and
audits 'duplicate of <id>'. A different target or title creates a new finding.

Tests in a fresh state dir: first f0001, case/space variant same id + duplicate,
different title f0002, same title on 192.0.2.5 f0003, three files, stored
severity still low, JSON report total 3, duplicate in audit. Red proof:
returning NULL from the lookup turns six checks red.

317 checks, coverage 77.0%, release-check green including distcheck.
This commit is contained in:
lotherk committed 2026-11-21 09:03:24 +00:00
1 parent 0309e04011
commit 97c79a7f1f
6 files changed
+141 -8

No files matched your search

+11 -1
View File
@@ -8,7 +8,17 @@ fields, and state from an older version is read by a newer one.
There are no git tags. The hash next to a version is the commit that introduced
it; the current entry is the commit that adds this section.
## 0.25.0 — 2026-11-21 — this change
## 0.26.0 — 2026-11-21 — this change
- `finding_add` deduplicates: the same target and title (case-insensitive,
whitespace runs collapsed, ends trimmed) returns the existing id with
`duplicate: true`, writes no second file, does not touch the stored fields,
and audits `duplicate of <id>`. A different target or title is a new finding.
- Tests: case/space variant returns f0001, different title gives f0002, same
title on another target gives f0003, report counts three, severity stays
`low`, duplicate audited. Red proof: disabling the lookup turns six checks red.
## 0.25.0 — 2026-11-21 — 0309e04
- Bound concurrent proxy children with `APEX_PROXY_MAX_CHILDREN` (default 64,
max 1024). SIGCHLD now sets a flag and the parent reaps with
+11 -5
View File
@@ -30,7 +30,7 @@ separate, explicitly armed switch.
```sh
make # apex-twin, ~239 kB static-ish binary, zero warnings under -Werror
make test # gate audit + 309 end-to-end checks, no mocks
make test # gate audit + 317 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
@@ -131,6 +131,12 @@ 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.
`finding_add` is idempotent for the same target and title: the second call
(case-insensitive, whitespace runs collapsed) returns the existing id with
`duplicate: true` instead of writing a second file, and the duplicate is
audited. The stored severity is not overwritten — use `finding_update` for
that. A different target or a different title is a new finding.
`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. `format: "json"` writes the same
@@ -364,7 +370,7 @@ finding and the next report no longer mentions it.
make test
```
309 checks, real binary, real DNS, real files written; no mocks. The suite was
317 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
@@ -395,7 +401,7 @@ this host is an SSH forward to another machine, and a foreign server answered
`{"serverInfo":{"name":"koder"}}` while the suite was green.
The suite is also run against `apex-twin-asan` (`make test-asan`), built with
`-fsanitize=address,undefined`. All 309 checks pass with no ASan or UBSan report,
`-fsanitize=address,undefined`. All 317 checks pass with no ASan or UBSan report,
and a separate batch of error paths, network captures and report generation was
run with leak detection on: no leaks. `make analyze` runs `gcc -fanalyzer` over
every translation unit; it was red first on `vendor/libmcp/mcp.h`, where
@@ -454,7 +460,7 @@ example.com checks; `make test` is the fast loop.
`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%** —
The first measured pass was 55.9%; the gap tests brought it to **77.0%** —
`tools_kb.c` 100%, `tools_traffic.c` 81.8%, `tools_web.c` 71.6%,
`tools_recon.c` 68.0%. A PTY-based Marauder simulator then raised
`tools_radio.c` from 31.7% to 76.2% without hardware: firmware `info`/`help`,
@@ -466,7 +472,7 @@ the copied scope reason is now bounded with a precision, not left to snprintf.
## Status
0.25.0. Built and run: 45 tools, 309 checks, 340 fuzz cases and 76.7% line
0.26.0. Built and run: 45 tools, 317 checks, 340 fuzz cases and 77.0% 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
+4 -1
View File
@@ -15,7 +15,7 @@
#include "mcp.h"
#define APEX_VERSION "0.25.0"
#define APEX_VERSION "0.26.0"
#define APEX_MAX_OUT (1u << 20)
#define APEX_PATH_MAX 512
#define APEX_PATH_FULL 1024
@@ -147,6 +147,9 @@ int apex_finding_update(const char *id, const char *status, const char *severity
const char *tags, const char *note);
int apex_finding_status_valid(const char *status);
int apex_finding_scan(apex_finding *out, int max);
/* Find a finding by target and title, both trimmed and case-insensitive.
* Returns a malloc'd id or NULL. */
char *apex_finding_find(const char *target, const char *title);
/* Load one finding by id. 0 = found, -1 = not found. */
int apex_finding_get(const char *id, apex_finding *out);
int apex_finding_list(mcp_buf *b, const char *target_filter, int limit);
+45
View File
@@ -12,6 +12,7 @@
* evidence file, and every proof line carries where it came from, so a report
* is a directory listing, not a memory of what happened.
*/
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
@@ -547,6 +548,50 @@ int apex_finding_scan(apex_finding *out, int max)
return count;
}
/* Case-insensitive comparison with whitespace runs treated as one space. */
static int same_text(const char *a, const char *b)
{
const char *pa = a != NULL ? a : "";
const char *pb = b != NULL ? b : "";
for (;;) {
while (*pa != '\0' && isspace((unsigned char)*pa))
pa++;
while (*pb != '\0' && isspace((unsigned char)*pb))
pb++;
if (*pa == '\0' || *pb == '\0')
return *pa == '\0' && *pb == '\0';
if (tolower((unsigned char)*pa) != tolower((unsigned char)*pb))
return 0;
pa++;
pb++;
}
}
char *apex_finding_find(const char *target, const char *title)
{
apex_finding *all;
char *hit = NULL;
int n;
int i;
if (target == NULL || title == NULL)
return NULL;
all = calloc(1024, sizeof(*all));
if (all == NULL)
return NULL;
n = apex_finding_scan(all, 1024);
for (i = 0; i < n; i++) {
if (same_text(target, all[i].target) &&
same_text(title, all[i].title)) {
hit = apex_strdup(all[i].id);
break;
}
}
free(all);
return hit;
}
int apex_finding_list(mcp_buf *b, const char *target_filter, int limit)
{
DIR *d;
+21 -1
View File
@@ -749,6 +749,26 @@ static void t_finding_add(mcp_conn *c, const mcp_json *args, void *user)
/* a finding about a target is a statement about a target: same gate */
if (!gated(c, "finding_add", target, 0))
return;
{
char *dup = apex_finding_find(target, title);
if (dup != NULL) {
char reason[64];
snprintf(reason, sizeof(reason), "duplicate of %s", dup);
apex_audit("finding_add", target, 0, 1, reason, NULL);
mcp_buf_init(&b);
mcp_buf_puts(&b, "{\"id\":");
apex_json_str(&b, dup);
mcp_buf_puts(&b, ",\"target\":");
apex_json_str(&b, target);
mcp_buf_puts(&b, ",\"duplicate\":true}");
mcp_reply_json(c, b.p);
mcp_buf_free(&b);
free(dup);
return;
}
}
id = apex_finding_add(target, title, severity, confidence, tool, evidence,
cwe, remediation, tags);
if (id == NULL) {
@@ -760,7 +780,7 @@ static void t_finding_add(mcp_conn *c, const mcp_json *args, void *user)
apex_json_str(&b, id);
mcp_buf_puts(&b, ",\"target\":");
apex_json_str(&b, target);
mcp_buf_puts(&b, "}");
mcp_buf_puts(&b, ",\"duplicate\":false}");
mcp_reply_json(c, b.p);
mcp_buf_free(&b);
(void)apex_notify_event("finding_added", id, target, title,
+49
View File
@@ -525,6 +525,55 @@ fixed = json.loads(text(by_id[14]))
check("a status is accepted in any case and stored canonical",
fixed["status"] == "fixed", fixed)
print("\n== finding dedup ==")
dedup_home = tempfile.mkdtemp(prefix="apex-dedup-")
write_scope(dedup_home, {"label": "dedup", "expires": "2099-12-31T23:59:59Z",
"allow_ip": ["192.0.2.0/24"],
"allow_domain": ["example.com"],
"allow_port": ["80"]})
res, _ = run([
call(1, "finding_add", target="example.com", title="Dup Test",
severity="low"),
call(2, "finding_add", target="example.com", title=" dUp tEsT ",
severity="high"),
call(3, "finding_add", target="example.com", title="Dup Test 2",
severity="low"),
call(4, "finding_add", target="192.0.2.5", title="Dup Test",
severity="low"),
call(5, "finding_list"),
call(6, "finding_read", id="f0001"),
call(7, "report_markdown", format="json", save=False),
call(8, "audit_log", limit=100),
], home=dedup_home)
by_id = {r.get("id"): r for r in res}
first = json.loads(text(by_id[1]))
second = json.loads(text(by_id[2]))
check("the first finding is stored", first["id"] == "f0001" and
first["duplicate"] is False, first)
check("a case/space variant is the same finding",
second["id"] == "f0001" and second["duplicate"] is True, second)
third = json.loads(text(by_id[3]))
check("a different title is a different finding",
third["id"] == "f0002" and third["duplicate"] is False, third)
fourth = json.loads(text(by_id[4]))
check("the same title on another target is a different finding",
fourth["id"] == "f0003" and fourth["duplicate"] is False, fourth)
listed = json.loads(text(by_id[5]))
check("only three finding files exist", len(listed) == 3, listed)
kept = json.loads(text(by_id[6]))
check("the duplicate attempt did not change the stored severity",
kept["severity"] == "low", kept)
report = json.loads(text(by_id[7]))
rdoc = json.loads(report["report"])
check("the report does not count the duplicate twice",
rdoc["counts"]["total"] == 3 and len(rdoc["findings"]) == 3,
rdoc["counts"])
audit = json.loads(text(by_id[8]))
check("the duplicate is audited",
any(e["tool"] == "finding_add" and "duplicate of f0001" in e["reason"]
for e in audit), [e for e in audit if e["tool"] == "finding_add"][:3])
shutil.rmtree(dedup_home, ignore_errors=True)
print("\n== asset inventory ==")
res, _ = run([
call(1, "asset_add", target="192.0.2.5", label="web", kind="ip",