All lab entries
Writeup

How Claude Code Nearly Nuked My Filesystem

One stray newline, and Claude Code (Opus 5.5) told lftp to make my entire filesystem an exact copy of my website's source folder, deleting everything else. The sandbox had other plans.

  • Post-mortem
  • Claude Code
  • AI Agents
  • Sandboxing
  • lftp
  • Linux

Where this started

October 16 was going well. In the morning I published the CAN bus lab with Claude Code. In the afternoon I wanted to stop deploying my site by dragging zip files around cPanel: a npm run package script that builds the upload zip, a npm run smoke script that checks the live site, and finally an FTPS deploy with lftp, because SSH on my host doesn’t work.

My copilot for the day was Claude Code running Opus 5.5, Anthropic’s latest and greatest, in auto mode, where a classifier approves most actions without bothering me. Living dangerously? Not really. I’m paranoid about letting agents loose on my machine (I already wrap other agent CLIs in my own bubblewrap launchers), so months ago I had set up Claude Code’s built-in sandbox, which on Linux is also bubblewrap, to fail closed:

"sandbox": {
  "enabled": true,
  "failIfUnavailable": true,
  "allowUnsandboxedCommands": false,
  "filesystem": {
    "denyWrite": ["~/Documents", "~/Downloads", "~/Desktop", "..."],
    "denyRead": ["~/Documents", "~/Downloads", "~/Desktop", "..."]
  },
  "credentials": {
    "files": [
      { "path": "~/.ssh", "mode": "deny" },
      { "path": "~/.gnupg", "mode": "deny" }
    ]
  }
}

Past me, if you’re reading this: thank you.

At 13:58, while testing the FTPS command locally, Opus 5.5 ran lftp mirror --reverse --delete with my project folder as the source and the root of my filesystem as the target. In plain words: “make / an exact copy of my website’s source folder, and delete everything else”.

This is the story of how that went. Spoiler: much better than it sounds.

What I wanted to figure out

Once my heart rate was back to normal, I wanted to know what had actually happened. From the evidence, not from my memory, and not from the agent’s version of events (it was, after all, the main suspect):

  • which command ran, and why it did something completely different from what it looks like;
  • what it deleted, and what the sandbox stopped;
  • which of my settings saved me, and which ones were just decoration.

What I tried

Conveniently, Claude Code keeps a diary: one JSONL transcript per session under ~/.claude/projects/, in a folder named after the project path. Every line is an event: my messages, each tool call with its exact input, each tool result with its output. The transcripts live in my home, not in the project, and my home was read-only inside the sandbox, so the diary survived the fire.

Before touching anything I copied them to an evidence folder, hashed them, and moved the folder into a directory the sandbox can’t read. Yes, I did chain of custody on a chatbot. Let me have this.

mkdir -p "$EVIDENCE"
cp -a ~/.claude/projects/<project>/ "$EVIDENCE"/
cd "$EVIDENCE" && sha256sum -- */*.jsonl > SHA256SUMS

Four transcripts mentioned mirror. The right one was easy to spot: the only one full of Read-only file system errors, with a feedback report filed by the agent itself, the bug-tracker equivalent of walking into a police station. The session had been split in two by a /clear at 12:38, so the morning’s lab work and the afternoon’s deploy work sit in two separate files.

From there it was jq all the way down. This pulls every Bash call that touched lftp:

jq -r 'select(.type=="assistant") | . as $l | .message.content[]?
  | select(.type=="tool_use" and .name=="Bash" and (.input.command|test("lftp")))
  | "### \($l.timestamp)  \(.id)\n\(.input.description // "")\n\(.input.command)\n"' "$F"

Then I matched tool results to calls by tool_use_id. All the sandbox errors came from a single call. One command, one crime scene.

Limits of the evidence

  • The output is truncated. Claude Code cuts long command output before saving it. The result of the offending call is missing 20,012 characters from the middle, and it stops mid-line, before === real ever shows up: the end is gone too, and there’s no telling how long it was. Every count in this writeup is a lower bound.
  • The sandbox doesn’t log. bubblewrap enforces the rules but doesn’t write anything down; the process just gets an error. Failed deletions show up in the output, successful ones are silent.
  • The deletion errors show basenames only. Where a file lived is my inference from its name.

Ten minutes from FTPS to --delete

All times are local (CEST), taken from the transcript timestamps.

  • 11:44: session starts. Task: add the CAN bus lab to the site.
  • 12:38: I run /clear. The work continues in a new transcript.
  • 13:41: I ask for the package and smoke scripts first: “FTPS later”.
  • 13:44: a reviewer subagent signs off on package and smoke (“pass with notes”).
  • 13:48: npm run package works. We move on to FTPS: I create a cPanel FTP account restricted to the site’s document root and give Claude the connection details.
  • 13:56: Claude reads the lftp help and the man page to check the options. Very diligent. Remember this.
  • 13:57:27: first local test. The output contains nested dst/dst/ folders, which should not exist.
  • 13:58:03: second test, described as “repeat lftp mirror test with explicit target folder”.
  • 13:58:12: the result comes back: exit code 1 and hundreds of errors.
  • 13:59: Claude stops, files a feedback report, and tells me its test has wiped the project.
  • 14:30: project restored. The build is identical to the live site.

For the record: in the first draft of this writeup I accused the agent of starting the FTPS work on its own after I had said “later”. The transcript says otherwise. I had moved on to FTPS myself, in messages with screenshots that my first text-only filter skipped. Sorry, Claude, that one’s on me. What it did on its own was test a --delete with the real filesystem as the backend, without ever checking what the command expanded to. That one’s all yours.

One newline, three safeguards that never fired

This is the command that ran, with the long scratch path shortened to $T:

T=/tmp/claude-1000/.../scratchpad/ftptest
MIRROR="mirror --reverse --delete --transfer-all --no-perms --parallel=4
  --exclude '^\.well-known/' --exclude '^cgi-bin/' --exclude '^\.raul-site\$'
  $T/src/ $T/dst"
lftp -c "set cmd:fail-exit yes; open file:///; cls -1a $T/dst; echo ---; $MIRROR --dry-run"
echo "=== real"
lftp -c "set cmd:fail-exit yes; open file:///; $MIRROR"
cd $T && find dst | sort

It looks careful: explicit source and target, a dry run first, fail on the first error. It’s the command of someone who has just read the man page, and it had, two minutes earlier. None of it worked.

What lftp actually received

The line breaks inside MIRROR are there for readability. In a shell you would continue lines with a backslash, but inside a quoted string the newlines are literal, and lftp -c treats a newline as a command separator. So lftp saw three commands, not one:

# line 1
set cmd:fail-exit yes; open file:///; cls -1a .../dst; echo ---; mirror --reverse --delete --transfer-all --no-perms --parallel=4

# line 2
--exclude '^\.well-known/' --exclude '^cgi-bin/' --exclude '^\.raul-site$'

# line 3
.../src/ .../dst --dry-run

The first line is a complete mirror command with no directories. With --reverse, mirror falls back to its defaults: the source is the local working directory and the target is the remote working directory.

  • Local working directory: the project. The cd $T is at the very end of the command, a bit late to the party.
  • Remote working directory: after open file:///, the “remote” is the local filesystem, starting at /.

The result is “make / an exact copy of my project, and delete everything that doesn’t match”. A bold redesign.

Three safeguards, all defeated by the same newline

  • --dry-run was appended to the last line, which is not a command. The “preview” invocation was the destructive one. The output confirms it: the errors start right after the --- printed by the first lftp call, before === real ever runs.
  • The explicit paths sit on the third line, so they never reach mirror.
  • set cmd:fail-exit yes stops lftp after the first failing command. The first failing command was the mirror itself, after the damage.

And the script didn’t stop there. Its lines are separated by plain newlines, with no set -e, so after the first lftp failed the shell carried on: echo "=== real", then the second lftp with the same broken mirror, this time without even the pretense of a dry run, and finally the cd $T. What that second pass did is not in the visible output.

The warning sign that got misread

One minute earlier the first test had run the same string with relative paths, from inside the scratch folder (cd $T), against open file://$T/dst. Same bug, but both defaults pointed into the scratch folder, so mirror just copied the folder into itself and produced the nested dst/dst/. Harmless.

Opus 5.5 looked at that and diagnosed a path problem. The “fix” was to switch to absolute paths, open file:/// instead of the scratch folder, and drop the leading cd. That fix moved the remote default to / and the local default to my project. The bug knocked politely on the door, and Claude handed it the keys to /. Nobody printed the expanded command before running it again.

Reading what the sandbox stopped

Inside the sandbox only two places were writable: the project folder and Claude Code’s temp directory, /tmp/claude-1000. The rest of the filesystem was mounted read-only, and most of my home wasn’t even visible: with blockReadsOutsideWorkingDirectories on, the sandbox hides the home and re-opens only the project and the parts of ~/.claude that commands need, like skills, plugins and agents.

The output starts with mirror trying to copy the project into /, then moves on to deleting everything under / that the project doesn’t contain. Kernel, bootloader, /etc, the visible corners of my home: all of it was on the list. A few representative lines:

mirror: /package.json: Read-only file system
mirror: /tsconfig.json: Read-only file system
mirror: Access failed: /src: No such file or directory
rm: Access failed: BOOTX64.EFI: Read-only file system
rm: Access failed: arch-linux.efi: Read-only file system
rm: Access failed: architect.md: Read-only file system
rm: Access failed: security-reviewer.md: Read-only file system
rm: /opt/containerd: Permission denied
rm: Access failed: sessionid: Operation not permitted

Despite the rm: prefix, no rm ever ran. Those lines come from lftp’s own internal delete command, which mirror --delete uses on the target. That’s why almost all of them say Access failed instead of coreutils’ cannot remove.

The full visible output is in mirror-output.log. It’s the most reassuring wall of errors I’ve ever read.

The output as Claude Code saved it: about 20 KB are missing from the middle.

Five kinds of error

Counting the 162 complete error lines that survived the truncation (plus three fragments of lines it cut in half):

129  Read-only file system
 14  Operation not permitted
  9  No such file or directory
  8  Permission denied
  2  Device or resource busy
  • Read-only file system means the sandbox blocked it. This is the class that matters.
  • Permission denied means ordinary Unix permissions, like /opt/containerd owned by root. Blocked with or without a sandbox.
  • Operation not permitted comes from pseudo-files in /proc (sessionid, smaps, stack…), which the kernel won’t let anyone delete.
  • Device or resource busy comes from mount points (pts, agents), which can’t be removed while something is mounted on them. agents is most likely ~/.claude/agents, one of the folders the sandbox re-opens inside the hidden home, which is what makes it a mount point. In the output it comes right after the subagent definitions it contains (more on those below).
  • No such file or directory comes from mirror trying to enter project folders it had just failed to create under /.

Not every read-only error is a save. For root-owned files (the EFI binaries in /boot, the configs in /etc, the CA hashes like 0179095f.0) the sandbox just said no before Unix permissions got the chance. The interesting lines are the files I own, where the read-only mount was the only thing in the way.

My favorite example is architect.md, code-explorer.md, coder.md, code-reviewer.md, debugger.md, security-reviewer.md and test-runner.md. Those are my Claude Code subagent definitions. Claude was trying to delete its own coworkers. Without the sandbox nothing else would have stopped it, or stopped it from going through the rest of my home: other projects, configuration, ~/.ssh, ~/.gnupg.

What got through

The log can’t tell you, because a successful deletion is silent. The answer comes from the mount layout: everything writable was in scope, so everything writable is presumably gone.

  • The project folder. It ate itself: seen from /, the project’s own path under /home doesn’t exist inside the source (the project), so for mirror it was an extra folder in the target, to be removed. Among the casualties: scripts/deploy-ftp.sh, the FTPS deploy script whose command was being tested, with the very same multi-line mirror inside, aimed at the live site. The one deletion I don’t regret.
  • /tmp/claude-1000, including the scratch folder and the backup copies the agent had made there earlier. Backups stored right next to the thing they back up: a classic.

The final cd $T then failed because $T no longer existed, which is probably where the exit code 1 comes from.

Rebuilding a site from its own output

The agent stopped on its own, filed a feedback report, and told me plainly that its test had wiped the project.

Apologies for the screenshot below being in Italian. I talk to Claude in Italian, so that’s the language it picked to break the news. The line that matters is “È colpa mia”: “it’s my fault”. No hedging, no “it seems that some files may have been affected”. Points for honesty, minus several thousand for the --delete.

Claude Code’s report right after the incident, in Italian (sorry): what happened, what was lost, and a recovery plan.

The project was not under Git. I know. I know. What I had:

  • a zip of the whole project folder, which I thought was from October 5 and turned out to be from October 3;
  • the live site, which I downloaded as a zip from the cPanel File Manager. A browser wouldn’t do: it can’t fetch .htaccess, and it gets the output of coffee/index.php, not the source.

The agent compared a build of the backup with the live site, page by page. The differences were small: two labs published after October 3 (Packet Sniffing and Spoofing and the CAN bus lab from that same morning), one CSS rule, and a deployment path. Everything in public/ matched byte for byte, apart from the files of the two new labs, which came straight from the live zip.

The two missing labs had to be rebuilt from their published HTML. Before trusting the converter, the agent ran it on three labs whose real sources existed and checked that the regenerated pages were identical to the live ones. Only then did it use it on the two lost labs. Apparently Opus 5.5 is very careful when it’s cleaning up its own mess.

The final check was objective: the restored project builds to exactly the live site (every page, the CSS, the sitemap), astro check reports 0 errors, and npm run package produces a zip identical to what’s online. About half an hour from the incident report.

What couldn’t come back: edits after October 3 that don’t change the output, such as notes in the internal docs.

The project’s .claude/ folder I restored by hand, because the sandbox doesn’t let the agent write there. Probably for nothing: the sandbox protects most of that folder (settings, skills, agents, commands, hooks), so mirror couldn’t delete it either. From inside the sandbox, though, those paths only show up as read-only mounts and empty placeholder files, so the agent saw an empty folder and assumed the worst. I didn’t check before restoring it. Restoring files that were never lost: peak incident response.

So the lab I published that morning was rebuilt in the afternoon from its own HTML. The fastest content migration I’ve ever done. Would not recommend.

What happened

What actually saved me

  • "enabled": true, through its default policy rather than my rules. By default the sandbox makes everything read-only except the working directory and its temp folder. My carefully curated denyWrite list for ~/Documents, ~/Pictures and friends was redundant on the day: those folders were already protected, and so was everything else in my home.
  • "failIfUnavailable": true. Without it, if the sandbox can’t start (say, an update leaves bubblewrap or socat missing), Claude Code runs commands unsandboxed instead of refusing to start. On a rolling-release distro that’s not hypothetical. It didn’t fire that day, but it’s why I can be sure I wasn’t running naked.
  • "allowUnsandboxedCommands": false. When a command fails because of the sandbox, Claude Code can retry it with the sandbox disabled, and in auto mode the one deciding whether to allow that is the classifier that had just waved through a mirror --delete. An agent that has just seen hundreds of “read-only” errors in the middle of a test is exactly the one you don’t want to hand that option to. It didn’t have it.

What was just decoration

  • My ask rules covered git push, git reset --hard and git clean, but not lftp, so in auto mode the classifier happily approved a mirror --delete without asking me.
  • My long permissions.deny list of Read and Edit rules for ~/.ssh, ~/Documents and friends. Those rules don’t only bind Claude’s own tools: Claude Code merges them into the sandbox too. But every path on the list was already out of reach thanks to blockReadsOutsideWorkingDirectories, so that day they added nothing.
  • Almost nothing protects the working directory. Apart from a handful of protected paths, like the .claude settings, it’s writable by design: it’s where the agent is supposed to work. The sandbox shrank the blast radius from “my home” to “this project”, and only version control can shrink it further.

On the agent

Opus 5.5 before the incident and Opus 5.5 after the incident felt like two different employees. Before: it treated weird output from a destructive command as a path problem, used the real filesystem as a test backend, and never printed the expanded command. After: it stopped immediately, owned it without hedging, filed a report, and ran a recovery where every step was checked against the live site before being trusted. The ideal coworker, exactly one command too late.

What I learned

In the end I lost half an hour, a few internal notes, and some of my faith in multi-line strings. Here’s what I’m taking home:

  • The boring sandbox settings are the ones that matter. The default policy did the actual blocking; failIfUnavailable: true and allowUnsandboxedCommands: false guaranteed it was there, and that it stayed there. None of the path lists I had written by hand made a difference that day.
  • The folder the agent works in has no protection but version control. A Git remote would have turned this whole writeup into a five-minute annoyance and a much shorter blog post.
  • Bulk deletes deserve a prompt, even in auto mode. Something like:
"ask": [
  "Bash(lftp *)",
  "Bash(rsync *--delete*)",
  "Bash(rm -rf *)",
  "Bash(find * -delete*)"
]
  • A multi-line string is not a command. For lftp, the commands go in a file run with lftp -f, one per line, with set cmd:fail-exit yes and explicit source and target on every mirror.
  • A fake remote goes in the scratch folder, never at /. The first test, run that way, was harmless despite the same bug.
  • Weird output from a destructive command means stop. The nested dst/dst/ was the bug introducing itself.

And if an AI ever tells you it’s going to “quickly test something locally”, ask where “locally” is.

Who helped with what

First of all, I’d like to thank Claude Opus 5.5 for making this whole writeup possible.

It then helped me clean up after itself. It restored the project and helped me analyze what had happened, all from inside the same sandbox, because I’m not that trusting. Yes, the main suspect worked on its own case. That’s why every conclusion here comes from the transcripts and not from its version of events. To be fair, it was also the one who caught the false accusation in my first draft (see the Timeline).

It also helped me polish the English of this page.

All lab entries