如何尽可能避免大模型误删文件
- 内容介绍
- 文章标签
- 相关推荐
看到好多佬友被cc,cx删全盘,讨论很多但是没有好的总结
在众多佬友讨论内容的基础上总结,也欢迎其他佬友纠正
加上我们的服务器也有其他人在用,因此从个人/管理的角度说说我现在的做法
[!Note]
尽量别用Powershell 尽量上wsl
一楼佬友说的对
我写的时候powershell的材料都得去查
1、最简单:不让ai执行rm【软限制+人工审批】
- 要删文件先让ai移动到一个新文件夹,然后自己再手动移除,只要是rm命令直接全部拒绝
- 设置全局提示词
~/.codex/AGENTS.md 或者 ~/.claude/CLAUDE.md
- 绝对禁止执行 rm、sudo rm、mkfs、dd 等涉及删除文件的命令
- 需要删除、移动、覆盖文件必须输出简单命令,不要使用管道等构建负责命令
- 如果存在trash-cli及其他类似物,使用trash删除文件,不存在trash-cli时删除文件默认移动到工作区文件夹下.Trash/下
- 也可以用审批规则再限制一下rm,但是cc和cx有时候写的很复杂的命令不一定能匹配到,尤其codex好像只匹配前缀
~/.codex/rules/block-rm.rules
prefix_rule(
pattern = ["rm"],
decision = "deny",
)
prefix_rule(
pattern = ["sudo", "rm"],
decision = "deny",
)
prefix_rule(
pattern = ["shred"],
decision = "deny",
)
prefix_rule(
pattern = ["mkfs"],
decision = "deny",
)
prefix_rule(
pattern = ["dd"],
decision = "deny",
)
~/.claude/settings.json
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"deny": [
"Bash(rm *)",
"Bash(sudo rm *)",
"Bash(shred *)",
"Bash(mkfs *)",
"Bash(dd *)",
"Bash(find * -delete *)",
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)"
],
"ask": [ ],
"allow": [ ]
}
}
[!warning]
方案1还是存在可能给rm写进脚本里面执行,上下文超长压缩失效等等情况,还是依赖人工审核审批
2、进一步:尽可能阻止rm
- 追求尽可能安全就是阻止全部rm,将rm链接到safe-rm或者trash(小白更推荐trash)
- Linux和mac中可以将trash-cli链接到/usr/local/bin/rm(系统)或者 ~/.local/bin/rm (用户)
- 其中mac建议用自带的trash或者brew安装,多数Linux包管理器有trash可以安装
- windows中可以编辑PowerShell Profile用function替换掉rm
- Linux和mac中可以将trash-cli链接到/usr/local/bin/rm(系统)或者 ~/.local/bin/rm (用户)
powershell
function rm {
trash @args
}
- trash-cli本身就有设置兼容rm的模式,虽然可能会导致以后习惯rm再去翻trash回收站的情况,但是可以有效杜绝这种情况
更进阶
因为管理服务器,所以vibe coding了一个脚本,实现替换rm
如果有人需要后面整理好我二编上来,现在只适配测试了Linux
可以系统级安装也可以用户级安装
核心是让用户自行决定屏蔽rm到哪一层,也支持管理员指定屏蔽到哪一层
· 1、屏蔽+提醒(保障系统中其他正常rm命令 即:白名单正常走safe-rm/rm,剩余rm红字提醒然后走trash)
· 2、屏蔽+不提醒(即直接链接trash,可能会导致用户rm的习惯,导致后续在其他服务器产生风险)
· 其中白名单还做了校验,rm必须执行的rm全部在白名单下面才能走白名单
[!notice]
方案2还存在的问题:
- 防不了直接/bin/rm调用 但是一般ai也不会这么用吧,也不能直接给系统里面rm干没
- api删除
- windows还有其他命令,有点乱乱的
3、最有效:
快照、备份、异地备份
网友解答:--【壹】--: 都由我来赎:
trash --verbose $fileName
佬,这里用文件名不合适吧,万一当前工作目录和目标目录有重名文件不炸了
还有就是只防rm不够吧,pwsh好多删除的命令可以用,还是尽量用wsl
我也不是很懂,bash,zsh用的多。pwsh看着就头大
--【贰】--:
毕竟也有Linux出问题的吧,虽然pwsh出问题的数量遥遥领先
论坛合订本还是有一些rm -rf的https://linux.do/t/topic/1585027
--【叁】--:
有总结呀
不要用 pwsh,结束
写那些规则什么的毛用没有
尊贵的 pwsh 语法直接把你的沙盒打的稀巴烂
--【肆】--:
那关键还是别让ai去做删除,多备份多快照才有用
pwsh+codex写超长命令+windows路径本身就乱 真的容易出问题
建议阅读前面我提到的其他佬的合订本吸取经验教训
--【伍】--:
写一个脚本,配置hook,这样应该可以避免吧
--【陆】--:
好的谢谢佬 我是一点都不懂 有点怕怕的
--【柒】--:
不能完全避免,备份是王道
--【捌】--:
大佬们看看 ai写的这个有没有问题 对正常运行有什么影响不
# 移除原有的 rm 别名
Remove-Item Alias:rm -Force -ErrorAction SilentlyContinue
function rm {
param(
[switch]$Force,
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$Path
)
# 保护的盘符和路径
$protectedPaths = @('C:\', 'D:\', 'E:\','F:\','G:\', 'C:\Windows', 'C:\Program Files', 'C:\Program Files (x86)')
foreach ($item in $Path) {
# 获取绝对路径
$fullPath = (Resolve-Path $item -ErrorAction SilentlyContinue).Path
if (-not $fullPath) {
$fullPath = $item
}
# 检查是否是受保护的路径
$isProtected = $false
foreach ($protected in $protectedPaths) {
if ($fullPath -eq $protected -or $fullPath -like "$protected*") {
if ($fullPath.Split('\').Count -le 2) { # 只保护根目录和一级目录
$isProtected = $true
break
}
}
}
if ($isProtected) {
Write-Host "BLOCKED: Cannot delete protected path '$fullPath'" -ForegroundColor Red
continue
}
if (Test-Path $item) {
$fileName = Split-Path $item -Leaf
if ($Force) {
Write-Host "WARNING: Permanently delete '$fileName'? This CANNOT be undone!" -ForegroundColor Red
$confirm = Read-Host "Type 'yes' to confirm"
if ($confirm -eq 'yes') {
Remove-Item $item -Force -Recurse
Write-Host "Permanently deleted: $fileName" -ForegroundColor Red
} else {
Write-Host "Cancelled" -ForegroundColor Yellow
}
} else {
Write-Host "Moving to trash: $fileName" -ForegroundColor Yellow
trash --verbose $fileName
}
} else {
Write-Host "File not found: $item" -ForegroundColor Red
}
}
}
看到好多佬友被cc,cx删全盘,讨论很多但是没有好的总结
在众多佬友讨论内容的基础上总结,也欢迎其他佬友纠正
加上我们的服务器也有其他人在用,因此从个人/管理的角度说说我现在的做法
[!Note]
尽量别用Powershell 尽量上wsl
一楼佬友说的对
我写的时候powershell的材料都得去查
1、最简单:不让ai执行rm【软限制+人工审批】
- 要删文件先让ai移动到一个新文件夹,然后自己再手动移除,只要是rm命令直接全部拒绝
- 设置全局提示词
~/.codex/AGENTS.md 或者 ~/.claude/CLAUDE.md
- 绝对禁止执行 rm、sudo rm、mkfs、dd 等涉及删除文件的命令
- 需要删除、移动、覆盖文件必须输出简单命令,不要使用管道等构建负责命令
- 如果存在trash-cli及其他类似物,使用trash删除文件,不存在trash-cli时删除文件默认移动到工作区文件夹下.Trash/下
- 也可以用审批规则再限制一下rm,但是cc和cx有时候写的很复杂的命令不一定能匹配到,尤其codex好像只匹配前缀
~/.codex/rules/block-rm.rules
prefix_rule(
pattern = ["rm"],
decision = "deny",
)
prefix_rule(
pattern = ["sudo", "rm"],
decision = "deny",
)
prefix_rule(
pattern = ["shred"],
decision = "deny",
)
prefix_rule(
pattern = ["mkfs"],
decision = "deny",
)
prefix_rule(
pattern = ["dd"],
decision = "deny",
)
~/.claude/settings.json
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"deny": [
"Bash(rm *)",
"Bash(sudo rm *)",
"Bash(shred *)",
"Bash(mkfs *)",
"Bash(dd *)",
"Bash(find * -delete *)",
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)"
],
"ask": [ ],
"allow": [ ]
}
}
[!warning]
方案1还是存在可能给rm写进脚本里面执行,上下文超长压缩失效等等情况,还是依赖人工审核审批
2、进一步:尽可能阻止rm
- 追求尽可能安全就是阻止全部rm,将rm链接到safe-rm或者trash(小白更推荐trash)
- Linux和mac中可以将trash-cli链接到/usr/local/bin/rm(系统)或者 ~/.local/bin/rm (用户)
- 其中mac建议用自带的trash或者brew安装,多数Linux包管理器有trash可以安装
- windows中可以编辑PowerShell Profile用function替换掉rm
- Linux和mac中可以将trash-cli链接到/usr/local/bin/rm(系统)或者 ~/.local/bin/rm (用户)
powershell
function rm {
trash @args
}
- trash-cli本身就有设置兼容rm的模式,虽然可能会导致以后习惯rm再去翻trash回收站的情况,但是可以有效杜绝这种情况
更进阶
因为管理服务器,所以vibe coding了一个脚本,实现替换rm
如果有人需要后面整理好我二编上来,现在只适配测试了Linux
可以系统级安装也可以用户级安装
核心是让用户自行决定屏蔽rm到哪一层,也支持管理员指定屏蔽到哪一层
· 1、屏蔽+提醒(保障系统中其他正常rm命令 即:白名单正常走safe-rm/rm,剩余rm红字提醒然后走trash)
· 2、屏蔽+不提醒(即直接链接trash,可能会导致用户rm的习惯,导致后续在其他服务器产生风险)
· 其中白名单还做了校验,rm必须执行的rm全部在白名单下面才能走白名单
[!notice]
方案2还存在的问题:
- 防不了直接/bin/rm调用 但是一般ai也不会这么用吧,也不能直接给系统里面rm干没
- api删除
- windows还有其他命令,有点乱乱的
3、最有效:
快照、备份、异地备份
网友解答:--【壹】--: 都由我来赎:
trash --verbose $fileName
佬,这里用文件名不合适吧,万一当前工作目录和目标目录有重名文件不炸了
还有就是只防rm不够吧,pwsh好多删除的命令可以用,还是尽量用wsl
我也不是很懂,bash,zsh用的多。pwsh看着就头大
--【贰】--:
毕竟也有Linux出问题的吧,虽然pwsh出问题的数量遥遥领先
论坛合订本还是有一些rm -rf的https://linux.do/t/topic/1585027
--【叁】--:
有总结呀
不要用 pwsh,结束
写那些规则什么的毛用没有
尊贵的 pwsh 语法直接把你的沙盒打的稀巴烂
--【肆】--:
那关键还是别让ai去做删除,多备份多快照才有用
pwsh+codex写超长命令+windows路径本身就乱 真的容易出问题
建议阅读前面我提到的其他佬的合订本吸取经验教训
--【伍】--:
写一个脚本,配置hook,这样应该可以避免吧
--【陆】--:
好的谢谢佬 我是一点都不懂 有点怕怕的
--【柒】--:
不能完全避免,备份是王道
--【捌】--:
大佬们看看 ai写的这个有没有问题 对正常运行有什么影响不
# 移除原有的 rm 别名
Remove-Item Alias:rm -Force -ErrorAction SilentlyContinue
function rm {
param(
[switch]$Force,
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$Path
)
# 保护的盘符和路径
$protectedPaths = @('C:\', 'D:\', 'E:\','F:\','G:\', 'C:\Windows', 'C:\Program Files', 'C:\Program Files (x86)')
foreach ($item in $Path) {
# 获取绝对路径
$fullPath = (Resolve-Path $item -ErrorAction SilentlyContinue).Path
if (-not $fullPath) {
$fullPath = $item
}
# 检查是否是受保护的路径
$isProtected = $false
foreach ($protected in $protectedPaths) {
if ($fullPath -eq $protected -or $fullPath -like "$protected*") {
if ($fullPath.Split('\').Count -le 2) { # 只保护根目录和一级目录
$isProtected = $true
break
}
}
}
if ($isProtected) {
Write-Host "BLOCKED: Cannot delete protected path '$fullPath'" -ForegroundColor Red
continue
}
if (Test-Path $item) {
$fileName = Split-Path $item -Leaf
if ($Force) {
Write-Host "WARNING: Permanently delete '$fileName'? This CANNOT be undone!" -ForegroundColor Red
$confirm = Read-Host "Type 'yes' to confirm"
if ($confirm -eq 'yes') {
Remove-Item $item -Force -Recurse
Write-Host "Permanently deleted: $fileName" -ForegroundColor Red
} else {
Write-Host "Cancelled" -ForegroundColor Yellow
}
} else {
Write-Host "Moving to trash: $fileName" -ForegroundColor Yellow
trash --verbose $fileName
}
} else {
Write-Host "File not found: $item" -ForegroundColor Red
}
}
}

