Skip to content

Commit 8553224

Browse files
committed
fix(workspace): treat '/' and '\' as the same separator in root matching
The Windows sensitive-root escape hatch broke on separator spelling: the grant is recorded from cbm_canonical_path (backslashes) while the indexer hands the candidate normalized to '/'. ws_is_ancestor_or_equal compared bytes, so an approved 'Program Files' root never matched the candidate and --approve-sensitive could not lift the refusal. Compare component boundaries with separator equivalence, consistent with ws_is_sep used everywhere else in this module. The containment rule (a separator byte is never part of a component name) is unchanged. Fixes DeusData#1718 Signed-off-by: Ulises Millan Guerrero <ulises.millanguerrero@gmail.com>
1 parent 010569f commit 8553224

2 files changed

Lines changed: 108 additions & 3 deletions

File tree

‎src/foundation/workspace.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,16 @@ static bool ws_any_component_matches(const char *path, const char *const *names,
212212
}
213213

214214
/* True when b is a or lives under a. Compares on a separator boundary so
215-
* "/a/bc" is not treated as living under "/a/b". */
215+
* "/a/bc" is not treated as living under "/a/b".
216+
*
217+
* '/' and '\' are equivalent here, as they are everywhere else in this module
218+
* (ws_is_sep). The Windows sensitive-grant escape hatch (#1718) records the
219+
* grant from cbm_canonical_path — backslashes — while the indexer candidate
220+
* arrives normalized to '/'. A byte-exact comparison made the grant never
221+
* match the candidate on Windows, so --approve-sensitive could not lift a
222+
* "Program Files" refusal. Comparing on the separator-equivalence boundary
223+
* keeps the containment semantics (a separator byte is never "part of a
224+
* component name") while erasing the platform spelling difference. */
216225
static bool ws_is_ancestor_or_equal(const char *a, const char *b) {
217226
if (!a || !b || !a[0] || !b[0]) {
218227
return false;
@@ -221,8 +230,13 @@ static bool ws_is_ancestor_or_equal(const char *a, const char *b) {
221230
while (la > 1 && ws_is_sep(a[la - 1])) {
222231
la--;
223232
}
224-
if (strncmp(a, b, la) != 0) {
225-
return false;
233+
for (size_t i = 0; i < la; i++) {
234+
if (a[i] == b[i]) {
235+
continue;
236+
}
237+
if (!(ws_is_sep(a[i]) && ws_is_sep(b[i]))) {
238+
return false;
239+
}
226240
}
227241
return b[la] == '\0' || ws_is_sep(b[la]);
228242
}

‎tests/test_workspace.c‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,96 @@ TEST(ws_manifest_approval_refuses_overbroad_requests) {
261261
PASS();
262262
}
263263

264+
/* #1718: the sensitive-root escape hatch broke on Windows separator spelling.
265+
* The grant is recorded from cbm_canonical_path — backslashes — while the
266+
* indexer hands the candidate already normalized to '/'. A byte-exact
267+
* comparison made the grant never match the candidate, so an approved
268+
* "Program Files" root stayed refused. '/' and '\' are equivalent everywhere
269+
* else in this module (ws_is_sep); the boundary comparison must treat them
270+
* equally. The classify_root home rule uses the same ws_paths_equal, so a
271+
* mixed-separator home spelling is the string-level regression check; the
272+
* FS-backed grant round-trip lives in the _WIN32 half, where the real
273+
* canonicalization exercises it. */
274+
TEST(ws_approve_sensitive_matches_across_separator_styles) {
275+
/* canonical spelled with '\' vs home spelled with '/' — must classify as
276+
* the same directory. Pre-fix: strncmp mismatch at the separator byte, so
277+
* the home rule never matched and an ordinary path under "C:\Users" fell
278+
* through to CBM_WS_ALLOW. */
279+
ASSERT_EQ(cbm_workspace_classify_root("C:\\Users\\dev", "C:/Users/dev", CACHE),
280+
CBM_WS_DENY_SENSITIVE);
281+
ASSERT_EQ(cbm_workspace_classify_root("C:/Users/dev", "C:\\Users\\dev", CACHE),
282+
CBM_WS_DENY_SENSITIVE);
283+
/* A sibling that differs after the same separator boundary is still a
284+
* different directory: "/a/bc" must never look like "/a/b". */
285+
ASSERT_EQ(cbm_workspace_classify_root("C:\\Users\\devx", "C:/Users/dev", CACHE), CBM_WS_ALLOW);
286+
#ifdef _WIN32
287+
/* Full grant round-trip on real filesystem paths: the grant stored with
288+
* '\' must lift the exact candidate the indexer passes with '/'. The
289+
* sensitive class is exercised by a credential-named directory (".ssh"),
290+
* which is DENY_SENSITIVE at any depth — equivalent to "Program Files"
291+
* for the refusal path under test. */
292+
char *cache = th_mktempdir("cbm_ws_sw");
293+
ASSERT(cache != NULL);
294+
char dir[1024];
295+
snprintf(dir, sizeof(dir), "%s/.ssh/cfg", cache);
296+
ASSERT_TRUE(cbm_mkdir_p(dir, 0700));
297+
char path[1024];
298+
snprintf(path, sizeof(path), "%s/allowed_roots", cache);
299+
300+
char grant[2048];
301+
snprintf(grant, sizeof(grant), "!%s\\.ssh\\cfg\n", cache);
302+
FILE *f = fopen(path, "w");
303+
ASSERT_NOT_NULL(f);
304+
(void)fprintf(f, "%s", grant);
305+
(void)fclose(f);
306+
307+
char candidate[2048];
308+
snprintf(candidate, sizeof(candidate), "%s/.ssh/cfg", cache);
309+
/* Convert to '/' spelling, exactly as cbm_normalize_path_sep does for the
310+
* indexer's candidate. */
311+
char *p;
312+
for (p = candidate; *p; p++) {
313+
if (*p == '\\') {
314+
*p = '/';
315+
}
316+
}
317+
char err[1024];
318+
err[0] = '\0';
319+
ASSERT_TRUE(cbm_workspace_root_allowed(candidate, NULL, cache, NULL, err, sizeof(err)));
320+
321+
/* An unapproved sibling under the same sensitive tree stays refused. It
322+
* lies outside every granted root, so the refusal is the boundary wording
323+
* (kept verbatim by contract), not the sensitive hint. */
324+
char sibling[2048];
325+
snprintf(sibling, sizeof(sibling), "%s/.ssh/keys", cache);
326+
for (p = sibling; *p; p++) {
327+
if (*p == '\\') {
328+
*p = '/';
329+
}
330+
}
331+
err[0] = '\0';
332+
ASSERT_FALSE(cbm_workspace_root_allowed(sibling, NULL, cache, NULL, err, sizeof(err)));
333+
ASSERT_TRUE(strstr(err, "outside the allowed root") != NULL);
334+
335+
/* A strict descendant INSIDE the approved sensitive grant is contained
336+
* (boundary passes) but not the exact grant, so the sensitive refusal
337+
* applies and the hint names the flag. */
338+
char descendant[2048];
339+
snprintf(descendant, sizeof(descendant), "%s/.ssh/cfg/sub", cache);
340+
for (p = descendant; *p; p++) {
341+
if (*p == '\\') {
342+
*p = '/';
343+
}
344+
}
345+
err[0] = '\0';
346+
ASSERT_FALSE(cbm_workspace_root_allowed(descendant, NULL, cache, NULL, err, sizeof(err)));
347+
ASSERT_TRUE(strstr(err, "--approve-sensitive") != NULL);
348+
349+
th_cleanup(cache);
350+
#endif
351+
PASS();
352+
}
353+
264354
SUITE(workspace) {
265355
RUN_TEST(ws_manifest_absent_is_not_an_error);
266356
RUN_TEST(ws_manifest_parses_entries_and_skips_comments);
@@ -278,4 +368,5 @@ SUITE(workspace) {
278368
RUN_TEST(ws_posix_matching_is_case_sensitive);
279369
RUN_TEST(ws_null_context_disables_dependent_checks);
280370
RUN_TEST(ws_every_verdict_has_a_reason);
371+
RUN_TEST(ws_approve_sensitive_matches_across_separator_styles);
281372
}

0 commit comments

Comments
 (0)