@@ -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. */
387390static 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
389399bool 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. */
399409static 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+
475543bool 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
541604bool 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 }
0 commit comments