[Bug] AI overwrote and cleared a file by executing WriteAllText with null $content without safety confirmation
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Description:
A critical bug caused all text in my local file to be completely erased, leaving the file entirely empty.
I asked the agent to perform a text replacement. The agent prompted me for approval, showing a benign "text replacement" action, so I approved it. However, the agent's actual PowerShell command was bugged: it executed WriteAllText while the $content variable was null.
Because the approval screen hid this bug, I unknowingly approved a destructive file overwrite. I also request token compensation for this session due to the resulting data loss and troubleshooting time.
Steps to Reproduce:
- Instruct the agent to replace text in a local file.
- The agent generates a
WriteAllTextPowerShell command but fails to assign data to the$contentvariable (leaving it null). - The agent asks for approval, presenting the action safely as just "text replacement".
- The user approves it.
- The file is instantly overwritten with null data, completely emptying the file.
Actual Behavior:
The file's contents were completely erased because a flawed WriteAllText command (with null content) was masked as a safe text replacement on the approval screen.
What Should Happen?
The agent must verify that variables (like $content) are not null and have sufficient length before executing file write operations. Also, the approval screen must accurately show the real payload so users are not tricked into approving destructive bugs.
Error Messages/Logs
No system error was thrown, but the agent admitted its internal failure:
"The file becoming empty was not an intentional deletion, but an unexpected side effect of a PowerShell command bug (WriteAllText was executed while $content was null). The operation you approved was shown as 'text replacement', so there was no way for you to know from the approval screen."
The agent admitted it lacked a safety guard to check `$content` length before writing.
Steps to Reproduce
- Instruct the agent to replace text in a local file.
- The agent generates a
WriteAllTextPowerShell command but fails to assign data to the$contentvariable (leaving it null). - The agent asks for approval, presenting the action safely as just "text replacement".
- The user approves it.
- The file is instantly overwritten with null data, completely emptying the file.
Claude Model
Sonnet (default)
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.197
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
PowerShell
Additional Information
<img width="1148" height="316" alt="Image" src="https://github.com/user-attachments/assets/cff147f7-f8a6-469b-a428-100d660a5c57" />
<img width="1129" height="272" alt="Image" src="https://github.com/user-attachments/assets/7b57c49d-b57c-4d62-aede-0c5fee05e214" />
4 Comments
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
The durable fix is provider-side (confirm before an empty/null write lands on an existing non-empty file). Until that exists, this failure class has two surfaces you can guard yourself, and they need different hooks because the wipe can arrive either as a
Writetool call or as a shell command.1. The
Write-tool surface. APreToolUsehook onWritethat blocks empty/whitespace-only content over an existing non-empty file. It fires only on the truncate-to-empty case, so normal edits pass. (cc-safe-setup ships this aswrite-empty-content-guard.sh— MIT.)2. The command surface — your actual case (
WriteAllTextwith$null). That does not go through theWritetool, so #1 won't see it. APreToolUsehook onBashthat matches the explicit empty-value forms:I verified it against your form and variants (7/7):
[IO.File]::WriteAllText($path, $null),powershell -c "[IO.File]::WriteAllText(\"C:\a.txt\", \"\")"(the escaped\"\"form),Set-Content -Path a.txt -Value $null,Out-File -FilePath a.txt -InputObject "".WriteAllText($path, $content),Set-Content -Value "hello",echo done > log.txt.Honest limits. Command-string matching is inherently brittle: it catches the explicit empty-value forms above, but a value that resolves to empty at runtime (a variable that happens to be null, an unusual quoting) can slip past. I deliberately do not match bare
> fileredirects, because clearing a log with> fileis a normal, intended operation and matching it would be noisy. So treat this as a targeted guard for the accidental-WriteAllText-null pattern, not a universal empty-write firewall — theWrite-tool guard in #1 is the robust half, and the real fix is the provider-side confirmation you asked for.WriteAllText with null content clearing your file with no safety prompt — this is the silent-destructive-edit failure exactly. I use a tool that handles it mechanically — an AI can't execute a write that empties/overwrites a file without a verified safety check first, and if one slips through it gets flagged for you to review and usually stopped. Might be worth a look.
The dangerous part is exactly what you said, the approval screen showed a benign looking text replacement while the actual command was a bugged full file overwrite, so approval was informed by the wrong thing. Any policy engine sitting between the model and the shell (immunity-agent included, github.com/PrismorSec/prismor) faces the same limitation unless it evaluates the literal command text rather than trusting the tool's own description of what it's about to do, and even command text evaluation wouldn't have caught this specific case since WriteAllText with a null content variable doesn't match any destructive pattern, it looks like a normal, successful write until the variable itself is empty. This is more of a null/empty-value guard problem than a permissions problem.