Feature: Add the file-write tools behind their own switch
P10-B adds delete_path, move_path and create_folder, and the switch they need. Reusing McpFileTransfer would have been wrong: uploading overwrites a file, deleting destroys a tree, and an operator who wants one may well not want the other. McpFileWrite is therefore a separate key, off by default, wired the way every other switch is - read from settings.ini at startup, reported as get_server_status.switches.file_write, and exposed with its own checkbox in the MCP settings dialog. The tools additionally require readonly to be off: a read-only server has no business modifying a host. No client change was needed: COMMAND_DELETE_FILE, COMMAND_DELETE_DIRECTORY, COMMAND_CREATE_FOLDER and COMMAND_RENAME_FILE already existed in the Windows file manager. The chain reuses the file-manager sub-connection and adds a phase - the main connection asks for the drive list, the sub-connection's first packet hands it to OnDriveList which sends the write command, and the client's TOKEN_*_FINISH ack moves the pending request from phase 0 to 1, where the tool sends COMMAND_LIST_FILES for the parent so the ack and the confirmation share one timeout. Only the IO thread touches the sub-connection; the tool thread just waits on the shared condvar, which keeps the sub-connection's lifetime out of its reach. The confirmation exists because the client answers unconditionally: it ignores the return values of DeleteFile, MoveFile and MakeSureDirectoryPathExists and there is no error token. ok therefore only means "the command arrived", and verified is the real answer - true when the parent listing shows the expected post-state, false when it shows the opposite, null when the listing itself did not come back (timeout or disconnect), since reporting that as false would be a lie. Linux, macOS and Android hosts are refused outright: those clients decode these paths as GBK while the server speaks UTF-8 to them for list_files, so a non-ASCII path would corrupt rather than fail. Paths are validated as UTF-8, converted to GBK exactly once, and reject %VAR%, relative paths, drive and share roots, and - for delete_path and for move_path's source - the Windows, Program Files, ProgramData, $Recycle.Bin and System Volume Information trees. create_folder gets no system-directory ban, because writing there is the same permission upload_file already has. Two defects surfaced while exercising this against a real client: create_folder's wire path has to end with a backslash, because MakeSureDirectoryPathExists treats a non-terminated last component as a file name and only creates the intermediate levels; and ResolveFileOpVerified(LookupInListing(...), isDir, ...) read isDir before the lookup ever set it - argument evaluation order is unspecified and MSVC evaluates right to left - so every create_folder reported verified=false even when the directory existed. Verified on a Debug x64 build against a Windows client over MCP: nested create_folder, upload, rename, delete, delete a tree, the failure shapes (delete with recursive=false on a directory, create_folder onto an existing file name, delete of a non-existent target) and every refusal (drive root, C:\Windows\Temp, C:\Users, %TEMP%, relative, empty, over-long, from==to, offline host, non-numeric id), with a Chinese-named directory created, renamed and deleted end to end; no file-manager window appeared in the server process during any of it. The smoke test gained section [4c], opt-in through -FileWriteDir, which asserts verified and never ok and walks a sandbox through the same path; two runs passed 69 checks with the sandbox removed afterwards. Bin/settings.ini was restored byte-exact and the Release instance was left alone. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
bb55f52b29
commit
27b5431130
7 files changed
+933
-13
No files matched your search
+141
-5
@@ -1,10 +1,14 @@
|
||||
<#
|
||||
服务端 MCP 只读冒烟测试。
|
||||
服务端 MCP 冒烟测试。
|
||||
|
||||
只调用「不会改变任何状态」的工具(列表/详情类),断言结构化字段齐全、
|
||||
默认只调用「不会改变任何状态」的工具(列表/详情类),断言结构化字段齐全、
|
||||
分页字段正确、鉴权与请求体上限生效。可在发布前对着任一台在线主机跑一遍,
|
||||
确认 MCP 链路与改动前相比没有回归。
|
||||
|
||||
唯一会写目标机的部分是 [4c] 文件写入,且必须显式给 -FileWriteDir 才会跑:
|
||||
它在那个目录下建一个 mcp_smoke_xxxx 沙箱,走完 create/upload/rename/delete
|
||||
后整棵删掉,不碰沙箱以外的任何东西。
|
||||
|
||||
用法:
|
||||
$env:YAMA_MCP_TOKEN = '<token>' # 与 service 的 McpToken 一致
|
||||
powershell -ExecutionPolicy Bypass -File tools\mcp_smoke.ps1
|
||||
@@ -21,6 +25,11 @@
|
||||
10000 条上限,测出来的是「部分结果」而不是功能是否可用。
|
||||
-AuditLog 可选,服务端审计日志路径(如 Bin\logs\mcp_audit.log)。给了才断言
|
||||
-32006 拒绝会写一条 CALL_DENIED。
|
||||
-FileWriteDir 可选,跑 delete_path / move_path / create_folder 的目标机目录
|
||||
(必须已存在且可写,如 C:\Users\Public 或 C:\Temp)。脚本只在其下
|
||||
新建 mcp_smoke_xxxx 沙箱并在结束时删掉它。不指定就跳过这一节。
|
||||
注意 upload_file 的 local_path 指的是服务端本机路径,所以这一节要求
|
||||
冒烟脚本与服务端在同一台机器上(默认的 127.0.0.1 即如此)。
|
||||
|
||||
退出码:0 全部通过;1 有失败项。token 不会被打印。
|
||||
#>
|
||||
@@ -30,7 +39,8 @@ param(
|
||||
[string]$HostId = '',
|
||||
[switch]$SkipHost,
|
||||
[string]$SearchPath = '',
|
||||
[string]$AuditLog = ''
|
||||
[string]$AuditLog = '',
|
||||
[string]$FileWriteDir = ''
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
@@ -242,10 +252,13 @@ if (-not $SkipHost) {
|
||||
$pname = [string]$firstProc.name
|
||||
$byName = Rpc '' $null 'list_processes' @{ id = $HostId; name = $pname; limit = 100 }
|
||||
$bn = $byName.result.structuredContent
|
||||
# totalAll 是「过滤前的总数」,两次调用之间目标机的进程本来就会增减(实测差过 1),
|
||||
# 所以留一点余量:要断言的是「过滤没有把 totalAll 一起缩掉」,不是快照完全相等。
|
||||
$drift = [Math]::Abs([int]$bn.totalAll - [int]$ps.totalAll)
|
||||
Check 'list_processes name 过滤命中且 totalAll 不变' `
|
||||
($byName.error -eq $null -and [int]$bn.totalAll -eq [int]$ps.totalAll -and
|
||||
($byName.error -eq $null -and $drift -le 10 -and
|
||||
[int]$bn.total -gt 0 -and [int]$bn.total -le [int]$bn.totalAll) `
|
||||
("total=" + $bn.total + " totalAll=" + $bn.totalAll + " baseAll=" + $ps.totalAll)
|
||||
("total=" + $bn.total + " totalAll=" + $bn.totalAll + " baseAll=" + $ps.totalAll + " drift=" + $drift)
|
||||
$lower = $pname.ToLowerInvariant()
|
||||
Check 'list_processes name 过滤结果都含该名字' `
|
||||
(@($bn.processes | Where-Object { -not ([string]$_.name).ToLowerInvariant().Contains($lower) }).Count -eq 0)
|
||||
@@ -378,6 +391,129 @@ if (-not $SkipHost -and $drive) {
|
||||
}
|
||||
}
|
||||
|
||||
# ===== 4c. 文件写入(P10-B:delete_path / move_path / create_folder) =====
|
||||
# 客户端对删除文件/删除目录/建目录/改名四条命令都是「做完就无条件回完成包」,ok 只证明命令送达;真正的结论在
|
||||
# verified(true=复核看到状态变了,false=复核看到没变,null=复核没做成)。所以这一节只断言
|
||||
# verified,不看 ok。顺带覆盖两处只有真机才暴露的坑:建目录要在路径末尾补反斜杠,
|
||||
# 以及复核要读 LookupInListing 的 isDir 出参。
|
||||
function Detail($r) {
|
||||
if ($null -ne $r -and $null -ne $r.error) { return [string]$r.error.message }
|
||||
$c = @($r.result.content)
|
||||
if ($c.Count -gt 0) { return [string]$c[0].text }
|
||||
return ''
|
||||
}
|
||||
function ErrCode($r) {
|
||||
if ($null -eq $r -or $null -eq $r.error) { return 0 }
|
||||
return [int]$r.error.code
|
||||
}
|
||||
function OkVerified($r) {
|
||||
if ($null -eq $r -or $null -ne $r.error) { return $false }
|
||||
$s = $r.result.structuredContent
|
||||
return (($s.ok -eq $true) -and ($s.verified -eq $true))
|
||||
}
|
||||
function ListingNames([string]$path) {
|
||||
$r = Rpc '' $null 'list_files' @{ id = $HostId; path = $path; limit = 100 }
|
||||
if ($r.error) { return @() }
|
||||
return @($r.result.structuredContent.files | ForEach-Object { [string]$_.name })
|
||||
}
|
||||
|
||||
$writeTools = @($tools | Where-Object { $_.name -in @('delete_path', 'move_path', 'create_folder') })
|
||||
if ($writeTools.Count -lt 3) {
|
||||
Write-Host "`n[4c] 文件写入:工具未出现(McpFileWrite 关或 McpReadonly 开),跳过。" -ForegroundColor Yellow
|
||||
} elseif ($SkipHost -or -not $HostId) {
|
||||
Write-Host "`n[4c] 文件写入:没有在线主机,跳过。" -ForegroundColor Yellow
|
||||
} elseif (-not $FileWriteDir) {
|
||||
Write-Host "`n[4c] 文件写入:未给 -FileWriteDir,跳过(这一节会在目标机上建/删文件)。" -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host "`n[4c] 文件写入 (根=$FileWriteDir)"
|
||||
$sandbox = $FileWriteDir.TrimEnd('\') + '\mcp_smoke_' + (Get-Random -Minimum 10000 -Maximum 99999)
|
||||
$uploadedName = ''
|
||||
try {
|
||||
# 一次建两层:既测「中间层一并创建」,也测路径末尾的反斜杠(少了它,客户端
|
||||
# MakeSureDirectoryPathExists 会把最后一级当成文件名,只建中间层)。
|
||||
$deep = $sandbox + '\sub\deep'
|
||||
$mk = Rpc '' $null 'create_folder' @{ id = $HostId; path = $deep }
|
||||
Check 'create_folder 建嵌套目录 verified=true' (OkVerified $mk) (Detail $mk)
|
||||
$names = ListingNames $sandbox
|
||||
Check 'create_folder 后父目录里能看到它' ($names -contains 'sub') ("names=" + ($names -join ','))
|
||||
|
||||
# upload_file 的返回值只有摘要,用列表复核它到底落没落盘
|
||||
$localFile = Join-Path $env:TEMP ('mcp_smoke_upload_' + (Get-Random -Minimum 1000 -Maximum 9999) + '.txt')
|
||||
Set-Content -LiteralPath $localFile -Value 'mcp smoke' -Encoding ascii
|
||||
$uploadedName = [System.IO.Path]::GetFileName($localFile)
|
||||
$uploadOk = $false
|
||||
if (@($tools | Where-Object { $_.name -eq 'upload_file' }).Count -gt 0) {
|
||||
$up = Rpc '' $null 'upload_file' @{ id = $HostId; local_path = $localFile; remote_dir = $sandbox }
|
||||
$uploadOk = ($null -eq $up.error)
|
||||
Check 'upload_file 正常返回' $uploadOk (Detail $up)
|
||||
} else {
|
||||
Write-Host " upload_file 未开启,跳过上传/改名/删文件三连。" -ForegroundColor Yellow
|
||||
}
|
||||
if ($uploadOk) {
|
||||
Check 'upload_file 后沙箱里能看到该文件' ((ListingNames $sandbox) -contains $uploadedName)
|
||||
|
||||
$renamed = $uploadedName -replace '\.txt$', '_renamed.txt'
|
||||
$mv = Rpc '' $null 'move_path' @{ id = $HostId; from = ($sandbox + '\' + $uploadedName); to = ($sandbox + '\' + $renamed) }
|
||||
Check 'move_path 改名 verified=true' (OkVerified $mv) (Detail $mv)
|
||||
$names = ListingNames $sandbox
|
||||
Check 'move_path 后新名字出现、旧名字消失' `
|
||||
(($names -contains $renamed) -and -not ($names -contains $uploadedName)) ("names=" + ($names -join ','))
|
||||
$uploadedName = $renamed
|
||||
|
||||
$rm = Rpc '' $null 'delete_path' @{ id = $HostId; path = ($sandbox + '\' + $renamed) }
|
||||
Check 'delete_path 删文件 verified=true' (OkVerified $rm) (Detail $rm)
|
||||
Check 'delete_path 后文件不在列表里' (-not ((ListingNames $sandbox) -contains $renamed))
|
||||
} elseif (@($tools | Where-Object { $_.name -eq 'upload_file' }).Count -gt 0) {
|
||||
# 上传失败时改名/删文件没有对象,跳过而不是再报两条失败(upload_file 那条已经记了)
|
||||
Write-Host " 上一步 upload_file 失败,跳过改名/删文件。" -ForegroundColor Yellow
|
||||
}
|
||||
Remove-Item -LiteralPath $localFile -Force -ErrorAction SilentlyContinue
|
||||
|
||||
# 非 ASCII 路径:服务端按 UTF-8 校验、只转一次 GBK,客户端再用 A 接口落盘
|
||||
$cnDir = $sandbox + '\中文目录'
|
||||
$cn = Rpc '' $null 'create_folder' @{ id = $HostId; path = $cnDir }
|
||||
Check 'create_folder 中文路径 verified=true' (OkVerified $cn) (Detail $cn)
|
||||
$cnRm = Rpc '' $null 'delete_path' @{ id = $HostId; path = $cnDir; recursive = $true }
|
||||
Check 'delete_path 中文路径 verified=true' (OkVerified $cnRm) (Detail $cnRm)
|
||||
|
||||
# recursive=false 落在目录上:客户端 DeleteFile 失败且不回错误包 → 复核必须说 false
|
||||
$mk2 = Rpc '' $null 'create_folder' @{ id = $HostId; path = ($sandbox + '\asdir') }
|
||||
if (OkVerified $mk2) {
|
||||
$bad = Rpc '' $null 'delete_path' @{ id = $HostId; path = ($sandbox + '\asdir') }
|
||||
Check 'delete_path 对目录用 recursive=false 时 verified=false' `
|
||||
(($null -eq $bad.error) -and ($bad.result.structuredContent.ok -eq $true) -and
|
||||
($bad.result.structuredContent.verified -eq $false)) (Detail $bad)
|
||||
}
|
||||
|
||||
# 越界与参数拒绝:全部发生在服务端,不该触达目标机
|
||||
$fwDrive = if ($drive) { $drive } else { $FileWriteDir.Substring(0, 2) }
|
||||
$fwRoot = $fwDrive.TrimEnd('\')
|
||||
$longPath = $sandbox + '\' + ('a' * 220)
|
||||
$cases = @(
|
||||
@{ name = '盘符根'; tool = 'delete_path'; args = @{ id = $HostId; path = $fwRoot }; code = -32602 }
|
||||
@{ name = '系统目录'; tool = 'delete_path'; args = @{ id = $HostId; path = ($fwRoot + '\Windows\Temp') }; code = -32602 }
|
||||
@{ name = '含 %VAR%'; tool = 'delete_path'; args = @{ id = $HostId; path = '%TEMP%\mcp_smoke_x' }; code = -32602 }
|
||||
@{ name = '相对路径'; tool = 'create_folder'; args = @{ id = $HostId; path = 'mcp_smoke_x' }; code = -32602 }
|
||||
@{ name = '超长路径'; tool = 'create_folder'; args = @{ id = $HostId; path = $longPath }; code = -32602 }
|
||||
@{ name = 'from 与 to 相同'; tool = 'move_path'; args = @{ id = $HostId; from = $deep; to = $deep.ToUpperInvariant() }; code = -32602 }
|
||||
@{ name = '主机不在线'; tool = 'create_folder'; args = @{ id = '1'; path = ($sandbox + '\x') }; code = -32002 }
|
||||
)
|
||||
foreach ($c in $cases) {
|
||||
$r = Rpc '' $null $c.tool $c.args
|
||||
Check ('参数被拒:' + $c.name + ' 返回 ' + $c.code) ((ErrCode $r) -eq $c.code) (Detail $r)
|
||||
}
|
||||
|
||||
# 收尾:整棵删掉沙箱,并用父目录列表复核
|
||||
$rmAll = Rpc '' $null 'delete_path' @{ id = $HostId; path = $sandbox; recursive = $true }
|
||||
Check 'delete_path 递归删沙箱 verified=true' (OkVerified $rmAll) (Detail $rmAll)
|
||||
$fwParent = $FileWriteDir.TrimEnd('\')
|
||||
Check '沙箱已不在父目录里' (-not ((ListingNames $fwParent) -contains [System.IO.Path]::GetFileName($sandbox)))
|
||||
} finally {
|
||||
# 中途失败时也要尽量不把沙箱留在目标机上(尽力而为,不再断言)
|
||||
$null = Rpc '' $null 'delete_path' @{ id = $HostId; path = $sandbox; recursive = $true }
|
||||
}
|
||||
}
|
||||
|
||||
# ===== 5. 策略拒绝(-32006) =====
|
||||
# 只在某个开关确实关闭时才有意义,所以先读 get_server_status 的 switches 再挑一个关着的功能。
|
||||
# 全部开启时跳过(不算失败)——这条不是每次都能测,但测到时能证明拒绝路径与审计都还在。
|
||||
|
||||
Reference in new issue
Block a user