dsh-extra-plan 0.0.0-stage → 1.7.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 +179 -2
- package/assets/presets/extra-plan/agent.cordis.yml +580 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-executor.generated.yml +115 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-planner.generated.yml +113 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-prober.generated.yml +107 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-reviewer.generated.yml +115 -0
- package/assets/presets/extra-plan/preset-definition.generated.yml +335 -0
- package/assets/presets/extra-plan/preset.yml +2 -0
- package/cordis.patch.yml +7 -0
- package/index.js +1671 -0
- package/lib/agent-runtime.js +172 -0
- package/lib/agent-session.js +65 -0
- package/lib/assembly-presentation.js +267 -0
- package/lib/client-carrier.js +7 -0
- package/lib/client.js +314 -0
- package/lib/extra-plan-gate-tool.js +387 -0
- package/lib/gate-decisions.js +744 -0
- package/lib/gate-words.js +108 -0
- package/lib/live-config.js +293 -0
- package/lib/log-retention.js +132 -0
- package/lib/model-routing.js +648 -0
- package/lib/path-policy.js +464 -0
- package/lib/plan-draft-tool.js +294 -0
- package/lib/planner-budget.js +136 -0
- package/lib/preset-defaults.generated.js +17 -0
- package/lib/preset-settings.js +3 -0
- package/lib/preset-sync.js +809 -0
- package/lib/preset-yaml.js +438 -0
- package/lib/role-contract.js +11 -0
- package/lib/run-code-scanner.js +360 -0
- package/lib/run-code-static.js +621 -0
- package/lib/runtime-diagnostics.js +89 -0
- package/lib/runtime-lifecycle.js +162 -0
- package/lib/runtime-static.js +23 -0
- package/lib/save-contract.js +263 -0
- package/lib/save-persistence.js +223 -0
- package/lib/save-plan-markdown-parser.js +369 -0
- package/lib/save-plan-validation-diagnostics.js +49 -0
- package/lib/save-plan-validation.js +812 -0
- package/lib/save-probe-validation.js +173 -0
- package/lib/save-source-verification.js +195 -0
- package/lib/save-tool-factories.js +270 -0
- package/lib/sdk-text-cache.js +161 -0
- package/lib/settings-contract.js +191 -0
- package/lib/settings.js +369 -0
- package/lib/shell-mutation.js +484 -0
- package/lib/usage-ledger-background.js +214 -0
- package/lib/usage-ledger-worker.js +116 -0
- package/lib/usage-ledger.js +1393 -0
- package/locale/en.json +6 -0
- package/locale/settings/en.json +6 -0
- package/locale/settings/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +56 -4
- package/scripts/generate-runtime-defaults.mjs +796 -0
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
// dsh-extra-plan shell mutation 辅助函数。
|
|
2
|
+
// 纯命令解码与跨平台写入形态匹配;不持有 host 或 apply 状态。
|
|
3
|
+
|
|
4
|
+
// PWSH 写动词判定(P2 位置判定):语法级写形态全文本匹配(git 写子命令/.NET 静态/COM FSO/
|
|
5
|
+
// Export-Csv/Export-Clixml/Tee-Object/Start-Transcript;1-6 流号与 *>/*>> 重定向,保留
|
|
6
|
+
// 2>&1/1>&2/*>&1 等 stream→stream 合并例外);裸写动词按命令位判定——段首、块关键字之后、
|
|
7
|
+
// 块体左花括号之后、PowerShell 赋值右值($var =、[type]$var =、粘连 $var=)、return 之后
|
|
8
|
+
// 与 $()/@(/( 子表达式入口均为命令位,参数位整词一律放行;裸 > / >> 由 redirectEvidence
|
|
9
|
+
// 按引号/左邻 =- 复合序列(=>、->)与右邻 & 流合并目标判定(圆括号深度不参与,子表达式内重定向仍判写;PowerShell 中裸 > 与 >= 一律判写、比较须用 -gt/-ge,右邻纯数字如 $i > 4 同样判写)。
|
|
10
|
+
export const PWSH_MUTATION = /\bgit\s+(add|commit|checkout|switch|restore|clean|rm|mv|reset)\b|\b(Export-Csv|Export-Clixml|Tee-Object|Start-Transcript)\b|\[System\.IO\.File\]::(WriteAllText|WriteAllBytes|AppendAllText|Delete|Move|Copy|Replace|Encrypt|Decrypt)|\[IO\.File\]::(WriteAllText|WriteAllBytes|AppendAllText|Delete|Move|Copy|Replace|Encrypt|Decrypt)|\[System\.IO\.(FileStream|StreamWriter|BinaryWriter)\]::new|\[System\.IO\.Compression\.ZipFile\]::(CreateFromDirectory|ExtractToDirectory)|\[System\.IO\.Directory\]::(Delete|Move|CreateDirectory)|New-Object\s+-ComObject\s+Scripting\.FileSystemObject|(?:[1-6]|\*)>>?(?!&(?:[1-6]|\*))/i
|
|
11
|
+
export const PWSH_BARE_WORDS = /\b(New-Item|Remove-Item|Rename-Item|Move-Item|Copy-Item|Set-Content|Add-Content|Clear-Content|Out-File|Set-Item|New-ItemProperty|Set-ItemProperty|Remove-ItemProperty|mkdir|rmdir|rd|del|erase|copy|move|ren|rename|xcopy|robocopy)\b/i
|
|
12
|
+
|
|
13
|
+
// bash 写命令(与 PWSH_MUTATION 严格对等,识别创建/修改/删除文件的操作;P2 起为位置判定):
|
|
14
|
+
// - 裸命令词:rm/mv/cp/mkdir/rmdir/touch/tee/chmod/chown/ln/install/rsync/truncate/
|
|
15
|
+
// fallocate/shred/zip 按命令位判定——按 ; 换行 && || | & 切段后每段跑命令位状态机:
|
|
16
|
+
// 段首、块关键字(try/if/for/while/else 等)之后、块体左花括号之后、块体右花括号
|
|
17
|
+
// 收尾后(衔接 catch/finally/else/elseif/while 续块)均为命令位,
|
|
18
|
+
// 参数位整词一律放行(如 grep -rn rm src/、echo "del done"、echo if then rm、
|
|
19
|
+
// 参数位的花括号 echo { rm } 放行);
|
|
20
|
+
// 段首为 sudo/env/nohup/command 时跳过包装词与选项,其后的命令词仍按命令位判定。
|
|
21
|
+
// - 已知边界(与 PWSH_MUTATION 对等):语法级写形态出现在参数位置仍全文本命中;命令位判定下
|
|
22
|
+
// 包管理器命令首词非写动词不拦(此前 install 裸词全文本匹配曾使 npm install -g 误拦,
|
|
23
|
+
// 本改动修复);首词即 install/rsync/truncate/fallocate/shred/zip 仍拦。
|
|
24
|
+
// - 已知边界(新增①):块内嵌套 shell(如 bash -c '…'、pwsh -Command '…')的递归判定受
|
|
25
|
+
// depth 上限约束(depth >= 4 直接返回 false),超深嵌套中的写形态可能漏拦。
|
|
26
|
+
// - 当前行为:splitShellSegments不以引号内静态分隔符切段,echo "x; then rm"不形成rm命令;
|
|
27
|
+
// 可执行$()与bash -c内层仍递归判定,不承诺完整shell语法或“带引号都只读”。
|
|
28
|
+
// - git 写子命令:add/commit/checkout/switch/restore/clean/rm/mv/reset
|
|
29
|
+
// - sed 原地修改:sed -i / sed -i.bak / sed --in-place(sed\s+(?:--in-place\b|(?:-[A-Za-z]*\s+)*-i\b):
|
|
30
|
+
// 覆盖 -i 前带其他短选项(如 sed -n -i),且不误拦 sed 脚本内容里的 -i 字符串
|
|
31
|
+
// (如 sed 's/-i/x/' file 只读输出);残余边界(极罕见):-e 带脚本参数后再 -i
|
|
32
|
+
// 的复合写法会漏拦,PWSH 无对等物,按严格对等不扩大)
|
|
33
|
+
// - 重定向写:> >> 2> 2>> &> >&(fd→fd 重定向属只读管道不拦截:2>&1/1>&2 由
|
|
34
|
+
// [0-9]?>>? 后负向前瞻排除 &N;>&2 由 >& 后负向前瞻排除数字)
|
|
35
|
+
|
|
36
|
+
export const BASH_MUTATION = /git\s+(add|commit|checkout|switch|restore|clean|rm|mv|reset)\b|sed\s+(?:--in-place\b|(?:-[A-Za-z]*\s+)*-i\b)|(?:[0-9]?>>?(?!&\d)|&>|>&(?!\d))|\bdd\b[^|]*\sof=|wget\s+.*-O\b|curl\s+.*-o\b|\bvi(m)?\s+\S|\bnano\s+\S|tar\s+-[A-Za-z]*c/i
|
|
37
|
+
export const BASH_BARE_WORDS = /\b(rm|mv|cp|mkdir|rmdir|touch|tee|chmod|chown|ln|install|rsync|truncate|fallocate|shred|zip)\b/i
|
|
38
|
+
|
|
39
|
+
export function commandTextOf(exec) {
|
|
40
|
+
const raw = exec.arguments
|
|
41
|
+
if (raw === undefined || raw === null) return ''
|
|
42
|
+
if (typeof raw === 'string') {
|
|
43
|
+
if (raw.length === 0) return ''
|
|
44
|
+
let parsed = null
|
|
45
|
+
try { parsed = JSON.parse(raw) } catch (error) { /* 非 JSON,原样使用 */ }
|
|
46
|
+
if (parsed !== null && typeof parsed === 'object' && typeof parsed.command === 'string') return parsed.command
|
|
47
|
+
return raw
|
|
48
|
+
}
|
|
49
|
+
if (typeof raw === 'object' && typeof raw.command === 'string') return raw.command
|
|
50
|
+
return ''
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function pwshCommandOf(exec) { return commandTextOf(exec) }
|
|
54
|
+
export function bashCommandOf(exec) { return commandTextOf(exec) }
|
|
55
|
+
|
|
56
|
+
const SHELL_PREFIX_WORDS = new Set(['sudo', 'env', 'nohup', 'command'])
|
|
57
|
+
const INNER_SHELL_WORDS = new Set(['pwsh', 'powershell', 'cmd', 'cmd.exe', 'bash', 'sh'])
|
|
58
|
+
|
|
59
|
+
function shellWords(segment) {
|
|
60
|
+
const words = []
|
|
61
|
+
let index = 0
|
|
62
|
+
while (index < segment.length) {
|
|
63
|
+
while (index < segment.length && /\s/.test(segment[index])) index += 1
|
|
64
|
+
if (index >= segment.length) break
|
|
65
|
+
const start = index
|
|
66
|
+
let value = ''
|
|
67
|
+
let quoted = false
|
|
68
|
+
let quote = ''
|
|
69
|
+
while (index < segment.length) {
|
|
70
|
+
const ch = segment[index]
|
|
71
|
+
if (quote !== '') {
|
|
72
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && index + 1 < segment.length) { value += segment[index + 1]; index += 2; continue }
|
|
73
|
+
if (ch === quote) { quote = ''; index += 1; continue }
|
|
74
|
+
value += ch
|
|
75
|
+
index += 1
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
if (/\s/.test(ch)) break
|
|
79
|
+
if (ch === "'" || ch === '"') { quote = ch; quoted = true; index += 1; continue }
|
|
80
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && index + 1 < segment.length) { value += segment[index + 1]; index += 2; continue }
|
|
81
|
+
value += ch
|
|
82
|
+
index += 1
|
|
83
|
+
}
|
|
84
|
+
words.push({ value, quoted, raw: segment.slice(start, index) })
|
|
85
|
+
}
|
|
86
|
+
return words
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const PWSH_DOWNLOAD_COMMANDS = new Set(['invoke-webrequest', 'iwr', 'invoke-restmethod', 'irm'])
|
|
90
|
+
// OutF is the shortest unambiguous prefix; a quoted value after ':' is still a parameter.
|
|
91
|
+
const PWSH_DOWNLOAD_OUTPUT = /^-outf(?:i(?:l(?:e)?)?)?(?::|$)/i
|
|
92
|
+
|
|
93
|
+
function pwshCommandSegmentsText(text) {
|
|
94
|
+
let quote = ''
|
|
95
|
+
const parts = []
|
|
96
|
+
let start = 0
|
|
97
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
98
|
+
const ch = text[i]
|
|
99
|
+
if (ch === '\u0060' && quote !== "'") {
|
|
100
|
+
const newlineLength = text.startsWith('\r\n', i + 1) ? 2 : /[\r\n]/.test(text[i + 1] ?? '') ? 1 : 0
|
|
101
|
+
if (newlineLength !== 0) {
|
|
102
|
+
parts.push(text.slice(start, i))
|
|
103
|
+
start = i + 1 + newlineLength
|
|
104
|
+
i = start - 1
|
|
105
|
+
} else i += 1
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
if (quote !== '') { if (ch === quote) quote = ''; continue }
|
|
109
|
+
if (ch === "'" || ch === '"') { quote = ch; continue }
|
|
110
|
+
// A hash inside a bare argument (such as a URL fragment) is not a line comment.
|
|
111
|
+
const lineComment = ch === '#' && (i === 0 || /[\s;|&(){}]/.test(text[i - 1]))
|
|
112
|
+
const blockComment = ch === '<' && text[i + 1] === '#'
|
|
113
|
+
if (!lineComment && !blockComment) continue
|
|
114
|
+
parts.push(text.slice(start, i), ' ')
|
|
115
|
+
if (blockComment) {
|
|
116
|
+
const end = text.indexOf('#>', i + 2)
|
|
117
|
+
i = end < 0 ? text.length : end + 1
|
|
118
|
+
} else {
|
|
119
|
+
while (i + 1 < text.length && !/[\r\n]/.test(text[i + 1])) i += 1
|
|
120
|
+
}
|
|
121
|
+
start = i + 1
|
|
122
|
+
}
|
|
123
|
+
parts.push(text.slice(start))
|
|
124
|
+
return parts.join('')
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function hasPwshDownloadOutput(words, start) {
|
|
128
|
+
let depth = 0
|
|
129
|
+
for (let i = start; i < words.length; i += 1) {
|
|
130
|
+
const raw = words[i].raw
|
|
131
|
+
if (depth === 0 && PWSH_DOWNLOAD_OUTPUT.test(raw)) return true
|
|
132
|
+
// Parameters inside a parenthesized expression or script block belong to that expression.
|
|
133
|
+
for (const ch of shellSyntaxText(raw)) {
|
|
134
|
+
if (ch === '(' || ch === '{') depth += 1
|
|
135
|
+
else if (ch === ')' || ch === '}') {
|
|
136
|
+
if (depth === 0) return false
|
|
137
|
+
depth -= 1
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return false
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function splitShellSegments(text) {
|
|
145
|
+
const segments = []
|
|
146
|
+
let start = 0
|
|
147
|
+
let quote = ''
|
|
148
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
149
|
+
const ch = text[i]
|
|
150
|
+
if (quote !== '') {
|
|
151
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && i + 1 < text.length) { i += 1; continue }
|
|
152
|
+
if (ch === quote) quote = ''
|
|
153
|
+
continue
|
|
154
|
+
}
|
|
155
|
+
if (ch === "'" || ch === '"') { quote = ch; continue }
|
|
156
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && i + 1 < text.length) { i += 1; continue }
|
|
157
|
+
if (ch === ';' || ch === '\n' || ch === '\r') {
|
|
158
|
+
segments.push(text.slice(start, i))
|
|
159
|
+
start = i + 1
|
|
160
|
+
continue
|
|
161
|
+
}
|
|
162
|
+
if (ch === '&' || ch === '|') {
|
|
163
|
+
segments.push(text.slice(start, i))
|
|
164
|
+
if (text[i + 1] === ch) i += 1
|
|
165
|
+
start = i + 1
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
segments.push(text.slice(start))
|
|
169
|
+
return segments
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function shellParenEnd(text, open) {
|
|
173
|
+
let depth = 0
|
|
174
|
+
let quote = ''
|
|
175
|
+
for (let i = open; i < text.length; i += 1) {
|
|
176
|
+
const ch = text[i]
|
|
177
|
+
if (quote !== '') {
|
|
178
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && i + 1 < text.length) { i += 1; continue }
|
|
179
|
+
if (ch === quote) quote = ''
|
|
180
|
+
continue
|
|
181
|
+
}
|
|
182
|
+
if (ch === "'" || ch === '"') { quote = ch; continue }
|
|
183
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && i + 1 < text.length) { i += 1; continue }
|
|
184
|
+
if (ch === '(') depth += 1
|
|
185
|
+
else if (ch === ')') {
|
|
186
|
+
depth -= 1
|
|
187
|
+
if (depth === 0) return i
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return text.length
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function executableShellSubexpressions(text, isPowerShell) {
|
|
194
|
+
const inners = []
|
|
195
|
+
let quote = ''
|
|
196
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
197
|
+
const ch = text[i]
|
|
198
|
+
if (quote === "'") {
|
|
199
|
+
if (ch === quote) quote = ''
|
|
200
|
+
continue
|
|
201
|
+
}
|
|
202
|
+
if (quote === '"') {
|
|
203
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && i + 1 < text.length) { i += 1; continue }
|
|
204
|
+
if (ch === quote) { quote = ''; continue }
|
|
205
|
+
} else {
|
|
206
|
+
if (ch === "'" || ch === '"') { quote = ch; continue }
|
|
207
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && i + 1 < text.length) { i += 1; continue }
|
|
208
|
+
}
|
|
209
|
+
if (ch !== '$' && !(isPowerShell && ch === '@')) continue
|
|
210
|
+
let open = i + 1
|
|
211
|
+
while (open < text.length && /\s/.test(text[open])) open += 1
|
|
212
|
+
if (text[open] !== '(') continue
|
|
213
|
+
const close = shellParenEnd(text, open)
|
|
214
|
+
if (close > open && close <= text.length) inners.push(text.slice(open + 1, close < text.length ? close : text.length))
|
|
215
|
+
if (close < text.length) i = close
|
|
216
|
+
}
|
|
217
|
+
return inners
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function shellSyntaxText(text) {
|
|
221
|
+
const chars = text.split('')
|
|
222
|
+
let quote = ''
|
|
223
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
224
|
+
const ch = text[i]
|
|
225
|
+
if (quote !== '') {
|
|
226
|
+
chars[i] = ' '
|
|
227
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && i + 1 < text.length) { chars[i + 1] = ' '; i += 1; continue }
|
|
228
|
+
if (ch === quote) quote = ''
|
|
229
|
+
continue
|
|
230
|
+
}
|
|
231
|
+
if (ch === "'" || ch === '"') { quote = ch; chars[i] = ' '; continue }
|
|
232
|
+
if ((ch === '\\' || ch === String.fromCharCode(96)) && i + 1 < text.length) { chars[i] = ' '; chars[i + 1] = ' '; i += 1 }
|
|
233
|
+
}
|
|
234
|
+
return chars.join('')
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const WRAPPER_OPTION_WITH_VALUE = new Set(['-u', '-g', '-h', '-C', '-p', '--user', '--group', '--chdir', '--host', '--unset', '--prompt'])
|
|
238
|
+
|
|
239
|
+
function isInnerShellWord(word) {
|
|
240
|
+
const normalized = word.toLowerCase()
|
|
241
|
+
return INNER_SHELL_WORDS.has(normalized) || /(?:^|[\\/])cmd\.exe$/.test(normalized)
|
|
242
|
+
}
|
|
243
|
+
function isEnvAssignment(word) {
|
|
244
|
+
return /^[A-Za-z_][A-Za-z0-9_]*=/.test(word)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const PS_ASSIGN_LHS = /^\$[\w:]+$|^\[[\w.]+\]\$?\w+$/
|
|
248
|
+
const PS_ASSIGN_GLUED = /^(?:\$[\w:]+|\[[\w.]+\]\$?\w+)\s*=(.*)$/
|
|
249
|
+
const PS_BLOCK_HEAD_GLUED = /^(?:try|catch|finally|if|elseif|else|foreach|for|while|do|switch)\s*\(/i
|
|
250
|
+
const PS_SUBEXPR = /^(\$\s*\(|@\s*\(|\()(.+)$/
|
|
251
|
+
// redirectEvidence:引号外、左邻非 =- 复合序列(=>、->)、右邻非 & 流合并目标即判写;PowerShell 中裸 > / >> 无条件为重定向运算符、>= 序列词法上即为重定向(比较须用 -gt/-ge),右邻纯数字如 $i > 4、$i > 0 同样判写;
|
|
252
|
+
// 圆括号深度不参与判定——$(Get-Date > f.txt) 等子表达式内重定向仍判写。
|
|
253
|
+
function redirectEvidence(text) {
|
|
254
|
+
let quote = ''
|
|
255
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
256
|
+
const ch = text[i]
|
|
257
|
+
if (quote !== '') { if ((ch === '\\' || ch === '`') && i + 1 < text.length) i += 1; else if (ch === quote) quote = ''; continue }
|
|
258
|
+
if (ch === '"' || ch === "'") { quote = ch; continue }
|
|
259
|
+
if (ch !== '>') continue
|
|
260
|
+
let left = i - 1
|
|
261
|
+
while (left >= 0 && /\s/.test(text[left])) left -= 1
|
|
262
|
+
if (left >= 0 && '=-'.indexOf(text[left]) !== -1) continue
|
|
263
|
+
let j = i + (text[i + 1] === '>' ? 1 : 0)
|
|
264
|
+
let right = j + 1
|
|
265
|
+
while (right < text.length && /\s/.test(text[right])) right += 1
|
|
266
|
+
if (right >= text.length || text[right] === '&') continue
|
|
267
|
+
return { rule: 'PWSH_MUTATION', value: '' }
|
|
268
|
+
}
|
|
269
|
+
return null
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// 块头关键字按 shell 语法比较:PowerShell 不区分大小写,bash 保留区分大小写;其后应跟块体左花括号。
|
|
273
|
+
const SHELL_BLOCK_HEAD_WORDS = new Set(['try', 'catch', 'finally', 'if', 'elseif', 'else', 'foreach', 'for', 'while', 'do', 'switch'])
|
|
274
|
+
// 段首块关键字(整词等值比较):以段首形式出现、其后不跟花括号,其后仍是命令位。
|
|
275
|
+
const SHELL_BLOCK_SEGMENT_WORDS = new Set(['then', 'do', 'else', 'elif', 'fi', 'done', 'esac', 'in'])
|
|
276
|
+
|
|
277
|
+
function syntaxRuleOf(syntaxRe) {
|
|
278
|
+
if (syntaxRe === PWSH_MUTATION) return 'PWSH_MUTATION'
|
|
279
|
+
if (syntaxRe === BASH_MUTATION) return 'BASH_MUTATION'
|
|
280
|
+
return null
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function bareRuleOf(bareRe) {
|
|
284
|
+
if (bareRe === PWSH_BARE_WORDS) return 'PWSH_BARE_WORDS'
|
|
285
|
+
if (bareRe === BASH_BARE_WORDS) return 'BASH_BARE_WORDS'
|
|
286
|
+
return null
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function mutationTextEvidence(text, syntaxRe, bareRe, depth) {
|
|
290
|
+
if (text === '' || depth >= 4) return null
|
|
291
|
+
const isPowerShell = syntaxRe === PWSH_MUTATION || bareRe === PWSH_BARE_WORDS
|
|
292
|
+
const segmentsText = isPowerShell ? pwshCommandSegmentsText(text) : text
|
|
293
|
+
if (syntaxRe.test(shellSyntaxText(segmentsText))) {
|
|
294
|
+
const rule = syntaxRuleOf(syntaxRe)
|
|
295
|
+
return rule === null ? null : { rule, value: '' }
|
|
296
|
+
}
|
|
297
|
+
if (isPowerShell) {
|
|
298
|
+
const redirect = redirectEvidence(segmentsText)
|
|
299
|
+
if (redirect !== null) return redirect
|
|
300
|
+
}
|
|
301
|
+
const nestedEvidenceOf = (inner) => {
|
|
302
|
+
const nestedEvidence = mutationTextEvidence(inner, syntaxRe, bareRe, depth + 1)
|
|
303
|
+
if (nestedEvidence !== null) return nestedEvidence
|
|
304
|
+
if (syntaxRe !== PWSH_MUTATION || bareRe !== PWSH_BARE_WORDS) {
|
|
305
|
+
const pwshEvidence = mutationTextEvidence(inner, PWSH_MUTATION, PWSH_BARE_WORDS, depth + 1)
|
|
306
|
+
if (pwshEvidence !== null) return pwshEvidence
|
|
307
|
+
}
|
|
308
|
+
if (syntaxRe !== BASH_MUTATION || bareRe !== BASH_BARE_WORDS) {
|
|
309
|
+
const bashEvidence = mutationTextEvidence(inner, BASH_MUTATION, BASH_BARE_WORDS, depth + 1)
|
|
310
|
+
if (bashEvidence !== null) return bashEvidence
|
|
311
|
+
}
|
|
312
|
+
return null
|
|
313
|
+
}
|
|
314
|
+
for (const inner of executableShellSubexpressions(segmentsText, isPowerShell)) {
|
|
315
|
+
const nestedEvidence = nestedEvidenceOf(inner)
|
|
316
|
+
if (nestedEvidence !== null) return nestedEvidence
|
|
317
|
+
}
|
|
318
|
+
const segs = splitShellSegments(segmentsText)
|
|
319
|
+
for (const seg of segs) {
|
|
320
|
+
const words = shellWords(seg)
|
|
321
|
+
let index = 0
|
|
322
|
+
let steps = 0
|
|
323
|
+
let expectCommand = true
|
|
324
|
+
let blockPending = false
|
|
325
|
+
let blockDepth = 0
|
|
326
|
+
let functionPending = false
|
|
327
|
+
while (index < words.length && steps < 32) {
|
|
328
|
+
steps += 1
|
|
329
|
+
const token = words[index]
|
|
330
|
+
const word = token.value
|
|
331
|
+
// 函数定义的函数名/参数位不参与裸写判定,函数体左花括号后恢复命令位。
|
|
332
|
+
if (functionPending) {
|
|
333
|
+
if (!token.quoted) {
|
|
334
|
+
const braceAt = word.indexOf('{')
|
|
335
|
+
if (braceAt !== -1) {
|
|
336
|
+
functionPending = false
|
|
337
|
+
blockDepth += 1
|
|
338
|
+
expectCommand = true
|
|
339
|
+
if (braceAt + 1 < word.length) { token.value = word.slice(braceAt + 1); continue }
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
index += 1
|
|
343
|
+
continue
|
|
344
|
+
}
|
|
345
|
+
// 步骤A:块头关键字之后等待块体左花括号;非花括号词一律跳过,不参与裸写动词判定
|
|
346
|
+
if (blockPending) {
|
|
347
|
+
const braceAt = token.quoted ? -1 : word.indexOf('{')
|
|
348
|
+
if (braceAt !== -1) {
|
|
349
|
+
blockPending = false
|
|
350
|
+
blockDepth += 1
|
|
351
|
+
expectCommand = true
|
|
352
|
+
if (braceAt + 1 < word.length) { token.value = word.slice(braceAt + 1); continue }
|
|
353
|
+
}
|
|
354
|
+
index += 1
|
|
355
|
+
continue
|
|
356
|
+
}
|
|
357
|
+
// 步骤B:块体右花括号只收尾(块深度出栈);真实块体收尾后回到命令位,
|
|
358
|
+
// 以便识别同一段内紧随其后的 catch/finally/else/elseif/while 等续块关键字
|
|
359
|
+
// (否则 try { … } catch { Remove-Item x } 的 catch 块会漏拦)。
|
|
360
|
+
if (word === '}') {
|
|
361
|
+
if (blockDepth > 0) { blockDepth -= 1; expectCommand = true }
|
|
362
|
+
index += 1
|
|
363
|
+
continue
|
|
364
|
+
}
|
|
365
|
+
// 步骤C:块体左花括号之后恢复命令位(switch/case 内嵌块、无关键字块等);
|
|
366
|
+
// 只有「块体」左花括号算数——已在块内或正处于命令位时的 { 才开块,
|
|
367
|
+
// 参数位的花括号(如 echo { rm })按普通参数放行,不恢复命令位。
|
|
368
|
+
if (!token.quoted && word.startsWith('{')) {
|
|
369
|
+
if (blockDepth > 0 || expectCommand) {
|
|
370
|
+
blockDepth += 1
|
|
371
|
+
expectCommand = true
|
|
372
|
+
if (word.length > 1) { token.value = word.slice(1); continue }
|
|
373
|
+
}
|
|
374
|
+
index += 1
|
|
375
|
+
continue
|
|
376
|
+
}
|
|
377
|
+
// 步骤D:只有命令位才参与裸写动词判定
|
|
378
|
+
if (expectCommand) {
|
|
379
|
+
if (!token.quoted && word.toLowerCase() === 'function') {
|
|
380
|
+
functionPending = true
|
|
381
|
+
expectCommand = false
|
|
382
|
+
index += 1
|
|
383
|
+
continue
|
|
384
|
+
}
|
|
385
|
+
if (!isPowerShell && !token.quoted) {
|
|
386
|
+
const joinedFunction = /^[A-Za-z_][A-Za-z0-9_]*\(\)(?:\{.*)?$/.test(word)
|
|
387
|
+
const splitFunction = /^[A-Za-z_][A-Za-z0-9_]*$/.test(word) && words[index + 1]?.value === '()' && words[index + 2]?.value?.startsWith('{')
|
|
388
|
+
if (joinedFunction || splitFunction) {
|
|
389
|
+
const braceAt = word.indexOf('{')
|
|
390
|
+
if (braceAt !== -1) {
|
|
391
|
+
blockDepth += 1
|
|
392
|
+
expectCommand = true
|
|
393
|
+
if (braceAt + 1 < word.length) { token.value = word.slice(braceAt + 1); continue }
|
|
394
|
+
} else {
|
|
395
|
+
functionPending = true
|
|
396
|
+
expectCommand = false
|
|
397
|
+
index += splitFunction ? 2 : 1
|
|
398
|
+
continue
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (isPowerShell) {
|
|
403
|
+
if (PS_ASSIGN_LHS.test(word) && index + 1 < words.length && words[index + 1].value === '=') { index += 2; continue }
|
|
404
|
+
const gluedAssign = PS_ASSIGN_GLUED.exec(word)
|
|
405
|
+
if (gluedAssign !== null) {
|
|
406
|
+
if (gluedAssign[1] !== '') { token.value = gluedAssign[1]; continue }
|
|
407
|
+
index += 1
|
|
408
|
+
continue
|
|
409
|
+
}
|
|
410
|
+
const subExpr = token.quoted ? null : PS_SUBEXPR.exec(word)
|
|
411
|
+
if (subExpr !== null && subExpr[2] !== '') { token.value = subExpr[2]; continue }
|
|
412
|
+
}
|
|
413
|
+
if (isEnvAssignment(word)) { index += 1; continue }
|
|
414
|
+
if (SHELL_PREFIX_WORDS.has(word)) {
|
|
415
|
+
index += 1
|
|
416
|
+
while (index < words.length) {
|
|
417
|
+
const optionToken = words[index]
|
|
418
|
+
const option = optionToken.value
|
|
419
|
+
if (isEnvAssignment(option)) { index += 1; continue }
|
|
420
|
+
if (option === '--') { index += 1; break }
|
|
421
|
+
if (!option.startsWith('-')) break
|
|
422
|
+
if (WRAPPER_OPTION_WITH_VALUE.has(option) && index + 1 < words.length) index += 2
|
|
423
|
+
else index += 1
|
|
424
|
+
}
|
|
425
|
+
continue
|
|
426
|
+
}
|
|
427
|
+
if (isInnerShellWord(word)) {
|
|
428
|
+
let commandIndex = index + 1
|
|
429
|
+
while (commandIndex < words.length && !/^(?:-Command|-c|\/c)$/i.test(words[commandIndex].value)) commandIndex += 1
|
|
430
|
+
if (commandIndex + 1 < words.length) {
|
|
431
|
+
const inner = words[commandIndex + 1].value
|
|
432
|
+
const nestedEvidence = mutationTextEvidence(inner, syntaxRe, bareRe, depth + 1)
|
|
433
|
+
if (nestedEvidence !== null) return nestedEvidence
|
|
434
|
+
const pwshEvidence = mutationTextEvidence(inner, PWSH_MUTATION, PWSH_BARE_WORDS, depth + 1)
|
|
435
|
+
if (pwshEvidence !== null) return pwshEvidence
|
|
436
|
+
const bashEvidence = mutationTextEvidence(inner, BASH_MUTATION, BASH_BARE_WORDS, depth + 1)
|
|
437
|
+
if (bashEvidence !== null) return bashEvidence
|
|
438
|
+
}
|
|
439
|
+
break
|
|
440
|
+
}
|
|
441
|
+
// 段首块关键字先于块头关键字判定:do/else 同属两集合,而它们后面通常直接跟命令
|
|
442
|
+
// (bash 的 while true; do touch f; done、if …; else rm x; fi),按块头处理会把
|
|
443
|
+
// 其后整段命令吞掉漏拦;两者都只把后面的词留在命令位。
|
|
444
|
+
const blockWord = isPowerShell ? word.toLowerCase() : word
|
|
445
|
+
if (SHELL_BLOCK_SEGMENT_WORDS.has(blockWord)) { index += 1; continue }
|
|
446
|
+
if (SHELL_BLOCK_HEAD_WORDS.has(blockWord)) { blockPending = true; index += 1; continue }
|
|
447
|
+
if (isPowerShell && !token.quoted && PS_BLOCK_HEAD_GLUED.test(word)) {
|
|
448
|
+
const braceAt = token.quoted ? -1 : word.indexOf('{')
|
|
449
|
+
if (braceAt !== -1) { blockDepth += 1; expectCommand = true; if (braceAt + 1 < word.length) { token.value = word.slice(braceAt + 1); continue } }
|
|
450
|
+
else { blockPending = true }
|
|
451
|
+
index += 1
|
|
452
|
+
continue
|
|
453
|
+
}
|
|
454
|
+
if (isPowerShell && word.toLowerCase() === 'return') { index += 1; continue }
|
|
455
|
+
if (token.quoted) { index += 1; continue }
|
|
456
|
+
if (isPowerShell && PWSH_DOWNLOAD_COMMANDS.has(word.toLowerCase()) && hasPwshDownloadOutput(words, index + 1)) {
|
|
457
|
+
return { rule: 'PWSH_MUTATION', value: '' }
|
|
458
|
+
}
|
|
459
|
+
const bareMatch = bareRe.exec(word)
|
|
460
|
+
if (bareMatch !== null) {
|
|
461
|
+
const rule = bareRuleOf(bareRe)
|
|
462
|
+
const value = bareMatch[1]
|
|
463
|
+
if (rule !== null && typeof value === 'string') return { rule, value }
|
|
464
|
+
}
|
|
465
|
+
expectCommand = false
|
|
466
|
+
index += 1
|
|
467
|
+
continue
|
|
468
|
+
}
|
|
469
|
+
// 步骤E:非命令位(参数位)整词一律放行
|
|
470
|
+
index += 1
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return null
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function mutationEvidence(commandOf, exec, syntaxRe, bareRe) {
|
|
477
|
+
const cmd = commandOf(exec)
|
|
478
|
+
return cmd !== '' ? mutationTextEvidence(cmd, syntaxRe, bareRe, 0) : null
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
export function pwshMutationEvidence(exec) { return mutationEvidence(pwshCommandOf, exec, PWSH_MUTATION, PWSH_BARE_WORDS) }
|
|
482
|
+
export function bashMutationEvidence(exec) { return mutationEvidence(bashCommandOf, exec, BASH_MUTATION, BASH_BARE_WORDS) }
|
|
483
|
+
export function pwshMutationMatches(exec) { return pwshMutationEvidence(exec) !== null }
|
|
484
|
+
export function bashMutationMatches(exec) { return bashMutationEvidence(exec) !== null }
|