[Bug] AI overwrote and cleared a file by executing WriteAllText with null $content without safety confirmation

Status Open
Reported on v2.1.197
Maintainer reply None cached
Activity 4 comments · opened Jul 1, 2026

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:

  1. Instruct the agent to replace text in a local file.
  2. The agent generates a WriteAllText PowerShell command but fails to assign data to the $content variable (leaving it null).
  3. The agent asks for approval, presenting the action safely as just "text replacement".
  4. The user approves it.
  5. 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

  1. Instruct the agent to replace text in a local file.
  2. The agent generates a WriteAllText PowerShell command but fails to assign data to the $content variable (leaving it null).
  3. The agent asks for approval, presenting the action safely as just "text replacement".
  4. The user approves it.
  5. 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" />

View original on GitHub ↗

4 Comments

github-actions[bot] · 2 months ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/67385
  2. https://github.com/anthropics/claude-code/issues/62085
  3. https://github.com/anthropics/claude-code/issues/53548

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

yurukusa · 1 month ago

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 Write tool call or as a shell command.

1. The Write-tool surface. A PreToolUse hook on Write that 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 as write-empty-content-guard.sh — MIT.)

2. The command surface — your actual case (WriteAllText with $null). That does not go through the Write tool, so #1 won't see it. A PreToolUse hook on Bash that matches the explicit empty-value forms:

#!/usr/bin/env bash
# PreToolUse (matcher: "Bash") — block WriteAllText/Set-Content/Out-File with a null/empty value (#72666)
input=$(cat)
cmd=$(printf '%s' "$input" | jq -r '.tool_input.command // empty' 2>/dev/null)
[ -z "$cmd" ] && exit 0
EMPTY='(\$null|\\*"\\*"|'"'"''"'"'|\[[Ss]tring\]::[Ee]mpty)'
if printf '%s' "$cmd" | grep -Eiq "WriteAllText\s*\([^,]+,\s*${EMPTY}\s*\)"; then
  echo "BLOCKED: WriteAllText is writing empty/null content — this wipes the file (#72666). Confirm intent first." >&2; exit 2
fi
if printf '%s' "$cmd" | grep -Eiq "(Set-Content|Out-File)\b[^|]*-(Value|InputObject)\s+${EMPTY}"; then
  echo "BLOCKED: Set-Content/Out-File is writing an empty/null value — this wipes the file (#72666)." >&2; exit 2
fi
exit 0

I verified it against your form and variants (7/7):

  • Blocked: [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 "".
  • Allowed: 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 > file redirects, because clearing a log with > file is 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 — the Write-tool guard in #1 is the robust half, and the real fix is the provider-side confirmation you asked for.

JonahAlexanian · 1 month ago

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.

Ar9av · 1 month ago

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.