Skip to content

Commit e5aabdf

Browse files
DeusDataclaude
andcommitted
fix(cli): follow user-owned symlinked config files in the editors
Every config editor (JSON-like, TOML, YAML, text) opens with O_NOFOLLOW and refuses symlinks, so a link planted at ~/.cursor/mcp.json cannot point a privileged writer at another file. Users keep exactly those files as symlinks into a dotfiles checkout; the blanket refusal made install and uninstall fail on them and, before the previous commit, turned uninstall into data loss (DeusData#1954). Decision C: a symlink the INVOKING user owns, resolving to a regular file the same user owns, is followed — as an explicit opt-in of the agent-configuration writers only. cbm_install_agent_configs and the uninstall activation register the user's configuration roots (HOME, XDG_CONFIG_HOME) for the duration of their work; a link whose own directory is outside every registered root, or any link seen by another editor caller, stays refused, so repository paths never follow. One shared helper (src/cli/config_edit_path.c) runs the race-safe sequence: lstat(link) → owner == invoking uid (a root process follows root-owned links only) → realpath → the target's parent directory opened as a dirfd (O_DIRECTORY|O_NOFOLLOW; owned by the invoking user or root, not writable by others unless sticky) → openat(dirfd, base, O_NOFOLLOW) → fstat: regular file, same owner, link count 1, and the same (dev, ino) as stat(realpath). Each editor reads through that validated descriptor, stages its temp file with openat(dirfd, …, O_CREAT|O_EXCL|O_NOFOLLOW), runs its pre-publish snapshot comparison against the resolved path, and commits with renameat on the pinned dirfd, so the link itself is never replaced. Every other link is refused with an observable reason (surfaced in the uninstall diagnostics): another owner, root over a non-root link, dangling, a directory or special target, a hard-linked or swapped target, a foreign or world-writable parent. Whether the target must also stay under a root is a pending user decision behind CBM_CONFIG_EDIT_TARGET_UNDER_ROOT (default: anywhere the user owns). Windows never follows reparse points. Tests: each editor suite gains a same-owner link edited in place under the opt-in (link preserved and still pointing at the same target, temp file staged beside the target, bytes identical to the same edit on a plain file, no stray temp files) and keeps its refusal cases; the JSON-like suite adds no-opt-in, outside-root, dangling, directory, identity-skew and world-writable-parent refusals. The foreign-owner and root-run refusals move the observer through a test seam (a test cannot create a foreign-owned link without root; a real root run demotes the link first). test_cli.c drives `uninstall -y` end to end through a symlinked ~/.cursor/mcp.json. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent c572ddc commit e5aabdf

13 files changed

Lines changed: 1560 additions & 72 deletions

‎Makefile.cbm‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ SANITIZE = -fsanitize=address,undefined -fno-omit-frame-pointer
8484
EDITOR_TEST_DEFINES = -DCBM_JSON_LIKE_ENABLE_TEST_API=1 \
8585
-DCBM_TOML_EDIT_ENABLE_TEST_API=1 -DCBM_YAML_ENABLE_TEST_API=1 \
8686
-DCBM_TEXT_EDIT_ENABLE_TEST_API=1 -DCBM_CLI_ENABLE_TEST_API=1 \
87+
-DCBM_CONFIG_EDIT_PATH_ENABLE_TEST_API=1 \
8788
-DCBM_DIAGNOSTICS_ENABLE_TEST_API=1 -DCBM_ENABLE_TEST_SEAMS=1
8889
# The build system is the single source of truth for "is this binary
8990
# instrumented": compiler-specific probes (__SANITIZE_ADDRESS__) miss
@@ -409,7 +410,7 @@ GIT_SRCS = src/git/git_context.c
409410
CLI_SRCS = src/cli/cli.c src/cli/progress_sink.c src/cli/hook_augment.c src/cli/client_adapter.c \
410411
src/cli/agent_clients.c src/cli/agent_profiles.c \
411412
src/cli/config_json_like.c src/cli/config_toml_edit.c src/cli/config_yaml_edit.c \
412-
src/cli/config_text_edit.c src/cli/activation_transaction.c
413+
src/cli/config_text_edit.c src/cli/config_edit_path.c src/cli/activation_transaction.c
413414

414415
# UI module (graph visualization)
415416
UI_SRCS = \

‎src/cli/cli.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "cli/agent_profiles.h"
99
#include "cli/cli.h"
1010
#include "cli/activation_transaction.h"
11+
#include "cli/config_edit_path.h"
1112
#include "cli/config_json_like.h"
1213
#include "cli/config_text_edit.h"
1314
#include "cli/config_toml_edit.h"
@@ -7776,6 +7777,21 @@ static void agent_uninstall_failure_record(const char *agent, const char *operat
77767777
(void)snprintf(entry->detail, sizeof(entry->detail), "%s", detail ? detail : "");
77777778
}
77787779

7780+
/* The agent-configuration writers opt in to following user-owned symlinked
7781+
* config files under the user's configuration roots (#1954, decision C);
7782+
* every other caller of the config editors keeps refusing links. Cleared by
7783+
* the same command when its configuration work is done. */
7784+
static void cli_config_follow_begin(const char *home) {
7785+
cbm_config_edit_path_follow_clear();
7786+
if (home && home[0]) {
7787+
(void)cbm_config_edit_path_follow_add_root(home);
7788+
}
7789+
const char *xdg_config = getenv("XDG_CONFIG_HOME");
7790+
if (xdg_config && xdg_config[0]) {
7791+
(void)cbm_config_edit_path_follow_add_root(xdg_config);
7792+
}
7793+
}
7794+
77797795
/* The closing list of what uninstall could not clean. Printed AFTER the
77807796
* executable and the indexes are gone, so nothing in it is a reason to keep
77817797
* the installation around — each line is one file the user removes an entry
@@ -7853,6 +7869,14 @@ static void describe_agent_config_target(const char *path, char *out, size_t out
78537869
: info.is_directory ? "directory"
78547870
: info.is_regular ? "regular file"
78557871
: "special file";
7872+
/* A refused symlink names the rule that refused it (#1954): the user
7873+
* then knows whether to fix ownership, the target, or the parent. */
7874+
char refusal[160];
7875+
if (info.is_symlink && cbm_config_edit_path_refusal(path, refusal, sizeof(refusal))) {
7876+
(void)snprintf(out, out_size, " (target: symlink, %lld bytes; not followed: %s)",
7877+
(long long)info.size, refusal);
7878+
return;
7879+
}
78567880
(void)snprintf(out, out_size, " (target: %s, %lld bytes)", kind, (long long)info.size);
78577881
}
78587882

@@ -9817,12 +9841,24 @@ static void install_additional_agent_configs(const cbm_detected_agents_t *agents
98179841
}
98189842
}
98199843

9844+
static int cbm_install_agent_configs_in_scope(const char *home, const char *binary_path, bool force,
9845+
bool dry_run, cbm_detected_agents_t *agents_in);
9846+
98209847
int cbm_install_agent_configs(const char *home, const char *binary_path, bool force, bool dry_run) {
98219848
g_agent_install_errors = 0;
98229849
cbm_detected_agents_t agents = cbm_detect_agents(home);
98239850
if (g_client_selection && !cli_clients_apply_selection(g_client_selection, &agents)) {
98249851
return CLI_ERR;
98259852
}
9853+
cli_config_follow_begin(home);
9854+
int result = cbm_install_agent_configs_in_scope(home, binary_path, force, dry_run, &agents);
9855+
cbm_config_edit_path_follow_clear();
9856+
return result;
9857+
}
9858+
9859+
static int cbm_install_agent_configs_in_scope(const char *home, const char *binary_path, bool force,
9860+
bool dry_run, cbm_detected_agents_t *agents_in) {
9861+
cbm_detected_agents_t agents = *agents_in;
98269862
if (!g_install_plan) {
98279863
print_detected_agents(&agents, home);
98289864
}
@@ -12108,13 +12144,15 @@ static int cli_uninstall_activate(void *opaque) {
1210812144
return CLI_TRUE;
1210912145
}
1211012146

12147+
cli_config_follow_begin(activation->home);
1211112148
if (activation->agents.claude_code) {
1211212149
uninstall_claude_code(activation->home, activation->bin_path, activation->dry_run);
1211312150
}
1211412151
uninstall_cli_agents(&activation->agents, activation->home, activation->dry_run);
1211512152
uninstall_editor_agents(&activation->agents, activation->home, activation->dry_run);
1211612153
uninstall_additional_agents(&activation->agents, activation->home, activation->dry_run);
1211712154
uninstall_agent_client_registry(activation->home, activation->dry_run);
12155+
cbm_config_edit_path_follow_clear();
1211812156

1211912157
/* Agent-config failures are collected, never a gate: an entry the editors
1212012158
* refuse to touch (a symlinked config, a foreign file, a malformed

0 commit comments

Comments
 (0)