Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

Merge branch 'main' into fix/issue-1718-approve-sensitive-separators

Martin Vogel5a04e424e7e5
diff --cc src/foundation/workspace.c
index 8c6b34b8d,eab52d48f..952e9aba0
--- a/src/foundation/workspace.c
+++ b/src/foundation/workspace.c
@@@ -211,17 -212,52 +212,61 @@@ static bool ws_any_component_matches(co
return false;
}
+ /* The default per-user Windows application prefix. Match from the drive root,
+ * by whole segments, so a project merely containing the same sequence (or a
+ * sibling such as Programs-src) is not captured. The second segment is the one
+ * and only user name; its spelling is deliberately unrestricted. */
+ static bool ws_is_windows_user_programs_tree(const char *path) {
+ if (!ws_is_windows_style(path)) {
+ return false;
+ }
+ static const char *const expected[] = {"Users", NULL, "AppData", "Local", "Programs"};
+ const char *p = path + ws_volume_prefix_len(path);
+ for (size_t i = 0; i < sizeof(expected) / sizeof(expected[0]); i++) {
+ while (ws_is_sep(*p)) {
+ p++;
+ }
+ if (!*p) {
+ return false;
+ }
+ const char *start = p;
+ while (*p && !ws_is_sep(*p)) {
+ p++;
+ }
+ if (expected[i] && !ws_component_equals(start, (size_t)(p - start), expected[i], true)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /* strncmp that treats "/" and "\" as the same character. On Windows a
+ * canonical path arrives with backslashes from the resolver and with forward
+ * slashes from callers that normalize, and both spell one directory. Stops at
+ * the end of either string like strncmp does. */
+ static bool ws_prefix_equal(const char *a, const char *b, size_t len) {
+ for (size_t i = 0; i < len; i++) {
+ if (a[i] != b[i] && !(ws_is_sep(a[i]) && ws_is_sep(b[i]))) {
+ return false;
+ }
+ if (a[i] == '\0') {
+ break;
+ }
+ }
+ return true;
+ }
+
/* True when b is a or lives under a. Compares on a separator boundary so
- * "/a/bc" is not treated as living under "/a/b". */
+ * "/a/bc" is not treated as living under "/a/b".
+ *
+ * '/' and '\' are equivalent here, as they are everywhere else in this module
+ * (ws_is_sep). The Windows sensitive-grant escape hatch (#1718) records the
+ * grant from cbm_canonical_path — backslashes — while the indexer candidate
+ * arrives normalized to '/'. A byte-exact comparison made the grant never
+ * match the candidate on Windows, so --approve-sensitive could not lift a
+ * "Program Files" refusal. Comparing on the separator-equivalence boundary
+ * keeps the containment semantics (a separator byte is never "part of a
+ * component name") while erasing the platform spelling difference. */
static bool ws_is_ancestor_or_equal(const char *a, const char *b) {
if (!a || !b || !a[0] || !b[0]) {
return false;
diff --cc tests/test_workspace.c
index 89af1d215,7f163db29..b45b28ec1
--- a/tests/test_workspace.c
+++ b/tests/test_workspace.c
@@@ -261,96 -414,406 +414,496 @@@ TEST(ws_manifest_approval_refuses_overb
PASS();
}
+/* #1718: the sensitive-root escape hatch broke on Windows separator spelling.
+ * The grant is recorded from cbm_canonical_path — backslashes — while the
+ * indexer hands the candidate already normalized to '/'. A byte-exact
+ * comparison made the grant never match the candidate, so an approved
+ * "Program Files" root stayed refused. '/' and '\' are equivalent everywhere
+ * else in this module (ws_is_sep); the boundary comparison must treat them
+ * equally. The classify_root home rule uses the same ws_paths_equal, so a
+ * mixed-separator home spelling is the string-level regression check; the
+ * FS-backed grant round-trip lives in the _WIN32 half, where the real
+ * canonicalization exercises it. */
+TEST(ws_approve_sensitive_matches_across_separator_styles) {
+ /* canonical spelled with '\' vs home spelled with '/' — must classify as
+ * the same directory. Pre-fix: strncmp mismatch at the separator byte, so
+ * the home rule never matched and an ordinary path under "C:\Users" fell
+ * through to CBM_WS_ALLOW. */
+ ASSERT_EQ(cbm_workspace_classify_root("C:\\Users\\dev", "C:/Users/dev", CACHE),
+ CBM_WS_DENY_SENSITIVE);
+ ASSERT_EQ(cbm_workspace_classify_root("C:/Users/dev", "C:\\Users\\dev", CACHE),
+ CBM_WS_DENY_SENSITIVE);
+ /* A sibling that differs after the same separator boundary is still a
+ * different directory: "/a/bc" must never look like "/a/b". */
+ ASSERT_EQ(cbm_workspace_classify_root("C:\\Users\\devx", "C:/Users/dev", CACHE), CBM_WS_ALLOW);
+#ifdef _WIN32
+ /* Full grant round-trip on real filesystem paths: the grant stored with
+ * '\' must lift the exact candidate the indexer passes with '/'. The
+ * sensitive class is exercised by a credential-named directory (".ssh"),
+ * which is DENY_SENSITIVE at any depth — equivalent to "Program Files"
+ * for the refusal path under test. */
+ char *cache = th_mktempdir("cbm_ws_sw");
+ ASSERT(cache != NULL);
+ char dir[1024];
+ snprintf(dir, sizeof(dir), "%s/.ssh/cfg", cache);
+ ASSERT_TRUE(cbm_mkdir_p(dir, 0700));
+ char path[1024];
+ snprintf(path, sizeof(path), "%s/allowed_roots", cache);
+
+ char grant[2048];
+ snprintf(grant, sizeof(grant), "!%s\\.ssh\\cfg\n", cache);
+ FILE *f = fopen(path, "w");
+ ASSERT_NOT_NULL(f);
+ (void)fprintf(f, "%s", grant);
+ (void)fclose(f);
+
+ char candidate[2048];
+ snprintf(candidate, sizeof(candidate), "%s/.ssh/cfg", cache);
+ /* Convert to '/' spelling, exactly as cbm_normalize_path_sep does for the
+ * indexer's candidate. */
+ char *p;
+ for (p = candidate; *p; p++) {
+ if (*p == '\\') {
+ *p = '/';
+ }
+ }
+ char err[1024];
+ err[0] = '\0';
+ ASSERT_TRUE(cbm_workspace_root_allowed(candidate, NULL, cache, NULL, err, sizeof(err)));
+
+ /* An unapproved sibling under the same sensitive tree stays refused. It
+ * lies outside every granted root, so the refusal is the boundary wording
+ * (kept verbatim by contract), not the sensitive hint. */
+ char sibling[2048];
+ snprintf(sibling, sizeof(sibling), "%s/.ssh/keys", cache);
+ for (p = sibling; *p; p++) {
+ if (*p == '\\') {
+ *p = '/';
+ }
+ }
+ err[0] = '\0';
+ ASSERT_FALSE(cbm_workspace_root_allowed(sibling, NULL, cache, NULL, err, sizeof(err)));
+ ASSERT_TRUE(strstr(err, "outside the allowed root") != NULL);
+
+ /* A strict descendant INSIDE the approved sensitive grant is contained
+ * (boundary passes) but not the exact grant, so the sensitive refusal
+ * applies and the hint names the flag. */
+ char descendant[2048];
+ snprintf(descendant, sizeof(descendant), "%s/.ssh/cfg/sub", cache);
+ for (p = descendant; *p; p++) {
+ if (*p == '\\') {
+ *p = '/';
+ }
+ }
+ err[0] = '\0';
+ ASSERT_FALSE(cbm_workspace_root_allowed(descendant, NULL, cache, NULL, err, sizeof(err)));
+ ASSERT_TRUE(strstr(err, "--approve-sensitive") != NULL);
+
+ th_cleanup(cache);
+#endif
+ PASS();
+}
+
+ /* ── Environment helpers ────────────────────────────────────────────────── */
+
+ static char *ws_env_save(const char *name) {
+ const char *value = getenv(name);
+ return value ? strdup(value) : NULL;
+ }
+
+ static void ws_env_restore(const char *name, char *saved) {
+ if (saved) {
+ (void)cbm_setenv(name, saved, 1);
+ free(saved);
+ } else {
+ (void)cbm_unsetenv(name);
+ }
+ }
+
+ /* The policy compares the home directory against canonical paths, so the
+ * helper must hand out the canonical form. A plain temp directory already
+ * shows the difference: on macOS /tmp is a firmlink to /private/tmp, and on
+ * Windows the temp path may carry a short (8.3) component. */
+ TEST(ws_home_dir_is_resolved) {
+ char *created = th_mktempdir("cbm_ws_home");
+ ASSERT_NOT_NULL(created);
+ char real[256];
+ snprintf(real, sizeof(real), "%s", created);
+ char expected[4096];
+ ASSERT_TRUE(cbm_canonical_path(real, expected, sizeof(expected)));
+ cbm_normalize_path_sep(expected);
+
+ char *saved_home = ws_env_save("HOME");
+ ASSERT_EQ(cbm_setenv("HOME", real, 1), 0);
+ const char *home = cbm_workspace_home_dir();
+ bool resolved = home && strcmp(home, expected) == 0;
+ ws_env_restore("HOME", saved_home);
+ th_cleanup(real);
+
+ ASSERT_TRUE(resolved);
+ PASS();
+ }
+
+ /* A home directory reached through a link — "/home" kept on another volume,
+ * an account whose HOME is itself a link — is still the home directory when
+ * a caller presents its resolved path, and must be refused as such. */
+ TEST(ws_linked_home_classified_as_home) {
+ #ifdef _WIN32
+ /* symlink() does not exist on Windows, and creating a symbolic link there
+ * needs a privilege an ordinary account (and the CI runner) does not hold.
+ * A directory junction (cmd.exe mklink /J) is the Windows shape of the
+ * same case and would need a cmd.exe fixture; the resolution the case
+ * depends on is covered on Windows by ws_home_dir_is_resolved, and the
+ * link itself is exercised on POSIX. */
+ SKIP_PLATFORM("Windows: symlink() unavailable; links need a privilege");
+ #else
+ char *created = th_mktempdir("cbm_ws_linked_home");
+ ASSERT_NOT_NULL(created);
+ char base[256];
+ snprintf(base, sizeof(base), "%s", created);
+ char real[512];
+ char link[512];
+ snprintf(real, sizeof(real), "%s/real", base);
+ snprintf(link, sizeof(link), "%s/link", base);
+ ASSERT_EQ(cbm_mkdir(real), 0);
+ ASSERT_EQ(symlink(real, link), 0);
+
+ char canonical_real[4096];
+ ASSERT_TRUE(cbm_canonical_path(real, canonical_real, sizeof(canonical_real)));
+ /* The fixture proves something only when the two spellings differ. */
+ ASSERT_TRUE(strcmp(link, canonical_real) != 0);
+
+ char *saved_home = ws_env_save("HOME");
+ ASSERT_EQ(cbm_setenv("HOME", link, 1), 0);
+ const char *home = cbm_workspace_home_dir();
+ bool resolved = home && strcmp(home, canonical_real) == 0;
+ cbm_ws_verdict_t verdict = cbm_workspace_classify_root(canonical_real, home, NULL);
+ ws_env_restore("HOME", saved_home);
+ th_cleanup(base);
+
+ ASSERT_TRUE(resolved);
+ ASSERT_EQ(verdict, CBM_WS_DENY_SENSITIVE);
+ PASS();
+ #endif
+ }
+
+ /* ── Manifest entries are judged in their resolved form ──────────────────── */
+
+ enum { WS_T_PATH = 4096 };
+
+ /* th_mktempdir hands back one static buffer, so copy at once — and in canonical
+ * form, because the policy compares resolved spellings and /tmp is a link on
+ * macOS. */
+ static bool ws_tempdir_canonical(const char *prefix, char *out, size_t out_sz) {
+ char *created = th_mktempdir(prefix);
+ return created && cbm_canonical_path(created, out, out_sz) == 1;
+ }
+
+ static bool ws_join(char *out, size_t out_sz, const char *a, const char *b) {
+ int n = snprintf(out, out_sz, "%s/%s", a, b);
+ return n > 0 && (size_t)n < out_sz;
+ }
+
+ /* Create <parent>/<name> and return it in canonical form. */
+ static bool ws_mkdir_canonical(const char *parent, const char *name, char *out, size_t out_sz) {
+ char made[WS_T_PATH];
+ if (!ws_join(made, sizeof(made), parent, name) || cbm_mkdir(made) != 0) {
+ return false;
+ }
+ return cbm_canonical_path(made, out, out_sz) == 1;
+ }
+
+ static void ws_manifest_write(const char *base, const char *body) {
+ char path[WS_T_PATH];
+ if (ws_join(path, sizeof(path), base, CBM_WS_MANIFEST_NAME)) {
+ ws_write(path, body);
+ }
+ }
+
+ static void ws_manifest_write_line(const char *base, const char *entry) {
+ char body[WS_T_PATH];
+ int n = snprintf(body, sizeof(body), "%s\n", entry);
+ if (n > 0 && (size_t)n < sizeof(body)) {
+ ws_manifest_write(base, body);
+ }
+ }
+
+ /* Put the manifest on record as approved by writing the store line directly,
+ * in the form cbm_workspace_manifest_approve writes ("<digest> <project>").
+ * This is the record an approval leaves behind when the entry was judged by
+ * its spelling, and the use-time check must stand on its own against it. */
+ static bool ws_manifest_record_approval(const char *cache, const char *base) {
+ cbm_ws_manifest_t m;
+ if (!cbm_workspace_manifest_read(base, &m) || !m.present) {
+ return false;
+ }
+ char store[WS_T_PATH];
+ char line[WS_T_PATH];
+ if (!ws_join(store, sizeof(store), cache, "approved_manifests")) {
+ return false;
+ }
+ int n = snprintf(line, sizeof(line), "%s %s\n", m.digest, base);
+ if (n <= 0 || (size_t)n >= sizeof(line)) {
+ return false;
+ }
+ ws_write(store, line);
+ return cbm_workspace_manifest_is_approved(cache, base, &m);
+ }
+
+ /* The directory a manifest entry names is the one that would be indexed, so
+ * that is the form the policy judges — at approval and again at use. Spelled
+ * through `..`, an entry reads as an ordinary path several components deep
+ * while naming the home directory or the volume root. */
+ TEST(ws_manifest_entry_classified_in_resolved_form) {
+ char base[WS_T_PATH];
+ char cache[WS_T_PATH];
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m5", base, sizeof(base)));
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m5c", cache, sizeof(cache)));
+
+ char home[WS_T_PATH];
+ char src[WS_T_PATH];
+ char home_sub[WS_T_PATH];
+ ASSERT_TRUE(ws_mkdir_canonical(base, "home", home, sizeof(home)));
+ ASSERT_TRUE(ws_mkdir_canonical(base, "src", src, sizeof(src)));
+ ASSERT_TRUE(ws_mkdir_canonical(home, "notes", home_sub, sizeof(home_sub)));
+
+ char entry[WS_T_PATH];
+ char expected[WS_T_PATH];
+ char err[1024];
+
+ /* "<base>/src/../home" is the home directory. */
+ ASSERT_TRUE(ws_join(entry, sizeof(entry), src, "../home"));
+ ws_manifest_write_line(base, entry);
+ ASSERT_FALSE(cbm_workspace_manifest_approve(cache, home, base, err, sizeof(err)));
+ ASSERT_NOT_NULL(strstr(err, entry));
+ ASSERT(snprintf(expected, sizeof(expected), "(resolves to %s)", home) > 0);
+ ASSERT_NOT_NULL(strstr(err, expected));
+ ASSERT_NOT_NULL(strstr(err, cbm_workspace_verdict_reason(CBM_WS_DENY_SENSITIVE)));
+
+ /* An approval already on record for this spelling does not outrank the
+ * use-time check. */
+ ASSERT_TRUE(ws_manifest_record_approval(cache, base));
+ ASSERT_FALSE(cbm_workspace_manifest_allows(cache, home, base, home_sub));
+
+ /* Enough `..` segments reach the volume root from anywhere. */
+ ASSERT_TRUE(ws_join(entry, sizeof(entry), base, "../../../../../../../../../../../.."));
+ ws_manifest_write_line(base, entry);
+ ASSERT_FALSE(cbm_workspace_manifest_approve(cache, home, base, err, sizeof(err)));
+ ASSERT_NOT_NULL(strstr(err, entry));
+ ASSERT_NOT_NULL(strstr(err, cbm_workspace_verdict_reason(CBM_WS_DENY_ABSOLUTE)));
+
+ th_cleanup(base);
+ th_cleanup(cache);
+ PASS();
+ }
+
+ /* An entry that does not resolve to a directory is refused at approval, naming
+ * the entry; at use it simply matches nothing, and the other entries of the
+ * same manifest keep working. */
+ TEST(ws_manifest_unresolvable_entry_refused) {
+ char base[WS_T_PATH];
+ char cache[WS_T_PATH];
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m6", base, sizeof(base)));
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m6c", cache, sizeof(cache)));
+
+ char entry[WS_T_PATH];
+ char err[1024];
+
+ /* Missing. */
+ ASSERT_TRUE(ws_join(entry, sizeof(entry), base, "missing"));
+ ws_manifest_write_line(base, entry);
+ ASSERT_FALSE(cbm_workspace_manifest_approve(cache, HOME, base, err, sizeof(err)));
+ ASSERT_NOT_NULL(strstr(err, entry));
+ ASSERT_NOT_NULL(strstr(err, "must name an existing directory"));
+
+ /* Present, but a file. */
+ ASSERT_TRUE(ws_join(entry, sizeof(entry), base, "notes.txt"));
+ ws_write(entry, "not a directory\n");
+ ws_manifest_write_line(base, entry);
+ ASSERT_FALSE(cbm_workspace_manifest_approve(cache, HOME, base, err, sizeof(err)));
+ ASSERT_NOT_NULL(strstr(err, entry));
+ ASSERT_NOT_NULL(strstr(err, "is not a directory"));
+
+ /* Relative: the file is read by a long-lived process whose working
+ * directory is unrelated to the project, so there is no base to resolve
+ * it against. */
+ ws_manifest_write_line(base, "src");
+ ASSERT_FALSE(cbm_workspace_manifest_approve(cache, HOME, base, err, sizeof(err)));
+ ASSERT_NOT_NULL(strstr(err, "requested path src:"));
+ ASSERT_NOT_NULL(strstr(err, "must be an absolute path"));
+
+ /* At use: a directory that disappeared after approval matches nothing and
+ * does not take the rest of the manifest with it. */
+ char present[WS_T_PATH];
+ char present_sub[WS_T_PATH];
+ char body[WS_T_PATH];
+ ASSERT_TRUE(ws_mkdir_canonical(base, "present", present, sizeof(present)));
+ ASSERT_TRUE(ws_mkdir_canonical(present, "inc", present_sub, sizeof(present_sub)));
+ ASSERT_TRUE(ws_join(entry, sizeof(entry), base, "gone"));
+ ASSERT(snprintf(body, sizeof(body), "%s\n%s\n", entry, present) > 0);
+ ws_manifest_write(base, body);
+ ASSERT_TRUE(ws_manifest_record_approval(cache, base));
+ ASSERT_TRUE(cbm_workspace_manifest_allows(cache, HOME, base, present_sub));
+ ASSERT_FALSE(cbm_workspace_manifest_allows(cache, HOME, base, base));
+
+ th_cleanup(base);
+ th_cleanup(cache);
+ PASS();
+ }
+
+ /* The ordinary shapes keep their result: an absolute existing directory, an
+ * entry equal to the project root, an entry outside every root in the grant
+ * store (the manifest is consulted on its own), and on POSIX a link to an
+ * ordinary directory. */
+ TEST(ws_manifest_ordinary_entries_unchanged) {
+ char base[WS_T_PATH];
+ char cache[WS_T_PATH];
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m7", base, sizeof(base)));
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m7c", cache, sizeof(cache)));
+
+ char sdk[WS_T_PATH];
+ char sdk_inc[WS_T_PATH];
+ char granted[WS_T_PATH];
+ ASSERT_TRUE(ws_mkdir_canonical(base, "sdk", sdk, sizeof(sdk)));
+ ASSERT_TRUE(ws_mkdir_canonical(sdk, "inc", sdk_inc, sizeof(sdk_inc)));
+ ASSERT_TRUE(ws_mkdir_canonical(base, "granted", granted, sizeof(granted)));
+
+ char err[1024];
+ /* The grant store names a root the sdk is not under. */
+ ASSERT_TRUE(cbm_workspace_grant_add(cache, HOME, granted, false, err, sizeof(err)));
+
+ ws_manifest_write_line(base, sdk);
+ ASSERT_TRUE(cbm_workspace_manifest_approve(cache, HOME, base, err, sizeof(err)));
+ ASSERT_STR_EQ(err, "");
+ ASSERT_TRUE(cbm_workspace_manifest_allows(cache, HOME, base, sdk_inc));
+ ASSERT_FALSE(cbm_workspace_manifest_allows(cache, HOME, base, granted));
+
+ /* Equal to the project root. */
+ ws_manifest_write_line(base, base);
+ ASSERT_TRUE(cbm_workspace_manifest_approve(cache, HOME, base, err, sizeof(err)));
+ ASSERT_TRUE(cbm_workspace_manifest_allows(cache, HOME, base, base));
+ ASSERT_TRUE(cbm_workspace_manifest_allows(cache, HOME, base, sdk_inc));
+
+ #ifndef _WIN32
+ /* A link to an ordinary directory is that directory. Windows is not
+ * covered here: symlink(2) has no counterpart in the test helpers, and
+ * CreateSymbolicLinkW needs a privilege the CI runner does not hold; the
+ * resolved-form rules themselves are exercised on every platform by the
+ * `..` cases above. */
+ char alias[WS_T_PATH];
+ ASSERT_TRUE(ws_join(alias, sizeof(alias), base, "alias"));
+ ASSERT_EQ(symlink(sdk, alias), 0);
+ ws_manifest_write_line(base, alias);
+ ASSERT_TRUE(cbm_workspace_manifest_approve(cache, HOME, base, err, sizeof(err)));
+ ASSERT_TRUE(cbm_workspace_manifest_allows(cache, HOME, base, sdk_inc));
+ #endif
+
+ th_cleanup(base);
+ th_cleanup(cache);
+ PASS();
+ }
+
+ #ifndef _WIN32
+ /* A link is judged by where it leads. Windows is not covered: symlink(2) has
+ * no counterpart in the test helpers and CreateSymbolicLinkW needs a privilege
+ * the CI runner does not hold; the resolved-form rules are exercised on every
+ * platform by ws_manifest_entry_classified_in_resolved_form. */
+ TEST(ws_manifest_link_to_sensitive_directory_refused) {
+ char base[WS_T_PATH];
+ char cache[WS_T_PATH];
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m8", base, sizeof(base)));
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m8c", cache, sizeof(cache)));
+
+ char home[WS_T_PATH];
+ char home_sub[WS_T_PATH];
+ char keys[WS_T_PATH];
+ char keys_sub[WS_T_PATH];
+ ASSERT_TRUE(ws_mkdir_canonical(base, "home", home, sizeof(home)));
+ ASSERT_TRUE(ws_mkdir_canonical(home, "notes", home_sub, sizeof(home_sub)));
+ ASSERT_TRUE(ws_mkdir_canonical(base, ".ssh", keys, sizeof(keys)));
+ ASSERT_TRUE(ws_mkdir_canonical(keys, "hosts", keys_sub, sizeof(keys_sub)));
+
+ char to_home[WS_T_PATH];
+ char to_keys[WS_T_PATH];
+ ASSERT_TRUE(ws_join(to_home, sizeof(to_home), base, "to-home"));
+ ASSERT_TRUE(ws_join(to_keys, sizeof(to_keys), base, "to-keys"));
+ ASSERT_EQ(symlink(home, to_home), 0);
+ ASSERT_EQ(symlink(keys, to_keys), 0);
+
+ char expected[WS_T_PATH];
+ char err[1024];
+
+ ws_manifest_write_line(base, to_home);
+ ASSERT_FALSE(cbm_workspace_manifest_approve(cache, home, base, err, sizeof(err)));
+ ASSERT_NOT_NULL(strstr(err, to_home));
+ ASSERT(snprintf(expected, sizeof(expected), "(resolves to %s)", home) > 0);
+ ASSERT_NOT_NULL(strstr(err, expected));
+ ASSERT_NOT_NULL(strstr(err, cbm_workspace_verdict_reason(CBM_WS_DENY_SENSITIVE)));
+
+ ws_manifest_write_line(base, to_keys);
+ ASSERT_FALSE(cbm_workspace_manifest_approve(cache, home, base, err, sizeof(err)));
+ ASSERT_NOT_NULL(strstr(err, to_keys));
+ ASSERT(snprintf(expected, sizeof(expected), "(resolves to %s)", keys) > 0);
+ ASSERT_NOT_NULL(strstr(err, expected));
+ ASSERT_NOT_NULL(strstr(err, cbm_workspace_verdict_reason(CBM_WS_DENY_SENSITIVE)));
+
+ /* On record as approved: the use-time check still refuses both. */
+ char body[WS_T_PATH];
+ ASSERT(snprintf(body, sizeof(body), "%s\n%s\n", to_home, to_keys) > 0);
+ ws_manifest_write(base, body);
+ ASSERT_TRUE(ws_manifest_record_approval(cache, base));
+ ASSERT_FALSE(cbm_workspace_manifest_allows(cache, home, base, home_sub));
+ ASSERT_FALSE(cbm_workspace_manifest_allows(cache, home, base, keys_sub));
+
+ th_cleanup(base);
+ th_cleanup(cache);
+ PASS();
+ }
+
+ /* Approval binds to the manifest text; where a link leads is read again each
+ * time the manifest is used. A link approved while it led to an ordinary
+ * directory and later pointed at the home directory grants nothing there.
+ * POSIX only, for the reason given at the test above. */
+ TEST(ws_manifest_link_retarget_is_seen_at_use) {
+ char base[WS_T_PATH];
+ char cache[WS_T_PATH];
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m9", base, sizeof(base)));
+ ASSERT_TRUE(ws_tempdir_canonical("cbm_ws_m9c", cache, sizeof(cache)));
+
+ char shared[WS_T_PATH];
+ char shared_sub[WS_T_PATH];
+ char home[WS_T_PATH];
+ char home_sub[WS_T_PATH];
+ ASSERT_TRUE(ws_mkdir_canonical(base, "shared", shared, sizeof(shared)));
+ ASSERT_TRUE(ws_mkdir_canonical(shared, "proto", shared_sub, sizeof(shared_sub)));
+ ASSERT_TRUE(ws_mkdir_canonical(base, "home", home, sizeof(home)));
+ ASSERT_TRUE(ws_mkdir_canonical(home, "notes", home_sub, sizeof(home_sub)));
+
+ char extra[WS_T_PATH];
+ ASSERT_TRUE(ws_join(extra, sizeof(extra), base, "extra"));
+ ASSERT_EQ(symlink(shared, extra), 0);
+
+ char err[1024];
+ ws_manifest_write_line(base, extra);
+ ASSERT_TRUE(cbm_workspace_manifest_approve(cache, home, base, err, sizeof(err)));
+ ASSERT_TRUE(cbm_workspace_manifest_allows(cache, home, base, shared_sub));
+
+ /* Same text, new target. */
+ ASSERT_EQ(unlink(extra), 0);
+ ASSERT_EQ(symlink(home, extra), 0);
+ cbm_ws_manifest_t m;
+ ASSERT_TRUE(cbm_workspace_manifest_read(base, &m));
+ ASSERT_TRUE(cbm_workspace_manifest_is_approved(cache, base, &m));
+
+ ASSERT_FALSE(cbm_workspace_manifest_allows(cache, home, base, home_sub));
+ ASSERT_FALSE(cbm_workspace_manifest_allows(cache, home, base, shared_sub));
+
+ th_cleanup(base);
+ th_cleanup(cache);
+ PASS();
+ }
+ #endif /* _WIN32 */
+
SUITE(workspace) {
RUN_TEST(ws_manifest_absent_is_not_an_error);
RUN_TEST(ws_manifest_parses_entries_and_skips_comments);
@@@ -368,5 -835,13 +925,14 @@@
RUN_TEST(ws_posix_matching_is_case_sensitive);
RUN_TEST(ws_null_context_disables_dependent_checks);
RUN_TEST(ws_every_verdict_has_a_reason);
+ RUN_TEST(ws_approve_sensitive_matches_across_separator_styles);
+ RUN_TEST(ws_home_dir_is_resolved);
+ RUN_TEST(ws_linked_home_classified_as_home);
+ RUN_TEST(ws_manifest_entry_classified_in_resolved_form);
+ RUN_TEST(ws_manifest_unresolvable_entry_refused);
+ RUN_TEST(ws_manifest_ordinary_entries_unchanged);
+ #ifndef _WIN32
+ RUN_TEST(ws_manifest_link_to_sensitive_directory_refused);
+ RUN_TEST(ws_manifest_link_retarget_is_seen_at_use);
+ #endif
}