Skip to content

Commit 7d74337

Browse files
authored
Merge pull request DeusData#2076 from DeusData/fix/index-worker-session-scope
fix(worker): scope the index worker to the request's repository, not the daemon's environment
2 parents 93486bc + 2b80b31 commit 7d74337

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
@@ -352,6 +352,12 @@ CBM_TEST_BINARY="$WATCHDOG_BINARY" bash "$ROOT/tests/test_worker_error_response.
352352
echo "=== Step 5e: watcher_enabled kill-switch regression (#335) ==="
353353
CBM_TEST_BINARY="$WATCHDOG_BINARY" bash "$ROOT/tests/test_watcher_disabled.sh"
354354

355+
# Step 5f: a supervised worker is scoped to the request the daemon admitted,
356+
# never to the CBM_ALLOWED_ROOT it inherited from the daemon starter's
357+
# environment. Reuses the prod binary built in Step 5.
358+
echo "=== Step 5f: worker request-scope regression ==="
359+
CBM_TEST_BINARY="$WATCHDOG_BINARY" bash "$ROOT/tests/test_worker_session_scope.sh"
360+
355361
# Step 6: security-strings URL allow-list regression. The MSYS2 CLANG64 toolchain
356362
# bakes its package-tracker URL into the static Windows .exe; the binary string
357363
# 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
@@ -789,6 +789,36 @@ static bool cli_first_nonspace_is_brace(const char *s) {
789789
static char *main_local_cli_daemon_execute(const char *tool_name, const char *args_json,
790790
bool quiet_requested);
791791

792+
/* A supervised worker runs in the DAEMON's environment, not the requesting
793+
* client's. The daemon admitted this request under the client's session policy
794+
* and re-executed us with the canonical repo_path in the args (both daemon
795+
* spawn paths rewrite it), so the request itself is the worker's workspace
796+
* scope: repository == session root == allowed root. Without this, a fresh
797+
* server fell back to the process-wide CBM_ALLOWED_ROOT inherited from whoever
798+
* started the daemon and refused every admitted session outside it. Returns
799+
* NULL once scoped, otherwise the reason to fail closed: a worker never indexes
800+
* under an ambient policy.
801+
*
802+
* A repository that cannot be canonicalized (it does not exist) can only have
803+
* been admitted by a session with no declared boundary, because containment
804+
* needs a real path. The worker mirrors that with an explicit unrestricted
805+
* policy - still never the environment fallback - so the pipeline reports the
806+
* missing repository as the tool error it always was, instead of the
807+
* supervisor misreading a refused worker as a crash. */
808+
static const char *main_index_worker_scope_request(cbm_mcp_server_t *srv, const char *args_json) {
809+
char *repo_path = cbm_mcp_get_string_arg(args_json, "repo_path");
810+
if (!repo_path || !repo_path[0]) {
811+
free(repo_path);
812+
return "request names no repo_path";
813+
}
814+
char canonical[MAIN_PATH_CAP];
815+
bool exists = cbm_canonical_path(repo_path, canonical, sizeof(canonical)) != 0;
816+
const char *scope = exists ? canonical : repo_path;
817+
bool scoped = cbm_mcp_server_set_session_context(srv, scope, exists ? scope : NULL);
818+
free(repo_path);
819+
return scoped ? NULL : "session context could not be installed";
820+
}
821+
792822
static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_locks,
793823
main_local_maintenance_context_t *maintenance_context) {
794824
cbm_cli_output_flags_t output_flags;
@@ -930,11 +960,15 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
930960
};
931961
bool maintenance_binding_failed = false;
932962
bool maintenance_cancelled = false;
963+
const char *worker_scope_refused = NULL;
933964
if (!index_worker) {
934965
result = main_local_cli_daemon_execute(tool_name, args_json, output_flags.quiet_requested);
935966
} else {
936967
srv = cbm_mcp_server_new(NULL);
937968
if (srv) {
969+
worker_scope_refused = main_index_worker_scope_request(srv, args_json);
970+
}
971+
if (srv && !worker_scope_refused) {
938972
/* The in-process worker is a standalone instance: it may not
939973
* launch MCP-session background tasks. It receives project_locks
940974
* from its own process-level coordination setup and therefore
@@ -947,8 +981,8 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
947981
main_local_cli_mutation_try_begin);
948982
}
949983
}
950-
maintenance_binding_failed = srv && !maintenance_context;
951-
if (srv && maintenance_context) {
984+
maintenance_binding_failed = srv && !worker_scope_refused && !maintenance_context;
985+
if (srv && !worker_scope_refused && maintenance_context) {
952986
main_local_maintenance_server_bind(maintenance_context, srv);
953987
result = cbm_mcp_handle_tool(srv, tool_name, args_json);
954988
/* Unbind under the same mutex used by cancellation before any
@@ -960,7 +994,10 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
960994
}
961995
}
962996
if (!result) {
963-
if (maintenance_binding_failed) {
997+
if (worker_scope_refused) {
998+
(void)fprintf(stderr, "error: request workspace scope invalid: %s\n",
999+
worker_scope_refused);
1000+
} else if (maintenance_binding_failed) {
9641001
(void)fprintf(stderr,
9651002
"error: local %s maintenance cancellation could not bind safely\n",
9661003
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)