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
1520static const char * HOME = "/Users/dev" ;
1621static 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+
378466SUITE (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