跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

test-windows-verified-file-deletion.ps1

59 行 · 3.0 KB历史下载
1#requires -Version 5.1
2$ErrorActionPreference = 'Stop'
3$source = Join-Path $PSScriptRoot '../packaging/windows'
4$root = Join-Path ([IO.Path]::GetTempPath()) ('delete-race-' + [guid]::NewGuid().ToString('N'))
5New-Item -ItemType Directory -Path $root | Out-Null
6try {
7 Add-Type -Path (Join-Path $source 'verified-file-deletion.cs')
8 $tokens = $null
9 $errors = $null
10 $ast = [Management.Automation.Language.Parser]::ParseFile((Join-Path $source 'installation-files.ps1'), [ref]$tokens, [ref]$errors)
11 if ($errors.Count) { throw 'The production installation helper has syntax errors.' }
12 $definitions = $ast.FindAll({ param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] }, $true)
13 foreach ($definition in $definitions) {
14 if ($definition.Name -in @('Open-ReadLocked', 'Stream-Hash', 'Initialize-VerifiedDeletion', 'Remove-MatchingFile')) {
15 . ([scriptblock]::Create($definition.Extent.Text))
16 }
17 }
18
19 $script:originalHash = (Get-Item function:Stream-Hash).ScriptBlock
20 $script:replacePath = Join-Path $root 'program.dll'
21 $script:renamedPath = Join-Path $root 'original-renamed.dll'
22 [IO.File]::WriteAllText($script:replacePath, 'verified old program')
23 $expected = (Get-FileHash $script:replacePath).Hash
24 function Stream-Hash([IO.Stream]$Stream) {
25 $hash = & $script:originalHash $Stream
26 [IO.File]::Move($script:replacePath, $script:renamedPath)
27 [IO.File]::WriteAllText($script:replacePath, 'new personal data')
28 return $hash
29 }
30 Remove-MatchingFile $root 'program.dll' $expected
31 $replacementRetained = [IO.File]::ReadAllText($script:replacePath) -eq 'new personal data'
32 $verifiedObjectDeleted = !(Test-Path $script:renamedPath)
33
34 $outside = Join-Path $root 'outside'
35 New-Item -ItemType Directory -Path $outside | Out-Null
36 [IO.File]::WriteAllText((Join-Path $outside 'owned.dll'), 'external target')
37 $link = Join-Path $root 'linked'
38 & cmd.exe /c mklink /J $link $outside | Out-Null
39 if ($LASTEXITCODE -ne 0) { throw 'Could not create the isolated junction fixture.' }
40 $reparseDenied = $false
41 try {
42 $stream = [CageVerifiedFile]::Open($root, 'linked/owned.dll')
43 $stream.Dispose()
44 } catch { $reparseDenied = $true }
45 $externalRetained = [IO.File]::ReadAllText((Join-Path $outside 'owned.dll')) -eq 'external target'
46 [pscustomobject]@{
47 ReplacementRetained = $replacementRetained
48 VerifiedObjectDeleted = $verifiedObjectDeleted
49 ReparseTraversalDenied = $reparseDenied
50 ExternalTargetRetained = $externalRetained
51 PowerShell = $PSVersionTable.PSVersion.ToString()
52 } | ConvertTo-Json -Compress
53 if (!$replacementRetained -or !$verifiedObjectDeleted -or !$reparseDenied -or !$externalRetained) {
54 throw 'Verified-handle deletion postconditions failed.'
55 }
56} finally {
57 if (Test-Path -LiteralPath (Join-Path $root 'linked')) { [IO.Directory]::Delete((Join-Path $root 'linked')) }
58 Remove-Item -LiteralPath $root -Recurse -Force
59}