Skip to content

Commit a08b9ec

Browse files
fix(worker): scope the index worker to the request's repository, not the daemon's environment
The daemon-spawned index worker inherited the daemon process environment and re-ran the allowed-root check without a session policy. The daemon's session policy is installed only in application.c (the sole production caller of cbm_mcp_server_set_session_context); after the daemon admits an index_repository request, index_supervisor.c builds the worker argv (`cli --index-worker --index-worker-build <fp> index_repository <args_json> --response-out ...`, no root anywhere) and subprocess.c spawns it with the daemon's environ. The worker then creates its own server with cbm_mcp_server_new(NULL), so handle_index_repository runs with no session policy and falls back to getenv("CBM_ALLOWED_ROOT") - the environment of whoever STARTED the daemon (bootstrap.c spawns it with the starter's environ). Observed: a daemon started with CBM_ALLOWED_ROOT=<rootA>; a client session for <rootB>/tiny is admitted by the daemon (index.supervisor.reap outcome=clean exit_code=0) and then refused by the worker with "<rootB>/tiny is outside the allowed root. To allow it, run: codebase-memory-mcp allow-root ...". Any daemon whose starter had CBM_ALLOWED_ROOT set broke every other session outside it. Fix: in the worker arm of run_cli, right after the worker's server is created and before any tool runs, read repo_path from the request args (both daemon spawn paths rewrite the worker args with the canonical repo_path), canonicalize it and install it explicitly as both session root and allowed root. The daemon already authorized the request; the worker only executes it. A repository that cannot be canonicalized does not exist and can only have been admitted by a session with no declared boundary (containment needs a real path), so the worker mirrors that with an explicit unrestricted policy - still never the environment fallback - and the pipeline keeps reporting the missing repository as the tool error it always was. A request without repo_path fails closed with "request workspace scope invalid". No argv or IPC change, no process environment mutation. Regression test: tests/test_worker_session_scope.sh drives the real binary as a supervised worker with CBM_ALLOWED_ROOT=<rootA> and repo_path=<rootB>/tiny and requires an indexed response with no boundary refusal; a worker without repo_path must exit nonzero on the scope check. Wired as Step 5f of scripts/test.sh next to the other real-binary worker contracts. RED on main (the exact "outside the allowed root" refusal above), GREEN with this change. Distilled from DeusData#1925 with co-author credit. Verification: test-runner suites index_supervisor, daemon_application, daemon, mcp, cli green; tests/test_worker_error_response.sh, test_worker_watchdog.sh and test_parent_watchdog.sh green against the fixed binary; make lint-ci and scripts/check-no-test-skips.sh clean. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com> Co-authored-by: Zhiyu <zhiyuzhang001@gmail.com>
1 parent 745794d commit a08b9ec

3 files changed

Lines changed: 153 additions & 3 deletions

File tree

‎scripts/test.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ CBM_TEST_BINARY="$WATCHDOG_BINARY" bash "$ROOT/tests/test_worker_error_response.
346346
echo "=== Step 5e: watcher_enabled kill-switch regression (#335) ==="
347347
CBM_TEST_BINARY="$WATCHDOG_BINARY" bash "$ROOT/tests/test_watcher_disabled.sh"
348348

349+
# Step 5f: a supervised worker is scoped to the request the daemon admitted,
350+
# never to the CBM_ALLOWED_ROOT it inherited from the daemon starter's
351+
# environment. Reuses the prod binary built in Step 5.
352+
echo "=== Step 5f: worker request-scope regression ==="
353+
CBM_TEST_BINARY="$WATCHDOG_BINARY" bash "$ROOT/tests/test_worker_session_scope.sh"
354+
349355
# Step 6: security-strings URL allow-list regression. The MSYS2 CLANG64 toolchain
350356
# bakes its package-tracker URL into the static Windows .exe; the binary string
351357
# audit must allow-list it (Windows-only — Linux smoke never saw it).

‎src/main.c‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,36 @@ static bool cli_first_nonspace_is_brace(const char *s) {
676676
static char *main_local_cli_daemon_execute(const char *tool_name, const char *args_json,
677677
bool quiet_requested);
678678

679+
/* A supervised worker runs in the DAEMON's environment, not the requesting
680+
* client's. The daemon admitted this request under the client's session policy
681+
* and re-executed us with the canonical repo_path in the args (both daemon
682+
* spawn paths rewrite it), so the request itself is the worker's workspace
683+
* scope: repository == session root == allowed root. Without this, a fresh
684+
* server fell back to the process-wide CBM_ALLOWED_ROOT inherited from whoever
685+
* started the daemon and refused every admitted session outside it. Returns
686+
* NULL once scoped, otherwise the reason to fail closed: a worker never indexes
687+
* under an ambient policy.
688+
*
689+
* A repository that cannot be canonicalized (it does not exist) can only have
690+
* been admitted by a session with no declared boundary, because containment
691+
* needs a real path. The worker mirrors that with an explicit unrestricted
692+
* policy - still never the environment fallback - so the pipeline reports the
693+
* missing repository as the tool error it always was, instead of the
694+
* supervisor misreading a refused worker as a crash. */
695+
static const char *main_index_worker_scope_request(cbm_mcp_server_t *srv, const char *args_json) {
696+
char *repo_path = cbm_mcp_get_string_arg(args_json, "repo_path");
697+
if (!repo_path || !repo_path[0]) {
698+
free(repo_path);
699+
return "request names no repo_path";
700+
}
701+
char canonical[MAIN_PATH_CAP];
702+
bool exists = cbm_canonical_path(repo_path, canonical, sizeof(canonical)) != 0;
703+
const char *scope = exists ? canonical : repo_path;
704+
bool scoped = cbm_mcp_server_set_session_context(srv, scope, exists ? scope : NULL);
705+
free(repo_path);
706+
return scoped ? NULL : "session context could not be installed";
707+
}
708+
679709
static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_locks,
680710
main_local_maintenance_context_t *maintenance_context) {
681711
cbm_cli_output_flags_t output_flags;
@@ -817,11 +847,15 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
817847
};
818848
bool maintenance_binding_failed = false;
819849
bool maintenance_cancelled = false;
850+
const char *worker_scope_refused = NULL;
820851
if (!index_worker) {
821852
result = main_local_cli_daemon_execute(tool_name, args_json, output_flags.quiet_requested);
822853
} else {
823854
srv = cbm_mcp_server_new(NULL);
824855
if (srv) {
856+
worker_scope_refused = main_index_worker_scope_request(srv, args_json);
857+
}
858+
if (srv && !worker_scope_refused) {
825859
/* The in-process worker is a standalone instance: it may not
826860
* launch MCP-session background tasks. It receives project_locks
827861
* from its own process-level coordination setup and therefore
@@ -834,8 +868,8 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
834868
main_local_cli_mutation_try_begin);
835869
}
836870
}
837-
maintenance_binding_failed = srv && !maintenance_context;
838-
if (srv && maintenance_context) {
871+
maintenance_binding_failed = srv && !worker_scope_refused && !maintenance_context;
872+
if (srv && !worker_scope_refused && maintenance_context) {
839873
main_local_maintenance_server_bind(maintenance_context, srv);
840874
result = cbm_mcp_handle_tool(srv, tool_name, args_json);
841875
/* Unbind under the same mutex used by cancellation before any
@@ -847,7 +881,10 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
847881
}
848882
}
849883
if (!result) {
850-
if (maintenance_binding_failed) {
884+
if (worker_scope_refused) {
885+
(void)fprintf(stderr, "error: request workspace scope invalid: %s\n",
886+
worker_scope_refused);
887+
} else if (maintenance_binding_failed) {
851888
(void)fprintf(stderr,
852889
"error: local %s maintenance cancellation could not bind safely\n",
853890
index_worker ? "worker" : "CLI");

‎tests/test_worker_session_scope.sh‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env bash
2+
# A supervised index worker (`cli --index-worker index_repository ...`) runs in
3+
# the DAEMON's environment, not the requesting client's. The daemon has already
4+
# admitted the request under the client's session policy and re-executes the
5+
# worker with the canonical repo_path in its args, so the worker must scope its
6+
# own workspace boundary to that request. Before the fix it built an unscoped
7+
# server, fell back to the process-wide CBM_ALLOWED_ROOT it inherited from the
8+
# daemon starter, and refused every admitted session outside that root with
9+
# "... is outside the allowed root". A missing repo_path must fail closed: a
10+
# worker never indexes under an ambient policy.
11+
set -euo pipefail
12+
13+
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
14+
BINARY="${CBM_TEST_BINARY:-${ROOT}/build/c/codebase-memory-mcp}"
15+
if [[ ! -x "${BINARY}" && -x "${BINARY}.exe" ]]; then
16+
BINARY="${BINARY}.exe"
17+
fi
18+
19+
if [[ ! -x "${BINARY}" ]]; then
20+
echo "missing binary: ${BINARY}" >&2
21+
exit 2
22+
fi
23+
24+
if command -v shasum >/dev/null 2>&1; then
25+
BUILD_FINGERPRINT="$(shasum -a 256 "${BINARY}" | awk '{print $1}')"
26+
elif command -v sha256sum >/dev/null 2>&1; then
27+
BUILD_FINGERPRINT="$(sha256sum "${BINARY}" | awk '{print $1}')"
28+
elif command -v openssl >/dev/null 2>&1; then
29+
BUILD_FINGERPRINT="$(openssl dgst -sha256 "${BINARY}" | awk '{print $NF}')"
30+
else
31+
echo "no SHA-256 command available for worker build binding" >&2
32+
exit 2
33+
fi
34+
if [[ ! "${BUILD_FINGERPRINT}" =~ ^[0-9a-f]{64}$ ]]; then
35+
echo "invalid worker build fingerprint: ${BUILD_FINGERPRINT}" >&2
36+
exit 2
37+
fi
38+
39+
# shellcheck source=../scripts/test-runtime.sh
40+
source "${ROOT}/scripts/test-runtime.sh"
41+
cbm_test_runtime_init
42+
tmpdir="${CBM_TEST_RUNTIME_ROOT}"
43+
cleanup() {
44+
cbm_test_runtime_cleanup "${BINARY}"
45+
}
46+
trap cleanup EXIT
47+
48+
# Product-facing path, the way the daemon hands it to the worker: canonical on
49+
# POSIX, mixed-style on a native Windows binary (test-runtime.sh's convention).
50+
product_path() {
51+
case "$(uname -s)" in
52+
MINGW*|MSYS*|CYGWIN*) cygpath -m "$1" ;;
53+
*) (cd "$1" && pwd -P) ;;
54+
esac
55+
}
56+
57+
# Root A is what the daemon's starter allowed; the admitted session lives under
58+
# root B. The two share no prefix.
59+
mkdir -p "${tmpdir}/root-a" "${tmpdir}/root-b/tiny"
60+
printf 'int tiny_main(void) { return 0; }\n' >"${tmpdir}/root-b/tiny/tiny.c"
61+
root_a="$(product_path "${tmpdir}/root-a")"
62+
repo="$(product_path "${tmpdir}/root-b/tiny")"
63+
64+
run_worker() {
65+
local args="$1" response="$2" out="$3" err="$4"
66+
CBM_ALLOWED_ROOT="${root_a}" \
67+
"${BINARY}" cli --index-worker \
68+
--index-worker-build "${BUILD_FINGERPRINT}" \
69+
index_repository "${args}" \
70+
--response-out "${response}" >"${out}" 2>"${err}"
71+
}
72+
73+
response="${tmpdir}/scoped.response"
74+
if ! run_worker "{\"repo_path\":\"${repo}\",\"mode\":\"fast\"}" "${response}" \
75+
"${tmpdir}/scoped.out" "${tmpdir}/scoped.err"; then
76+
echo "worker exited nonzero for an admitted request" >&2
77+
cat "${tmpdir}/scoped.err" >&2
78+
exit 1
79+
fi
80+
if [[ ! -s "${response}" ]]; then
81+
echo "worker delivered no response" >&2
82+
exit 1
83+
fi
84+
if grep -q 'outside the allowed root' "${response}"; then
85+
echo "worker re-decided the workspace boundary from the daemon environment" >&2
86+
cat "${response}" >&2
87+
exit 1
88+
fi
89+
if ! grep -q '"status":"indexed"' "${response}"; then
90+
echo "worker did not index the admitted repository" >&2
91+
cat "${response}" >&2
92+
exit 1
93+
fi
94+
95+
# Fail closed: no repo_path means no request scope, so no indexing at all.
96+
unscoped="${tmpdir}/unscoped.response"
97+
if run_worker '{"mode":"fast"}' "${unscoped}" "${tmpdir}/unscoped.out" "${tmpdir}/unscoped.err"; then
98+
echo "worker ran without a request workspace scope" >&2
99+
exit 1
100+
fi
101+
if [[ -s "${unscoped}" ]] || ! grep -q 'request workspace scope invalid' "${tmpdir}/unscoped.err"; then
102+
echo "worker without repo_path did not fail closed on scope" >&2
103+
cat "${tmpdir}/unscoped.err" >&2
104+
exit 1
105+
fi
106+
107+
echo "ok: index worker is scoped to the admitted request, not the daemon environment"

0 commit comments

Comments
 (0)