Skip to content

rtk init --codex writes through a symlinked AGENTS.md, so a cloned repo can redirect the write outside the project #4156

Open

Description

@KuSh

Pre-existing on develop (verified at 0924356b, rtk 0.48.0). Surfaced while reviewing #4119, which is unrelated to it — #4119 documents this case rather than guarding it, because atomic_write preserving a user-maintained symlink is intentional (#653) and pinned by tests.

Summary

git stores symlinks, so a repository can ship AGENTS.md as a relative symlink pointing outside the working tree. rtk init --codex reads through the link and writes through it: @RTK.md is appended to a file outside the project, exit 0, no warning. NamedTempFile::persist also replaces the inode, so the target's mode resets from 644 to 600.

No local setup is required by the victim — cloning the repository and running rtk init --codex is enough.

Reproduction

W=$(mktemp -d); cd "$W"
mkdir outside origin
printf 'user notes\n' > outside/AGENTS.md
chmod 644 outside/AGENTS.md

cd origin
git init -q -b main .; git config user.email [email protected]; git config user.name t
ln -s ../outside/AGENTS.md AGENTS.md
git add -A; git commit -qm "ship AGENTS.md as a relative symlink"

cd "$W"; git clone -q origin clone; cd clone
rtk init --codex < /dev/null

cat "$W/outside/AGENTS.md"
stat -c '%a' "$W/outside/AGENTS.md"

Observed

exit=0

$ cat $W/outside/AGENTS.md
user notes

@RTK.md

$ stat -c '%a' $W/outside/AGENTS.md
600

The file outside the clone was modified and its mode changed from 644 to 600. rtk init reported success and printed AGENTS.md: @RTK.md reference added, naming the in-project path.

Expected

rtk init --codex confines its project-mode writes to the project. A project AGENTS.md that resolves outside the working tree should at least produce a warning naming the resolved target, rather than a silent write and a silent mode change.

Mechanism

  • patch_agents_md (src/hooks/init.rs) reads with fs::read_to_string(path), which follows the link.
  • It writes with atomic_write, whose resolve_atomic_target calls fs::canonicalize specifically so the rename lands on the real file and the symlink survives.
  • NamedTempFile::persist renames a fresh temp file over the resolved target, so the target's previous inode (and its mode) is replaced.

RTK.md is not affected: it is written with fs::rename, which does not follow a symlink at the destination.

Impact

Bounded: one line of text appended to an attacker-chosen path writable by the user, plus a permission reset. No execution. The interesting part is that it needs nothing from the victim but a clone and rtk init --codex.

Source: src/hooks/init.rs — patch_agents_md, atomic_write, resolve_atomic_target.

Same hostile-clone vector at a second site, filed separately: the .bak copy in backup_and_atomic_write.

Activity

  1. added
    bug
    on Sep 20, 2026
  2. KuSh commented on Oct 30, 2026

    @KuSh
    CollaboratorAuthor

    The uninstall side does the same. rtk init --codex --uninstall edits a symlinked project AGENTS.md through the link: the @RTK.md line is removed from the file outside the working tree, exit 0. This is the same on develop (5ab6291) and on #4463's head. #4463's containment guard covers .codex/hooks.json and its .bak, not AGENTS.md.

    W=$(mktemp -d); mkdir -p "$W/proj" "$W/out"; cd "$W/proj"; git init -q .
    printf 'theirs\[email protected]\n' > "$W/out/AGENTS.md"; ln -s "$W/out/AGENTS.md" AGENTS.md
    rtk init --codex --uninstall; echo "exit=$?"; cat "$W/out/AGENTS.md"   # exit=0, '@RTK.md' gone from the outside file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:securitybughooks

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions