Skip to content

Commit bf0dccf

Browse files
committed
feat(cli): add allow-root to declare an indexing root
The grant store was readable but nothing could write to it, so only CBM_ALLOWED_ROOT could declare a root. `allow-root <path>` records one, `--list` shows what is recorded, and `--approve-sensitive` is required for a home or credential directory. Roots that are refused outright — volume and drive roots, paths too broad to index as a unit — cannot be granted at all. Enrollment is a command a person types and deliberately nothing else. That is the property the whole store exists for: neither an indexed repository nor a tool caller can widen its own boundary. A confirmation delivered through the MCP surface would be answered by the same agent that may have been influenced, so it would not be a human decision. The path is canonicalized before recording, because the policy is defined over resolved paths and a grant stored as a symlink would not match the resolved path the indexer later presents. Classified stateless in the daemon bootstrap: it writes one line of user-level config and reads nothing from a project, so enrolling a root must not depend on daemon state. Without that classification the argument fell through to MCP server mode and exited silently on EOF. Verified end to end: /etc refused as too broad, $HOME refused pending the flag, a project directory recorded, a granted root indexes, and a non-granted root is refused naming the exact command that would allow it. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 7814dac commit bf0dccf

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎src/daemon/bootstrap.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ cbm_daemon_process_role_t cbm_daemon_process_role(int argc, char *const argv[])
162162
"install",
163163
"uninstall",
164164
"update",
165+
/* allow-root writes one line of user-level config and reads nothing from a
166+
* project, so it needs no daemon. Listed here rather than routed through
167+
* the daemon so enrolling a root cannot depend on daemon state. */
168+
"allow-root",
165169
};
166170
/* Stop at the first top-level mode token. Tool names, flag values, and JSON
167171
* following `cli` are opaque user input: a search query named "install"

‎src/main.c‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ enum {
5252
#include "foundation/log.h"
5353
#include "foundation/diagnostics.h"
5454
#include "foundation/platform.h"
55+
#include "foundation/workspace.h"
5556
#include "foundation/compat.h"
5657
#include "foundation/compat_fs.h"
5758
#include "foundation/compat_thread.h"
@@ -921,6 +922,81 @@ static void print_help(void) {
921922

922923
/* Try to handle a subcommand (cli/install/uninstall/update/config/--version/--help).
923924
* Returns -1 if no subcommand matched, otherwise the exit code. */
925+
/* `allow-root [--approve-sensitive] <path>` — record an indexing root.
926+
*
927+
* Enrollment lives here, in a command a person types, and deliberately nowhere
928+
* else: the whole point of the grant store is that neither an indexed repository
929+
* nor a tool caller can widen its own boundary. A confirmation delivered through
930+
* the MCP surface would be answered by the same agent that may have been
931+
* influenced, so it would not be a human decision at all. */
932+
static int main_run_allow_root(int argc, char **argv) {
933+
const char *path = NULL;
934+
bool approve_sensitive = false;
935+
bool list_only = false;
936+
for (int i = 0; i < argc; i++) {
937+
if (strcmp(argv[i], "--approve-sensitive") == 0) {
938+
approve_sensitive = true;
939+
} else if (strcmp(argv[i], "--list") == 0) {
940+
list_only = true;
941+
} else if (argv[i][0] == '-') {
942+
(void)fprintf(stderr, "error: unknown option: %s\n", argv[i]);
943+
return EXIT_FAILURE;
944+
} else if (!path) {
945+
path = argv[i];
946+
} else {
947+
(void)fprintf(stderr, "error: only one path may be given\n");
948+
return EXIT_FAILURE;
949+
}
950+
}
951+
952+
const char *cache_dir = cbm_workspace_cache_dir();
953+
if (!cache_dir || !cache_dir[0]) {
954+
(void)fprintf(stderr, "error: cache directory could not be resolved\n");
955+
return EXIT_FAILURE;
956+
}
957+
958+
if (list_only || !path) {
959+
char listing[CBM_SZ_8K];
960+
if (cbm_workspace_grant_list(cache_dir, listing, sizeof(listing))) {
961+
printf("allowed roots:\n%s", listing);
962+
} else {
963+
printf("no allowed roots recorded — indexing is unconfined apart from the "
964+
"always-refused roots (see docs/CONFIGURATION.md)\n");
965+
}
966+
if (!path && !list_only) {
967+
(void)fprintf(stderr,
968+
"usage: codebase-memory-mcp allow-root [--approve-sensitive] <path>\n"
969+
" codebase-memory-mcp allow-root --list\n");
970+
return EXIT_FAILURE;
971+
}
972+
return 0;
973+
}
974+
975+
/* Canonicalize before recording: the policy is defined over resolved paths,
976+
* and a grant stored as a symlink would not match the resolved repo path the
977+
* indexer later presents. */
978+
char canonical[CBM_PATH_MAX];
979+
if (!cbm_canonical_path(path, canonical, sizeof(canonical))) {
980+
(void)fprintf(stderr, "error: cannot resolve path: %s\n", path);
981+
return EXIT_FAILURE;
982+
}
983+
if (!cbm_is_dir(canonical)) {
984+
(void)fprintf(stderr, "error: not a directory: %s\n", canonical);
985+
return EXIT_FAILURE;
986+
}
987+
988+
char err[CBM_SZ_1K];
989+
if (!cbm_workspace_grant_add(cache_dir, cbm_workspace_home_dir(), canonical, approve_sensitive,
990+
err, sizeof(err))) {
991+
(void)fprintf(stderr, "refused: %s\n", err[0] ? err : "not an allowable root");
992+
return EXIT_FAILURE;
993+
}
994+
printf("allowed root recorded: %s\n", canonical);
995+
printf("note: with at least one root recorded, indexing is now confined to the "
996+
"recorded roots.\n");
997+
return 0;
998+
}
999+
9241000
static int handle_subcommand(int argc, char **argv, cbm_project_lock_manager_t *project_locks,
9251001
main_local_maintenance_context_t *maintenance_context) {
9261002
/* First scan: global flags */
@@ -938,6 +1014,9 @@ static int handle_subcommand(int argc, char **argv, cbm_project_lock_manager_t *
9381014
print_help();
9391015
return 0;
9401016
}
1017+
if (strcmp(argv[i], "allow-root") == 0) {
1018+
return main_run_allow_root(argc - i - SKIP_ONE, argv + i + SKIP_ONE);
1019+
}
9411020
if (strcmp(argv[i], "cli") == 0) {
9421021
cbm_mem_init_with_cap(cbm_mem_ram_fraction_for_total(cbm_system_info().total_ram),
9431022
cbm_index_worker_memory_budget_bytes());

0 commit comments

Comments
 (0)