dsh-delete-guard 0.0.0-stage → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "dsh-delete-guard",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "2.2.3",
4
+ "description": "Force every DSH file deletion through the OS Recycle Bin. Intercepts shell tool calls at tools/pre-execute (pwsh, bash, cmd, the persistent terminal), rewrites destructive delete commands into a recoverable recycle-bin move (Win32 SHFileOperation / SHQueryRecycleBin), and blocks permanent-erase escapes.",
5
+ "type": "module",
6
+ "main": "lib/index.js",
7
+ "exports": {
8
+ ".": "./lib/index.js",
9
+ "./guard": "./lib/guard.js",
10
+ "./recycle": "./lib/recycle.js",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "license": "MIT",
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "author": "deepestevan",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/deepestevan/dsh-delete-guard.git"
21
+ },
22
+ "homepage": "https://github.com/deepestevan/dsh-delete-guard#readme",
23
+ "bugs": {
24
+ "url": "https://github.com/deepestevan/dsh-delete-guard/issues"
25
+ },
26
+ "dsh": {
27
+ "runtime": "host",
28
+ "bundle": {
29
+ "patch": "./cordis.patch.yml"
30
+ }
31
+ },
32
+ "files": [
33
+ "lib",
34
+ "scripts",
35
+ "cordis.patch.yml",
36
+ "README.md",
37
+ "README.en.md",
38
+ "README.ja.md",
39
+ "CHANGELOG.md",
40
+ "SECURITY.md"
41
+ ],
42
+ "scripts": {
43
+ "test": "node --test \"test/*.test.mjs\""
44
+ },
45
+ "engines": {
46
+ "node": ">=20.0.0"
47
+ },
48
+ "os": [
49
+ "win32"
50
+ ],
51
+ "keywords": [
52
+ "dsh-plugin",
53
+ "deepseek-harness",
54
+ "dsh",
55
+ "recycle-bin",
56
+ "trash",
57
+ "safe-delete",
58
+ "undelete",
59
+ "guardrails",
60
+ "hooks",
61
+ "windows"
62
+ ]
63
+ }
@@ -0,0 +1,271 @@
1
+ <#
2
+ .SYNOPSIS
3
+ Recycle Bin bridge for the dsh-delete-guard plugin.
4
+
5
+ .DESCRIPTION
6
+ Reads one JSON request from stdin and writes one JSON response to stdout.
7
+
8
+ Requests:
9
+ { "op": "recycle", "paths": ["C:\\a\\b.txt", ...] }
10
+ { "op": "usage", "drive": "C:" }
11
+ Responses:
12
+ { "recycled": [...], "failures": [{ "path": "...", "error": "..." }] }
13
+ { "items": 12, "bytes": 345678 }
14
+ { "freeBytes": 99999999 }
15
+
16
+ The recycle operation uses SHFileOperationW with FOF_ALLOWUNDO, the same
17
+ Win32 path Explorer uses, so the bin's $I/$R metadata is written correctly.
18
+ A failed move is reported, never silently escalated to a permanent delete.
19
+ Paths whose volume has no bin at all (UNC shares, exFAT/FAT32 volumes) are
20
+ refused before the call: SHFileOperationW would erase them and return 0.
21
+ #>
22
+
23
+ Set-StrictMode -Version Latest
24
+ $ErrorActionPreference = 'Stop'
25
+
26
+ # stdin/stdout are UTF-8 by contract with the Node bridge in lib/recycle.js.
27
+ # [Console]::InputEncoding is NOT UTF-8 here: it follows the OEM code page (936
28
+ # on a zh-CN box), so reading through [Console]::In decodes the incoming UTF-8
29
+ # bytes as GBK. Every non-ASCII path then arrives as mojibake (UTF-8 bytes
30
+ # reinterpreted as GBK), and the shell rejects the whole batch as invalid.
31
+ # Decode the raw standard-input stream explicitly instead of trusting the
32
+ # console's configured code page.
33
+ [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
34
+ $Utf8NoBom = [System.Text.UTF8Encoding]::new($false)
35
+
36
+ # --- Win32 interop -----------------------------------------------------------
37
+
38
+ if (-not ('RecycleBinNative' -as [type])) {
39
+ Add-Type -TypeDefinition @'
40
+ using System;
41
+ using System.Runtime.InteropServices;
42
+
43
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 8)]
44
+ public struct SHFILEOPSTRUCTW
45
+ {
46
+ public IntPtr hwnd;
47
+ public uint wFunc;
48
+ [MarshalAs(UnmanagedType.LPWStr)] public string pFrom;
49
+ [MarshalAs(UnmanagedType.LPWStr)] public string pTo;
50
+ public ushort fFlags;
51
+ public bool fAnyOperationsAborted;
52
+ public IntPtr hNameMappings;
53
+ [MarshalAs(UnmanagedType.LPWStr)] public string lpszProgressTitle;
54
+ }
55
+
56
+ [StructLayout(LayoutKind.Sequential)]
57
+ public struct SHQUERYRBINFO
58
+ {
59
+ public int cbSize;
60
+ public long i64Size;
61
+ public long i64NumItems;
62
+ }
63
+
64
+ public static class RecycleBinNative
65
+ {
66
+ public const uint FO_DELETE = 0x0003;
67
+ public const ushort FOF_SILENT = 0x0004;
68
+ public const ushort FOF_NOCONFIRMATION = 0x0010;
69
+ public const ushort FOF_ALLOWUNDO = 0x0040;
70
+ public const ushort FOF_NOERRORUI = 0x0400;
71
+
72
+ [DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
73
+ public static extern int SHFileOperationW(ref SHFILEOPSTRUCTW lpFileOp);
74
+
75
+ [DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
76
+ public static extern int SHQueryRecycleBinW(string pszRootPath, ref SHQUERYRBINFO pSHQueryRBInfo);
77
+ }
78
+ '@ -ErrorAction Stop
79
+ }
80
+
81
+ # --- Helpers -----------------------------------------------------------------
82
+
83
+ function Write-Json {
84
+ param([Parameter(Mandatory)] $Object)
85
+ $json = $Object | ConvertTo-Json -Depth 6 -Compress
86
+ [Console]::Out.Write($json)
87
+ }
88
+
89
+ function Resolve-NormalizedPath {
90
+ <# Resolve to an absolute path without requiring existence. #>
91
+ param([Parameter(Mandatory)][string] $Path)
92
+ $expanded = [Environment]::ExpandEnvironmentVariables($Path)
93
+ $full = [System.IO.Path]::GetFullPath($expanded)
94
+ # GetFullPath keeps a trailing separator on directories; SHFileOperationW
95
+ # requires a double-null terminated list and tolerates either form, but a
96
+ # single canonical form keeps the response comparable to the request.
97
+ return $full.TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
98
+ }
99
+
100
+ function Get-DriveRoot {
101
+ param([string] $Path)
102
+ if ([string]::IsNullOrWhiteSpace($Path)) { return $env:SystemDrive + '\' }
103
+ try {
104
+ $root = [System.IO.Path]::GetPathRoot((Resolve-NormalizedPath -Path $Path))
105
+ if ([string]::IsNullOrEmpty($root)) { return $env:SystemDrive + '\' }
106
+ return $root
107
+ } catch {
108
+ return $env:SystemDrive + '\'
109
+ }
110
+ }
111
+
112
+ # --- Operations --------------------------------------------------------------
113
+
114
+ function Get-ShFileOperationMessage {
115
+ <# Map the documented SHFileOperation error codes to something readable. #>
116
+ param([Parameter(Mandatory)][int] $Code)
117
+ # A bare `switch` statement with explicit `return`s: `return switch (...) { ... }`
118
+ # is a parse error on this pwsh build ("unexpected token '{'").
119
+ switch ($Code) {
120
+ 0x2 { return 'The path no longer exists.' }
121
+ 0x3 { return 'The directory does not exist.' }
122
+ 0x71 { return 'The source and destination are the same.' }
123
+ 0x72 { return 'The operation was cancelled.' }
124
+ 0x74 { return 'The path is too long.' }
125
+ 0x75 { return 'The destination is a subfolder of the source.' }
126
+ 0x76 { return 'The operation is not supported.' }
127
+ 0x78 { return 'Access was denied.' }
128
+ 0x79 { return 'The operation was aborted.' }
129
+ 0x7A { return 'The source is in use by another process.' }
130
+ 0x7C { return 'The path is invalid.' }
131
+ default { return ("SHFileOperationW failed with code 0x{0:X}" -f $Code) }
132
+ }
133
+ }
134
+
135
+ function Test-RecyclableVolume {
136
+ <#
137
+ Can this path's volume actually absorb a recycle?
138
+
139
+ The Recycle Bin is a per-volume NTFS facility. On a UNC path, or on a
140
+ volume whose file system has no bin store (exFAT / FAT32), SHFileOperationW
141
+ with FOF_ALLOWUNDO + FOF_NOCONFIRMATION + FOF_SILENT does **not** fail: it
142
+ erases the item and returns 0. The post-check in Invoke-RecycleOne only
143
+ proves the path is gone, which is exactly what a permanent delete looks
144
+ like. Reproduced 2026-10-23 on a UNC share: file gone, no $I/$R record on
145
+ any local volume, SHQueryRecycleBin unchanged — and the tool had reported
146
+ "Moved to the Recycle Bin". Refuse up front instead; refusing is the whole
147
+ contract of this plugin.
148
+
149
+ Returns $null when the path can be recycled, otherwise the refusal text.
150
+ #>
151
+ param([Parameter(Mandatory)][string] $Path)
152
+
153
+ if ($Path -like '\\*') {
154
+ return 'A network location has no Recycle Bin, so recycling here would erase the item permanently. Copy it to a local NTFS path first, or delete it from Explorer where Windows can warn the user.'
155
+ }
156
+ $root = Get-DriveRoot -Path $Path
157
+ $format = ''
158
+ try { $format = ([System.IO.DriveInfo]::new($root)).DriveFormat } catch { $format = '' }
159
+ if ($format -eq 'NTFS' -or $format -eq 'ReFS') { return $null }
160
+ $shown = if ([string]::IsNullOrWhiteSpace($format)) { 'unknown' } else { $format }
161
+ return "Volume $root has no Recycle Bin (file system: $shown), so recycling here would erase the item permanently. Move it to an NTFS volume first, or delete it from Explorer where Windows can warn the user."
162
+ }
163
+
164
+ function Invoke-RecycleOne {
165
+ <#
166
+ Recycle a single path.
167
+
168
+ SHFileOperationW takes a whole double-null-terminated list and applies it
169
+ as one transaction: a single unusable entry makes the call fail wholesale,
170
+ with no way to tell which entry was at fault. Calling it per path keeps
171
+ one bad operand from turning a good batch into a total failure, and makes
172
+ each reported error belong to the path it names.
173
+
174
+ Returns $null on success, otherwise the error text for this path.
175
+ #>
176
+ param([Parameter(Mandatory)][string] $Path)
177
+
178
+ $refusal = Test-RecyclableVolume -Path $Path
179
+ if ($null -ne $refusal) { return $refusal }
180
+
181
+ $op = New-Object SHFILEOPSTRUCTW
182
+ $op.wFunc = [RecycleBinNative]::FO_DELETE
183
+ $op.pFrom = $Path + "`0`0"
184
+ $op.pTo = $null
185
+ $op.fFlags = [ushort](
186
+ [RecycleBinNative]::FOF_ALLOWUNDO -bor
187
+ [RecycleBinNative]::FOF_NOCONFIRMATION -bor
188
+ [RecycleBinNative]::FOF_SILENT -bor
189
+ [RecycleBinNative]::FOF_NOERRORUI
190
+ )
191
+
192
+ $rc = [RecycleBinNative]::SHFileOperationW([ref]$op)
193
+ if ($rc -ne 0) { return (Get-ShFileOperationMessage -Code $rc) }
194
+ if ($op.fAnyOperationsAborted) { return 'The shell aborted the move to the Recycle Bin.' }
195
+ # The call reports success for the whole request; confirm this path actually left.
196
+ if (Test-Path -LiteralPath $Path) { return 'The path still exists after the recycle operation.' }
197
+ return $null
198
+ }
199
+
200
+ function Invoke-Recycle {
201
+ param([Parameter(Mandatory)] $Request)
202
+
203
+ $recycled = [System.Collections.Generic.List[string]]::new()
204
+ $failures = [System.Collections.Generic.List[object]]::new()
205
+
206
+ $rawPaths = @($Request.paths)
207
+ foreach ($p in $rawPaths) {
208
+ if ($null -eq $p -or ([string]$p).Trim().Length -eq 0) { continue }
209
+
210
+ $resolved = $null
211
+ try {
212
+ $resolved = Resolve-NormalizedPath -Path ([string]$p)
213
+ } catch {
214
+ $failures.Add(@{ path = [string]$p; error = "unresolvable path: $($_.Exception.Message)" })
215
+ continue
216
+ }
217
+
218
+ $error = Invoke-RecycleOne -Path $resolved
219
+ if ($null -eq $error) {
220
+ $recycled.Add($resolved)
221
+ } else {
222
+ $failures.Add(@{ path = $resolved; error = $error })
223
+ }
224
+ }
225
+
226
+ Write-Json @{ recycled = $recycled; failures = $failures }
227
+ }
228
+
229
+ function Invoke-Usage {
230
+ param([Parameter(Mandatory)] $Request)
231
+
232
+ $drive = if ($Request.PSObject.Properties.Name -contains 'drive' -and $Request.drive) { [string]$Request.drive } else { $env:SystemDrive }
233
+ if ($drive -notmatch '\\$') { $drive = $drive + '\' }
234
+
235
+ $info = New-Object SHQUERYRBINFO
236
+ $info.cbSize = [System.Runtime.InteropServices.Marshal]::SizeOf([type][SHQUERYRBINFO])
237
+ $rc = [RecycleBinNative]::SHQueryRecycleBinW($drive, [ref]$info)
238
+ if ($rc -ne 0) {
239
+ Write-Json @{ items = 0; bytes = 0; error = "SHQueryRecycleBinW failed with code 0x$('{0:X}' -f $rc)" }
240
+ return
241
+ }
242
+ Write-Json @{ items = [int64]$info.i64NumItems; bytes = [int64]$info.i64Size }
243
+ }
244
+
245
+ # There is deliberately no `capacity` op. On an NTFS volume the bin is on the
246
+ # same volume as the item, so a recycle is a rename and frees nothing — a
247
+ # free-space gate would refuse harmless deletes and let harmful ones through.
248
+ # What this bridge checks instead is whether the volume has a bin at all
249
+ # (Test-RecyclableVolume), which is the difference between a recycle and an
250
+ # erase.
251
+
252
+ # --- Entry point -------------------------------------------------------------
253
+
254
+ try {
255
+ $reader = [System.IO.StreamReader]::new([Console]::OpenStandardInput(), $Utf8NoBom, $false)
256
+ $stdin = $reader.ReadToEnd()
257
+ $reader.Dispose()
258
+ if ([string]::IsNullOrWhiteSpace($stdin)) {
259
+ Write-Json @{ error = 'empty request' }
260
+ exit 0
261
+ }
262
+ $request = $stdin | ConvertFrom-Json
263
+
264
+ switch ([string]$request.op) {
265
+ 'recycle' { Invoke-Recycle -Request $request }
266
+ 'usage' { Invoke-Usage -Request $request }
267
+ default { Write-Json @{ error = "unknown op: $($request.op)" } }
268
+ }
269
+ } catch {
270
+ Write-Json @{ error = $_.Exception.Message }
271
+ }