@@ -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. */
396399static 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
398408bool 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. */
408418static 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+
484552bool 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 }
0 commit comments