{"slug": "i-made-my-ai-agent-rule-files-read-only-5-of-8-edits-still-bypassed-it-and-the", "title": "I Made My AI Agent Rule Files Read-Only. 5 of 8 Edits Still Bypassed It (And the Real Fix Broke Git)", "summary": "A developer benchmarked protections for coding-agent rule files (AGENTS.md, CLAUDE.md, Cursor rules, and .claude/skills) on macOS 26.2 and found that chmod 444 blocked only 3 of 8 file-mutation vectors, with mv -f, rm -f && cp, sed -i, and new skill drops all succeeding. OS-level immutable flags (chflags uchg, the macOS counterpart to chattr +i) blocked all 8 vectors but broke git merge and left git checkout exiting 0 with a dirty AGENTS.md in the working tree. The developer attributes the gap to POSIX semantics, where unlink and rename permissions are governed by the containing directory rather than the target file.", "body_md": "Last weekend I ran `chmod 444` on `AGENTS.md`, `CLAUDE.md`, `.cursor/rules/safety.mdc`, and `.claude/skills/safe-deploy.md` inside a local Git test repository, assuming read-only file permissions would stop a coding agent from rewriting its own guardrails. When I ran 8 ordinary file mutation commands against that repository on macOS 26.2, `chmod 444` blocked only 3 of them. The other 5 commands, including `mv -f`, `rm -f && cp`, `sed -i ''`, and creating a new file in `.claude/skills/`, exited 0 and altered the agent's instructions without a warning.\n\nSwitching to OS-level immutable flags (`chflags uchg` on macOS, the counterpart to `chattr +i` on Linux) closed all 8 mutation paths. It also broke `git merge` and turned `git checkout` into a trap that exited 0 while leaving the previous branch's `AGENTS.md` modified in the working tree.\n\nTeams now store operational boundaries across plain repository files: [AGENTS.md](https://agents.md/), `CLAUDE.md`, [Cursor rules](https://cursor.com/docs/context/rules) in `.cursor/rules/`, and reusable skill files under `.claude/skills/`. Any coding agent that has shell access or an atomic file editor shares the same user ID as the developer running it. If a long session drifts or hits an indirect prompt injection from an issue comment or scraped web page, those rule files sit inside the exact directory tree the agent is allowed to touch.\n\nThe default reaction is to mark the rule files read-only with `chmod 444`. That intuition breaks because POSIX permissions on unlink and rename do not belong to the target file. Under the Linux [rename(2) manual](https://man7.org/linux/man-pages/man2/rename.2.html), `EACCES` triggers when write permission is denied for the directory containing `oldpath` or `newpath`. The [unlink(2) manual](https://man7.org/linux/man-pages/man2/unlink.2.html) applies the same check: write access to the directory containing the path controls whether a file can be removed.\n\nBecause your repository root must stay writable for normal code edits, any tool that writes a temporary file and renames it over `AGENTS.md` succeeds even when `AGENTS.md` is mode `0444`. That includes atomic editor saves, `sed -i`, `mv -f`, and `git` itself.\n\nOn December 1, 2026, I ran an isolated local benchmark script on macOS 26.2 (`arm64`, Python 3.14.10, Apple Git 2.50.1) against a fresh Git repository containing `AGENTS.md`, `CLAUDE.md`, `.cursor/rules/safety.mdc`, and `.claude/skills/safe-deploy.md`. Each protection mode ran in a clean temporary repository against 8 mutation vectors plus 2 real Git operations (`git merge --ff-only update-rules` and `git checkout update-rules`, where the `update-rules` branch adds one line to `AGENTS.md`).\n\n| Protection Mode | Blocked (of 8) | Direct Write / Truncate ( `V1` to`V3` ) | Root `mv` /`rm+cp` /`sed -i` (`V4` to`V6` ) | New Skill Drop ( `V7` ) | Cursor Rule Atomic `mv` (`V8` ) | `git merge` | `git checkout` | \n|---|---|---|---|---|---|---|---|\n| Unprotected baseline | 0 / 8 | Bypassed (3/3) | Bypassed (3/3) | Bypassed | Bypassed | OK (rc=0) | OK (rc=0) | \n| `chmod 444` files only | 3 / 8 | Blocked (3/3) | Bypassed (3/3) | Bypassed | Bypassed | OK (rc=0) | OK (rc=0) | \n| `chmod 444` files +`chmod 555` rule dirs | 5 / 8 | Blocked (3/3) | Bypassed (3/3) | Blocked | Blocked | OK (rc=0) | OK (rc=0) | \n| `chflags uchg` files only | 7 / 8 | Blocked (3/3) | Blocked (3/3) | Bypassed | Blocked | Failed (rc=1) | Exit 0, dirty `M AGENTS.md` | \n| `chflags uchg` files + rule dirs | 8 / 8 | Blocked (3/3) | Blocked (3/3) | Blocked | Blocked | Failed (rc=1) | Exit 0, dirty `M AGENTS.md` | \n\nHere is the direct terminal output from the `chmod 444` mode and the full `chflags uchg` mode in the benchmark log:\n\n```\n--- Mode: 2_chmod_444_files_only (Blocked 3/8 mutation vectors) ---\n  V1_python_open_w         | rc=1 | BLOCKED            | PermissionError: [Errno 13] Permission denied: 'AGENTS.md'\n  V2_shell_redirect        | rc=1 | BLOCKED            | /bin/sh: AGENTS.md: Permission denied\n  V3_os_truncate           | rc=1 | BLOCKED            | PermissionError: [Errno 13] Permission denied: 'CLAUDE.md'\n  V4_atomic_mv_replace     | rc=0 | BYPASSED (mutated) |\n  V5_rm_f_and_cp           | rc=0 | BYPASSED (mutated) |\n  V6_sed_inplace           | rc=0 | BYPASSED (mutated) |\n  V7_new_skill_drop        | rc=0 | BYPASSED (mutated) |\n  V8_cursor_rule_atomic_mv | rc=0 | BYPASSED (mutated) |\n  UX_git_merge_update      | rc=0 | OK                 | Updating 4d438aa..9f1ae4d Fast-forward AGENTS.md | 1 + 1 file changed, 1 insertion(+)\n\n--- Mode: 5_chflags_uchg_files_and_dirs (Blocked 8/8 mutation vectors) ---\n  V1_python_open_w         | rc=1 | BLOCKED            | PermissionError: [Errno 1] Operation not permitted: 'AGENTS.md'\n  V2_shell_redirect        | rc=1 | BLOCKED            | /bin/sh: AGENTS.md: Operation not permitted\n  V3_os_truncate           | rc=1 | BLOCKED            | PermissionError: [Errno 1] Operation not permitted: 'CLAUDE.md'\n  V4_atomic_mv_replace     | rc=1 | BLOCKED            | mv: rename .tmp_agents to AGENTS.md: Operation not permitted\n  V5_rm_f_and_cp           | rc=1 | BLOCKED            | rm: AGENTS.md: Operation not permitted\n  V6_sed_inplace           | rc=1 | BLOCKED            | sed: rename(): Operation not permitted\n  V7_new_skill_drop        | rc=1 | BLOCKED            | /bin/sh: .claude/skills/rogue-skill.md: Operation not permitted\n  V8_cursor_rule_atomic_mv | rc=1 | BLOCKED            | mv: rename .tmp_rule to .cursor/rules/safety.mdc: Operation not permitted\n  UX_git_merge_update      | rc=1 | BLOCKED_UNTIL_UNLOCK | error: unable to unlink old 'AGENTS.md': Operation not permitted Updating d5584e9..fdbf60a\n  UX_git_checkout_branch   | rc=0 | OK                   | error: unable to unlink old 'AGENTS.md': Operation not permitted Switched to branch 'update-rules' M AGENTS.md\n```\n\nTwo details in that run caught me off guard.\n\nLocking `.claude/skills` and `.cursor/rules` with `chmod 555` stopped drops into those subdirectories, yet root `AGENTS.md` and `CLAUDE.md` still failed 3 out of 8 checks (`V4` through `V6`). You cannot run `chmod 555 .` on the repository root without breaking every build artifact, lockfile update, and new source file in the project.\n\nLocking only the individual files with `chflags uchg` (Mode 4) blocked 7 of 8 vectors, yet left `V7_new_skill_drop` wide open. An agent does not need to overwrite `safe-deploy.md` when it can create `rogue-skill.md` right beside it inside a writable `.claude/skills/` or `.cursor/rules/` directory. Locking the subdirectories themselves closed the 8th vector.\n\n`git checkout` hazard under immutable flags\nMode 5 stopped every mutation vector with `Errno 1: Operation not permitted`. Under the FreeBSD and macOS [chflags(1) manual](https://man.freebsd.org/cgi/man.cgi?query=chflags&sektion=1), `uchg` sets the user immutable flag, which prevents both direct writes and unlinking or renaming the inode. On Linux, the [chattr(1) manual](https://man7.org/linux/man-pages/man1/chattr.1.html) defines the `i` attribute the same way: a file with the `i` attribute cannot be modified, deleted, or renamed, and only root or a process with `CAP_LINUX_IMMUTABLE` can set or clear it.\n\nThat inode lock stops `git merge` loudly (`rc=1`, `error: unable to unlink old 'AGENTS.md': Operation not permitted`). That failure is easy to notice.\n\nThe surprise was `git checkout update-rules`. Git printed `error: unable to unlink old 'AGENTS.md': Operation not permitted`, switched `HEAD` to `update-rules` anyway, exited with status `0`, and left `AGENTS.md` showing `M AGENTS.md` in `git status`. Because the file on disk still held the older commit from `main`, any developer or script that checks only the exit code and then runs `git commit -am` will silently revert the rule update that `update-rules` introduced.\n\nOn macOS, there is another catch: `uchg` is a user-level flag, so the same user account that set `chflags uchg` can also run `chflags nouchg` from a shell tool. On Linux, `sudo chattr +i` resists an unprivileged agent process because clearing `+i` requires `CAP_LINUX_IMMUTABLE`, though passwordless `sudo` would still defeat it.\n\nBecause no single layer covers both normal Git updates and shell bypasses, a practical setup combines two controls.\n\nStart by blocking both file-edit tools and flag-changing shell commands in the agent's deterministic permission layer. In [Claude Code's permissions documentation](https://code.claude.com/docs/en/permissions), rules evaluate in order: `deny`, then `ask`, then `allow`. The [permission modes guide](https://code.claude.com/docs/en/permission-modes) notes that deny rules block in every mode, including `bypassPermissions`.\n\n```\n{\n  \"permissions\": {\n    \"deny\": [\n      \"Edit(./AGENTS.md)\",\n      \"Edit(./CLAUDE.md)\",\n      \"Edit(./.cursor/rules/**)\",\n      \"Edit(./.claude/skills/**)\",\n      \"Edit(./.claude/settings.json)\",\n      \"Bash(chflags *)\",\n      \"Bash(chattr *)\",\n      \"Bash(chmod *)\"\n    ]\n  }\n}\n```\n\nNext, keep a short lock and unlock helper outside the agent's writable workspace (or run it manually) whenever you pull changes and switch branches that modify rule files:\n\n``` bash\n#!/usr/bin/env bash\nset -euo pipefail\n\nTARGETS=(AGENTS.md CLAUDE.md .cursor/rules .cursor/rules/* .claude/skills .claude/skills/*)\n\nlock_rules() {\n  for p in \"${TARGETS[@]}\"; do\n    [[ -e \"$p\" ]] && chflags uchg \"$p\"\n  done\n}\n\nunlock_rules() {\n  for p in \"${TARGETS[@]}\"; do\n    [[ -e \"$p\" ]] && chflags nouchg \"$p\"\n  done\n}\n\ncase \"${1:-}\" in\n  lock)   lock_rules ;;\n  unlock) unlock_rules ;;\n  git)\n    shift\n    unlock_rules\n    trap lock_rules EXIT\n    git \"$@\"\n    ;;\n  *)\n    echo \"Usage: $0 {lock|unlock|git <git-args...>}\" >&2\n    exit 2\n    ;;\nesac\n```\n\nIn the benchmark's verification pass, running the locked merge failed with `rc=1`, unlocking and running `git merge --ff-only update-rules` succeeded with `rc=0`, and re-locking immediately blocked `mv -f .tmp_a AGENTS.md` again with `Operation not permitted`. Wrapping `git pull`, `git merge`, and `git checkout` through `trap lock_rules EXIT` also removes the silent dirty-file checkout hazard because the files unlock for the Git operation and re-lock as soon as Git exits.\n\nIf you run agents inside Linux containers or CI runners, you get an even stronger boundary by mounting `AGENTS.md`, `.cursor/rules`, and `.claude/skills` as read-only bind mounts (`:ro`) or setting `sudo chattr +i` outside the agent's UID. In that environment the agent has neither `CAP_LINUX_IMMUTABLE` nor a way to remount the volume.\n\nI added the macOS and Linux lockdown commands from this test to [lock-agent-rules-and-skills-against-self-edits.md](https://github.com/seankwon816/ai-coding-agent-tips/blob/main/tips/lock-agent-rules-and-skills-against-self-edits.md) alongside our zero-dependency local config auditor [audit_agent_configs.py](https://github.com/seankwon816/ai-coding-agent-tips/blob/main/scripts/audit_agent_configs.py) if you want to check for missing `permissions.deny` rules and wildcard allowances in your own repo.\n\nWhat setup has worked in your repos to keep `AGENTS.md`, `CLAUDE.md`, and local skill directories safe from agent self-edits while keeping everyday `git pull` and branch switches painless?", "url": "https://wpnews.pro/news/i-made-my-ai-agent-rule-files-read-only-5-of-8-edits-still-bypassed-it-and-the", "canonical_source": "https://dev.to/seanjhkwon/i-made-my-ai-agent-rule-files-read-only-5-of-8-edits-still-bypassed-it-and-the-real-fix-broke-git-41mj", "published_at": "2026-12-01 13:40:34+00:00", "updated_at": "2026-12-01 13:44:12.013572+00:00", "lang": "en", "topics": ["ai-agents", "ai-safety", "developer-tools", "ai-tools"], "entities": ["Cursor", "Git", "macOS", "Claude Code"], "also_reported_by": [], "takeaway": "chmod 444 on agent rule files blocked only 3 of 8 mutation vectors because unlink and rename depend on directory write permission, while chflags uchg blocked all 8 but broke git merge and left git checkout exiting 0 with a dirty AGENTS.md.", "recommendation": {"advice": "Do not rely on chmod 444 to protect agent rule files; test immutable flags against your git workflows first, since chflags uchg blocked all 8 vectors but broke merge and checkout.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/i-made-my-ai-agent-rule-files-read-only-5-of-8-edits-still-bypassed-it-and-the", "markdown": "https://wpnews.pro/news/i-made-my-ai-agent-rule-files-read-only-5-of-8-edits-still-bypassed-it-and-the.md", "text": "https://wpnews.pro/news/i-made-my-ai-agent-rule-files-read-only-5-of-8-edits-still-bypassed-it-and-the.txt", "jsonld": "https://wpnews.pro/news/i-made-my-ai-agent-rule-files-read-only-5-of-8-edits-still-bypassed-it-and-the.jsonld"}}