Skip to content

Commit 3e2de74

Browse files
DeusDataMark Murawski
andcommitted
feat(workspace): record broad approvals with allow-root --approve-broad
Replace the PATH_ALLOW_BROAD environment variable with a recorded grant, the shape agreed in review: `codebase-memory-mcp allow-root --approve-broad <path>` records an explicit human approval for one root refused as too broad (CBM_WS_DENY_TOO_SHALLOW), mirroring --approve-sensitive end to end. - cbm_workspace_grant_add() takes approve_broad next to approve_sensitive. Each flag lifts only its own verdict, absolute denials stay unliftable, and a refusal names the flag that would lift it. - The approval is stored with a '^' marker, listed by allow-root --list as "(approved broad)", and honoured by cbm_workspace_root_allowed() only for CBM_WS_DENY_TOO_SHALLOW and only on an exact path match, keeping the exact-match rule of the original change. - cbm_workspace_root_allowed() returns to its previous signature. Every caller (MCP index_repository and index-job start, auto-index, daemon sessions, the HTTP UI) reads the same grant store, so stdio and daemon sessions decide identically and nothing depends on the environment a daemon was started with. - The too-broad refusal names `allow-root --approve-broad <path>`, a command a person runs. - cbm_workspace_classify_root() and cbm_workspace_verdict_is_overridable() are unchanged. The regression test keeps its name and its exact-match, sibling, absolute and sensitive checks, now against the grant store, and adds that a broad marker never lifts a sensitive verdict. Co-authored-by: Mark Murawski <github@kobaz.net> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent b9df260 commit 3e2de74

10 files changed

Lines changed: 228 additions & 134 deletions

File tree

‎docs/CONFIGURATION.md‎

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

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

‎src/daemon/application.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,7 @@ static bool application_session_workspace_allowed(const cbm_daemon_application_s
521521
bool allowed =
522522
root && root[0] &&
523523
cbm_workspace_root_allowed(root, cbm_workspace_home_dir(), cbm_workspace_cache_dir(),
524-
cbm_mcp_server_allowed_root(session->mcp),
525-
getenv("PATH_ALLOW_BROAD"), boundary_error,
524+
cbm_mcp_server_allowed_root(session->mcp), boundary_error,
526525
sizeof(boundary_error));
527526
if (!allowed) {
528527
cbm_log_warn("daemon.workspace.skipped", "operation", operation, "detail",

‎src/foundation/workspace.c‎

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

389399
bool cbm_workspace_grant_path(const char *cache_dir, char *out, size_t out_sz) {
390400
if (!cache_dir || !cache_dir[0] || !out || out_sz == 0) {
@@ -397,7 +407,8 @@ bool cbm_workspace_grant_path(const char *cache_dir, char *out, size_t out_sz) {
397407
/* Walk the grant file, invoking visit for each entry. visit returns true to stop.
398408
* Returns the number of entries seen. */
399409
static int ws_grant_walk(const char *cache_dir,
400-
bool (*visit)(const char *root, bool sensitive, void *ctx), void *ctx) {
410+
bool (*visit)(const char *root, ws_grant_kind_t kind, void *ctx),
411+
void *ctx) {
401412
char store[WS_LINE_MAX];
402413
if (!cbm_workspace_grant_path(cache_dir, store, sizeof(store))) {
403414
return 0;
@@ -416,13 +427,18 @@ static int ws_grant_walk(const char *cache_dir,
416427
if (len == 0 || line[0] == '#') {
417428
continue;
418429
}
419-
bool sensitive = line[0] == WS_SENSITIVE_MARK;
420-
const char *root = sensitive ? line + 1 : line;
430+
ws_grant_kind_t kind = WS_GRANT_ORDINARY;
431+
if (line[0] == WS_SENSITIVE_MARK) {
432+
kind = WS_GRANT_SENSITIVE;
433+
} else if (line[0] == WS_BROAD_MARK) {
434+
kind = WS_GRANT_BROAD;
435+
}
436+
const char *root = kind == WS_GRANT_ORDINARY ? line : line + 1;
421437
if (!root[0]) {
422438
continue;
423439
}
424440
seen++;
425-
if (visit && visit(root, sensitive, ctx)) {
441+
if (visit && visit(root, kind, ctx)) {
426442
break;
427443
}
428444
}
@@ -434,14 +450,21 @@ typedef struct {
434450
const char *candidate;
435451
bool contained; /* candidate is at or below some granted root */
436452
bool exact_sensitive; /* candidate is itself a grant marked sensitive */
453+
bool exact_broad; /* candidate is itself a grant marked broad */
437454
} ws_match_t;
438455

439-
static bool ws_match_visit(const char *root, bool sensitive, void *ctx) {
456+
static bool ws_match_visit(const char *root, ws_grant_kind_t kind, void *ctx) {
440457
ws_match_t *m = ctx;
441458
if (cbm_path_within_root(root, m->candidate)) {
442459
m->contained = true;
443-
if (sensitive && ws_paths_equal(root, m->candidate)) {
444-
m->exact_sensitive = true;
460+
/* Exact equality, never a prefix: an approval names one root, and must
461+
* not quietly cover every root below it. */
462+
if (kind != WS_GRANT_ORDINARY && ws_paths_equal(root, m->candidate)) {
463+
if (kind == WS_GRANT_SENSITIVE) {
464+
m->exact_sensitive = true;
465+
} else {
466+
m->exact_broad = true;
467+
}
445468
}
446469
}
447470
return false; /* visit every entry: a later one may be the exact match */
@@ -453,10 +476,15 @@ typedef struct {
453476
size_t used;
454477
} ws_list_t;
455478

456-
static bool ws_list_visit(const char *root, bool sensitive, void *ctx) {
479+
static bool ws_list_visit(const char *root, ws_grant_kind_t kind, void *ctx) {
457480
ws_list_t *l = ctx;
458-
int n = snprintf(l->out + l->used, l->out_sz - l->used, "%s%s\n",
459-
sensitive ? "(approved) " : "", root);
481+
const char *label = "";
482+
if (kind == WS_GRANT_SENSITIVE) {
483+
label = "(approved) ";
484+
} else if (kind == WS_GRANT_BROAD) {
485+
label = "(approved broad) ";
486+
}
487+
int n = snprintf(l->out + l->used, l->out_sz - l->used, "%s%s\n", label, root);
460488
if (n > 0 && (size_t)n < l->out_sz - l->used) {
461489
l->used += (size_t)n;
462490
}
@@ -472,9 +500,49 @@ bool cbm_workspace_grant_list(const char *cache_dir, char *out, size_t out_sz) {
472500
return ws_grant_walk(cache_dir, ws_list_visit, &l) > 0;
473501
}
474502

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

488556
cbm_ws_verdict_t verdict = cbm_workspace_classify_root(canonical_path, home_dir, cache_dir);
489-
if (verdict != CBM_WS_ALLOW) {
490-
if (!cbm_workspace_verdict_is_overridable(verdict)) {
491-
if (err) {
492-
snprintf(err, err_sz, "%s", cbm_workspace_verdict_reason(verdict));
493-
}
494-
return false;
495-
}
496-
if (!approve_sensitive) {
497-
/* Refuse by default and name the flag, rather than asking a question
498-
* whose answer we cannot authenticate. */
499-
if (err) {
500-
snprintf(err, err_sz, "%s; re-run with --approve-sensitive if that is intended",
501-
cbm_workspace_verdict_reason(verdict));
502-
}
503-
return false;
504-
}
557+
if (verdict != CBM_WS_ALLOW &&
558+
!ws_grant_verdict_approved(verdict, approve_sensitive, approve_broad, err, err_sz)) {
559+
return false;
505560
}
506561

507562
bool sensitive = verdict == CBM_WS_DENY_SENSITIVE;
563+
bool broad = verdict == CBM_WS_DENY_TOO_SHALLOW;
508564

509-
/* Already present is success, not a duplicate error. A sensitive approval
565+
/* Already present is success, not a duplicate error. An explicit approval
510566
* is narrower: an old ordinary grant (or an ordinary ancestor) establishes
511567
* containment, but does not record the exact human-approved exception that
512568
* cbm_workspace_root_allowed requires. Append that exact marked entry once. */
513-
ws_match_t existing = {canonical_path, false, false};
569+
ws_match_t existing = {canonical_path, false, false, false};
514570
(void)ws_grant_walk(cache_dir, ws_match_visit, &existing);
515-
if (existing.contained && (!sensitive || existing.exact_sensitive)) {
571+
if (existing.contained && (!sensitive || existing.exact_sensitive) &&
572+
(!broad || existing.exact_broad)) {
516573
return true;
517574
}
518575

@@ -530,7 +587,13 @@ bool cbm_workspace_grant_add(const char *cache_dir, const char *home_dir,
530587
}
531588
return false;
532589
}
533-
(void)fprintf(f, "%s%s\n", sensitive ? "!" : "", canonical_path);
590+
char mark[2] = {'\0', '\0'};
591+
if (sensitive) {
592+
mark[0] = WS_SENSITIVE_MARK;
593+
} else if (broad) {
594+
mark[0] = WS_BROAD_MARK;
595+
}
596+
(void)fprintf(f, "%s%s\n", mark, canonical_path);
534597
bool ok = fclose(f) == 0;
535598
if (!ok && err) {
536599
snprintf(err, err_sz, "cannot write %s", store);
@@ -539,8 +602,8 @@ bool cbm_workspace_grant_add(const char *cache_dir, const char *home_dir,
539602
}
540603

541604
bool cbm_workspace_root_allowed(const char *canonical_path, const char *home_dir,
542-
const char *cache_dir, const char *configured_root,
543-
const char *allow_broad_root, char *err, size_t err_sz) {
605+
const char *cache_dir, const char *configured_root, char *err,
606+
size_t err_sz) {
544607
if (err && err_sz) {
545608
err[0] = '\0';
546609
}
@@ -551,7 +614,7 @@ bool cbm_workspace_root_allowed(const char *canonical_path, const char *home_dir
551614
return false;
552615
}
553616

554-
ws_match_t match = {canonical_path, false, false};
617+
ws_match_t match = {canonical_path, false, false, false};
555618
int grants = ws_grant_walk(cache_dir, ws_match_visit, &match);
556619

557620
/* A configured root behaves as an additional grant so existing
@@ -596,37 +659,23 @@ bool cbm_workspace_root_allowed(const char *canonical_path, const char *home_dir
596659
return true;
597660
}
598661
/* An explicit human approval recorded for exactly this path is the only thing
599-
* that lifts a sensitive refusal. Absolute refusals cannot be lifted at all. */
662+
* that lifts a sensitive or a too-broad refusal, each by its own marker: a
663+
* broad approval says "index this whole tree on purpose", which is not a
664+
* statement about credentials, and the reverse. Exact match, never a prefix,
665+
* so one broad approval cannot cover every root below it — the "/Users"-style
666+
* breadth the depth rule exists to catch. Absolute refusals cannot be lifted. */
600667
if (verdict == CBM_WS_DENY_SENSITIVE && match.exact_sensitive) {
601668
return true;
602669
}
603-
/* PATH_ALLOW_BROAD is a separate, narrower lever from the grant store above:
604-
* an operator has named this exact root as one they have deliberately chosen
605-
* to index whole, accepting that it may span multiple unrelated projects --
606-
* the tradeoff CBM_WS_DENY_TOO_SHALLOW normally refuses on their behalf. It
607-
* must match canonical_path exactly, the same way an exact sensitive grant
608-
* does above: a prefix match would let one broad allowance quietly cover
609-
* every root below it too, which is the "/Users"-style breadth this rule
610-
* exists to catch. It is environment, not a recorded grant, so it is not
611-
* listed by cbm_workspace_grant_list and cannot lift CBM_WS_DENY_ABSOLUTE or
612-
* CBM_WS_DENY_SENSITIVE. */
613-
if (verdict == CBM_WS_DENY_TOO_SHALLOW && allow_broad_root && allow_broad_root[0] &&
614-
ws_paths_equal(canonical_path, allow_broad_root)) {
670+
if (verdict == CBM_WS_DENY_TOO_SHALLOW && match.exact_broad) {
615671
return true;
616672
}
617673
if (err) {
618-
if (cbm_workspace_verdict_is_overridable(verdict)) {
674+
const char *flag = ws_verdict_approval_flag(verdict);
675+
if (flag) {
619676
snprintf(err, err_sz,
620-
"%s: %s. To index it anyway, run: codebase-memory-mcp allow-root "
621-
"--approve-sensitive %s",
622-
canonical_path, cbm_workspace_verdict_reason(verdict), canonical_path);
623-
} else if (verdict == CBM_WS_DENY_TOO_SHALLOW) {
624-
/* No allow-root command exists for this one -- the grant store is a
625-
* human-approved list of real project roots, and this path is being
626-
* refused for being the opposite of that. PATH_ALLOW_BROAD is the
627-
* deliberate, narrower escape hatch: set it to this exact path. */
628-
snprintf(err, err_sz, "%s: %s. To index it anyway, set PATH_ALLOW_BROAD=%s",
629-
canonical_path, cbm_workspace_verdict_reason(verdict), canonical_path);
677+
"%s: %s. To index it anyway, run: codebase-memory-mcp allow-root %s %s",
678+
canonical_path, cbm_workspace_verdict_reason(verdict), flag, canonical_path);
630679
} else {
631680
snprintf(err, err_sz, "%s: %s", canonical_path, cbm_workspace_verdict_reason(verdict));
632681
}

‎src/foundation/workspace.h‎

Lines changed: 16 additions & 15 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,27 +85,24 @@ 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);
9298

9399
/* The whole decision, used by every entry point that accepts a repo path.
94100
* canonical_path must already be canonicalized. configured_root is the legacy
95101
* CBM_ALLOWED_ROOT / session policy, treated as an additional grant, or NULL.
96-
* allow_broad_root is PATH_ALLOW_BROAD (or NULL): an operator-set exact match
97-
* for canonical_path that lifts a CBM_WS_DENY_TOO_SHALLOW verdict only. It is
98-
* process environment, not a recorded grant, so it does not appear in
99-
* cbm_workspace_grant_list and does not widen CBM_WS_DENY_ABSOLUTE or
100-
* CBM_WS_DENY_SENSITIVE. On refusal, err receives a message that names the
101-
* command which would fix it. */
102+
* On refusal, err receives a message that names the command which would fix it. */
102103
bool cbm_workspace_root_allowed(const char *canonical_path, const char *home_dir,
103-
const char *cache_dir, const char *configured_root,
104-
const char *allow_broad_root, char *err, size_t err_sz);
104+
const char *cache_dir, const char *configured_root, char *err,
105+
size_t err_sz);
105106

106107
/* The home and cache directories the policy should be evaluated against. Shared
107108
* so two callers cannot derive them differently and reach different verdicts for

0 commit comments

Comments
 (0)