Skip to content

Commit 8c7b3bd

Browse files
authored
Merge pull request DeusData#1786 from umi008/fix/issue-1718-approve-sensitive-separators
fix(workspace): treat '/' and '\' as the same separator in root matching (DeusData#1718)
2 parents 9c89041 + 0286569 commit 8c7b3bd

2 files changed

Lines changed: 105 additions & 1 deletion

File tree

‎src/foundation/workspace.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,16 @@ static bool ws_prefix_equal(const char *a, const char *b, size_t len) {
257257
}
258258

259259
/* True when b is a or lives under a. Compares on a separator boundary so
260-
* "/a/bc" is not treated as living under "/a/b". */
260+
* "/a/bc" is not treated as living under "/a/b".
261+
*
262+
* '/' and '\' are equivalent here, as they are everywhere else in this module
263+
* (ws_is_sep). The Windows sensitive-grant escape hatch (#1718) records the
264+
* grant from cbm_canonical_path — backslashes — while the indexer candidate
265+
* arrives normalized to '/'. A byte-exact comparison made the grant never
266+
* match the candidate on Windows, so --approve-sensitive could not lift a
267+
* "Program Files" refusal. Comparing on the separator-equivalence boundary
268+
* keeps the containment semantics (a separator byte is never "part of a
269+
* component name") while erasing the platform spelling difference. */
261270
static bool ws_is_ancestor_or_equal(const char *a, const char *b) {
262271
if (!a || !b || !a[0] || !b[0]) {
263272
return false;

‎tests/test_workspace.c‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

419513
static 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

Comments
 (0)