@@ -414,6 +414,100 @@ TEST(ws_manifest_approval_refuses_overbroad_requests) {
414414 PASS ();
415415}
416416
417+ /* #1718: the sensitive-root escape hatch broke on Windows separator spelling.
418+ * The grant is recorded from cbm_canonical_path — backslashes — while the
419+ * indexer hands the candidate already normalized to '/'. A byte-exact
420+ * comparison made the grant never match the candidate, so an approved
421+ * "Program Files" root stayed refused. '/' and '\' are equivalent everywhere
422+ * else in this module (ws_is_sep); the boundary comparison must treat them
423+ * equally. The classify_root home rule uses the same ws_paths_equal, so a
424+ * mixed-separator home spelling is the string-level regression check; the
425+ * FS-backed grant round-trip lives in the _WIN32 half, where the real
426+ * canonicalization exercises it. */
427+ TEST (ws_approve_sensitive_matches_across_separator_styles ) {
428+ /* canonical spelled with '\' vs home spelled with '/' — must classify as
429+ * the same directory. Pre-fix: strncmp mismatch at the separator byte, so
430+ * the home rule never matched and an ordinary path under "C:\Users" fell
431+ * through to CBM_WS_ALLOW. */
432+ ASSERT_EQ (cbm_workspace_classify_root ("C:\\Users\\dev" , "C:/Users/dev" , CACHE ),
433+ CBM_WS_DENY_SENSITIVE );
434+ ASSERT_EQ (cbm_workspace_classify_root ("C:/Users/dev" , "C:\\Users\\dev" , CACHE ),
435+ CBM_WS_DENY_SENSITIVE );
436+ /* A sibling that differs after the same separator boundary is still a
437+ * different directory: "/a/bc" must never look like "/a/b". */
438+ ASSERT_EQ (cbm_workspace_classify_root ("C:\\Users\\devx" , "C:/Users/dev" , CACHE ), CBM_WS_ALLOW );
439+ #ifdef _WIN32
440+ /* Full grant round-trip on real filesystem paths: the grant stored with
441+ * '\' must lift the exact candidate the indexer passes with '/'. The
442+ * sensitive class is exercised by a credential-named directory (".ssh"),
443+ * which is DENY_SENSITIVE at any depth — equivalent to "Program Files"
444+ * for the refusal path under test. */
445+ char * cache = th_mktempdir ("cbm_ws_sw" );
446+ ASSERT (cache != NULL );
447+ char dir [1024 ];
448+ snprintf (dir , sizeof (dir ), "%s/.ssh/cfg" , cache );
449+ ASSERT_TRUE (cbm_mkdir_p (dir , 0700 ));
450+ char path [1024 ];
451+ snprintf (path , sizeof (path ), "%s/allowed_roots" , cache );
452+
453+ char grant [2048 ];
454+ snprintf (grant , sizeof (grant ), "!%s\\.ssh\\cfg\n" , cache );
455+ FILE * f = cbm_fopen (path , "wb" );
456+ ASSERT_NOT_NULL (f );
457+ (void )fprintf (f , "%s" , grant );
458+ (void )fclose (f );
459+
460+ char candidate [2048 ];
461+ snprintf (candidate , sizeof (candidate ), "%s/.ssh/cfg" , cache );
462+ /* Convert to '/' spelling, exactly as cbm_normalize_path_sep does for the
463+ * indexer's candidate. */
464+ char * p ;
465+ for (p = candidate ; * p ; p ++ ) {
466+ if (* p == '\\' ) {
467+ * p = '/' ;
468+ }
469+ }
470+ char err [1024 ];
471+ err [0 ] = '\0' ;
472+ ASSERT_TRUE (cbm_workspace_root_allowed (candidate , NULL , cache , NULL , err , sizeof (err )));
473+
474+ /* An unapproved sibling under the same sensitive tree stays refused. It
475+ * lies outside every granted root, so the refusal is the boundary wording
476+ * (kept verbatim by contract), not the sensitive hint. */
477+ char sibling [2048 ];
478+ snprintf (sibling , sizeof (sibling ), "%s/.ssh/keys" , cache );
479+ for (p = sibling ; * p ; p ++ ) {
480+ if (* p == '\\' ) {
481+ * p = '/' ;
482+ }
483+ }
484+ err [0 ] = '\0' ;
485+ ASSERT_FALSE (cbm_workspace_root_allowed (sibling , NULL , cache , NULL , err , sizeof (err )));
486+ ASSERT_TRUE (strstr (err , "outside the allowed root" ) != NULL );
487+
488+ /* A strict descendant INSIDE the approved sensitive grant is contained
489+ * (boundary passes) but not the exact grant, so the sensitive refusal
490+ * applies and the hint names the flag. */
491+ char descendant [2048 ];
492+ snprintf (descendant , sizeof (descendant ), "%s/.ssh/cfg/sub" , cache );
493+ for (p = descendant ; * p ; p ++ ) {
494+ if (* p == '\\' ) {
495+ * p = '/' ;
496+ }
497+ }
498+ /* Containment canonicalizes the candidate, which on Windows opens it
499+ * (CreateFileW, OPEN_EXISTING): a descendant that is not on disk is
500+ * "outside the allowed root" instead of reaching the sensitive hint. */
501+ ASSERT_TRUE (cbm_mkdir_p (descendant , 0700 ));
502+ err [0 ] = '\0' ;
503+ ASSERT_FALSE (cbm_workspace_root_allowed (descendant , NULL , cache , NULL , err , sizeof (err )));
504+ ASSERT_TRUE (strstr (err , "--approve-sensitive" ) != NULL );
505+
506+ th_cleanup (cache );
507+ #endif
508+ PASS ();
509+ }
510+
417511/* ── Environment helpers ────────────────────────────────────────────────── */
418512
419513static char * ws_env_save (const char * name ) {
@@ -835,6 +929,7 @@ SUITE(workspace) {
835929 RUN_TEST (ws_posix_matching_is_case_sensitive );
836930 RUN_TEST (ws_null_context_disables_dependent_checks );
837931 RUN_TEST (ws_every_verdict_has_a_reason );
932+ RUN_TEST (ws_approve_sensitive_matches_across_separator_styles );
838933 RUN_TEST (ws_home_dir_is_resolved );
839934 RUN_TEST (ws_linked_home_classified_as_home );
840935 RUN_TEST (ws_manifest_entry_classified_in_resolved_form );
0 commit comments