Skip to content

Commit a3831c9

Browse files
authored
Merge pull request DeusData#2017 from kobaz/feat/path-allow-broad-too-shallow-override
feat(workspace): add PATH_ALLOW_BROAD override for too-shallow roots
2 parents d807b1e + 9ae55eb commit a3831c9

7 files changed

Lines changed: 228 additions & 64 deletions

File tree

‎docs/CONFIGURATION.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ root because they are too broad or too sensitive to index as a unit:
245245
- a credential directory at any depth — `.ssh`, `.aws`, `.gnupg`, `.kube`,
246246
`.docker`, `.netrc`, `.git-credentials`, `.password-store`, macOS `Keychains`.
247247

248+
A single top-level directory can still be indexed as one root on purpose, for
249+
example a container or build machine that keeps its project in `/work`, `/app` or
250+
`/src`, by approving that exact path in a terminal:
251+
`codebase-memory-mcp allow-root --approve-broad /work`. The approval is recorded
252+
like any other allowed root, listed as `(approved broad)` by `allow-root --list`,
253+
and covers no other top-level directory; as with any recorded root, indexing is
254+
then confined to the recorded roots. Filesystem, drive and share roots cannot be
255+
approved.
256+
248257
Two limits are worth stating plainly. This constrains *scope*, not
249258
*sensitivity*: inside a root that is allowed, every file the process can read may
250259
be indexed and later returned. And the credential list is a denylist, so it

‎src/foundation/workspace.c‎

Lines changed: 109 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,18 @@ enum { WS_LINE_MAX = 4096 };
392392
/* A line may carry a leading '!' meaning "a person explicitly approved this
393393
* sensitive root". The marker is stored rather than inferred so re-classifying
394394
* later (a new credential name is added to the list, say) cannot silently
395-
* upgrade an ordinary grant into a sensitive one. */
395+
* upgrade an ordinary grant into a sensitive one. A leading '^' is the same
396+
* kind of record for a root refused as too broad (allow-root --approve-broad).
397+
* Neither character can begin a canonical absolute path, so a marked line can
398+
* never be misread as an ordinary grant. */
396399
static const char WS_SENSITIVE_MARK = '!';
400+
static const char WS_BROAD_MARK = '^';
401+
402+
typedef enum {
403+
WS_GRANT_ORDINARY = 0,
404+
WS_GRANT_SENSITIVE, /* explicit approval of a CBM_WS_DENY_SENSITIVE root */
405+
WS_GRANT_BROAD, /* explicit approval of a CBM_WS_DENY_TOO_SHALLOW root */
406+
} ws_grant_kind_t;
397407

398408
bool cbm_workspace_grant_path(const char *cache_dir, char *out, size_t out_sz) {
399409
if (!cache_dir || !cache_dir[0] || !out || out_sz == 0) {
@@ -406,7 +416,8 @@ bool cbm_workspace_grant_path(const char *cache_dir, char *out, size_t out_sz) {
406416
/* Walk the grant file, invoking visit for each entry. visit returns true to stop.
407417
* Returns the number of entries seen. */
408418
static int ws_grant_walk(const char *cache_dir,
409-
bool (*visit)(const char *root, bool sensitive, void *ctx), void *ctx) {
419+
bool (*visit)(const char *root, ws_grant_kind_t kind, void *ctx),
420+
void *ctx) {
410421
char store[WS_LINE_MAX];
411422
if (!cbm_workspace_grant_path(cache_dir, store, sizeof(store))) {
412423
return 0;
@@ -425,13 +436,18 @@ static int ws_grant_walk(const char *cache_dir,
425436
if (len == 0 || line[0] == '#') {
426437
continue;
427438
}
428-
bool sensitive = line[0] == WS_SENSITIVE_MARK;
429-
const char *root = sensitive ? line + 1 : line;
439+
ws_grant_kind_t kind = WS_GRANT_ORDINARY;
440+
if (line[0] == WS_SENSITIVE_MARK) {
441+
kind = WS_GRANT_SENSITIVE;
442+
} else if (line[0] == WS_BROAD_MARK) {
443+
kind = WS_GRANT_BROAD;
444+
}
445+
const char *root = kind == WS_GRANT_ORDINARY ? line : line + 1;
430446
if (!root[0]) {
431447
continue;
432448
}
433449
seen++;
434-
if (visit && visit(root, sensitive, ctx)) {
450+
if (visit && visit(root, kind, ctx)) {
435451
break;
436452
}
437453
}
@@ -443,14 +459,21 @@ typedef struct {
443459
const char *candidate;
444460
bool contained; /* candidate is at or below some granted root */
445461
bool exact_sensitive; /* candidate is itself a grant marked sensitive */
462+
bool exact_broad; /* candidate is itself a grant marked broad */
446463
} ws_match_t;
447464

448-
static bool ws_match_visit(const char *root, bool sensitive, void *ctx) {
465+
static bool ws_match_visit(const char *root, ws_grant_kind_t kind, void *ctx) {
449466
ws_match_t *m = ctx;
450467
if (cbm_path_within_root(root, m->candidate)) {
451468
m->contained = true;
452-
if (sensitive && ws_paths_equal(root, m->candidate)) {
453-
m->exact_sensitive = true;
469+
/* Exact equality, never a prefix: an approval names one root, and must
470+
* not quietly cover every root below it. */
471+
if (kind != WS_GRANT_ORDINARY && ws_paths_equal(root, m->candidate)) {
472+
if (kind == WS_GRANT_SENSITIVE) {
473+
m->exact_sensitive = true;
474+
} else {
475+
m->exact_broad = true;
476+
}
454477
}
455478
}
456479
return false; /* visit every entry: a later one may be the exact match */
@@ -462,10 +485,15 @@ typedef struct {
462485
size_t used;
463486
} ws_list_t;
464487

465-
static bool ws_list_visit(const char *root, bool sensitive, void *ctx) {
488+
static bool ws_list_visit(const char *root, ws_grant_kind_t kind, void *ctx) {
466489
ws_list_t *l = ctx;
467-
int n = snprintf(l->out + l->used, l->out_sz - l->used, "%s%s\n",
468-
sensitive ? "(approved) " : "", root);
490+
const char *label = "";
491+
if (kind == WS_GRANT_SENSITIVE) {
492+
label = "(approved) ";
493+
} else if (kind == WS_GRANT_BROAD) {
494+
label = "(approved broad) ";
495+
}
496+
int n = snprintf(l->out + l->used, l->out_sz - l->used, "%s%s\n", label, root);
469497
if (n > 0 && (size_t)n < l->out_sz - l->used) {
470498
l->used += (size_t)n;
471499
}
@@ -481,9 +509,49 @@ bool cbm_workspace_grant_list(const char *cache_dir, char *out, size_t out_sz) {
481509
return ws_grant_walk(cache_dir, ws_list_visit, &l) > 0;
482510
}
483511

512+
/* The allow-root flag that records an explicit approval for a refused verdict,
513+
* or NULL when nothing can lift it (absolute denials). */
514+
static const char *ws_verdict_approval_flag(cbm_ws_verdict_t verdict) {
515+
if (verdict == CBM_WS_DENY_TOO_SHALLOW) {
516+
return "--approve-broad";
517+
}
518+
if (cbm_workspace_verdict_is_overridable(verdict)) {
519+
return "--approve-sensitive";
520+
}
521+
return NULL;
522+
}
523+
524+
/* True when a refused verdict carries the explicit approval that lifts it. Each
525+
* flag lifts only its own verdict: --approve-sensitive never widens breadth and
526+
* --approve-broad never widens sensitivity; absolute denials lift for neither.
527+
* Refuses by default and names the flag, rather than asking a question whose
528+
* answer we cannot authenticate. */
529+
static bool ws_grant_verdict_approved(cbm_ws_verdict_t verdict, bool approve_sensitive,
530+
bool approve_broad, char *err, size_t err_sz) {
531+
bool approved = false;
532+
if (verdict == CBM_WS_DENY_TOO_SHALLOW) {
533+
approved = approve_broad;
534+
} else if (cbm_workspace_verdict_is_overridable(verdict)) {
535+
approved = approve_sensitive;
536+
}
537+
if (approved) {
538+
return true;
539+
}
540+
if (err) {
541+
const char *flag = ws_verdict_approval_flag(verdict);
542+
if (flag) {
543+
snprintf(err, err_sz, "%s; re-run with %s if that is intended",
544+
cbm_workspace_verdict_reason(verdict), flag);
545+
} else {
546+
snprintf(err, err_sz, "%s", cbm_workspace_verdict_reason(verdict));
547+
}
548+
}
549+
return false;
550+
}
551+
484552
bool cbm_workspace_grant_add(const char *cache_dir, const char *home_dir,
485-
const char *canonical_path, bool approve_sensitive, char *err,
486-
size_t err_sz) {
553+
const char *canonical_path, bool approve_sensitive, bool approve_broad,
554+
char *err, size_t err_sz) {
487555
if (err && err_sz) {
488556
err[0] = '\0';
489557
}
@@ -495,33 +563,22 @@ bool cbm_workspace_grant_add(const char *cache_dir, const char *home_dir,
495563
}
496564

497565
cbm_ws_verdict_t verdict = cbm_workspace_classify_root(canonical_path, home_dir, cache_dir);
498-
if (verdict != CBM_WS_ALLOW) {
499-
if (!cbm_workspace_verdict_is_overridable(verdict)) {
500-
if (err) {
501-
snprintf(err, err_sz, "%s", cbm_workspace_verdict_reason(verdict));
502-
}
503-
return false;
504-
}
505-
if (!approve_sensitive) {
506-
/* Refuse by default and name the flag, rather than asking a question
507-
* whose answer we cannot authenticate. */
508-
if (err) {
509-
snprintf(err, err_sz, "%s; re-run with --approve-sensitive if that is intended",
510-
cbm_workspace_verdict_reason(verdict));
511-
}
512-
return false;
513-
}
566+
if (verdict != CBM_WS_ALLOW &&
567+
!ws_grant_verdict_approved(verdict, approve_sensitive, approve_broad, err, err_sz)) {
568+
return false;
514569
}
515570

516571
bool sensitive = verdict == CBM_WS_DENY_SENSITIVE;
572+
bool broad = verdict == CBM_WS_DENY_TOO_SHALLOW;
517573

518-
/* Already present is success, not a duplicate error. A sensitive approval
574+
/* Already present is success, not a duplicate error. An explicit approval
519575
* is narrower: an old ordinary grant (or an ordinary ancestor) establishes
520576
* containment, but does not record the exact human-approved exception that
521577
* cbm_workspace_root_allowed requires. Append that exact marked entry once. */
522-
ws_match_t existing = {canonical_path, false, false};
578+
ws_match_t existing = {canonical_path, false, false, false};
523579
(void)ws_grant_walk(cache_dir, ws_match_visit, &existing);
524-
if (existing.contained && (!sensitive || existing.exact_sensitive)) {
580+
if (existing.contained && (!sensitive || existing.exact_sensitive) &&
581+
(!broad || existing.exact_broad)) {
525582
return true;
526583
}
527584

@@ -539,7 +596,13 @@ bool cbm_workspace_grant_add(const char *cache_dir, const char *home_dir,
539596
}
540597
return false;
541598
}
542-
(void)fprintf(f, "%s%s\n", sensitive ? "!" : "", canonical_path);
599+
char mark[2] = {'\0', '\0'};
600+
if (sensitive) {
601+
mark[0] = WS_SENSITIVE_MARK;
602+
} else if (broad) {
603+
mark[0] = WS_BROAD_MARK;
604+
}
605+
(void)fprintf(f, "%s%s\n", mark, canonical_path);
543606
bool ok = fclose(f) == 0;
544607
if (!ok && err) {
545608
snprintf(err, err_sz, "cannot write %s", store);
@@ -560,7 +623,7 @@ bool cbm_workspace_root_allowed(const char *canonical_path, const char *home_dir
560623
return false;
561624
}
562625

563-
ws_match_t match = {canonical_path, false, false};
626+
ws_match_t match = {canonical_path, false, false, false};
564627
int grants = ws_grant_walk(cache_dir, ws_match_visit, &match);
565628

566629
/* A configured root behaves as an additional grant so existing
@@ -605,17 +668,23 @@ bool cbm_workspace_root_allowed(const char *canonical_path, const char *home_dir
605668
return true;
606669
}
607670
/* An explicit human approval recorded for exactly this path is the only thing
608-
* that lifts a sensitive refusal. Absolute and shallow refusals cannot be
609-
* lifted at all. */
671+
* that lifts a sensitive or a too-broad refusal, each by its own marker: a
672+
* broad approval says "index this whole tree on purpose", which is not a
673+
* statement about credentials, and the reverse. Exact match, never a prefix,
674+
* so one broad approval cannot cover every root below it — the "/Users"-style
675+
* breadth the depth rule exists to catch. Absolute refusals cannot be lifted. */
610676
if (verdict == CBM_WS_DENY_SENSITIVE && match.exact_sensitive) {
611677
return true;
612678
}
679+
if (verdict == CBM_WS_DENY_TOO_SHALLOW && match.exact_broad) {
680+
return true;
681+
}
613682
if (err) {
614-
if (cbm_workspace_verdict_is_overridable(verdict)) {
683+
const char *flag = ws_verdict_approval_flag(verdict);
684+
if (flag) {
615685
snprintf(err, err_sz,
616-
"%s: %s. To index it anyway, run: codebase-memory-mcp allow-root "
617-
"--approve-sensitive %s",
618-
canonical_path, cbm_workspace_verdict_reason(verdict), canonical_path);
686+
"%s: %s. To index it anyway, run: codebase-memory-mcp allow-root %s %s",
687+
canonical_path, cbm_workspace_verdict_reason(verdict), flag, canonical_path);
619688
} else {
620689
snprintf(err, err_sz, "%s: %s", canonical_path, cbm_workspace_verdict_reason(verdict));
621690
}

‎src/foundation/workspace.h‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
typedef enum {
2727
CBM_WS_ALLOW = 0,
28-
/* Fewer path components than the platform minimum — e.g. "/etc", "/home". */
28+
/* Fewer path components than the platform minimum — e.g. "/etc", "/home".
29+
* Lifted only for one exact path by a recorded broad approval
30+
* (allow-root --approve-broad), never by a manifest or a tool call. */
2931
CBM_WS_DENY_TOO_SHALLOW,
3032
/* A filesystem root, a drive root, a UNC share root, or a path that is or
3133
* contains the cbm cache directory. Never overridable. */
@@ -42,8 +44,10 @@ cbm_ws_verdict_t cbm_workspace_classify_root(const char *canonical_path, const c
4244
/* Stable, human-facing reason for a verdict. Never NULL. */
4345
const char *cbm_workspace_verdict_reason(cbm_ws_verdict_t verdict);
4446

45-
/* True when an explicit human action may override this verdict. Absolute denials
46-
* and shallow paths are never overridable; sensitive ones are. */
47+
/* True when a sensitive approval (allow-root --approve-sensitive) may override
48+
* this verdict. Absolute denials never are. A shallow path is not overridable
49+
* in this sense either: only an exact broad approval lifts it, see
50+
* cbm_workspace_grant_add. */
4751
bool cbm_workspace_verdict_is_overridable(cbm_ws_verdict_t verdict);
4852

4953
/* Number of path components below the volume root, ignoring repeated and
@@ -81,11 +85,13 @@ bool cbm_path_within_root(const char *root_path, const char *abs_path);
8185
bool cbm_workspace_grant_path(const char *cache_dir, char *out, size_t out_sz);
8286

8387
/* Record canonical_path as an allowed root. approve_sensitive records an explicit
84-
* human override for a CBM_WS_DENY_SENSITIVE path; absolute denials and shallow
85-
* paths are never grantable. Returns false and fills err on refusal. */
88+
* human override for a CBM_WS_DENY_SENSITIVE path; approve_broad records one for a
89+
* CBM_WS_DENY_TOO_SHALLOW path. Each flag lifts only its own verdict, and only
90+
* for exactly this path. Absolute denials are never grantable. Returns false and
91+
* fills err on refusal. */
8692
bool cbm_workspace_grant_add(const char *cache_dir, const char *home_dir,
87-
const char *canonical_path, bool approve_sensitive, char *err,
88-
size_t err_sz);
93+
const char *canonical_path, bool approve_sensitive, bool approve_broad,
94+
char *err, size_t err_sz);
8995

9096
/* Newline-separated granted roots into out. True when at least one exists. */
9197
bool cbm_workspace_grant_list(const char *cache_dir, char *out, size_t out_sz);

‎src/main.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,21 +1179,27 @@ static void print_help(void) {
11791179

11801180
/* Try to handle a subcommand (cli/install/uninstall/update/config/--version/--help).
11811181
* Returns -1 if no subcommand matched, otherwise the exit code. */
1182-
/* `allow-root [--approve-sensitive] <path>` — record an indexing root.
1182+
/* `allow-root [--approve-sensitive | --approve-broad] <path>` — record an
1183+
* indexing root.
11831184
*
11841185
* Enrollment lives here, in a command a person types, and deliberately nowhere
11851186
* else: the whole point of the grant store is that neither an indexed repository
11861187
* nor a tool caller can widen its own boundary. A confirmation delivered through
11871188
* the MCP surface would be answered by the same agent that may have been
1188-
* influenced, so it would not be a human decision at all. */
1189+
* influenced, so it would not be a human decision at all. --approve-broad is the
1190+
* same kind of record for one exact root refused as too broad ("/work", "/app"),
1191+
* so it is listed, revocable and seen identically by stdio and daemon sessions. */
11891192
static int main_run_allow_root(int argc, char **argv) {
11901193
const char *path = NULL;
11911194
bool approve_sensitive = false;
1195+
bool approve_broad = false;
11921196
bool list_only = false;
11931197
bool approve_manifest = false;
11941198
for (int i = 0; i < argc; i++) {
11951199
if (strcmp(argv[i], "--approve-sensitive") == 0) {
11961200
approve_sensitive = true;
1201+
} else if (strcmp(argv[i], "--approve-broad") == 0) {
1202+
approve_broad = true;
11971203
} else if (strcmp(argv[i], "--list") == 0) {
11981204
list_only = true;
11991205
} else if (strcmp(argv[i], "--approve-manifest") == 0) {
@@ -1225,7 +1231,8 @@ static int main_run_allow_root(int argc, char **argv) {
12251231
}
12261232
if (!path && !list_only) {
12271233
(void)fprintf(stderr,
1228-
"usage: codebase-memory-mcp allow-root [--approve-sensitive] <path>\n"
1234+
"usage: codebase-memory-mcp allow-root [--approve-sensitive | "
1235+
"--approve-broad] <path>\n"
12291236
" codebase-memory-mcp allow-root --approve-manifest <project>\n"
12301237
" codebase-memory-mcp allow-root --list\n");
12311238
return EXIT_FAILURE;
@@ -1268,7 +1275,7 @@ static int main_run_allow_root(int argc, char **argv) {
12681275

12691276
char err[CBM_SZ_1K];
12701277
if (!cbm_workspace_grant_add(cache_dir, cbm_workspace_home_dir(), canonical, approve_sensitive,
1271-
err, sizeof(err))) {
1278+
approve_broad, err, sizeof(err))) {
12721279
(void)fprintf(stderr, "refused: %s\n", err[0] ? err : "not an allowable root");
12731280
return EXIT_FAILURE;
12741281
}

‎tests/test_daemon_application.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,8 +2643,9 @@ TEST(daemon_application_sensitive_root_blocks_auto_index_but_preserves_controls)
26432643
app_wait_for_active_jobs(application, 0);
26442644

26452645
char grant_error[APP_TEST_PATH_CAP];
2646-
bool approved = ordinary_reaped && cbm_workspace_grant_add(cache, sensitive, sensitive, true,
2647-
grant_error, sizeof(grant_error));
2646+
bool approved =
2647+
ordinary_reaped && cbm_workspace_grant_add(cache, sensitive, sensitive, true, false,
2648+
grant_error, sizeof(grant_error));
26482649
bool approved_initialized =
26492650
approved && app_test_initialize_profile(&callbacks, approved_session, sensitive,
26502651
CBM_MCP_TOOL_PROFILE_ALL, NULL, NULL);
@@ -2769,8 +2770,9 @@ TEST(daemon_application_sensitive_root_blocks_watch_but_preserves_controls) {
27692770
bool ordinary_released = ordinary_watched && cbm_watcher_watch_count(watcher) == 0;
27702771

27712772
char grant_error[APP_TEST_PATH_CAP];
2772-
bool approved = ordinary_released && cbm_workspace_grant_add(cache, sensitive, sensitive, true,
2773-
grant_error, sizeof(grant_error));
2773+
bool approved =
2774+
ordinary_released && cbm_workspace_grant_add(cache, sensitive, sensitive, true, false,
2775+
grant_error, sizeof(grant_error));
27742776
bool approved_initialized =
27752777
approved && app_test_initialize_profile(&callbacks, approved_session, sensitive,
27762778
CBM_MCP_TOOL_PROFILE_ALL, NULL, NULL);

‎tests/test_mcp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,7 @@ static int issue403_initialize_count_calls(const char *session_root, bool approv
20462046
char err[1024];
20472047
bool approved =
20482048
!approve_sensitive || cbm_workspace_grant_add(cache, cbm_workspace_home_dir(), session_root,
2049-
true, err, sizeof(err));
2049+
true, false, err, sizeof(err));
20502050
cbm_config_t *cfg = approved ? cbm_config_open(cache) : NULL;
20512051
cbm_mcp_server_t *srv = cfg ? cbm_mcp_server_new(NULL) : NULL;
20522052
int calls = -2;

0 commit comments

Comments
 (0)