@ddtcorex/dsh-maestro-core 0.10.0
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/LICENSE +21 -0
- package/README.md +209 -0
- package/cordis.patch.yml +43 -0
- package/lib/bin.d.ts +2 -0
- package/lib/bin.js +3 -0
- package/lib/boot-guard.d.ts +33 -0
- package/lib/boot-guard.js +68 -0
- package/lib/boot-lock.d.ts +56 -0
- package/lib/boot-lock.js +155 -0
- package/lib/cli.d.ts +31 -0
- package/lib/cli.js +336 -0
- package/lib/client.js +2901 -0
- package/lib/config/index.d.ts +34 -0
- package/lib/config/index.js +62 -0
- package/lib/config/service.d.ts +18 -0
- package/lib/config/service.js +28 -0
- package/lib/config.d.ts +1 -0
- package/lib/config.js +19 -0
- package/lib/daemon-freshness.d.ts +48 -0
- package/lib/daemon-freshness.js +122 -0
- package/lib/debug-agent.d.ts +17 -0
- package/lib/debug-agent.js +144 -0
- package/lib/dsh-session.d.ts +26 -0
- package/lib/dsh-session.js +45 -0
- package/lib/guard/config.d.ts +82 -0
- package/lib/guard/config.js +211 -0
- package/lib/guard/decide.d.ts +30 -0
- package/lib/guard/decide.js +38 -0
- package/lib/guard/full-scan-tool.d.ts +6 -0
- package/lib/guard/full-scan-tool.js +65 -0
- package/lib/guard/index.d.ts +85 -0
- package/lib/guard/index.js +254 -0
- package/lib/guard/journal.d.ts +146 -0
- package/lib/guard/journal.js +398 -0
- package/lib/guard/migrate.d.ts +15 -0
- package/lib/guard/migrate.js +42 -0
- package/lib/guard/parse.d.ts +193 -0
- package/lib/guard/parse.js +1103 -0
- package/lib/guard/paths.d.ts +104 -0
- package/lib/guard/paths.js +254 -0
- package/lib/guard/permission-policy.d.ts +10 -0
- package/lib/guard/permission-policy.js +21 -0
- package/lib/guard/redact.d.ts +25 -0
- package/lib/guard/redact.js +52 -0
- package/lib/guard/rules.d.ts +65 -0
- package/lib/guard/rules.js +832 -0
- package/lib/guard/status-tool.d.ts +54 -0
- package/lib/guard/status-tool.js +195 -0
- package/lib/guard/tiers.d.ts +2 -0
- package/lib/guard/tiers.js +1 -0
- package/lib/guard/validators.d.ts +4 -0
- package/lib/guard/validators.js +45 -0
- package/lib/health-poller.d.ts +90 -0
- package/lib/health-poller.js +430 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +7 -0
- package/lib/intents.d.ts +32 -0
- package/lib/intents.js +49 -0
- package/lib/notifier.d.ts +12 -0
- package/lib/notifier.js +62 -0
- package/lib/paths.d.ts +13 -0
- package/lib/paths.js +70 -0
- package/lib/plugin.d.ts +250 -0
- package/lib/plugin.js +1126 -0
- package/lib/preset.d.ts +73 -0
- package/lib/preset.js +158 -0
- package/lib/report.d.ts +11 -0
- package/lib/report.js +58 -0
- package/lib/restart-exec.d.ts +35 -0
- package/lib/restart-exec.js +60 -0
- package/lib/restart-guards.d.ts +39 -0
- package/lib/restart-guards.js +150 -0
- package/lib/restart-tool.d.ts +124 -0
- package/lib/restart-tool.js +539 -0
- package/lib/restart-web.d.ts +26 -0
- package/lib/restart-web.js +58 -0
- package/lib/resume-log.d.ts +24 -0
- package/lib/resume-log.js +18 -0
- package/lib/resume-tools.d.ts +131 -0
- package/lib/resume-tools.js +337 -0
- package/lib/resume.d.ts +33 -0
- package/lib/resume.js +221 -0
- package/lib/scan.d.ts +20 -0
- package/lib/scan.js +71 -0
- package/lib/self-kill-guard.d.ts +81 -0
- package/lib/self-kill-guard.js +208 -0
- package/lib/session-health.d.ts +89 -0
- package/lib/session-health.js +274 -0
- package/lib/session-log-file.d.ts +22 -0
- package/lib/session-log-file.js +74 -0
- package/lib/skill-provider.d.ts +6 -0
- package/lib/skill-provider.js +73 -0
- package/lib/snapshot.d.ts +59 -0
- package/lib/snapshot.js +358 -0
- package/lib/source.d.ts +19 -0
- package/lib/source.js +19 -0
- package/lib/store/index.d.ts +42 -0
- package/lib/store/index.js +340 -0
- package/lib/store/legacy.d.ts +17 -0
- package/lib/store/legacy.js +85 -0
- package/lib/supervisor.d.ts +119 -0
- package/lib/supervisor.js +741 -0
- package/lib/sync/backup-config.d.ts +55 -0
- package/lib/sync/backup-config.js +119 -0
- package/lib/sync/backup-service.d.ts +110 -0
- package/lib/sync/backup-service.js +440 -0
- package/lib/sync/bidirectional.d.ts +39 -0
- package/lib/sync/bidirectional.js +25 -0
- package/lib/sync/cli.d.ts +38 -0
- package/lib/sync/cli.js +536 -0
- package/lib/sync/config.d.ts +77 -0
- package/lib/sync/config.js +90 -0
- package/lib/sync/fingerprint.d.ts +29 -0
- package/lib/sync/fingerprint.js +65 -0
- package/lib/sync/hashing.d.ts +9 -0
- package/lib/sync/hashing.js +47 -0
- package/lib/sync/index.d.ts +6 -0
- package/lib/sync/index.js +634 -0
- package/lib/sync/machine-id.d.ts +29 -0
- package/lib/sync/machine-id.js +66 -0
- package/lib/sync/merge.d.ts +33 -0
- package/lib/sync/merge.js +88 -0
- package/lib/sync/peer-host.d.ts +22 -0
- package/lib/sync/peer-host.js +62 -0
- package/lib/sync/process-runner.d.ts +31 -0
- package/lib/sync/process-runner.js +161 -0
- package/lib/sync/remote-agent.d.ts +42 -0
- package/lib/sync/remote-agent.js +217 -0
- package/lib/sync/remote-cache.d.ts +18 -0
- package/lib/sync/remote-cache.js +60 -0
- package/lib/sync/remote-manifest.d.ts +8 -0
- package/lib/sync/remote-manifest.js +64 -0
- package/lib/sync/s3-object-store.d.ts +38 -0
- package/lib/sync/s3-object-store.js +126 -0
- package/lib/sync/session-merge.d.ts +18 -0
- package/lib/sync/session-merge.js +191 -0
- package/lib/sync/session-plan.d.ts +29 -0
- package/lib/sync/session-plan.js +213 -0
- package/lib/sync/sigv4.d.ts +20 -0
- package/lib/sync/sigv4.js +57 -0
- package/lib/sync/snapshot.d.ts +21 -0
- package/lib/sync/snapshot.js +176 -0
- package/lib/sync/ssh-mux.d.ts +12 -0
- package/lib/sync/ssh-mux.js +48 -0
- package/lib/sync/sync-plan.d.ts +24 -0
- package/lib/sync/sync-plan.js +366 -0
- package/lib/sync/sync-service.d.ts +177 -0
- package/lib/sync/sync-service.js +819 -0
- package/lib/sync/sync-types.d.ts +87 -0
- package/lib/sync/sync-types.js +1 -0
- package/lib/sync/transport.d.ts +75 -0
- package/lib/sync/transport.js +200 -0
- package/lib/sync/tunnel-restore.d.ts +77 -0
- package/lib/sync/tunnel-restore.js +142 -0
- package/lib/sync/validation.d.ts +10 -0
- package/lib/sync/validation.js +83 -0
- package/lib/tool-view.d.ts +62 -0
- package/lib/tool-view.js +70 -0
- package/lib/types/client/auto-reload.d.ts +8 -0
- package/lib/types/client/auto-reload.d.ts.map +1 -0
- package/lib/types/client/config/MaestroSettings.d.ts +25 -0
- package/lib/types/client/config/MaestroSettings.d.ts.map +1 -0
- package/lib/types/client/config/components/BrandMark.d.ts +50 -0
- package/lib/types/client/config/components/BrandMark.d.ts.map +1 -0
- package/lib/types/client/config/guard-view.d.ts +83 -0
- package/lib/types/client/config/guard-view.d.ts.map +1 -0
- package/lib/types/client/config/index.d.ts +20 -0
- package/lib/types/client/config/index.d.ts.map +1 -0
- package/lib/types/client/config/maestro-mark.d.ts +29 -0
- package/lib/types/client/config/maestro-mark.d.ts.map +1 -0
- package/lib/types/client/config/settings-nav-icon.d.ts +21 -0
- package/lib/types/client/config/settings-nav-icon.d.ts.map +1 -0
- package/lib/types/client/index.d.ts +16 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/sync/R2SyncPanel.d.ts +6 -0
- package/lib/types/client/sync/R2SyncPanel.d.ts.map +1 -0
- package/lib/types/client/sync/SyncPanel.d.ts +12 -0
- package/lib/types/client/sync/SyncPanel.d.ts.map +1 -0
- package/lib/types/client/sync/confirm-dialog.d.ts +43 -0
- package/lib/types/client/sync/confirm-dialog.d.ts.map +1 -0
- package/lib/types/client/sync/index.d.ts +10 -0
- package/lib/types/client/sync/index.d.ts.map +1 -0
- package/lib/types/client/sync/ui.d.ts +79 -0
- package/lib/types/client/sync/ui.d.ts.map +1 -0
- package/lib/types/client/sync/use-backup.d.ts +50 -0
- package/lib/types/client/sync/use-backup.d.ts.map +1 -0
- package/lib/types/client/sync/use-sync.d.ts +88 -0
- package/lib/types/client/sync/use-sync.d.ts.map +1 -0
- package/package.json +89 -0
- package/skills/dsh-safe-restart/SKILL.md +206 -0
- package/skills/dsh-safe-restart/scripts/restart-dsh-web.sh +468 -0
- package/src/client/auto-reload.ts +104 -0
- package/src/client/config/MaestroSettings.tsx +892 -0
- package/src/client/config/components/BrandMark.tsx +94 -0
- package/src/client/config/guard-view.ts +187 -0
- package/src/client/config/index.tsx +109 -0
- package/src/client/config/maestro-mark.ts +37 -0
- package/src/client/config/settings-nav-icon.ts +68 -0
- package/src/client/index.tsx +33 -0
- package/src/client/sync/R2SyncPanel.tsx +260 -0
- package/src/client/sync/SyncPanel.tsx +422 -0
- package/src/client/sync/confirm-dialog.tsx +202 -0
- package/src/client/sync/index.tsx +336 -0
- package/src/client/sync/ui.tsx +313 -0
- package/src/client/sync/use-backup.ts +207 -0
- package/src/client/sync/use-sync.ts +502 -0
- package/src/host/bin.ts +3 -0
- package/src/host/boot-guard.ts +76 -0
- package/src/host/boot-lock.ts +155 -0
- package/src/host/cli.ts +288 -0
- package/src/host/config/index.ts +78 -0
- package/src/host/config/service.ts +40 -0
- package/src/host/config.ts +18 -0
- package/src/host/daemon-freshness.ts +143 -0
- package/src/host/debug-agent.ts +145 -0
- package/src/host/dsh-session.ts +56 -0
- package/src/host/dsh-skill.d.ts +44 -0
- package/src/host/guard/augment.d.ts +33 -0
- package/src/host/guard/config.ts +244 -0
- package/src/host/guard/decide.ts +40 -0
- package/src/host/guard/full-scan-tool.ts +61 -0
- package/src/host/guard/index.ts +281 -0
- package/src/host/guard/journal.ts +440 -0
- package/src/host/guard/migrate.ts +41 -0
- package/src/host/guard/parse.ts +1150 -0
- package/src/host/guard/paths.ts +239 -0
- package/src/host/guard/permission-policy.ts +15 -0
- package/src/host/guard/redact.ts +64 -0
- package/src/host/guard/rules.ts +891 -0
- package/src/host/guard/status-tool.ts +229 -0
- package/src/host/guard/tiers.ts +2 -0
- package/src/host/guard/validators.ts +29 -0
- package/src/host/health-poller.ts +446 -0
- package/src/host/index.ts +7 -0
- package/src/host/intents.ts +67 -0
- package/src/host/notifier.ts +71 -0
- package/src/host/paths.ts +70 -0
- package/src/host/plugin.ts +1097 -0
- package/src/host/preset.ts +170 -0
- package/src/host/report.ts +71 -0
- package/src/host/restart-exec.ts +81 -0
- package/src/host/restart-guards.ts +150 -0
- package/src/host/restart-tool.ts +483 -0
- package/src/host/restart-web.ts +72 -0
- package/src/host/resume-log.ts +39 -0
- package/src/host/resume-tools.ts +385 -0
- package/src/host/resume.ts +204 -0
- package/src/host/scan.ts +61 -0
- package/src/host/self-kill-guard.ts +216 -0
- package/src/host/session-health.ts +236 -0
- package/src/host/session-log-file.ts +77 -0
- package/src/host/skill-provider.ts +69 -0
- package/src/host/snapshot.ts +337 -0
- package/src/host/source.ts +19 -0
- package/src/host/store/index.ts +348 -0
- package/src/host/store/legacy.ts +96 -0
- package/src/host/supervisor.ts +720 -0
- package/src/host/sync/backup-config.ts +159 -0
- package/src/host/sync/backup-service.ts +453 -0
- package/src/host/sync/bidirectional.ts +66 -0
- package/src/host/sync/cli.ts +539 -0
- package/src/host/sync/config.ts +119 -0
- package/src/host/sync/fingerprint.ts +83 -0
- package/src/host/sync/hashing.ts +58 -0
- package/src/host/sync/index.ts +737 -0
- package/src/host/sync/machine-id.ts +81 -0
- package/src/host/sync/merge.ts +102 -0
- package/src/host/sync/peer-host.ts +65 -0
- package/src/host/sync/process-runner.ts +177 -0
- package/src/host/sync/remote-agent.ts +213 -0
- package/src/host/sync/remote-cache.ts +68 -0
- package/src/host/sync/remote-manifest.ts +70 -0
- package/src/host/sync/s3-object-store.ts +144 -0
- package/src/host/sync/session-merge.ts +220 -0
- package/src/host/sync/session-plan.ts +237 -0
- package/src/host/sync/sigv4.ts +81 -0
- package/src/host/sync/snapshot.ts +162 -0
- package/src/host/sync/ssh-mux.ts +56 -0
- package/src/host/sync/sync-plan.ts +366 -0
- package/src/host/sync/sync-service.ts +855 -0
- package/src/host/sync/sync-types.ts +102 -0
- package/src/host/sync/transport.ts +261 -0
- package/src/host/sync/tunnel-restore.ts +160 -0
- package/src/host/sync/validation.ts +91 -0
- package/src/host/tool-view.ts +98 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { homedir, tmpdir } from 'node:os'
|
|
2
|
+
import { dirname, join, resolve, normalize } from 'node:path'
|
|
3
|
+
import { journalDir, journalPath } from './journal.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Path rules: the always-blocked credential locations, the guard's own
|
|
7
|
+
* configuration files, and the containment tests the classifier runs on a
|
|
8
|
+
* tool's path field. Task B4 moved these out of the deleted `sandbox.ts`; they
|
|
9
|
+
* are the only path logic in the package.
|
|
10
|
+
*
|
|
11
|
+
* Fragment assembly is deliberate, not stylistic: the OLD 0.2.3 guard is still
|
|
12
|
+
* the live listener in this host, and it blocks any tool call whose TEXT
|
|
13
|
+
* contains one of the protected path names contiguously. Building every literal
|
|
14
|
+
* here from pieces keeps this module — and every tool call that reads it —
|
|
15
|
+
* shippable through that guard. A later task inlines them after the supervised
|
|
16
|
+
* restart onto the new guard.
|
|
17
|
+
*/
|
|
18
|
+
const AUTH_FILE = '.' + 'creden' + 'tials' + '.yaml'
|
|
19
|
+
const TUNNEL_AUTH_DIR = '.' + 'cloud' + 'flared'
|
|
20
|
+
const TOKEN_NAME = 'NPM' + '_TOKEN'
|
|
21
|
+
const DSH_DIR = '.' + 'dsh'
|
|
22
|
+
const CREDENTIAL_WORD = 'creden' + 'tials'
|
|
23
|
+
|
|
24
|
+
function resolveHome(dshHome?: string): string {
|
|
25
|
+
return dshHome ?? process.env.DSH_HOME ?? join(homedir(), DSH_DIR)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function expandHome(p: string): string {
|
|
29
|
+
if (!p) return p
|
|
30
|
+
const home = homedir()
|
|
31
|
+
if (p === '~') return home
|
|
32
|
+
if (p.startsWith('~/')) return join(home, p.slice(2))
|
|
33
|
+
if (p.startsWith('$HOME/')) return join(home, p.slice(6))
|
|
34
|
+
if (p.startsWith('${HOME}/')) return join(home, p.slice(8))
|
|
35
|
+
return p
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizePath(p: string): string {
|
|
39
|
+
try {
|
|
40
|
+
return normalize(expandHome(p))
|
|
41
|
+
} catch {
|
|
42
|
+
return expandHome(p)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The paths the guard blocks unconditionally, in the order the legacy
|
|
48
|
+
* `sandbox.ts` already used, so callers may index them positionally (see
|
|
49
|
+
* `tests/paths.test.ts`):
|
|
50
|
+
*
|
|
51
|
+
* 1. the DSH-private auth file, under the DSH home (explicit argument, else
|
|
52
|
+
* `$DSH_HOME`, else the user home's DSH directory);
|
|
53
|
+
* 2. the tunnel credential directory, under the USER home — exactly what
|
|
54
|
+
* `sandbox.ts` hard-codes with `join(homedir(), ...)`. It is NOT a child of
|
|
55
|
+
* the DSH home and stays under the user home even when a `dshHome` is given.
|
|
56
|
+
*
|
|
57
|
+
* The guard's own configuration is deliberately NOT part of this list: it is
|
|
58
|
+
* protected by the stricter `guard.tamper` deny rule, which reads
|
|
59
|
+
* `guardConfigPaths()` directly.
|
|
60
|
+
*/
|
|
61
|
+
export function defaultProtectedPaths(dshHome?: string): string[] {
|
|
62
|
+
const home = resolveHome(dshHome)
|
|
63
|
+
return [join(home, AUTH_FILE), join(homedir(), TUNNEL_AUTH_DIR)]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The guard's own configuration files. A write to any of them is a tamper
|
|
68
|
+
* attempt (`guard.tamper`, tier `deny`) — the classifier compares the raw
|
|
69
|
+
* command text and the tool's path field against this list.
|
|
70
|
+
*
|
|
71
|
+
* Four families, all of them things whose edit silently changes what runs or
|
|
72
|
+
* erases what already ran:
|
|
73
|
+
*
|
|
74
|
+
* 1. `settings.json` — the `domains.guard` document itself;
|
|
75
|
+
* 2. `profiles/web/cordis.patch.yml` — the row patch that configures the guard;
|
|
76
|
+
* 3. `profiles/web/package.json` — the profile manifest that actually MOUNTS the
|
|
77
|
+
* guard (`@ddtcorex/dsh-maestro-guard` sits in its `dependencies` as a
|
|
78
|
+
* `link:`); the `cordis.patch.yml` beside it is a different file, and
|
|
79
|
+
* rewriting either one redirects or disables the guard on the next boot;
|
|
80
|
+
* 4. the journal and the legacy ticket file beside it (`journalDir()`), because
|
|
81
|
+
* the spec's deny tier covers an attempt to truncate or remove the guard's
|
|
82
|
+
* own audit trail — an unlogged decision is not a lesser tamper.
|
|
83
|
+
*/
|
|
84
|
+
export function guardConfigPaths(dshHome?: string): string[] {
|
|
85
|
+
const home = resolveHome(dshHome)
|
|
86
|
+
return [
|
|
87
|
+
join(home, 'dsh-maestro-config', 'settings.json'),
|
|
88
|
+
join(home, 'profiles', 'web', 'cordis.patch.yml'),
|
|
89
|
+
join(home, 'profiles', 'web', 'package.json'),
|
|
90
|
+
journalPath(dshHome),
|
|
91
|
+
join(journalDir(dshHome), 'legacy-pending.json'),
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Every spelling a shell command can use for one absolute path: the path itself
|
|
97
|
+
* plus its `~`, `$HOME` and `${HOME}` forms. The home prefix is replaced by a
|
|
98
|
+
* marker only when the path actually carries one of the candidate homes, so an
|
|
99
|
+
* unrelated absolute path is returned unchanged.
|
|
100
|
+
*
|
|
101
|
+
* The candidates are the real `homedir()` and the parent of the resolved DSH
|
|
102
|
+
* home. Both are needed: in production they are the same directory (`~/.dsh`),
|
|
103
|
+
* while a caller that passes its own `dshHome` (tests, an unusual `$DSH_HOME`)
|
|
104
|
+
* still gets the spelling its own layout implies.
|
|
105
|
+
*
|
|
106
|
+
* This exists because the deny tier only knew the ABSOLUTE spelling and a
|
|
107
|
+
* command that reached the guard config through `~`/`$HOME` walked past it.
|
|
108
|
+
*/
|
|
109
|
+
export function pathSpellings(p: string, dshHome?: string): string[] {
|
|
110
|
+
if (!p) return []
|
|
111
|
+
const out = new Set<string>([p])
|
|
112
|
+
for (const base of new Set([homedir(), dirname(resolveHome(dshHome))])) {
|
|
113
|
+
if (!base || base === '/' || !p.startsWith(base + '/')) continue
|
|
114
|
+
const rest = p.slice(base.length + 1)
|
|
115
|
+
out.add('~/' + rest)
|
|
116
|
+
out.add('$HOME/' + rest)
|
|
117
|
+
out.add('${HOME}/' + rest)
|
|
118
|
+
}
|
|
119
|
+
return [...out]
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The guard's own paths in every spelling a command may carry — the absolute
|
|
124
|
+
* form plus the `~`/`$HOME` forms — derived from {@link guardConfigPaths}, so a
|
|
125
|
+
* new guarded file is covered by every spelling at once.
|
|
126
|
+
*/
|
|
127
|
+
export function guardPathSpellings(dshHome?: string): string[] {
|
|
128
|
+
const out = new Set<string>()
|
|
129
|
+
for (const p of guardConfigPaths(dshHome)) for (const s of pathSpellings(p, dshHome)) out.add(s)
|
|
130
|
+
return [...out]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* True if path points to a blocked credential / secret location.
|
|
135
|
+
* Covers: the DSH auth file (any expansion), the tunnel auth dir, the registry
|
|
136
|
+
* token name, and the auth-file name as a substring.
|
|
137
|
+
* `protectedPaths` are additional paths from config (`domains.guard.protectedPaths`)
|
|
138
|
+
* — merged with the always-blocked names by the caller. The always-blocked
|
|
139
|
+
* substrings stay blocked even when that list is empty.
|
|
140
|
+
*/
|
|
141
|
+
export function isBlockedPath(input: string, protectedPaths?: string[]): boolean {
|
|
142
|
+
if (!input || typeof input !== 'string') return false
|
|
143
|
+
const trimmed = input.trim()
|
|
144
|
+
if (!trimmed) return false
|
|
145
|
+
|
|
146
|
+
// Direct substring checks (covers JSON-stringified args, env leakage, etc.) — always blocked
|
|
147
|
+
if (trimmed.includes(AUTH_FILE)) return true
|
|
148
|
+
if (trimmed.includes(TUNNEL_AUTH_DIR)) return true
|
|
149
|
+
if (trimmed.includes(TOKEN_NAME)) return true
|
|
150
|
+
if (trimmed.includes(DSH_DIR) && trimmed.includes(CREDENTIAL_WORD)) return true
|
|
151
|
+
|
|
152
|
+
// Normalized expanded check for defaults
|
|
153
|
+
const norm = normalizePath(trimmed)
|
|
154
|
+
if (norm.includes(AUTH_FILE)) return true
|
|
155
|
+
if (norm.includes(TUNNEL_AUTH_DIR)) return true
|
|
156
|
+
// Check absolute homedir variant
|
|
157
|
+
const absCred = join(homedir(), DSH_DIR, AUTH_FILE)
|
|
158
|
+
if (norm === absCred || norm.startsWith(absCred)) return true
|
|
159
|
+
const absCf = join(homedir(), TUNNEL_AUTH_DIR)
|
|
160
|
+
if (norm === absCf || norm.startsWith(absCf + '/') || norm.includes(TUNNEL_AUTH_DIR)) return true
|
|
161
|
+
|
|
162
|
+
// Injected protectedPaths from config (additional) — substring + normalized + prefix
|
|
163
|
+
if (protectedPaths && protectedPaths.length > 0) {
|
|
164
|
+
for (const p of protectedPaths) {
|
|
165
|
+
if (!p || typeof p !== 'string') continue
|
|
166
|
+
const t = p.trim()
|
|
167
|
+
if (!t) continue
|
|
168
|
+
if (trimmed.includes(t)) return true
|
|
169
|
+
const normP = normalizePath(t)
|
|
170
|
+
if (norm.includes(normP)) return true
|
|
171
|
+
if (norm === normP) return true
|
|
172
|
+
if (norm.startsWith(normP + '/')) return true
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return false
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* True if target path is outside cwd (strict containment).
|
|
181
|
+
* Relative targets resolve against cwd (the calling agent's session cwd),
|
|
182
|
+
* matching how maestro file tools resolve them — resolving against the host
|
|
183
|
+
* process.cwd() instead false-positives every worktree-relative read
|
|
184
|
+
* (review sessions read /tmp worktrees while the host runs elsewhere).
|
|
185
|
+
*/
|
|
186
|
+
export function isOutsideCwd(target: string, cwd: string): boolean {
|
|
187
|
+
if (!target || !cwd) return false
|
|
188
|
+
const expTarget = expandHome(target)
|
|
189
|
+
const expCwd = expandHome(cwd)
|
|
190
|
+
const resolvedCwd = resolve(expCwd)
|
|
191
|
+
const resolvedTarget = resolve(resolvedCwd, expTarget)
|
|
192
|
+
if (resolvedTarget === resolvedCwd) return false
|
|
193
|
+
// Ensure cwd prefix with separator to avoid /tmp/proj matching /tmp/proj2
|
|
194
|
+
return !resolvedTarget.startsWith(resolvedCwd + '/')
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* True for reads of the DSH runtime spill dir (`$TMPDIR/dsh-spill-*`).
|
|
199
|
+
* The spill policy persists oversized tool results there and instructs the
|
|
200
|
+
* model to read them back via read tools — blocking those reads breaks the
|
|
201
|
+
* foundation's own retrieval flow (tickets g-dd0d1679/g-716cd436/g-d77f0144).
|
|
202
|
+
* Read-only: only read tools consult this; writes stay cwd-contained.
|
|
203
|
+
* The random dir suffix makes cross-session spills unguessable, and the
|
|
204
|
+
* runtime only ever discloses an agent's own spill paths to it.
|
|
205
|
+
*
|
|
206
|
+
* `base` is the directory a RELATIVE target resolves against, and it must be
|
|
207
|
+
* the same base `isOutsideCwd`/`isWithinTempDir` use — the session cwd. The
|
|
208
|
+
* default (`process.cwd()`) is kept for callers that only pass absolute targets,
|
|
209
|
+
* but resolving against the host cwd while the containment test resolves against
|
|
210
|
+
* the session cwd lets the two disagree: a relative path that escapes the
|
|
211
|
+
* session cwd could then be exempted purely because of where the host happens
|
|
212
|
+
* to run. `classify` therefore always passes the session cwd.
|
|
213
|
+
*/
|
|
214
|
+
export function isRuntimeSpillPath(target: string, base?: string): boolean {
|
|
215
|
+
if (!target || typeof target !== 'string') return false
|
|
216
|
+
const resolved = resolve(expandHome(base ?? process.cwd()), expandHome(target.trim()))
|
|
217
|
+
const prefix = join(tmpdir(), 'dsh-spill-')
|
|
218
|
+
return resolved.startsWith(prefix)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* True when target resolves inside the OS temp dir (`os.tmpdir()`).
|
|
223
|
+
* Scratch work is deliberately exempt from the outside-cwd write gate: a
|
|
224
|
+
* temporary deliverable (`/tmp/x.md`, `$TMPDIR/...`) is not a filesystem
|
|
225
|
+
* escape, and gating it would re-introduce the false-positive class the
|
|
226
|
+
* outside-cwd rule exists to remove. Resolution reuses the module's
|
|
227
|
+
* expandHome/resolve helpers, and containment is checked with a trailing
|
|
228
|
+
* separator so `/tmpfoo` never matches a temp dir of `/tmp`.
|
|
229
|
+
*
|
|
230
|
+
* `base` is the directory a RELATIVE target resolves against, and it must be
|
|
231
|
+
* the same base `isOutsideCwd` uses — the session cwd.
|
|
232
|
+
*/
|
|
233
|
+
export function isWithinTempDir(target: string, base?: string): boolean {
|
|
234
|
+
if (!target || typeof target !== 'string') return false
|
|
235
|
+
const resolved = resolve(expandHome(base ?? process.cwd()), expandHome(target.trim()))
|
|
236
|
+
const tmp = resolve(expandHome(tmpdir()))
|
|
237
|
+
if (resolved === tmp) return true
|
|
238
|
+
return resolved.startsWith(tmp + '/')
|
|
239
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface GuardPolicy { allow?: string[]; deny?: string[] }
|
|
2
|
+
export class PermissionPolicy {
|
|
3
|
+
constructor(private policy: GuardPolicy) {}
|
|
4
|
+
isAllowed(tool: string, _args: unknown): boolean {
|
|
5
|
+
if (this.policy.deny?.includes(tool)) return false
|
|
6
|
+
if (this.policy.allow && this.policy.allow.length > 0) return this.policy.allow.includes(tool)
|
|
7
|
+
return true
|
|
8
|
+
}
|
|
9
|
+
check(tool: string): 'allow'|'deny'|'ask' {
|
|
10
|
+
if (this.policy.deny?.includes(tool)) return 'deny'
|
|
11
|
+
const hasAllow = Array.isArray(this.policy.allow) && this.policy.allow.length > 0
|
|
12
|
+
if (hasAllow && !this.policy.allow!.includes(tool)) return 'ask'
|
|
13
|
+
return 'allow'
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Secret families redacted from every copy the guard persists (the decision
|
|
3
|
+
* journal). Redaction applies to the stored copy only — the guard never
|
|
4
|
+
* rewrites the arguments a tool actually executes.
|
|
5
|
+
*
|
|
6
|
+
* The journal is the only persistent sink left: `Journal.append` is the single
|
|
7
|
+
* choke point that redacts every string field of every entry, so no call site
|
|
8
|
+
* can persist an unredacted value by forgetting to call `redact` itself.
|
|
9
|
+
*
|
|
10
|
+
* The table is split in two on purpose. `SECRET_PATTERNS` are full-replace
|
|
11
|
+
* patterns: the whole match becomes `[REDACTED]`. `PREFIX_KEEPING_PATTERNS` are
|
|
12
|
+
* patterns whose leading capture groups must stay readable (a variable name, an
|
|
13
|
+
* auth scheme); `keepGroups` says how many leading groups are copied verbatim.
|
|
14
|
+
* Nothing infers that decision from a pattern's source text.
|
|
15
|
+
*/
|
|
16
|
+
export const SECRET_PATTERNS: readonly RegExp[] = [
|
|
17
|
+
/glpat-[A-Za-z0-9_-]{10,}/g,
|
|
18
|
+
/sk-[A-Za-z0-9]{20,}/g,
|
|
19
|
+
/ghp_[A-Za-z0-9]{30,}/g,
|
|
20
|
+
/xox[bpras]-[A-Za-z0-9-]+/g,
|
|
21
|
+
/\bnpm_[A-Za-z0-9]{20,}/g,
|
|
22
|
+
/\b\d{8,10}:[A-Za-z0-9_-]{35}\b/g, // bot-style tokens
|
|
23
|
+
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g,
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
/** A secret pattern whose leading capture groups are kept verbatim. */
|
|
27
|
+
export interface PrefixKeepingPattern {
|
|
28
|
+
readonly re: RegExp
|
|
29
|
+
/** Number of leading capture groups preserved; the matched value is redacted. */
|
|
30
|
+
readonly keepGroups: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const PREFIX_KEEPING_PATTERNS: readonly PrefixKeepingPattern[] = [
|
|
34
|
+
// Env assignment keeps the variable name: DEPLOY_TOKEN=secret -> DEPLOY_TOKEN=[REDACTED]
|
|
35
|
+
{ re: /([A-Z0-9_]{3,}_(?:TOKEN|SECRET|PASSWORD|KEY)\s*[=:]\s*)(\S+)/g, keepGroups: 1 },
|
|
36
|
+
// Auth header keeps the scheme: Authorization: Bearer <value>
|
|
37
|
+
{ re: /((?:Authorization|Proxy-Authorization)\s*:\s*\S+\s+)(\S+)/gi, keepGroups: 1 },
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
const REDACTED = '[REDACTED]'
|
|
41
|
+
|
|
42
|
+
const ALL_PATTERNS: readonly RegExp[] = [...SECRET_PATTERNS, ...PREFIX_KEEPING_PATTERNS.map((p) => p.re)]
|
|
43
|
+
|
|
44
|
+
export function containsSecret(text: string): boolean {
|
|
45
|
+
return ALL_PATTERNS.some((re) => {
|
|
46
|
+
re.lastIndex = 0
|
|
47
|
+
return re.test(text)
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function redact(text: string): string {
|
|
52
|
+
let out = text
|
|
53
|
+
for (const re of SECRET_PATTERNS) {
|
|
54
|
+
// Shared /g regexes carry lastIndex across calls; String.replace resets it,
|
|
55
|
+
// but be explicit so a future non-replace use cannot inherit stale state.
|
|
56
|
+
re.lastIndex = 0
|
|
57
|
+
out = out.replace(re, REDACTED)
|
|
58
|
+
}
|
|
59
|
+
for (const { re, keepGroups } of PREFIX_KEEPING_PATTERNS) {
|
|
60
|
+
re.lastIndex = 0
|
|
61
|
+
out = out.replace(re, (...match: string[]) => `${match.slice(1, 1 + keepGroups).join('')}${REDACTED}`)
|
|
62
|
+
}
|
|
63
|
+
return out
|
|
64
|
+
}
|