Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

feat(foundation): per-project request manifest with content-keyed approval

Martin Vogel80d52797f61d
diff --git a/src/foundation/workspace.c b/src/foundation/workspace.c
index 43e1fad38..a85e17822 100644
--- a/src/foundation/workspace.c
+++ b/src/foundation/workspace.c
@@ -7,6 +7,7 @@
#include "foundation/compat.h"
#include "foundation/compat_fs.h"
#include "foundation/platform.h"
+#include "foundation/sha256.h"
#include <stdlib.h>
@@ -511,6 +512,18 @@ bool cbm_workspace_root_allowed(const char *canonical_path, const char *home_dir
* then applies to paths that ARE inside the declared root but are still too
* broad to index as one unit. */
bool boundary_declared = grants > 0 || configured;
+ /* No manifest consultation here, deliberately.
+ *
+ * A project's manifest authorizes outside roots FOR THAT PROJECT, so the
+ * question it answers is "may project P pull in tree T", not "may T be indexed
+ * standalone". This function only ever sees one path, so it has no project
+ * context to ask that question with — an earlier draft passed the candidate as
+ * its own project root, which read a manifest that by definition was not the
+ * one that requested it and so never authorized anything.
+ *
+ * The consuming half belongs where the project context exists: discovery
+ * walking a project's approved extra roots. cbm_workspace_manifest_allows is
+ * the query that half will use. */
if (boundary_declared && !match.contained && !configured_contains) {
if (err) {
/* Keep the "outside the allowed root" wording: changing it broke an
@@ -564,3 +577,214 @@ const char *cbm_workspace_home_dir(void) {
const char *cbm_workspace_cache_dir(void) {
return cbm_resolve_cache_dir();
}
+
+/* ── Per-project request manifest ─────────────────────────────────────────── */
+
+bool cbm_workspace_manifest_read(const char *project_root, cbm_ws_manifest_t *out) {
+ if (!out) {
+ return false;
+ }
+ memset(out, 0, sizeof(*out));
+ if (!project_root || !project_root[0]) {
+ return true; /* nothing to read is not a malformed manifest */
+ }
+
+ char path[WS_LINE_MAX];
+ int n = snprintf(path, sizeof(path), "%s/%s", project_root, CBM_WS_MANIFEST_NAME);
+ if (n <= 0 || (size_t)n >= sizeof(path)) {
+ return true;
+ }
+ FILE *f = cbm_fopen(path, "rb");
+ if (!f) {
+ return true;
+ }
+
+ /* Hash the raw bytes, not the parsed entries: a comment or ordering change is
+ * still a change the approver has not seen, and should lapse approval. */
+ char *raw = NULL;
+ size_t raw_len = 0;
+ char chunk[WS_LINE_MAX];
+ size_t got = 0;
+ while ((got = fread(chunk, 1, sizeof(chunk), f)) > 0) {
+ char *grown = realloc(raw, raw_len + got);
+ if (!grown) {
+ free(raw);
+ (void)fclose(f);
+ return false;
+ }
+ raw = grown;
+ memcpy(raw + raw_len, chunk, got);
+ raw_len += got;
+ }
+ (void)fclose(f);
+ out->present = true;
+ cbm_sha256_hex(raw ? raw : "", raw_len, out->digest);
+
+ /* Parse entries out of the same bytes. */
+ bool ok = true;
+ size_t line_start = 0;
+ for (size_t i = 0; i <= raw_len && ok; i++) {
+ bool eol = (i == raw_len) || raw[i] == '\n';
+ if (!eol) {
+ continue;
+ }
+ size_t len = i - line_start;
+ while (len > 0 && (raw[line_start + len - 1] == '\r' || raw[line_start + len - 1] == ' ')) {
+ len--;
+ }
+ if (len > 0 && raw[line_start] != '#') {
+ if (len >= sizeof(out->entries[0]) || out->count >= CBM_WS_MANIFEST_MAX_ENTRIES) {
+ ok = false;
+ break;
+ }
+ /* A control character in a path is never legitimate and is exactly how
+ * a crafted entry would try to smuggle a second value past a reader. */
+ for (size_t k = 0; k < len; k++) {
+ unsigned char c = (unsigned char)raw[line_start + k];
+ if (c < 0x20) {
+ ok = false;
+ break;
+ }
+ }
+ if (!ok) {
+ break;
+ }
+ memcpy(out->entries[out->count], raw + line_start, len);
+ out->entries[out->count][len] = '\0';
+ out->count++;
+ }
+ line_start = i + 1;
+ }
+ free(raw);
+ if (!ok) {
+ memset(out, 0, sizeof(*out));
+ }
+ return ok;
+}
+
+static bool ws_approval_path(const char *cache_dir, char *out, size_t out_sz) {
+ if (!cache_dir || !cache_dir[0]) {
+ return false;
+ }
+ int n = snprintf(out, out_sz, "%s/approved_manifests", cache_dir);
+ return n > 0 && (size_t)n < out_sz;
+}
+
+bool cbm_workspace_manifest_is_approved(const char *cache_dir, const char *project_root,
+ const cbm_ws_manifest_t *manifest) {
+ if (!manifest || !manifest->present || !manifest->digest[0] || !project_root) {
+ return false;
+ }
+ char store[WS_LINE_MAX];
+ if (!ws_approval_path(cache_dir, store, sizeof(store))) {
+ return false;
+ }
+ FILE *f = cbm_fopen(store, "r");
+ if (!f) {
+ return false;
+ }
+ char line[WS_LINE_MAX];
+ bool found = false;
+ while (!found && fgets(line, (int)sizeof(line), f)) {
+ size_t len = strlen(line);
+ while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r')) {
+ line[--len] = '\0';
+ }
+ char *sep = strchr(line, ' ');
+ if (!sep) {
+ continue;
+ }
+ *sep = '\0';
+ /* Both the digest and the project must match: an approval is for this
+ * content in this project, not for the content anywhere. */
+ found = strcmp(line, manifest->digest) == 0 && ws_paths_equal(sep + 1, project_root);
+ }
+ (void)fclose(f);
+ return found;
+}
+
+bool cbm_workspace_manifest_approve(const char *cache_dir, const char *home_dir,
+ const char *project_root, char *err, size_t err_sz) {
+ if (err && err_sz) {
+ err[0] = '\0';
+ }
+ cbm_ws_manifest_t m;
+ if (!cbm_workspace_manifest_read(project_root, &m)) {
+ if (err) {
+ snprintf(err, err_sz,
+ "%s is malformed (control characters, an over-long entry, or "
+ "more than %d entries)",
+ CBM_WS_MANIFEST_NAME, CBM_WS_MANIFEST_MAX_ENTRIES);
+ }
+ return false;
+ }
+ if (!m.present) {
+ if (err) {
+ snprintf(err, err_sz, "no %s in %s", CBM_WS_MANIFEST_NAME,
+ project_root ? project_root : "(none)");
+ }
+ return false;
+ }
+
+ /* Approving a manifest must not become a way around the breadth policy: every
+ * requested entry has to stand on its own as an indexing root. */
+ for (int i = 0; i < m.count; i++) {
+ cbm_ws_verdict_t v = cbm_workspace_classify_root(m.entries[i], home_dir, cache_dir);
+ if (v != CBM_WS_ALLOW) {
+ if (err) {
+ snprintf(err, err_sz, "requested path %s: %s", m.entries[i],
+ cbm_workspace_verdict_reason(v));
+ }
+ return false;
+ }
+ }
+
+ char store[WS_LINE_MAX];
+ if (!ws_approval_path(cache_dir, store, sizeof(store))) {
+ if (err) {
+ snprintf(err, err_sz, "cache path too long");
+ }
+ return false;
+ }
+ if (cbm_workspace_manifest_is_approved(cache_dir, project_root, &m)) {
+ return true;
+ }
+ FILE *f = cbm_fopen(store, "a");
+ if (!f) {
+ if (err) {
+ snprintf(err, err_sz, "cannot write %s", store);
+ }
+ return false;
+ }
+ (void)fprintf(f, "%s %s\n", m.digest, project_root);
+ bool ok = fclose(f) == 0;
+ if (!ok && err) {
+ snprintf(err, err_sz, "cannot write %s", store);
+ }
+ return ok;
+}
+
+bool cbm_workspace_manifest_allows(const char *cache_dir, const char *home_dir,
+ const char *project_root, const char *candidate) {
+ if (!candidate || !candidate[0]) {
+ return false;
+ }
+ cbm_ws_manifest_t m;
+ if (!cbm_workspace_manifest_read(project_root, &m) || !m.present) {
+ return false;
+ }
+ if (!cbm_workspace_manifest_is_approved(cache_dir, project_root, &m)) {
+ return false;
+ }
+ for (int i = 0; i < m.count; i++) {
+ /* Re-classify at use time as well as at approval time: the credential list
+ * may have grown since, and a stored approval must not outrank it. */
+ if (cbm_workspace_classify_root(m.entries[i], home_dir, cache_dir) != CBM_WS_ALLOW) {
+ continue;
+ }
+ if (cbm_path_within_root(m.entries[i], candidate)) {
+ return true;
+ }
+ }
+ return false;
+}
diff --git a/src/foundation/workspace.h b/src/foundation/workspace.h
index ef1a2e5ef..276079155 100644
--- a/src/foundation/workspace.h
+++ b/src/foundation/workspace.h
@@ -105,3 +105,52 @@ const char *cbm_workspace_home_dir(void);
const char *cbm_workspace_cache_dir(void);
#endif /* CBM_FOUNDATION_WORKSPACE_H */
+
+/*
+ * ── Per-project request manifest (.cbmpathwhitelist) ─────────────────────────
+ *
+ * A repository may ship a manifest listing outside roots it would like indexed
+ * alongside it. The file REQUESTS; it never GRANTS. Both attackers in this
+ * project's threat model can write a file inside a repository — a malicious
+ * indexed repo, and an agent with project write access — so a repo-local file
+ * cannot be authoritative without handing them the boundary.
+ *
+ * Approval is recorded user-level and keyed to a SHA-256 of the manifest bytes,
+ * so editing the file (a `git pull` that widens the requests, say) lapses the
+ * approval and asks again. Without the hash, a repo approved once could widen
+ * itself forever. This is direnv's model, and it is the only shape that keeps the
+ * ergonomics of a checked-in file without making the file a permission slip.
+ */
+
+#define CBM_WS_MANIFEST_NAME ".cbmpathwhitelist"
+
+enum { CBM_WS_MANIFEST_MAX_ENTRIES = 64 };
+
+typedef struct {
+ /* Requested roots, verbatim from the file (already control-char screened). */
+ char entries[CBM_WS_MANIFEST_MAX_ENTRIES][1024];
+ int count;
+ /* SHA-256 hex of the raw file bytes; empty when there is no manifest. */
+ char digest[65];
+ bool present;
+} cbm_ws_manifest_t;
+
+/* Read <project_root>/.cbmpathwhitelist. Returns false only on a malformed file
+ * (an entry containing control characters, or more entries than the cap); a
+ * missing file is success with present=false. */
+bool cbm_workspace_manifest_read(const char *project_root, cbm_ws_manifest_t *out);
+
+/* True when this exact manifest content has been approved for this project. A
+ * changed manifest is a different digest and so is not approved. */
+bool cbm_workspace_manifest_is_approved(const char *cache_dir, const char *project_root,
+ const cbm_ws_manifest_t *manifest);
+
+/* Record approval for the manifest currently on disk. Refuses when a requested
+ * entry would not be allowable as a root on its own — approving a manifest must
+ * not become a way around the breadth policy. */
+bool cbm_workspace_manifest_approve(const char *cache_dir, const char *home_dir,
+ const char *project_root, char *err, size_t err_sz);
+
+/* True when candidate is at or below an APPROVED manifest entry of project_root. */
+bool cbm_workspace_manifest_allows(const char *cache_dir, const char *home_dir,
+ const char *project_root, const char *candidate);
diff --git a/src/main.c b/src/main.c
index 8a0ceb646..04bbfbc72 100644
--- a/src/main.c
+++ b/src/main.c
@@ -933,11 +933,14 @@ static int main_run_allow_root(int argc, char **argv) {
const char *path = NULL;
bool approve_sensitive = false;
bool list_only = false;
+ bool approve_manifest = false;
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "--approve-sensitive") == 0) {
approve_sensitive = true;
} else if (strcmp(argv[i], "--list") == 0) {
list_only = true;
+ } else if (strcmp(argv[i], "--approve-manifest") == 0) {
+ approve_manifest = true;
} else if (argv[i][0] == '-') {
(void)fprintf(stderr, "error: unknown option: %s\n", argv[i]);
return EXIT_FAILURE;
@@ -966,12 +969,33 @@ static int main_run_allow_root(int argc, char **argv) {
if (!path && !list_only) {
(void)fprintf(stderr,
"usage: codebase-memory-mcp allow-root [--approve-sensitive] <path>\n"
+ " codebase-memory-mcp allow-root --approve-manifest <project>\n"
" codebase-memory-mcp allow-root --list\n");
return EXIT_FAILURE;
}
return 0;
}
+ if (approve_manifest) {
+ /* Approve the manifest a project ships, keyed to its current content. The
+ * file only ever requests; this is the human action that grants. */
+ char canonical_project[CBM_PATH_MAX];
+ if (!cbm_canonical_path(path, canonical_project, sizeof(canonical_project))) {
+ (void)fprintf(stderr, "error: cannot resolve path: %s\n", path);
+ return EXIT_FAILURE;
+ }
+ char merr[CBM_SZ_1K];
+ if (!cbm_workspace_manifest_approve(cache_dir, cbm_workspace_home_dir(), canonical_project,
+ merr, sizeof(merr))) {
+ (void)fprintf(stderr, "refused: %s\n", merr[0] ? merr : "manifest not approvable");
+ return EXIT_FAILURE;
+ }
+ printf("manifest approved for %s\n", canonical_project);
+ printf("note: editing %s lapses this approval and it must be granted again.\n",
+ CBM_WS_MANIFEST_NAME);
+ return 0;
+ }
+
/* Canonicalize before recording: the policy is defined over resolved paths,
* and a grant stored as a symlink would not match the resolved repo path the
* indexer later presents. */
diff --git a/tests/test_workspace.c b/tests/test_workspace.c
index c46f96866..736b4477f 100644
--- a/tests/test_workspace.c
+++ b/tests/test_workspace.c
@@ -6,7 +6,11 @@
*/
#include "../src/foundation/compat.h"
#include "test_framework.h"
+#include "test_helpers.h"
#include "foundation/workspace.h"
+#include "foundation/compat_fs.h"
+#include <stdio.h>
+#include <string.h>
static const char *HOME = "/Users/dev";
static const char *CACHE = "/Users/dev/.cache/codebase-memory-mcp";
@@ -155,7 +159,114 @@ TEST(ws_every_verdict_has_a_reason) {
PASS();
}
+/* ── Per-project request manifest ───────────────────────────────────────── */
+
+static void ws_write(const char *path, const char *body) {
+ FILE *f = cbm_fopen(path, "wb");
+ if (f) {
+ (void)fputs(body, f);
+ (void)fclose(f);
+ }
+}
+
+TEST(ws_manifest_absent_is_not_an_error) {
+ char *base = th_mktempdir("cbm_ws_m0");
+ ASSERT(base != NULL);
+ cbm_ws_manifest_t m;
+ ASSERT_TRUE(cbm_workspace_manifest_read(base, &m));
+ ASSERT_FALSE(m.present);
+ ASSERT_EQ(m.count, 0);
+ th_cleanup(base);
+ PASS();
+}
+
+TEST(ws_manifest_parses_entries_and_skips_comments) {
+ char *base = th_mktempdir("cbm_ws_m1");
+ ASSERT(base != NULL);
+ char path[1024];
+ snprintf(path, sizeof(path), "%s/%s", base, CBM_WS_MANIFEST_NAME);
+ ws_write(path, "# a comment\n/opt/sdk\n\n/srv/protos\n");
+ cbm_ws_manifest_t m;
+ ASSERT_TRUE(cbm_workspace_manifest_read(base, &m));
+ ASSERT_TRUE(m.present);
+ ASSERT_EQ(m.count, 2);
+ ASSERT_STR_EQ(m.entries[0], "/opt/sdk");
+ ASSERT_STR_EQ(m.entries[1], "/srv/protos");
+ ASSERT_EQ((int)strlen(m.digest), 64);
+ th_cleanup(base);
+ PASS();
+}
+
+/* A control character is how a crafted entry would smuggle a second value past a
+ * line reader — the same shape as the newline splitting in the scoped file list. */
+TEST(ws_manifest_rejects_control_characters) {
+ char *base = th_mktempdir("cbm_ws_m2");
+ ASSERT(base != NULL);
+ char path[1024];
+ snprintf(path, sizeof(path), "%s/%s", base, CBM_WS_MANIFEST_NAME);
+ ws_write(path, "/opt/sdk\t\x01evil\n");
+ cbm_ws_manifest_t m;
+ ASSERT_FALSE(cbm_workspace_manifest_read(base, &m));
+ ASSERT_EQ(m.count, 0);
+ th_cleanup(base);
+ PASS();
+}
+
+/* THE property the design rests on: a manifest grants nothing until a person
+ * approves it, and editing it lapses that approval rather than inheriting it. */
+TEST(ws_manifest_approval_is_keyed_to_content) {
+ char *base = th_mktempdir("cbm_ws_m3");
+ char *cache = th_mktempdir("cbm_ws_m3c");
+ ASSERT(base != NULL);
+ ASSERT(cache != NULL);
+ char path[1024];
+ snprintf(path, sizeof(path), "%s/%s", base, CBM_WS_MANIFEST_NAME);
+ ws_write(path, "/opt/sdk\n");
+
+ cbm_ws_manifest_t before;
+ ASSERT_TRUE(cbm_workspace_manifest_read(base, &before));
+ /* Unapproved grants nothing. */
+ ASSERT_FALSE(cbm_workspace_manifest_is_approved(cache, base, &before));
+
+ char err[1024];
+ ASSERT_TRUE(cbm_workspace_manifest_approve(cache, HOME, base, err, sizeof(err)));
+ ASSERT_TRUE(cbm_workspace_manifest_is_approved(cache, base, &before));
+
+ /* Widen the requests, as a `git pull` would. Approval must lapse. */
+ ws_write(path, "/opt/sdk\n/srv/protos\n");
+ cbm_ws_manifest_t after;
+ ASSERT_TRUE(cbm_workspace_manifest_read(base, &after));
+ ASSERT_TRUE(strcmp(before.digest, after.digest) != 0);
+ ASSERT_FALSE(cbm_workspace_manifest_is_approved(cache, base, &after));
+
+ th_cleanup(base);
+ th_cleanup(cache);
+ PASS();
+}
+
+/* Approving a manifest must not become a route around the breadth policy. */
+TEST(ws_manifest_approval_refuses_overbroad_requests) {
+ char *base = th_mktempdir("cbm_ws_m4");
+ char *cache = th_mktempdir("cbm_ws_m4c");
+ ASSERT(base != NULL);
+ ASSERT(cache != NULL);
+ char path[1024];
+ snprintf(path, sizeof(path), "%s/%s", base, CBM_WS_MANIFEST_NAME);
+ ws_write(path, "/etc\n");
+ char err[1024];
+ ASSERT_FALSE(cbm_workspace_manifest_approve(cache, HOME, base, err, sizeof(err)));
+ ASSERT_TRUE(strstr(err, "too broad") != NULL);
+ th_cleanup(base);
+ th_cleanup(cache);
+ PASS();
+}
+
SUITE(workspace) {
+ RUN_TEST(ws_manifest_absent_is_not_an_error);
+ RUN_TEST(ws_manifest_parses_entries_and_skips_comments);
+ RUN_TEST(ws_manifest_rejects_control_characters);
+ RUN_TEST(ws_manifest_approval_is_keyed_to_content);
+ RUN_TEST(ws_manifest_approval_refuses_overbroad_requests);
RUN_TEST(ws_depth_counts_components_below_the_volume);
RUN_TEST(ws_volume_roots_are_absolutely_denied);
RUN_TEST(ws_non_absolute_paths_are_denied);