@causyne/agent 1.20.120 → 1.20.122
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/README.md +3 -3
- package/package.json +1 -1
- package/src/acp/agentCatalog.js +81 -26
- package/src/acp/agentCatalog.ts +73 -29
- package/src/acp/client.js +210 -53
- package/src/acp/client.ts +202 -52
- package/src/acp/peerProfiles.js +55 -22
- package/src/acp/peerProfiles.ts +51 -22
- package/src/acp/protocol.js +37 -8
- package/src/acp/protocol.ts +30 -8
- package/src/acp/server.js +123 -25
- package/src/acp/server.ts +124 -24
- package/src/agent/errorClassifier.js +117 -0
- package/src/agent/errorClassifier.ts +165 -0
- package/src/agent/promptIntent.js +200 -0
- package/src/agent/promptIntent.ts +203 -0
- package/src/agent/toolArgValidator.js +97 -0
- package/src/agent/toolArgValidator.ts +112 -0
- package/src/agentLoop.js +33 -344
- package/src/agentLoop.ts +51 -352
- package/src/approvalGrants.js +19 -7
- package/src/approvalGrants.ts +20 -7
- package/src/approvalPolicy.js +48 -15
- package/src/approvalPolicy.ts +46 -15
- package/src/autoUpdate.js +116 -10
- package/src/autoUpdate.ts +149 -10
- package/src/browser/causyneBrowser.js +3 -2
- package/src/browser/causyneBrowser.ts +3 -2
- package/src/browser/searchProviders.js +1 -1
- package/src/browser/searchProviders.ts +1 -1
- package/src/cliMode.js +16 -2
- package/src/commands.js +10 -9
- package/src/config.js +288 -272
- package/src/config.ts +342 -0
- package/src/configVault.js +271 -0
- package/src/configVault.ts +273 -0
- package/src/continuum/compactor.js +2 -1
- package/src/continuum/compactor.ts +2 -1
- package/src/continuum/sanitizer.js +10 -3
- package/src/continuum/sanitizer.ts +9 -3
- package/src/continuum/ui/continuumPickerView.js +3 -2
- package/src/continuum/ui/continuumPickerView.ts +3 -2
- package/src/core/contextCompactor.js +70 -11
- package/src/core/contextCompactor.ts +83 -11
- package/src/cortex/vectorDb.js +26 -15
- package/src/cortex/vectorDb.ts +26 -16
- package/src/creative/liveBridge.js +7 -5
- package/src/creative/liveBridge.ts +7 -5
- package/src/creative/studioScripts.js +16 -5
- package/src/creative/studioScripts.ts +14 -5
- package/src/decisionPolicy.js +16 -4
- package/src/decisionPolicy.ts +14 -4
- package/src/documentEngine/antiSlopAuditor.js +10 -5
- package/src/documentEngine/antiSlopAuditor.ts +11 -5
- package/src/documentEngine/documentForge.js +47 -15
- package/src/documentEngine/documentForge.ts +43 -14
- package/src/dynamicCatalogue.js +42 -73
- package/src/dynamicCatalogue.ts +74 -60
- package/src/engines/design.js +2 -1
- package/src/engines/design.ts +2 -1
- package/src/engines/film.js +4 -4
- package/src/engines/film.ts +2 -2
- package/src/engines/light.js +2 -1
- package/src/engines/light.ts +2 -1
- package/src/engines/registry.js +1 -1
- package/src/engines/registry.ts +1 -1
- package/src/engines/reverseHarness.js +4 -3
- package/src/engines/reverseHarness.ts +4 -3
- package/src/entrypoint.js +77 -4
- package/src/headlessCli.js +305 -0
- package/src/headlessCli.ts +381 -0
- package/src/historyPruner.js +3 -2
- package/src/historyPruner.ts +3 -2
- package/src/hooks.js +3 -0
- package/src/hooks.ts +3 -0
- package/src/implementationPlan.js +21 -11
- package/src/implementationPlan.ts +21 -11
- package/src/infra/atomicWriter.js +4 -1
- package/src/infra/atomicWriter.ts +4 -1
- package/src/launchIntent.js +1 -1
- package/src/launchIntent.ts +1 -1
- package/src/mcp/mcpClient.js +38 -11
- package/src/mcp/mcpClient.ts +44 -11
- package/src/mcp/mcpRegistry.js +2 -5
- package/src/mcp/mcpRegistry.ts +2 -5
- package/src/mcp/mcpSecurity.js +189 -0
- package/src/mcp/mcpSecurity.ts +190 -0
- package/src/memory/scopedFacts.js +1 -1
- package/src/memory/scopedFacts.ts +1 -1
- package/src/memory/symbolFolder.js +1 -1
- package/src/memory/symbolFolder.ts +1 -1
- package/src/modelRouter.js +1 -1
- package/src/modelRouter.ts +1 -1
- package/src/observer/evidence.js +10 -4
- package/src/observer/evidence.ts +10 -4
- package/src/osKeychain.js +263 -0
- package/src/osKeychain.ts +287 -0
- package/src/perception.js +3 -2
- package/src/perception.ts +3 -2
- package/src/planCache.js +2 -2
- package/src/planCache.ts +2 -2
- package/src/pluginsFactory/pluginsFactory.js +19 -11
- package/src/pluginsFactory/pluginsFactory.ts +18 -11
- package/src/promptMentions.js +2 -2
- package/src/promptMentions.ts +2 -2
- package/src/providers/acp.js +1 -1
- package/src/providers/acp.ts +1 -1
- package/src/providers/anthropic.js +328 -385
- package/src/providers/anthropic.ts +386 -0
- package/src/providers/antigravity.js +10 -4
- package/src/providers/antigravity.ts +10 -4
- package/src/providers/base.js +221 -319
- package/src/providers/base.ts +340 -0
- package/src/providers/byokHttp.js +397 -0
- package/src/providers/byokHttp.ts +497 -0
- package/src/providers/byokStream.js +748 -0
- package/src/providers/byokStream.ts +863 -0
- package/src/providers/dashscope-byok.js +98 -81
- package/src/providers/dashscope-byok.ts +114 -0
- package/src/providers/deepseek.js +74 -64
- package/src/providers/deepseek.ts +99 -0
- package/src/providers/factory.js +35 -28
- package/src/providers/factory.ts +40 -0
- package/src/providers/gemini.js +229 -253
- package/src/providers/gemini.ts +261 -0
- package/src/providers/loopGuard.js +9 -5
- package/src/providers/loopGuard.ts +10 -5
- package/src/providers/moonshot.js +71 -64
- package/src/providers/moonshot.ts +93 -0
- package/src/providers/muse.js +82 -152
- package/src/providers/muse.ts +122 -0
- package/src/providers/ollama.js +116 -93
- package/src/providers/ollama.ts +135 -0
- package/src/providers/openai-compatible.js +114 -65
- package/src/providers/openai-compatible.ts +139 -0
- package/src/providers/openai.js +22 -15
- package/src/providers/openai.ts +30 -0
- package/src/providers/openrouter.js +194 -98
- package/src/providers/openrouter.ts +202 -97
- package/src/providers/pi.js +6 -0
- package/src/providers/pi.ts +6 -0
- package/src/providers/qwen-byok.js +97 -71
- package/src/providers/qwen-byok.ts +113 -0
- package/src/providers/toolContracts.js +40 -14
- package/src/providers/toolContracts.ts +35 -14
- package/src/providers/wireNormalizer.js +78 -11
- package/src/providers/wireNormalizer.ts +68 -12
- package/src/providers/xai.js +74 -64
- package/src/providers/xai.ts +97 -0
- package/src/reliability/assetEvidence.js +4 -2
- package/src/reliability/assetEvidence.ts +4 -2
- package/src/sandbox.js +486 -530
- package/src/sandbox.ts +552 -0
- package/src/sandboxPolicy.js +1287 -0
- package/src/sandboxPolicy.ts +1381 -0
- package/src/sessionJournal.js +55 -3
- package/src/sessionJournal.ts +58 -3
- package/src/skills/skillResourceSafety.js +19 -8
- package/src/skills/skillResourceSafety.ts +16 -7
- package/src/skillsManager.js +3 -3
- package/src/skillsManager.ts +3 -3
- package/src/smartToolsUse.js +28 -4
- package/src/smartToolsUse.ts +29 -7
- package/src/stallGuard.js +45 -22
- package/src/stallGuard.ts +46 -22
- package/src/subagentJournal.js +6 -4
- package/src/subagentJournal.ts +6 -4
- package/src/textDiff.js +6 -1
- package/src/textDiff.ts +6 -1
- package/src/timeMachine.js +7 -1
- package/src/timeMachine.ts +7 -1
- package/src/tokenTracker.js +43 -28
- package/src/tokenTracker.ts +44 -30
- package/src/toolPacks.js +3 -1
- package/src/toolPacks.ts +3 -1
- package/src/tools/archive.js +245 -35
- package/src/tools/archive.ts +244 -35
- package/src/tools/computerUse.js +7 -3
- package/src/tools/computerUse.ts +7 -3
- package/src/tools/creativeApps.js +13 -8
- package/src/tools/creativeApps.ts +22 -9
- package/src/tools/deferredMcpSearch.js +26 -14
- package/src/tools/deferredMcpSearch.ts +26 -14
- package/src/tools/desktopOpen.js +69 -10
- package/src/tools/desktopOpen.ts +70 -11
- package/src/tools/documentForge.js +167 -82
- package/src/tools/documentForge.ts +169 -84
- package/src/tools/fsEdit.js +101 -30
- package/src/tools/fsEdit.ts +105 -32
- package/src/tools/fsFind.js +8 -5
- package/src/tools/fsFind.ts +7 -4
- package/src/tools/fsRead.js +119 -13
- package/src/tools/fsRead.ts +124 -13
- package/src/tools/fsWrite.js +17 -5
- package/src/tools/fsWrite.ts +17 -7
- package/src/tools/generateMediaViaApi.js +82 -19
- package/src/tools/generateMediaViaApi.ts +80 -19
- package/src/tools/memoryRecall.js +62 -11
- package/src/tools/memoryRecall.ts +70 -13
- package/src/tools/memoryWrite.js +27 -14
- package/src/tools/memoryWrite.ts +30 -14
- package/src/tools/multiEdit.js +100 -30
- package/src/tools/multiEdit.ts +104 -32
- package/src/tools/nativeEngine.js +33 -18
- package/src/tools/nativeEngine.ts +31 -18
- package/src/tools/openSourceToolProvisioner.js +49 -14
- package/src/tools/openSourceToolProvisioner.ts +54 -15
- package/src/tools/osSandbox.js +513 -0
- package/src/tools/osSandbox.ts +588 -0
- package/src/tools/payloadSanitizer.js +40 -8
- package/src/tools/payloadSanitizer.ts +39 -8
- package/src/tools/peerAgent.js +96 -34
- package/src/tools/peerAgent.ts +114 -37
- package/src/tools/persistentShell.js +8 -3
- package/src/tools/pluginsFactory.js +38 -6
- package/src/tools/pluginsFactory.ts +42 -6
- package/src/tools/readNativeSkill.js +10 -8
- package/src/tools/readNativeSkill.ts +10 -8
- package/src/tools/requestDecision.js +10 -3
- package/src/tools/requestDecision.ts +10 -3
- package/src/tools/resolveAutomator.js +91 -17
- package/src/tools/resolveAutomator.ts +102 -17
- package/src/tools/reverseEngineering.js +168 -41
- package/src/tools/reverseEngineering.ts +161 -41
- package/src/tools/subprocessEnv.js +197 -0
- package/src/tools/subprocessEnv.ts +212 -0
- package/src/tools/sysRun.js +108 -35
- package/src/tools/sysRun.ts +119 -31
- package/src/tools/toolDispatch.js +24 -1
- package/src/tools/toolDispatch.ts +21 -1
- package/src/tools/toolOutputStore.js +62 -23
- package/src/tools/toolOutputStore.ts +56 -23
- package/src/tools/toolScanner.js +13 -10
- package/src/tools/toolScanner.ts +13 -10
- package/src/tools/toolSelfRepair.js +94 -28
- package/src/tools/toolSelfRepair.ts +96 -28
- package/src/tools/uiForge.js +71 -6
- package/src/tools/uiForge.ts +74 -6
- package/src/tools/viewImage.js +62 -17
- package/src/tools/viewImage.ts +57 -12
- package/src/tools/visionInspector.js +15 -3
- package/src/tools/visionInspector.ts +15 -3
- package/src/tools/walkerSafety.js +2 -0
- package/src/tools/walkerSafety.ts +1 -0
- package/src/tools/webFetch.js +105 -10
- package/src/tools/webFetch.ts +105 -11
- package/src/tui/banner.js +3 -2
- package/src/tui/banner.ts +3 -2
- package/src/tui/consoleGuard.js +12 -4
- package/src/tui/consoleGuard.ts +9 -4
- package/src/tui/messageInteraction.js +2 -3
- package/src/tui/messageInteraction.ts +2 -3
- package/src/tui/modelPicker.js +1 -0
- package/src/tui/modelPicker.ts +1 -0
- package/src/tui/overlay.js +3 -1
- package/src/tui/overlay.ts +3 -1
- package/src/tui/sessionPicker.js +3 -2
- package/src/tui/sessionPicker.ts +3 -2
- package/src/tui/sessionPickerView.js +7 -1
- package/src/tui/sessionPickerView.ts +6 -1
- package/src/tui/toolOutputNormalizer.js +2 -3
- package/src/tui/toolOutputNormalizer.ts +4 -3
- package/src/tui/viewport.js +4 -4
- package/src/tui/viewport.ts +5 -5
- package/src/tui/workspaceIdentity.js +2 -1
- package/src/tui/workspaceIdentity.ts +2 -1
- package/src/uncertainty.js +8 -3
- package/src/uncertainty.ts +8 -3
- package/src/utils/paths.js +8 -5
- package/src/utils/paths.ts +9 -7
- package/src/vfeTracker.js +9 -3
- package/src/vfeTracker.ts +9 -3
- package/src/config.d.ts +0 -2
- package/src/providers/base.d.ts +0 -94
- package/src/providers/factory.d.ts +0 -1
- package/src/sandbox.d.ts +0 -1
|
@@ -0,0 +1,1287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Causyne Agent — Sandbox Workspace, Credential & Shell Command Policy
|
|
3
|
+
*
|
|
4
|
+
* Linear-time (O(n), zero ReDoS) security helpers supporting `src/sandbox.js`:
|
|
5
|
+
* 1. Canonical blocked-workspace-root checks (POSIX + Windows + symlink resolution)
|
|
6
|
+
* enforced uniformly by both `updateWorkspacePaths()` and `addWorkspacePath()`.
|
|
7
|
+
* 2. Expanded credential-path recognition (`.env*` excluding example/sample/template
|
|
8
|
+
* files, `.envrc`, `.git-credentials`, `.pypirc`, `.pgpass`, `.my.cnf`, private
|
|
9
|
+
* SSH key filenames, and cloud service-account credential JSONs).
|
|
10
|
+
* 3. Quote-aware shell parser that extracts nested command substitutions
|
|
11
|
+
* (`$(...)` and backticks, including inside double quotes), strips leading env
|
|
12
|
+
* assignments and wrapper commands (`env`, `command`, `nohup`, `nice`, `time`,
|
|
13
|
+
* `timeout`, `exec`, `stdbuf`, `builtin`, `caffeinate`), resolves absolute
|
|
14
|
+
* executable basenames (`/bin/rm` -> `rm`), and extracts redirection + `tee`
|
|
15
|
+
* write targets even when quoted or prefixed with `$HOME`.
|
|
16
|
+
*/
|
|
17
|
+
import os from 'node:os';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { isWithinPhysicalPath, physicalPath } from './agent/workspaceBoundary.js';
|
|
20
|
+
const MAX_SUBSTITUTION_DEPTH = 16;
|
|
21
|
+
const MAX_SUBSTITUTIONS = 64;
|
|
22
|
+
const SAFE_ENV_TEMPLATE_EXACT = new Set([
|
|
23
|
+
'.env.example',
|
|
24
|
+
'.env.sample',
|
|
25
|
+
'.env.template',
|
|
26
|
+
'.env.dist',
|
|
27
|
+
'.env.defaults',
|
|
28
|
+
'.env.schema'
|
|
29
|
+
]);
|
|
30
|
+
const SAFE_ENV_TEMPLATE_SUFFIXES = [
|
|
31
|
+
'.example',
|
|
32
|
+
'.sample',
|
|
33
|
+
'.template',
|
|
34
|
+
'.dist'
|
|
35
|
+
];
|
|
36
|
+
const CREDENTIAL_DIR_SEGMENTS = new Set([
|
|
37
|
+
'.ssh',
|
|
38
|
+
'.aws',
|
|
39
|
+
'.gnupg',
|
|
40
|
+
'.kube',
|
|
41
|
+
'.causyne'
|
|
42
|
+
]);
|
|
43
|
+
const CREDENTIAL_EXACT_BASENAMES = new Set([
|
|
44
|
+
'.netrc',
|
|
45
|
+
'.npmrc',
|
|
46
|
+
'.envrc',
|
|
47
|
+
'.git-credentials',
|
|
48
|
+
'.pypirc',
|
|
49
|
+
'.pgpass',
|
|
50
|
+
'.my.cnf',
|
|
51
|
+
'id_rsa',
|
|
52
|
+
'id_ed25519',
|
|
53
|
+
'id_ecdsa',
|
|
54
|
+
'id_dsa',
|
|
55
|
+
'credentials.json',
|
|
56
|
+
'application_default_credentials.json',
|
|
57
|
+
'.htpasswd',
|
|
58
|
+
'kubeconfig',
|
|
59
|
+
'terraform.tfvars',
|
|
60
|
+
'terraform.tfvars.json',
|
|
61
|
+
'.terraformrc'
|
|
62
|
+
]);
|
|
63
|
+
const SENSITIVE_WRITE_EXACT_BASENAMES = new Set([
|
|
64
|
+
'.bashrc',
|
|
65
|
+
'.bash_profile',
|
|
66
|
+
'.bash_login',
|
|
67
|
+
'.profile',
|
|
68
|
+
'.zshrc',
|
|
69
|
+
'.zprofile',
|
|
70
|
+
'.zshenv',
|
|
71
|
+
'.zlogin',
|
|
72
|
+
'.npmrc',
|
|
73
|
+
'.netrc',
|
|
74
|
+
'.git-credentials',
|
|
75
|
+
'.pypirc',
|
|
76
|
+
'.envrc'
|
|
77
|
+
]);
|
|
78
|
+
const SHELL_INTERPRETERS = new Set([
|
|
79
|
+
'sh',
|
|
80
|
+
'bash',
|
|
81
|
+
'zsh',
|
|
82
|
+
'dash',
|
|
83
|
+
'ksh',
|
|
84
|
+
'fish',
|
|
85
|
+
'ash',
|
|
86
|
+
'pwsh',
|
|
87
|
+
'powershell'
|
|
88
|
+
]);
|
|
89
|
+
const LEADING_SHELL_KEYWORDS = new Set([
|
|
90
|
+
'!',
|
|
91
|
+
'(',
|
|
92
|
+
'{',
|
|
93
|
+
'if',
|
|
94
|
+
'then',
|
|
95
|
+
'else',
|
|
96
|
+
'elif',
|
|
97
|
+
'do',
|
|
98
|
+
'while',
|
|
99
|
+
'until',
|
|
100
|
+
'case'
|
|
101
|
+
]);
|
|
102
|
+
const POSIX_TEMP_ALLOWLIST = [
|
|
103
|
+
'/tmp',
|
|
104
|
+
'/private/tmp',
|
|
105
|
+
'/private/var/tmp',
|
|
106
|
+
'/private/var/folders',
|
|
107
|
+
'/var/tmp',
|
|
108
|
+
'/var/folders'
|
|
109
|
+
];
|
|
110
|
+
const POSIX_BLOCKED_WORKSPACE_ROOTS = [
|
|
111
|
+
'/etc',
|
|
112
|
+
'/private/etc',
|
|
113
|
+
'/usr',
|
|
114
|
+
'/bin',
|
|
115
|
+
'/sbin',
|
|
116
|
+
'/System',
|
|
117
|
+
'/var',
|
|
118
|
+
'/private',
|
|
119
|
+
'/Library',
|
|
120
|
+
'/dev',
|
|
121
|
+
'/proc',
|
|
122
|
+
'/sys',
|
|
123
|
+
'/boot',
|
|
124
|
+
'/root'
|
|
125
|
+
];
|
|
126
|
+
function expandLeadingTilde(targetPath) {
|
|
127
|
+
if (targetPath === '~')
|
|
128
|
+
return os.homedir();
|
|
129
|
+
if (targetPath.startsWith('~/') || targetPath.startsWith('~\\')) {
|
|
130
|
+
return path.join(os.homedir(), targetPath.slice(2));
|
|
131
|
+
}
|
|
132
|
+
return targetPath;
|
|
133
|
+
}
|
|
134
|
+
function isWithinForPlatform(child, parent, platform) {
|
|
135
|
+
if (platform === 'win32') {
|
|
136
|
+
const rel = path.win32.relative(parent.toLowerCase(), child.toLowerCase());
|
|
137
|
+
return rel === '' || (rel !== '..' && !rel.startsWith('..\\') && !path.win32.isAbsolute(rel));
|
|
138
|
+
}
|
|
139
|
+
return isWithinPhysicalPath(child, parent);
|
|
140
|
+
}
|
|
141
|
+
function stripWindowsDevicePrefix(candidate) {
|
|
142
|
+
if (candidate.startsWith('\\\\?\\') || candidate.startsWith('\\\\.\\')) {
|
|
143
|
+
return candidate.slice(4);
|
|
144
|
+
}
|
|
145
|
+
if (candidate.startsWith('//?/') || candidate.startsWith('//./')) {
|
|
146
|
+
return candidate.slice(4);
|
|
147
|
+
}
|
|
148
|
+
return candidate;
|
|
149
|
+
}
|
|
150
|
+
function isBlockedResolvedRoot(rawCandidate, platform) {
|
|
151
|
+
const candidate = platform === 'win32' ? stripWindowsDevicePrefix(rawCandidate) : rawCandidate;
|
|
152
|
+
const pathApi = platform === 'win32' ? path.win32 : path.posix;
|
|
153
|
+
const parsedRoot = pathApi.parse(candidate).root;
|
|
154
|
+
if (parsedRoot && candidate.toLowerCase() === parsedRoot.toLowerCase()) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
if (platform === 'win32') {
|
|
158
|
+
const sysRoot = process.env.SystemRoot || process.env.SYSTEMROOT || 'C:\\Windows';
|
|
159
|
+
const winBlocked = [
|
|
160
|
+
sysRoot,
|
|
161
|
+
'C:\\Windows',
|
|
162
|
+
'C:\\Program Files',
|
|
163
|
+
'C:\\Program Files (x86)',
|
|
164
|
+
'C:\\ProgramData',
|
|
165
|
+
'C:\\System Volume Information'
|
|
166
|
+
];
|
|
167
|
+
return winBlocked.some(root => isWithinForPlatform(candidate, root, 'win32'));
|
|
168
|
+
}
|
|
169
|
+
if (POSIX_TEMP_ALLOWLIST.some(allowed => isWithinForPlatform(candidate, allowed, platform))) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
return POSIX_BLOCKED_WORKSPACE_ROOTS.some(root => isWithinForPlatform(candidate, root, platform));
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Returns true if `targetPath` is a system-critical root, filesystem root,
|
|
176
|
+
* invalid/dangling symlink, or resolves via symlinks into a blocked system root.
|
|
177
|
+
* Used uniformly by `updateWorkspacePaths()` and `addWorkspacePath()`.
|
|
178
|
+
*/
|
|
179
|
+
export function isBlockedWorkspaceRoot(targetPath, options) {
|
|
180
|
+
if (typeof targetPath !== 'string' || targetPath.trim().length === 0 || targetPath.includes('\0'))
|
|
181
|
+
return true;
|
|
182
|
+
const platform = options?.platform ?? process.platform;
|
|
183
|
+
const expanded = expandLeadingTilde(targetPath.trim());
|
|
184
|
+
const resolved = platform === 'win32' ? path.win32.resolve(expanded) : path.resolve(expanded);
|
|
185
|
+
if (isBlockedResolvedRoot(resolved, platform))
|
|
186
|
+
return true;
|
|
187
|
+
if (platform === process.platform) {
|
|
188
|
+
const realTarget = physicalPath(resolved);
|
|
189
|
+
if (!realTarget)
|
|
190
|
+
return true;
|
|
191
|
+
if (isBlockedResolvedRoot(realTarget, platform))
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Recognizes credential stores, private key files, and secret env files
|
|
198
|
+
* without backtracking regexes. Explicitly excludes `.env.example`, `.env.sample`,
|
|
199
|
+
* `.env.template`, and `.env.dist` templates to prevent developer false positives.
|
|
200
|
+
*/
|
|
201
|
+
export function isExtendedCredentialPath(targetPath) {
|
|
202
|
+
if (typeof targetPath !== 'string' || targetPath.length === 0 || targetPath.includes('\0'))
|
|
203
|
+
return false;
|
|
204
|
+
const normalized = targetPath.replace(/\\/g, '/').trim();
|
|
205
|
+
if (!normalized)
|
|
206
|
+
return false;
|
|
207
|
+
const trimmed = normalized.endsWith('/') && normalized.length > 1
|
|
208
|
+
? normalized.slice(0, -1)
|
|
209
|
+
: normalized;
|
|
210
|
+
const parts = trimmed.split('/').filter(Boolean);
|
|
211
|
+
if (parts.length === 0)
|
|
212
|
+
return false;
|
|
213
|
+
const lowerParts = parts.map(p => p.toLowerCase());
|
|
214
|
+
for (const part of lowerParts) {
|
|
215
|
+
if (CREDENTIAL_DIR_SEGMENTS.has(part))
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
for (let i = 0; i < lowerParts.length - 1; i++) {
|
|
219
|
+
if (lowerParts[i] === 'library' && lowerParts[i + 1] === 'keychains')
|
|
220
|
+
return true;
|
|
221
|
+
if (lowerParts[i] === '.docker' && lowerParts[i + 1] === 'config.json')
|
|
222
|
+
return true;
|
|
223
|
+
if (lowerParts[i] === 'gh' && lowerParts[i + 1] === 'hosts.yml')
|
|
224
|
+
return true;
|
|
225
|
+
if (lowerParts[i] === 'gcloud' && (lowerParts[i + 1] === 'credentials.db' || lowerParts[i + 1] === 'access_tokens.db'))
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
const baseLower = lowerParts[lowerParts.length - 1] ?? '';
|
|
229
|
+
if (CREDENTIAL_EXACT_BASENAMES.has(baseLower))
|
|
230
|
+
return true;
|
|
231
|
+
if (baseLower === '.env')
|
|
232
|
+
return true;
|
|
233
|
+
if (baseLower.startsWith('.env.')) {
|
|
234
|
+
if (SAFE_ENV_TEMPLATE_EXACT.has(baseLower))
|
|
235
|
+
return false;
|
|
236
|
+
for (const suffix of SAFE_ENV_TEMPLATE_SUFFIXES) {
|
|
237
|
+
if (baseLower.endsWith(suffix))
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
if ((baseLower.startsWith('service-account') || baseLower.startsWith('service_account')) &&
|
|
243
|
+
baseLower.endsWith('.json')) {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
const isSafeTemplate = SAFE_ENV_TEMPLATE_SUFFIXES.some(suffix => baseLower.endsWith(suffix));
|
|
247
|
+
if (!isSafeTemplate &&
|
|
248
|
+
!baseLower.endsWith('.pub') &&
|
|
249
|
+
(baseLower.endsWith('.pem') || baseLower.endsWith('.key') || baseLower.endsWith('.p12') || baseLower.endsWith('.pfx'))) {
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Returns true if a file target is a high-risk persistence or credential target
|
|
256
|
+
* for shell redirection (`>`, `>>`) or `tee`.
|
|
257
|
+
*/
|
|
258
|
+
export function isSensitiveWriteTarget(rawTarget) {
|
|
259
|
+
if (typeof rawTarget !== 'string' || rawTarget.length === 0 || rawTarget.includes('\0'))
|
|
260
|
+
return false;
|
|
261
|
+
let target = rawTarget.trim();
|
|
262
|
+
if ((target.startsWith('"') && target.endsWith('"') && target.length >= 2) ||
|
|
263
|
+
(target.startsWith("'") && target.endsWith("'") && target.length >= 2)) {
|
|
264
|
+
target = target.slice(1, -1);
|
|
265
|
+
}
|
|
266
|
+
target = target.replace(/\\/g, '/');
|
|
267
|
+
// Normalize common home-directory expansions for matching
|
|
268
|
+
if (target === '~' || target.startsWith('~/')) {
|
|
269
|
+
target = `/home/user${target.slice(1)}`;
|
|
270
|
+
}
|
|
271
|
+
else if (target.startsWith('$home/') || target.startsWith('${home}/')) {
|
|
272
|
+
const slash = target.indexOf('/');
|
|
273
|
+
target = `/home/user${target.slice(slash)}`;
|
|
274
|
+
}
|
|
275
|
+
else if (target.startsWith('$HOME/') || target.startsWith('${HOME}/')) {
|
|
276
|
+
const slash = target.indexOf('/');
|
|
277
|
+
target = `/home/user${target.slice(slash)}`;
|
|
278
|
+
}
|
|
279
|
+
else if (target.startsWith('$ZDOTDIR/') ||
|
|
280
|
+
target.startsWith('${ZDOTDIR}/') ||
|
|
281
|
+
/^%(?:userprofile|home|appdata)%\//i.test(target)) {
|
|
282
|
+
const slash = target.indexOf('/');
|
|
283
|
+
target = `/home/user${target.slice(slash)}`;
|
|
284
|
+
}
|
|
285
|
+
const lower = target.toLowerCase();
|
|
286
|
+
const parts = lower.split('/').filter(Boolean);
|
|
287
|
+
if (parts.length === 0)
|
|
288
|
+
return false;
|
|
289
|
+
const base = parts[parts.length - 1] ?? '';
|
|
290
|
+
if (SENSITIVE_WRITE_EXACT_BASENAMES.has(base))
|
|
291
|
+
return true;
|
|
292
|
+
// Protected system / persistence directories
|
|
293
|
+
if (lower.startsWith('/etc/') || lower === '/etc' || lower.includes('/etc/'))
|
|
294
|
+
return true;
|
|
295
|
+
for (let i = 0; i < parts.length; i++) {
|
|
296
|
+
const seg = parts[i];
|
|
297
|
+
if (seg === '.ssh' || seg === '.causyne' || seg === '.aws' || seg === '.kube' || seg === '.gnupg') {
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
if (seg === 'crontab' || seg === 'cron.d')
|
|
301
|
+
return true;
|
|
302
|
+
if (seg === '.git' && (parts[i + 1] === 'hooks' || parts[i + 1] === 'config'))
|
|
303
|
+
return true;
|
|
304
|
+
if (seg === '.docker' && parts[i + 1] === 'config.json')
|
|
305
|
+
return true;
|
|
306
|
+
if (seg === '.config' && parts[i + 1] === 'fish' && parts[i + 2] === 'config.fish')
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Single-pass quote-aware scanner that masks literal single/double-quoted text
|
|
313
|
+
* while extracting executable command substitutions (`$(...)` and backticks)
|
|
314
|
+
* both outside quotes and inside double quotes (single quotes stay literal).
|
|
315
|
+
*/
|
|
316
|
+
export function parseMaskAndExtractSubstitutions(input, depth = 0) {
|
|
317
|
+
let masked = '';
|
|
318
|
+
const substitutions = [];
|
|
319
|
+
const len = input.length;
|
|
320
|
+
let i = 0;
|
|
321
|
+
while (i < len) {
|
|
322
|
+
const ch = input[i];
|
|
323
|
+
// Backslash escape outside quotes
|
|
324
|
+
if (ch === '\\' && i + 1 < len) {
|
|
325
|
+
masked += ch + input[i + 1];
|
|
326
|
+
i += 2;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
// Single-quoted literal span (no variable or command substitution in POSIX)
|
|
330
|
+
if (ch === "'") {
|
|
331
|
+
i++;
|
|
332
|
+
while (i < len && input[i] !== "'") {
|
|
333
|
+
i++;
|
|
334
|
+
}
|
|
335
|
+
if (i < len)
|
|
336
|
+
i++; // consume closing quote
|
|
337
|
+
masked += "''";
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
// Double-quoted span: literal text is masked, but `$(...)` and `` `...` `` EXECUTE!
|
|
341
|
+
if (ch === '"') {
|
|
342
|
+
i++;
|
|
343
|
+
while (i < len && input[i] !== '"') {
|
|
344
|
+
const inner = input[i];
|
|
345
|
+
if (inner === '\\' && i + 1 < len) {
|
|
346
|
+
i += 2;
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (inner === '$' && input[i + 1] === '(') {
|
|
350
|
+
const captured = readDollarSubshell(input, i + 2);
|
|
351
|
+
if (captured.content.trim().length > 0) {
|
|
352
|
+
substitutions.push(captured.content);
|
|
353
|
+
}
|
|
354
|
+
i = captured.nextIndex;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
if (inner === '`') {
|
|
358
|
+
const captured = readBacktickSubshell(input, i + 1);
|
|
359
|
+
if (captured.content.trim().length > 0) {
|
|
360
|
+
substitutions.push(captured.content);
|
|
361
|
+
}
|
|
362
|
+
i = captured.nextIndex;
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
i++;
|
|
366
|
+
}
|
|
367
|
+
if (i < len)
|
|
368
|
+
i++; // consume closing quote
|
|
369
|
+
masked += '""';
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
// Unquoted `$(...)` command substitution
|
|
373
|
+
if (ch === '$' && input[i + 1] === '(') {
|
|
374
|
+
const captured = readDollarSubshell(input, i + 2);
|
|
375
|
+
if (captured.content.trim().length > 0) {
|
|
376
|
+
substitutions.push(captured.content);
|
|
377
|
+
}
|
|
378
|
+
masked += ' ';
|
|
379
|
+
i = captured.nextIndex;
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
// Unquoted backtick command substitution
|
|
383
|
+
if (ch === '`') {
|
|
384
|
+
const captured = readBacktickSubshell(input, i + 1);
|
|
385
|
+
if (captured.content.trim().length > 0) {
|
|
386
|
+
substitutions.push(captured.content);
|
|
387
|
+
}
|
|
388
|
+
masked += ' ';
|
|
389
|
+
i = captured.nextIndex;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
masked += ch;
|
|
393
|
+
i++;
|
|
394
|
+
}
|
|
395
|
+
if (substitutions.length > MAX_SUBSTITUTIONS) {
|
|
396
|
+
substitutions.length = MAX_SUBSTITUTIONS;
|
|
397
|
+
}
|
|
398
|
+
if (depth < MAX_SUBSTITUTION_DEPTH && substitutions.length > 0 && substitutions.length < MAX_SUBSTITUTIONS) {
|
|
399
|
+
const nested = [];
|
|
400
|
+
for (const sub of substitutions) {
|
|
401
|
+
if (substitutions.length + nested.length >= MAX_SUBSTITUTIONS)
|
|
402
|
+
break;
|
|
403
|
+
const parsed = parseMaskAndExtractSubstitutions(sub, depth + 1);
|
|
404
|
+
for (const deeper of parsed.substitutions) {
|
|
405
|
+
if (substitutions.length + nested.length >= MAX_SUBSTITUTIONS)
|
|
406
|
+
break;
|
|
407
|
+
nested.push(deeper);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
for (const deeper of nested) {
|
|
411
|
+
substitutions.push(deeper);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return { masked, substitutions };
|
|
415
|
+
}
|
|
416
|
+
function readDollarSubshell(input, startIndex) {
|
|
417
|
+
const len = input.length;
|
|
418
|
+
let i = startIndex;
|
|
419
|
+
let parenDepth = 1;
|
|
420
|
+
let inSingle = false;
|
|
421
|
+
let inDouble = false;
|
|
422
|
+
while (i < len && parenDepth > 0) {
|
|
423
|
+
const ch = input[i];
|
|
424
|
+
if (inSingle) {
|
|
425
|
+
if (ch === "'")
|
|
426
|
+
inSingle = false;
|
|
427
|
+
i++;
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
if (inDouble) {
|
|
431
|
+
if (ch === '\\' && i + 1 < len) {
|
|
432
|
+
i += 2;
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (ch === '"')
|
|
436
|
+
inDouble = false;
|
|
437
|
+
i++;
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
if (ch === '\\' && i + 1 < len) {
|
|
441
|
+
i += 2;
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (ch === "'") {
|
|
445
|
+
inSingle = true;
|
|
446
|
+
i++;
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
if (ch === '"') {
|
|
450
|
+
inDouble = true;
|
|
451
|
+
i++;
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
if (ch === '(') {
|
|
455
|
+
parenDepth++;
|
|
456
|
+
i++;
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (ch === ')') {
|
|
460
|
+
parenDepth--;
|
|
461
|
+
if (parenDepth === 0) {
|
|
462
|
+
return { content: input.slice(startIndex, i), nextIndex: i + 1 };
|
|
463
|
+
}
|
|
464
|
+
i++;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
i++;
|
|
468
|
+
}
|
|
469
|
+
return { content: input.slice(startIndex, i), nextIndex: i };
|
|
470
|
+
}
|
|
471
|
+
function readBacktickSubshell(input, startIndex) {
|
|
472
|
+
const len = input.length;
|
|
473
|
+
let i = startIndex;
|
|
474
|
+
while (i < len) {
|
|
475
|
+
const ch = input[i];
|
|
476
|
+
if (ch === '\\' && i + 1 < len) {
|
|
477
|
+
i += 2;
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
if (ch === '`') {
|
|
481
|
+
return { content: input.slice(startIndex, i), nextIndex: i + 1 };
|
|
482
|
+
}
|
|
483
|
+
i++;
|
|
484
|
+
}
|
|
485
|
+
return { content: input.slice(startIndex, i), nextIndex: i };
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Splits a masked shell command string into sequential command lists and
|
|
489
|
+
* pipe-connected pipeline groups while keeping `2>&1` / `>&2` descriptor
|
|
490
|
+
* duplications intact.
|
|
491
|
+
*/
|
|
492
|
+
export function splitIntoPipelinesAndSegments(maskedText) {
|
|
493
|
+
const listGroups = [];
|
|
494
|
+
let current = '';
|
|
495
|
+
const len = maskedText.length;
|
|
496
|
+
let i = 0;
|
|
497
|
+
const pushGroup = () => {
|
|
498
|
+
const trimmed = current.trim();
|
|
499
|
+
if (trimmed.length > 0)
|
|
500
|
+
listGroups.push(trimmed);
|
|
501
|
+
current = '';
|
|
502
|
+
};
|
|
503
|
+
while (i < len) {
|
|
504
|
+
const ch = maskedText[i];
|
|
505
|
+
const next = maskedText[i + 1];
|
|
506
|
+
if (ch === ';' || ch === '\n') {
|
|
507
|
+
pushGroup();
|
|
508
|
+
i++;
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
if (ch === '&' && next === '&') {
|
|
512
|
+
pushGroup();
|
|
513
|
+
i += 2;
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
if (ch === '|' && next === '|') {
|
|
517
|
+
pushGroup();
|
|
518
|
+
i += 2;
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
if (ch === '&') {
|
|
522
|
+
const prev = i > 0 ? maskedText[i - 1] : '';
|
|
523
|
+
// Keep `>&1`, `2>&1`, `<&0`, `&>` redirections intact
|
|
524
|
+
if (prev === '>' || prev === '<' || next === '>') {
|
|
525
|
+
current += ch;
|
|
526
|
+
i++;
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
pushGroup();
|
|
530
|
+
i++;
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
current += ch;
|
|
534
|
+
i++;
|
|
535
|
+
}
|
|
536
|
+
pushGroup();
|
|
537
|
+
const segments = [];
|
|
538
|
+
const pipelines = [];
|
|
539
|
+
for (const group of listGroups) {
|
|
540
|
+
const stages = [];
|
|
541
|
+
let stage = '';
|
|
542
|
+
for (let j = 0; j < group.length; j++) {
|
|
543
|
+
const ch = group[j];
|
|
544
|
+
if (ch === '|') {
|
|
545
|
+
const trimmed = stage.trim();
|
|
546
|
+
if (trimmed.length > 0) {
|
|
547
|
+
stages.push(trimmed);
|
|
548
|
+
segments.push(trimmed);
|
|
549
|
+
}
|
|
550
|
+
stage = '';
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
stage += ch;
|
|
554
|
+
}
|
|
555
|
+
const trimmed = stage.trim();
|
|
556
|
+
if (trimmed.length > 0) {
|
|
557
|
+
stages.push(trimmed);
|
|
558
|
+
segments.push(trimmed);
|
|
559
|
+
}
|
|
560
|
+
if (stages.length > 0) {
|
|
561
|
+
pipelines.push(stages);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return { segments, pipelines };
|
|
565
|
+
}
|
|
566
|
+
function isEnvAssignmentToken(token) {
|
|
567
|
+
const eq = token.indexOf('=');
|
|
568
|
+
if (eq <= 0)
|
|
569
|
+
return false;
|
|
570
|
+
const name = token.slice(0, eq);
|
|
571
|
+
const first = name.charCodeAt(0);
|
|
572
|
+
const isFirstValid = (first >= 97 && first <= 122) || // a-z
|
|
573
|
+
(first >= 65 && first <= 90) || // A-Z
|
|
574
|
+
first === 95; // _
|
|
575
|
+
if (!isFirstValid)
|
|
576
|
+
return false;
|
|
577
|
+
for (let i = 1; i < name.length; i++) {
|
|
578
|
+
const code = name.charCodeAt(i);
|
|
579
|
+
const valid = (code >= 97 && code <= 122) ||
|
|
580
|
+
(code >= 65 && code <= 90) ||
|
|
581
|
+
(code >= 48 && code <= 57) ||
|
|
582
|
+
code === 95;
|
|
583
|
+
if (!valid)
|
|
584
|
+
return false;
|
|
585
|
+
}
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
function isStandaloneRedirectOperator(token) {
|
|
589
|
+
return (token === '>' ||
|
|
590
|
+
token === '>>' ||
|
|
591
|
+
token === '<' ||
|
|
592
|
+
token === '1>' ||
|
|
593
|
+
token === '1>>' ||
|
|
594
|
+
token === '2>' ||
|
|
595
|
+
token === '2>>' ||
|
|
596
|
+
token === '&>' ||
|
|
597
|
+
token === '&>>');
|
|
598
|
+
}
|
|
599
|
+
function isAttachedRedirectToken(token) {
|
|
600
|
+
return (token.startsWith('>') ||
|
|
601
|
+
token.startsWith('<') ||
|
|
602
|
+
token.startsWith('1>') ||
|
|
603
|
+
token.startsWith('2>') ||
|
|
604
|
+
token.startsWith('&>'));
|
|
605
|
+
}
|
|
606
|
+
function resolveExecutableBasename(token) {
|
|
607
|
+
const normalized = token.replace(/\\/g, '/');
|
|
608
|
+
const slash = normalized.lastIndexOf('/');
|
|
609
|
+
let base = slash !== -1 ? normalized.slice(slash + 1) : normalized;
|
|
610
|
+
if (base.endsWith('.exe') && base.length > 4) {
|
|
611
|
+
base = base.slice(0, -4);
|
|
612
|
+
}
|
|
613
|
+
return base;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Normalizes one already-lowercased, quote-masked shell segment by stripping
|
|
617
|
+
* leading env assignments (`FOO=1`), redirections, control keywords, and
|
|
618
|
+
* benign command wrappers (`env`, `command`, `nohup`, `nice`, `time`, `timeout`,
|
|
619
|
+
* `exec`, `stdbuf`, `builtin`, `caffeinate`), then resolving the executable
|
|
620
|
+
* basename (`/bin/rm` -> `rm`).
|
|
621
|
+
*
|
|
622
|
+
* Preserves read-only lookup semantics for `command -v` / `command -V` so
|
|
623
|
+
* existence probes do not trigger false alarms.
|
|
624
|
+
*/
|
|
625
|
+
export function normalizeExecutableSegment(segment) {
|
|
626
|
+
const tokens = segment.trim().split(/\s+/).filter(Boolean);
|
|
627
|
+
let idx = 0;
|
|
628
|
+
// Bounded normalization loop
|
|
629
|
+
for (let guard = 0; guard < 32 && idx < tokens.length; guard++) {
|
|
630
|
+
const tok = tokens[idx];
|
|
631
|
+
// Strip leading parentheses attached to a token, e.g. `(rm`
|
|
632
|
+
if (tok.startsWith('(') && tok.length > 1) {
|
|
633
|
+
tokens[idx] = tok.slice(1);
|
|
634
|
+
continue;
|
|
635
|
+
}
|
|
636
|
+
if (LEADING_SHELL_KEYWORDS.has(tok)) {
|
|
637
|
+
idx++;
|
|
638
|
+
continue;
|
|
639
|
+
}
|
|
640
|
+
if (isStandaloneRedirectOperator(tok)) {
|
|
641
|
+
idx += 2; // operator + filename
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
if (isAttachedRedirectToken(tok)) {
|
|
645
|
+
idx++;
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
if (isEnvAssignmentToken(tok)) {
|
|
649
|
+
idx++;
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
const base = resolveExecutableBasename(tok);
|
|
653
|
+
if (base === 'env') {
|
|
654
|
+
idx++;
|
|
655
|
+
while (idx < tokens.length) {
|
|
656
|
+
const next = tokens[idx];
|
|
657
|
+
if (next === '--') {
|
|
658
|
+
idx++;
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
if (next === '-u' || next === '-c' || next === '-s' || next === '--unset' || next === '--chdir') {
|
|
662
|
+
idx += 2;
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
if (next.startsWith('-')) {
|
|
666
|
+
idx++;
|
|
667
|
+
continue;
|
|
668
|
+
}
|
|
669
|
+
if (isEnvAssignmentToken(next)) {
|
|
670
|
+
idx++;
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
if (base === 'command') {
|
|
678
|
+
let scan = idx + 1;
|
|
679
|
+
let isQueryOnly = false;
|
|
680
|
+
while (scan < tokens.length) {
|
|
681
|
+
const next = tokens[scan];
|
|
682
|
+
if (next === '--') {
|
|
683
|
+
scan++;
|
|
684
|
+
break;
|
|
685
|
+
}
|
|
686
|
+
if (next.startsWith('-') && next.length > 1) {
|
|
687
|
+
// POSIX `command -v` and `command -V` only look up the path; never execute
|
|
688
|
+
if (next.includes('v')) {
|
|
689
|
+
isQueryOnly = true;
|
|
690
|
+
}
|
|
691
|
+
scan++;
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
break;
|
|
695
|
+
}
|
|
696
|
+
if (isQueryOnly) {
|
|
697
|
+
const rest = tokens.slice(idx);
|
|
698
|
+
return {
|
|
699
|
+
normalized: rest.join(' '),
|
|
700
|
+
executable: 'command',
|
|
701
|
+
args: rest.slice(1),
|
|
702
|
+
isReadOnlyLookup: true
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
idx = scan;
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
if (base === 'nohup' || base === 'builtin' || base === 'setsid') {
|
|
709
|
+
idx++;
|
|
710
|
+
if (tokens[idx] === '--')
|
|
711
|
+
idx++;
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
if (base === 'time') {
|
|
715
|
+
idx++;
|
|
716
|
+
while (idx < tokens.length && tokens[idx].startsWith('-')) {
|
|
717
|
+
if (tokens[idx] === '--') {
|
|
718
|
+
idx++;
|
|
719
|
+
break;
|
|
720
|
+
}
|
|
721
|
+
idx++;
|
|
722
|
+
}
|
|
723
|
+
continue;
|
|
724
|
+
}
|
|
725
|
+
if (base === 'nice') {
|
|
726
|
+
idx++;
|
|
727
|
+
while (idx < tokens.length && tokens[idx].startsWith('-')) {
|
|
728
|
+
const flag = tokens[idx];
|
|
729
|
+
if (flag === '--') {
|
|
730
|
+
idx++;
|
|
731
|
+
break;
|
|
732
|
+
}
|
|
733
|
+
if (flag === '-n' || flag === '--adjustment') {
|
|
734
|
+
idx += 2;
|
|
735
|
+
continue;
|
|
736
|
+
}
|
|
737
|
+
idx++;
|
|
738
|
+
}
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
if (base === 'exec') {
|
|
742
|
+
idx++;
|
|
743
|
+
while (idx < tokens.length && tokens[idx].startsWith('-')) {
|
|
744
|
+
const flag = tokens[idx];
|
|
745
|
+
if (flag === '--') {
|
|
746
|
+
idx++;
|
|
747
|
+
break;
|
|
748
|
+
}
|
|
749
|
+
if (flag === '-a') {
|
|
750
|
+
idx += 2;
|
|
751
|
+
continue;
|
|
752
|
+
}
|
|
753
|
+
idx++;
|
|
754
|
+
}
|
|
755
|
+
continue;
|
|
756
|
+
}
|
|
757
|
+
if (base === 'stdbuf' || base === 'ionice' || base === 'taskset') {
|
|
758
|
+
idx++;
|
|
759
|
+
while (idx < tokens.length && tokens[idx].startsWith('-')) {
|
|
760
|
+
const flag = tokens[idx];
|
|
761
|
+
if (flag === '--') {
|
|
762
|
+
idx++;
|
|
763
|
+
break;
|
|
764
|
+
}
|
|
765
|
+
if (flag === '-i' || flag === '-o' || flag === '-e' || flag === '-c' || flag === '-n' || flag === '-p') {
|
|
766
|
+
idx += 2;
|
|
767
|
+
continue;
|
|
768
|
+
}
|
|
769
|
+
idx++;
|
|
770
|
+
}
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
if (base === 'caffeinate') {
|
|
774
|
+
idx++;
|
|
775
|
+
while (idx < tokens.length && tokens[idx].startsWith('-')) {
|
|
776
|
+
const flag = tokens[idx];
|
|
777
|
+
if (flag === '--') {
|
|
778
|
+
idx++;
|
|
779
|
+
break;
|
|
780
|
+
}
|
|
781
|
+
if (flag === '-t' || flag === '-w') {
|
|
782
|
+
idx += 2;
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
idx++;
|
|
786
|
+
}
|
|
787
|
+
continue;
|
|
788
|
+
}
|
|
789
|
+
if (base === 'timeout') {
|
|
790
|
+
idx++;
|
|
791
|
+
while (idx < tokens.length && tokens[idx].startsWith('-')) {
|
|
792
|
+
const flag = tokens[idx];
|
|
793
|
+
if (flag === '--') {
|
|
794
|
+
idx++;
|
|
795
|
+
break;
|
|
796
|
+
}
|
|
797
|
+
if (flag === '-k' || flag === '-s' || flag === '--signal' || flag === '--kill-after') {
|
|
798
|
+
idx += 2;
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
idx++;
|
|
802
|
+
}
|
|
803
|
+
// Consume duration operand (e.g. `10`, `30s`, `2m`)
|
|
804
|
+
if (idx < tokens.length && /^\d+(?:\.\d+)?[smhd]?$/.test(tokens[idx])) {
|
|
805
|
+
idx++;
|
|
806
|
+
}
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
break;
|
|
810
|
+
}
|
|
811
|
+
if (idx >= tokens.length) {
|
|
812
|
+
return { normalized: '', executable: '', args: [], isReadOnlyLookup: false };
|
|
813
|
+
}
|
|
814
|
+
const executable = resolveExecutableBasename(tokens[idx]);
|
|
815
|
+
const args = tokens.slice(idx + 1);
|
|
816
|
+
const normalized = args.length > 0 ? `${executable} ${args.join(' ')}` : executable;
|
|
817
|
+
return {
|
|
818
|
+
normalized,
|
|
819
|
+
executable,
|
|
820
|
+
args,
|
|
821
|
+
isReadOnlyLookup: false
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Splits a raw command string into sequential/piped stages while ignoring
|
|
826
|
+
* separators (`;`, `&&`, `||`, `|`, `&`, `\n`) inside single or double quotes.
|
|
827
|
+
*/
|
|
828
|
+
function splitRawIntoStages(rawCommand) {
|
|
829
|
+
const stages = [];
|
|
830
|
+
let current = '';
|
|
831
|
+
const len = rawCommand.length;
|
|
832
|
+
let i = 0;
|
|
833
|
+
let inSingle = false;
|
|
834
|
+
let inDouble = false;
|
|
835
|
+
const pushStage = () => {
|
|
836
|
+
const trimmed = current.trim();
|
|
837
|
+
if (trimmed.length > 0)
|
|
838
|
+
stages.push(trimmed);
|
|
839
|
+
current = '';
|
|
840
|
+
};
|
|
841
|
+
while (i < len) {
|
|
842
|
+
const ch = rawCommand[i];
|
|
843
|
+
const next = rawCommand[i + 1];
|
|
844
|
+
if (inSingle) {
|
|
845
|
+
current += ch;
|
|
846
|
+
if (ch === "'")
|
|
847
|
+
inSingle = false;
|
|
848
|
+
i++;
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
if (inDouble) {
|
|
852
|
+
if (ch === '\\' && i + 1 < len) {
|
|
853
|
+
current += ch + next;
|
|
854
|
+
i += 2;
|
|
855
|
+
continue;
|
|
856
|
+
}
|
|
857
|
+
current += ch;
|
|
858
|
+
if (ch === '"')
|
|
859
|
+
inDouble = false;
|
|
860
|
+
i++;
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
if (ch === '\\' && i + 1 < len) {
|
|
864
|
+
current += ch + next;
|
|
865
|
+
i += 2;
|
|
866
|
+
continue;
|
|
867
|
+
}
|
|
868
|
+
if (ch === "'") {
|
|
869
|
+
inSingle = true;
|
|
870
|
+
current += ch;
|
|
871
|
+
i++;
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
if (ch === '"') {
|
|
875
|
+
inDouble = true;
|
|
876
|
+
current += ch;
|
|
877
|
+
i++;
|
|
878
|
+
continue;
|
|
879
|
+
}
|
|
880
|
+
if (ch === ';' || ch === '\n') {
|
|
881
|
+
pushStage();
|
|
882
|
+
i++;
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
if (ch === '|' && next === '|') {
|
|
886
|
+
pushStage();
|
|
887
|
+
i += 2;
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
890
|
+
if (ch === '&' && next === '&') {
|
|
891
|
+
pushStage();
|
|
892
|
+
i += 2;
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
if (ch === '|') {
|
|
896
|
+
pushStage();
|
|
897
|
+
i++;
|
|
898
|
+
continue;
|
|
899
|
+
}
|
|
900
|
+
if (ch === '&') {
|
|
901
|
+
const prev = i > 0 ? rawCommand[i - 1] : '';
|
|
902
|
+
if (prev === '>' || prev === '<' || next === '>') {
|
|
903
|
+
current += ch;
|
|
904
|
+
i++;
|
|
905
|
+
continue;
|
|
906
|
+
}
|
|
907
|
+
pushStage();
|
|
908
|
+
i++;
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
current += ch;
|
|
912
|
+
i++;
|
|
913
|
+
}
|
|
914
|
+
pushStage();
|
|
915
|
+
return stages;
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* Extracts unquoted and quoted file targets written via redirection (`>`, `>>`)
|
|
919
|
+
* or `tee` across a raw command string without matching operators inside quotes.
|
|
920
|
+
*/
|
|
921
|
+
export function extractWriteTargetsFromRaw(rawCommand) {
|
|
922
|
+
const targets = [];
|
|
923
|
+
const len = rawCommand.length;
|
|
924
|
+
let i = 0;
|
|
925
|
+
let inSingle = false;
|
|
926
|
+
let inDouble = false;
|
|
927
|
+
const readShellWord = (start) => {
|
|
928
|
+
let j = start;
|
|
929
|
+
while (j < len && (rawCommand[j] === ' ' || rawCommand[j] === '\t'))
|
|
930
|
+
j++;
|
|
931
|
+
let word = '';
|
|
932
|
+
while (j < len) {
|
|
933
|
+
const c = rawCommand[j];
|
|
934
|
+
if (c === ' ' || c === '\t' || c === '\n' || c === ';' || c === '|' || c === '&' || c === '<' || c === '>') {
|
|
935
|
+
break;
|
|
936
|
+
}
|
|
937
|
+
if (c === "'") {
|
|
938
|
+
j++;
|
|
939
|
+
while (j < len && rawCommand[j] !== "'") {
|
|
940
|
+
word += rawCommand[j];
|
|
941
|
+
j++;
|
|
942
|
+
}
|
|
943
|
+
if (j < len)
|
|
944
|
+
j++;
|
|
945
|
+
continue;
|
|
946
|
+
}
|
|
947
|
+
if (c === '"') {
|
|
948
|
+
j++;
|
|
949
|
+
while (j < len && rawCommand[j] !== '"') {
|
|
950
|
+
if (rawCommand[j] === '\\' && j + 1 < len) {
|
|
951
|
+
word += rawCommand[j + 1];
|
|
952
|
+
j += 2;
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
word += rawCommand[j];
|
|
956
|
+
j++;
|
|
957
|
+
}
|
|
958
|
+
if (j < len)
|
|
959
|
+
j++;
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
if (c === '\\' && j + 1 < len) {
|
|
963
|
+
word += rawCommand[j + 1];
|
|
964
|
+
j += 2;
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
word += c;
|
|
968
|
+
j++;
|
|
969
|
+
}
|
|
970
|
+
return { word, nextIndex: j };
|
|
971
|
+
};
|
|
972
|
+
while (i < len) {
|
|
973
|
+
const ch = rawCommand[i];
|
|
974
|
+
if (inSingle) {
|
|
975
|
+
if (ch === "'")
|
|
976
|
+
inSingle = false;
|
|
977
|
+
i++;
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
980
|
+
if (inDouble) {
|
|
981
|
+
if (ch === '\\' && i + 1 < len) {
|
|
982
|
+
i += 2;
|
|
983
|
+
continue;
|
|
984
|
+
}
|
|
985
|
+
if (ch === '"')
|
|
986
|
+
inDouble = false;
|
|
987
|
+
i++;
|
|
988
|
+
continue;
|
|
989
|
+
}
|
|
990
|
+
if (ch === '\\' && i + 1 < len) {
|
|
991
|
+
i += 2;
|
|
992
|
+
continue;
|
|
993
|
+
}
|
|
994
|
+
if (ch === "'") {
|
|
995
|
+
inSingle = true;
|
|
996
|
+
i++;
|
|
997
|
+
continue;
|
|
998
|
+
}
|
|
999
|
+
if (ch === '"') {
|
|
1000
|
+
inDouble = true;
|
|
1001
|
+
i++;
|
|
1002
|
+
continue;
|
|
1003
|
+
}
|
|
1004
|
+
if (ch === '>') {
|
|
1005
|
+
// Handle `>>`, `>|`, and `>&`: FD duplications (`2>&1`, `>&2`, `>&-`) are not files,
|
|
1006
|
+
// whereas `>& file` in Bash/Zsh redirects stdout+stderr to `file`.
|
|
1007
|
+
let next = i + 1;
|
|
1008
|
+
if (rawCommand[next] === '&') {
|
|
1009
|
+
const { word, nextIndex } = readShellWord(next + 1);
|
|
1010
|
+
if (word && !/^(?:\d+-?|-)$/.test(word)) {
|
|
1011
|
+
targets.push(word);
|
|
1012
|
+
}
|
|
1013
|
+
i = nextIndex;
|
|
1014
|
+
continue;
|
|
1015
|
+
}
|
|
1016
|
+
if (rawCommand[next] === '>' || rawCommand[next] === '|') {
|
|
1017
|
+
next++;
|
|
1018
|
+
}
|
|
1019
|
+
const { word, nextIndex } = readShellWord(next);
|
|
1020
|
+
if (word && !word.startsWith('&')) {
|
|
1021
|
+
targets.push(word);
|
|
1022
|
+
}
|
|
1023
|
+
i = nextIndex;
|
|
1024
|
+
continue;
|
|
1025
|
+
}
|
|
1026
|
+
i++;
|
|
1027
|
+
}
|
|
1028
|
+
// Also capture `tee [-a|-i] <target...>` destinations on unquoted pipe/command stages
|
|
1029
|
+
const stages = splitRawIntoStages(rawCommand);
|
|
1030
|
+
for (const seg of stages) {
|
|
1031
|
+
const words = tokenizeRawShellWords(seg);
|
|
1032
|
+
if (words.length < 2)
|
|
1033
|
+
continue;
|
|
1034
|
+
const norm = normalizeExecutableSegment(words.map(w => w.toLowerCase()).join(' '));
|
|
1035
|
+
if (norm.executable === 'tee') {
|
|
1036
|
+
// Re-scan original-case words after the `tee` token
|
|
1037
|
+
const teeIndex = words.findIndex(w => resolveExecutableBasename(w.toLowerCase()) === 'tee');
|
|
1038
|
+
if (teeIndex !== -1) {
|
|
1039
|
+
for (let k = teeIndex + 1; k < words.length; k++) {
|
|
1040
|
+
const arg = words[k];
|
|
1041
|
+
if (arg === '--')
|
|
1042
|
+
continue;
|
|
1043
|
+
if (arg.startsWith('-'))
|
|
1044
|
+
continue;
|
|
1045
|
+
targets.push(arg);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
return targets;
|
|
1051
|
+
}
|
|
1052
|
+
function tokenizeRawShellWords(segment) {
|
|
1053
|
+
const words = [];
|
|
1054
|
+
const len = segment.length;
|
|
1055
|
+
let i = 0;
|
|
1056
|
+
while (i < len) {
|
|
1057
|
+
while (i < len && (segment[i] === ' ' || segment[i] === '\t' || segment[i] === '\n'))
|
|
1058
|
+
i++;
|
|
1059
|
+
if (i >= len)
|
|
1060
|
+
break;
|
|
1061
|
+
let word = '';
|
|
1062
|
+
while (i < len) {
|
|
1063
|
+
const ch = segment[i];
|
|
1064
|
+
if (ch === ' ' || ch === '\t' || ch === '\n')
|
|
1065
|
+
break;
|
|
1066
|
+
if (ch === "'") {
|
|
1067
|
+
i++;
|
|
1068
|
+
while (i < len && segment[i] !== "'") {
|
|
1069
|
+
word += segment[i];
|
|
1070
|
+
i++;
|
|
1071
|
+
}
|
|
1072
|
+
if (i < len)
|
|
1073
|
+
i++;
|
|
1074
|
+
continue;
|
|
1075
|
+
}
|
|
1076
|
+
if (ch === '"') {
|
|
1077
|
+
i++;
|
|
1078
|
+
while (i < len && segment[i] !== '"') {
|
|
1079
|
+
if (segment[i] === '\\' && i + 1 < len) {
|
|
1080
|
+
word += segment[i + 1];
|
|
1081
|
+
i += 2;
|
|
1082
|
+
continue;
|
|
1083
|
+
}
|
|
1084
|
+
word += segment[i];
|
|
1085
|
+
i++;
|
|
1086
|
+
}
|
|
1087
|
+
if (i < len)
|
|
1088
|
+
i++;
|
|
1089
|
+
continue;
|
|
1090
|
+
}
|
|
1091
|
+
if (ch === '\\' && i + 1 < len) {
|
|
1092
|
+
word += segment[i + 1];
|
|
1093
|
+
i += 2;
|
|
1094
|
+
continue;
|
|
1095
|
+
}
|
|
1096
|
+
word += ch;
|
|
1097
|
+
i++;
|
|
1098
|
+
}
|
|
1099
|
+
if (word.length > 0)
|
|
1100
|
+
words.push(word);
|
|
1101
|
+
}
|
|
1102
|
+
return words;
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Detects destructive recursive-force removals regardless of flag order or
|
|
1106
|
+
* flag bundling (e.g. `rm -r -f /`, `rm -f -r ~`, `rm -rfv /`, `rm --recursive --force *`).
|
|
1107
|
+
*/
|
|
1108
|
+
export function isExtremeRemovalSegment(seg) {
|
|
1109
|
+
if (seg.executable !== 'rm')
|
|
1110
|
+
return false;
|
|
1111
|
+
let hasRecursive = false;
|
|
1112
|
+
let hasForce = false;
|
|
1113
|
+
const targets = [];
|
|
1114
|
+
let endOfFlags = false;
|
|
1115
|
+
for (const arg of seg.args) {
|
|
1116
|
+
if (!endOfFlags && arg === '--') {
|
|
1117
|
+
endOfFlags = true;
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
if (!endOfFlags && arg.startsWith('--')) {
|
|
1121
|
+
if (arg === '--recursive')
|
|
1122
|
+
hasRecursive = true;
|
|
1123
|
+
if (arg === '--force')
|
|
1124
|
+
hasForce = true;
|
|
1125
|
+
continue;
|
|
1126
|
+
}
|
|
1127
|
+
if (!endOfFlags && arg.startsWith('-') && arg.length > 1) {
|
|
1128
|
+
const flags = arg.slice(1);
|
|
1129
|
+
if (flags.includes('r'))
|
|
1130
|
+
hasRecursive = true;
|
|
1131
|
+
if (flags.includes('f'))
|
|
1132
|
+
hasForce = true;
|
|
1133
|
+
continue;
|
|
1134
|
+
}
|
|
1135
|
+
targets.push(arg);
|
|
1136
|
+
}
|
|
1137
|
+
if (!hasRecursive || !hasForce)
|
|
1138
|
+
return false;
|
|
1139
|
+
for (const target of targets) {
|
|
1140
|
+
if (target === '/' ||
|
|
1141
|
+
target.startsWith('/') ||
|
|
1142
|
+
target === '~' ||
|
|
1143
|
+
target.startsWith('~/') ||
|
|
1144
|
+
target.startsWith('~') ||
|
|
1145
|
+
target.startsWith('*') ||
|
|
1146
|
+
target === '.' ||
|
|
1147
|
+
target === './' ||
|
|
1148
|
+
target.startsWith('./*') ||
|
|
1149
|
+
target === '..' ||
|
|
1150
|
+
target === '../' ||
|
|
1151
|
+
target.startsWith('../*') ||
|
|
1152
|
+
/^[a-z]:[\\/]?$/.test(target) ||
|
|
1153
|
+
target === '$home' ||
|
|
1154
|
+
target.startsWith('$home/') ||
|
|
1155
|
+
target === '${home}' ||
|
|
1156
|
+
target.startsWith('${home}/')) {
|
|
1157
|
+
// Allow `/tmp/...`? Wait — `rm -rf /tmp/foo` is dangerous, not necessarily root wipe,
|
|
1158
|
+
// EXCEPT existing classifier explicitly treated `/rm\s+(-rf|-fr)\s+[\/~]/` as extreme!
|
|
1159
|
+
// Preserve exact strictness: any leading `/` or `~` or `*` with recursive+force is extreme.
|
|
1160
|
+
return true;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
return false;
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Detects fetch-and-execute pipelines (`curl ... | bash`, `wget ... | /bin/zsh`,
|
|
1167
|
+
* `curl ... | sudo sh`, `curl ... | env bash`) in linear time.
|
|
1168
|
+
*/
|
|
1169
|
+
export function isFetchAndExecPipeline(pipelines) {
|
|
1170
|
+
for (const pipeline of pipelines) {
|
|
1171
|
+
if (pipeline.length < 2)
|
|
1172
|
+
continue;
|
|
1173
|
+
let sawFetcher = false;
|
|
1174
|
+
for (const stage of pipeline) {
|
|
1175
|
+
if (stage.executable === 'curl' || stage.executable === 'wget') {
|
|
1176
|
+
sawFetcher = true;
|
|
1177
|
+
continue;
|
|
1178
|
+
}
|
|
1179
|
+
if (!sawFetcher)
|
|
1180
|
+
continue;
|
|
1181
|
+
if (SHELL_INTERPRETERS.has(stage.executable))
|
|
1182
|
+
return true;
|
|
1183
|
+
// Handle `curl ... | sudo|su|doas|pkexec|xargs bash`
|
|
1184
|
+
if (stage.executable === 'sudo' ||
|
|
1185
|
+
stage.executable === 'su' ||
|
|
1186
|
+
stage.executable === 'doas' ||
|
|
1187
|
+
stage.executable === 'pkexec' ||
|
|
1188
|
+
stage.executable === 'xargs') {
|
|
1189
|
+
const valueFlags = new Set([
|
|
1190
|
+
'-u', '-g', '-c', '-d', '-h', '-p', '-r', '-t', '-a', '-s',
|
|
1191
|
+
'--user', '--group', '--prompt', '--chdir', '--shell'
|
|
1192
|
+
]);
|
|
1193
|
+
for (let i = 0; i < stage.args.length; i++) {
|
|
1194
|
+
const arg = stage.args[i];
|
|
1195
|
+
if (arg === '--') {
|
|
1196
|
+
const next = stage.args[i + 1];
|
|
1197
|
+
if (next && SHELL_INTERPRETERS.has(resolveExecutableBasename(next)))
|
|
1198
|
+
return true;
|
|
1199
|
+
break;
|
|
1200
|
+
}
|
|
1201
|
+
if (arg.startsWith('-')) {
|
|
1202
|
+
if (valueFlags.has(arg))
|
|
1203
|
+
i++;
|
|
1204
|
+
continue;
|
|
1205
|
+
}
|
|
1206
|
+
if (SHELL_INTERPRETERS.has(resolveExecutableBasename(arg)))
|
|
1207
|
+
return true;
|
|
1208
|
+
break;
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
return false;
|
|
1214
|
+
}
|
|
1215
|
+
/**
|
|
1216
|
+
* Parses a shell command string (including nested `$(...)` and backtick
|
|
1217
|
+
* substitutions) into normalized executable segments, pipelines, and write targets.
|
|
1218
|
+
*/
|
|
1219
|
+
export function analyzeShellCommand(rawCommand) {
|
|
1220
|
+
if (typeof rawCommand !== 'string') {
|
|
1221
|
+
return {
|
|
1222
|
+
maskedTopLevel: '',
|
|
1223
|
+
allRawUnits: [],
|
|
1224
|
+
allMaskedUnits: [],
|
|
1225
|
+
normalizedSegments: [],
|
|
1226
|
+
unwrappedRawSegments: [],
|
|
1227
|
+
normalizedPipelines: [],
|
|
1228
|
+
writeTargets: []
|
|
1229
|
+
};
|
|
1230
|
+
}
|
|
1231
|
+
const lower = rawCommand.trim().toLowerCase();
|
|
1232
|
+
const topParsed = parseMaskAndExtractSubstitutions(lower, 0);
|
|
1233
|
+
const allRawUnits = [rawCommand.trim()];
|
|
1234
|
+
const allMaskedUnits = [topParsed.masked];
|
|
1235
|
+
// Also keep original-case substitutions for accurate path-target extraction
|
|
1236
|
+
const rawCaseParsed = parseMaskAndExtractSubstitutions(rawCommand.trim(), 0);
|
|
1237
|
+
for (const sub of rawCaseParsed.substitutions) {
|
|
1238
|
+
allRawUnits.push(sub);
|
|
1239
|
+
}
|
|
1240
|
+
for (const sub of topParsed.substitutions) {
|
|
1241
|
+
const subParsed = parseMaskAndExtractSubstitutions(sub, MAX_SUBSTITUTION_DEPTH);
|
|
1242
|
+
allMaskedUnits.push(subParsed.masked);
|
|
1243
|
+
}
|
|
1244
|
+
const normalizedSegments = [];
|
|
1245
|
+
const normalizedPipelines = [];
|
|
1246
|
+
for (const unit of allMaskedUnits) {
|
|
1247
|
+
const { pipelines } = splitIntoPipelinesAndSegments(unit);
|
|
1248
|
+
for (const pipeline of pipelines) {
|
|
1249
|
+
const normPipeline = [];
|
|
1250
|
+
for (const stage of pipeline) {
|
|
1251
|
+
const norm = normalizeExecutableSegment(stage);
|
|
1252
|
+
if (norm.normalized.length > 0) {
|
|
1253
|
+
normalizedSegments.push(norm);
|
|
1254
|
+
normPipeline.push(norm);
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
if (normPipeline.length > 0) {
|
|
1258
|
+
normalizedPipelines.push(normPipeline);
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
const unwrappedRawSegments = [];
|
|
1263
|
+
const writeTargets = [];
|
|
1264
|
+
for (const rawUnit of allRawUnits) {
|
|
1265
|
+
for (const target of extractWriteTargetsFromRaw(rawUnit)) {
|
|
1266
|
+
writeTargets.push(target);
|
|
1267
|
+
}
|
|
1268
|
+
for (const stage of splitRawIntoStages(rawUnit)) {
|
|
1269
|
+
const words = tokenizeRawShellWords(stage);
|
|
1270
|
+
if (words.length === 0)
|
|
1271
|
+
continue;
|
|
1272
|
+
const norm = normalizeExecutableSegment(words.map(w => w.toLowerCase()).join(' '));
|
|
1273
|
+
if (norm.normalized.length > 0) {
|
|
1274
|
+
unwrappedRawSegments.push(norm);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
return {
|
|
1279
|
+
maskedTopLevel: topParsed.masked,
|
|
1280
|
+
allRawUnits,
|
|
1281
|
+
allMaskedUnits,
|
|
1282
|
+
normalizedSegments,
|
|
1283
|
+
unwrappedRawSegments,
|
|
1284
|
+
normalizedPipelines,
|
|
1285
|
+
writeTargets
|
|
1286
|
+
};
|
|
1287
|
+
}
|