Skip to content

Commit 7bb1de0

Browse files
authored
Merge pull request #2479 from DeusData/fix/workspace-resolved-home
fix(workspace): compare the home directory in its canonical form
2 parents f40b1a5 + 2e6dc08 commit 7bb1de0

3 files changed

Lines changed: 142 additions & 9 deletions

File tree

‎src/foundation/workspace.c‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "foundation/compat.h"
88
#include "foundation/compat_fs.h"
9+
#include "foundation/constants.h"
910
#include "foundation/platform.h"
1011
#include "foundation/sha256.h"
1112

@@ -239,6 +240,22 @@ static bool ws_is_windows_user_programs_tree(const char *path) {
239240
return true;
240241
}
241242

243+
/* strncmp that treats "/" and "\" as the same character. On Windows a
244+
* canonical path arrives with backslashes from the resolver and with forward
245+
* slashes from callers that normalize, and both spell one directory. Stops at
246+
* the end of either string like strncmp does. */
247+
static bool ws_prefix_equal(const char *a, const char *b, size_t len) {
248+
for (size_t i = 0; i < len; i++) {
249+
if (a[i] != b[i] && !(ws_is_sep(a[i]) && ws_is_sep(b[i]))) {
250+
return false;
251+
}
252+
if (a[i] == '\0') {
253+
break;
254+
}
255+
}
256+
return true;
257+
}
258+
242259
/* True when b is a or lives under a. Compares on a separator boundary so
243260
* "/a/bc" is not treated as living under "/a/b". */
244261
static bool ws_is_ancestor_or_equal(const char *a, const char *b) {
@@ -249,7 +266,7 @@ static bool ws_is_ancestor_or_equal(const char *a, const char *b) {
249266
while (la > 1 && ws_is_sep(a[la - 1])) {
250267
la--;
251268
}
252-
if (strncmp(a, b, la) != 0) {
269+
if (!ws_prefix_equal(a, b, la)) {
253270
return false;
254271
}
255272
return b[la] == '\0' || ws_is_sep(b[la]);
@@ -600,14 +617,34 @@ bool cbm_workspace_root_allowed(const char *canonical_path, const char *home_dir
600617
/* ── Environment helpers ──────────────────────────────────────────────────── */
601618

602619
/* Callers should not each re-derive these; a caller that resolved the home
603-
* directory differently would classify the same path differently. */
620+
* directory differently would classify the same path differently.
621+
*
622+
* The value is the CANONICAL form of $HOME (then %USERPROFILE%), through the
623+
* same resolver callers apply to the path they classify. The policy compares
624+
* the two byte for byte, and a home reached through a link — "/home" kept on
625+
* another volume, a macOS firmlink, a HOME that is itself a link — differs
626+
* from its resolved form, so the raw value would never match and the home
627+
* rule would not fire. A home that cannot be resolved (it does not exist)
628+
* falls back to the raw value, which is all the comparison could use anyway.
629+
*
630+
* Storage is per thread and recomputed on every call, as cbm_resolve_cache_dir
631+
* does: no lock, a changed environment is seen at once, and the pointer stays
632+
* valid until the next call on the same thread. */
604633
const char *cbm_workspace_home_dir(void) {
605-
const char *home = getenv("HOME");
606-
if (home && home[0]) {
607-
return home;
634+
static CBM_TLS char resolved[CBM_SZ_4K];
635+
char raw[CBM_SZ_4K];
636+
const char *home = cbm_safe_getenv("HOME", raw, sizeof(raw), NULL);
637+
if (!home || !home[0]) {
638+
home = cbm_safe_getenv("USERPROFILE", raw, sizeof(raw), NULL);
639+
}
640+
if (!home || !home[0]) {
641+
return NULL;
642+
}
643+
if (!cbm_canonical_path(home, resolved, sizeof(resolved))) {
644+
memcpy(resolved, raw, strlen(raw) + 1);
608645
}
609-
home = getenv("USERPROFILE");
610-
return (home && home[0]) ? home : NULL;
646+
cbm_normalize_path_sep(resolved);
647+
return resolved;
611648
}
612649

613650
const char *cbm_workspace_cache_dir(void) {

‎tests/test_mcp.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,8 +1963,14 @@ TEST(mcp_issue403_sensitive_root_stops_before_discovery_count) {
19631963
}
19641964

19651965
TEST(mcp_issue403_explicit_approval_preserves_auto_index) {
1966-
char *sensitive_home = th_mktempdir("cbm_mcp_403_home");
1967-
ASSERT_NOT_NULL(sensitive_home);
1966+
char *created = th_mktempdir("cbm_mcp_403_home");
1967+
ASSERT_NOT_NULL(created);
1968+
/* The session root is presented in its resolved form, as the daemon does,
1969+
* because the home helper hands out the resolved home: with the raw
1970+
* spelling the two differ on macOS (/tmp is a firmlink) and the root
1971+
* would pass as an ordinary one, never reaching the approval path. */
1972+
char sensitive_home[4096];
1973+
ASSERT_TRUE(cbm_canonical_path(created, sensitive_home, sizeof(sensitive_home)));
19681974
const char *saved_home = getenv("HOME");
19691975
char *saved_home_copy = saved_home ? strdup(saved_home) : NULL;
19701976
cbm_setenv("HOME", sensitive_home, 1);

‎tests/test_workspace.c‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
#include "test_helpers.h"
1010
#include "foundation/workspace.h"
1111
#include "foundation/compat_fs.h"
12+
#include "foundation/platform.h"
1213
#include <stdio.h>
14+
#include <stdlib.h>
1315
#include <string.h>
16+
#ifndef _WIN32
17+
#include <unistd.h>
18+
#endif
1419

1520
static const char *HOME = "/Users/dev";
1621
static const char *CACHE = "/Users/dev/.cache/codebase-memory-mcp";
@@ -375,6 +380,89 @@ TEST(ws_manifest_approval_refuses_overbroad_requests) {
375380
PASS();
376381
}
377382

383+
/* ── Environment helpers ────────────────────────────────────────────────── */
384+
385+
static char *ws_env_save(const char *name) {
386+
const char *value = getenv(name);
387+
return value ? strdup(value) : NULL;
388+
}
389+
390+
static void ws_env_restore(const char *name, char *saved) {
391+
if (saved) {
392+
(void)cbm_setenv(name, saved, 1);
393+
free(saved);
394+
} else {
395+
(void)cbm_unsetenv(name);
396+
}
397+
}
398+
399+
/* The policy compares the home directory against canonical paths, so the
400+
* helper must hand out the canonical form. A plain temp directory already
401+
* shows the difference: on macOS /tmp is a firmlink to /private/tmp, and on
402+
* Windows the temp path may carry a short (8.3) component. */
403+
TEST(ws_home_dir_is_resolved) {
404+
char *created = th_mktempdir("cbm_ws_home");
405+
ASSERT_NOT_NULL(created);
406+
char real[256];
407+
snprintf(real, sizeof(real), "%s", created);
408+
char expected[4096];
409+
ASSERT_TRUE(cbm_canonical_path(real, expected, sizeof(expected)));
410+
cbm_normalize_path_sep(expected);
411+
412+
char *saved_home = ws_env_save("HOME");
413+
ASSERT_EQ(cbm_setenv("HOME", real, 1), 0);
414+
const char *home = cbm_workspace_home_dir();
415+
bool resolved = home && strcmp(home, expected) == 0;
416+
ws_env_restore("HOME", saved_home);
417+
th_cleanup(real);
418+
419+
ASSERT_TRUE(resolved);
420+
PASS();
421+
}
422+
423+
/* A home directory reached through a link — "/home" kept on another volume,
424+
* an account whose HOME is itself a link — is still the home directory when
425+
* a caller presents its resolved path, and must be refused as such. */
426+
TEST(ws_linked_home_classified_as_home) {
427+
#ifdef _WIN32
428+
/* symlink() does not exist on Windows, and creating a symbolic link there
429+
* needs a privilege an ordinary account (and the CI runner) does not hold.
430+
* A directory junction (cmd.exe mklink /J) is the Windows shape of the
431+
* same case and would need a cmd.exe fixture; the resolution the case
432+
* depends on is covered on Windows by ws_home_dir_is_resolved, and the
433+
* link itself is exercised on POSIX. */
434+
SKIP_PLATFORM("Windows: symlink() unavailable; links need a privilege");
435+
#else
436+
char *created = th_mktempdir("cbm_ws_linked_home");
437+
ASSERT_NOT_NULL(created);
438+
char base[256];
439+
snprintf(base, sizeof(base), "%s", created);
440+
char real[512];
441+
char link[512];
442+
snprintf(real, sizeof(real), "%s/real", base);
443+
snprintf(link, sizeof(link), "%s/link", base);
444+
ASSERT_EQ(cbm_mkdir(real), 0);
445+
ASSERT_EQ(symlink(real, link), 0);
446+
447+
char canonical_real[4096];
448+
ASSERT_TRUE(cbm_canonical_path(real, canonical_real, sizeof(canonical_real)));
449+
/* The fixture proves something only when the two spellings differ. */
450+
ASSERT_TRUE(strcmp(link, canonical_real) != 0);
451+
452+
char *saved_home = ws_env_save("HOME");
453+
ASSERT_EQ(cbm_setenv("HOME", link, 1), 0);
454+
const char *home = cbm_workspace_home_dir();
455+
bool resolved = home && strcmp(home, canonical_real) == 0;
456+
cbm_ws_verdict_t verdict = cbm_workspace_classify_root(canonical_real, home, NULL);
457+
ws_env_restore("HOME", saved_home);
458+
th_cleanup(base);
459+
460+
ASSERT_TRUE(resolved);
461+
ASSERT_EQ(verdict, CBM_WS_DENY_SENSITIVE);
462+
PASS();
463+
#endif
464+
}
465+
378466
SUITE(workspace) {
379467
RUN_TEST(ws_manifest_absent_is_not_an_error);
380468
RUN_TEST(ws_manifest_parses_entries_and_skips_comments);
@@ -396,4 +484,6 @@ SUITE(workspace) {
396484
RUN_TEST(ws_posix_matching_is_case_sensitive);
397485
RUN_TEST(ws_null_context_disables_dependent_checks);
398486
RUN_TEST(ws_every_verdict_has_a_reason);
487+
RUN_TEST(ws_home_dir_is_resolved);
488+
RUN_TEST(ws_linked_home_classified_as_home);
399489
}

0 commit comments

Comments
 (0)