dsh-chief 1.3.0 → 1.4.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/CHANGELOG.md +42 -12
- package/README.en.md +143 -116
- package/README.md +134 -142
- package/api-routes.js +19 -2
- package/board-actions.js +72 -1
- package/board-schema.js +1 -1
- package/chief-approvals.js +241 -81
- package/chief-archives.js +35 -0
- package/chief-commands.js +615 -108
- package/chief-executor.js +279 -555
- package/chief-network.js +157 -8
- package/chief-review.js +33 -1
- package/chief-state.js +497 -81
- package/chief-view.js +21 -6
- package/client.js +1953 -506
- package/model-tools.js +78 -2
- package/package.json +35 -24
- package/runtime-hooks.js +108 -2
- package/safety/audit-log.js +95 -0
- package/safety/command-analyzer.js +339 -0
- package/safety/guard-walls.js +993 -0
- package/safety/index.js +78 -0
- package/safety/invariant-guards.js +280 -0
- package/safety/path-guard.js +227 -0
- package/safety/policy-engine.js +387 -0
- package/safety/policy-plugin.js +391 -0
- package/safety/tool-gate.js +278 -0
- package/subagent-runtime.js +9 -2
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* safety/command-analyzer.js
|
|
3
|
+
*
|
|
4
|
+
* Security command normalization, compound pipeline splitting, nested shell
|
|
5
|
+
* unwrapping, and inline script payload extraction for DSH Chief.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export function decodeEncodedPowerShell(command) {
|
|
9
|
+
const decodedParts = []
|
|
10
|
+
const raw = String(command || '')
|
|
11
|
+
const re = /(?:^|\s)-(?:e|ec|en|enc|enco|encod|encode|encoded|encodedc|encodedco|encodedcom|encodedcomm|encodedcomma|encodedcomman|encodedcommand)\s+([A-Za-z0-9+/=]{8,})/gi
|
|
12
|
+
let m
|
|
13
|
+
while ((m = re.exec(raw)) !== null) {
|
|
14
|
+
try {
|
|
15
|
+
const buf = Buffer.from(m[1], 'base64')
|
|
16
|
+
for (const enc of ['utf16le', 'utf8']) {
|
|
17
|
+
const rawTxt = buf.toString(enc)
|
|
18
|
+
if (/[\x00-\x08\x0e-\x1f\ufffd]/.test(rawTxt)) continue
|
|
19
|
+
const txt = rawTxt.trim()
|
|
20
|
+
if (txt.length >= 3 && /^[ -~\t\r\n\u4e00-\u9fff]+$/.test(txt)) {
|
|
21
|
+
decodedParts.push(txt)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
} catch {}
|
|
25
|
+
}
|
|
26
|
+
return decodedParts.join(' ')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function extractCanonicalToolSubcommands(text) {
|
|
30
|
+
const extras = []
|
|
31
|
+
const s = String(text || '')
|
|
32
|
+
const gitValFlags = new Set(['-c', '--git-dir', '--work-tree', '--namespace', '--config-env', '--exec-path', '-p', '--paginate'])
|
|
33
|
+
const gitRe = /\bgit(?:\.exe)?\s+([^\n;|&`]+)/gi
|
|
34
|
+
let gm
|
|
35
|
+
while ((gm = gitRe.exec(s)) !== null) {
|
|
36
|
+
const tokens = gm[1].trim().split(/\s+/).filter(Boolean)
|
|
37
|
+
let i = 0
|
|
38
|
+
while (i < tokens.length) {
|
|
39
|
+
const tok = tokens[i]
|
|
40
|
+
if (tok === '-C' || gitValFlags.has(tok)) {
|
|
41
|
+
i += 2
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
if (tok.startsWith('-')) {
|
|
45
|
+
i += 1
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
break
|
|
49
|
+
}
|
|
50
|
+
if (i < tokens.length) {
|
|
51
|
+
const rest = tokens.slice(i).join(' ')
|
|
52
|
+
extras.push(`git ${tokens[i]}`, `git ${rest}`)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const npmRe = /\bnpm(?:\.cmd|\.exe)?\s+([^\n;|&`]+)/gi
|
|
56
|
+
let nm
|
|
57
|
+
while ((nm = npmRe.exec(s)) !== null) {
|
|
58
|
+
const tokens = nm[1].trim().split(/\s+/).filter(Boolean)
|
|
59
|
+
let i = 0
|
|
60
|
+
while (i < tokens.length && tokens[i].startsWith('-')) {
|
|
61
|
+
i += (tokens[i] === '--registry' || tokens[i] === '--tag' || tokens[i] === '--prefix') ? 2 : 1
|
|
62
|
+
}
|
|
63
|
+
if (i < tokens.length) {
|
|
64
|
+
extras.push(`npm ${tokens.slice(i).join(' ')}`)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return extras.join(' ')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function normalizeCommandForSecurity(command) {
|
|
71
|
+
const nfkc = String(command || '').normalize('NFKC')
|
|
72
|
+
if (!nfkc) return ''
|
|
73
|
+
const psDecoded = decodeEncodedPowerShell(nfkc)
|
|
74
|
+
const base = psDecoded ? `${nfkc} ${psDecoded}` : nfkc
|
|
75
|
+
const concatCollapsed = base.replace(/(['"`])\s*\+\s*\1/g, '')
|
|
76
|
+
const escapedQuotesUnwrapped = concatCollapsed
|
|
77
|
+
.replace(/\\+(["'`])/g, '$1')
|
|
78
|
+
.replace(/(["'`])\\+(?=[a-zA-Z0-9_.-])/g, '$1')
|
|
79
|
+
.replace(/["'`]/g, '')
|
|
80
|
+
const caretCollapsed = escapedQuotesUnwrapped.replace(/\^([a-zA-Z0-9_.-])/g, '$1')
|
|
81
|
+
const quoteStripped = concatCollapsed.replace(/["'`]/g, '')
|
|
82
|
+
const shellUnescaped = caretCollapsed.replace(/\\+([ \t!$&()*,.;<>?@[\]^`{|}~])/g, '$1')
|
|
83
|
+
const spaceCollapsed = shellUnescaped.replace(/\s+/g, ' ').trim()
|
|
84
|
+
const canonicalTools = [
|
|
85
|
+
extractCanonicalToolSubcommands(spaceCollapsed),
|
|
86
|
+
extractCanonicalToolSubcommands(caretCollapsed.replace(/\s+/g, ' ').trim()),
|
|
87
|
+
].filter(Boolean).join(' ')
|
|
88
|
+
return [nfkc, psDecoded, concatCollapsed, quoteStripped, escapedQuotesUnwrapped, caretCollapsed, spaceCollapsed, canonicalTools]
|
|
89
|
+
.filter(Boolean)
|
|
90
|
+
.join(' \n ')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function unwrapOuterQuotes(text) {
|
|
94
|
+
const s = String(text || '').trim()
|
|
95
|
+
if (s.length >= 2) {
|
|
96
|
+
const first = s[0]
|
|
97
|
+
const last = s[s.length - 1]
|
|
98
|
+
if ((first === '"' && last === '"') || (first === "'" && last === "'") || (first === '`' && last === '`')) {
|
|
99
|
+
return s.slice(1, -1).trim()
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return s
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function splitTopLevelShellSegments(input) {
|
|
106
|
+
const s = String(input || '')
|
|
107
|
+
const segments = []
|
|
108
|
+
let cur = ''
|
|
109
|
+
let quote = ''
|
|
110
|
+
let escaped = false
|
|
111
|
+
|
|
112
|
+
const flush = () => {
|
|
113
|
+
const trimmed = cur.trim()
|
|
114
|
+
if (trimmed) segments.push(trimmed)
|
|
115
|
+
cur = ''
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for (let i = 0; i < s.length; i += 1) {
|
|
119
|
+
const ch = s[i]
|
|
120
|
+
const next = i + 1 < s.length ? s[i + 1] : ''
|
|
121
|
+
const prev = i > 0 ? s[i - 1] : ''
|
|
122
|
+
|
|
123
|
+
if (escaped) {
|
|
124
|
+
cur += ch
|
|
125
|
+
escaped = false
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
if (ch === '\\' && quote !== "'") {
|
|
129
|
+
cur += ch
|
|
130
|
+
escaped = true
|
|
131
|
+
continue
|
|
132
|
+
}
|
|
133
|
+
if (quote) {
|
|
134
|
+
cur += ch
|
|
135
|
+
if (ch === quote) quote = ''
|
|
136
|
+
continue
|
|
137
|
+
}
|
|
138
|
+
if (ch === '"' || ch === "'" || ch === '`') {
|
|
139
|
+
quote = ch
|
|
140
|
+
cur += ch
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (ch === '\r' || ch === '\n' || ch === ';') {
|
|
145
|
+
flush()
|
|
146
|
+
continue
|
|
147
|
+
}
|
|
148
|
+
if ((ch === '&' && next === '&') || (ch === '|' && next === '|')) {
|
|
149
|
+
flush()
|
|
150
|
+
i += 1
|
|
151
|
+
continue
|
|
152
|
+
}
|
|
153
|
+
if (ch === '|') {
|
|
154
|
+
flush()
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
if (ch === '&') {
|
|
158
|
+
// Preserve fd redirections like `2>&1`, `>&2`, `&>`
|
|
159
|
+
if (prev === '>' || prev === '<' || next === '>') {
|
|
160
|
+
cur += ch
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
flush()
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
cur += ch
|
|
167
|
+
}
|
|
168
|
+
flush()
|
|
169
|
+
return segments
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function splitCompoundCommand(command, maxDepth = 4) {
|
|
173
|
+
const out = []
|
|
174
|
+
const seen = new Set()
|
|
175
|
+
|
|
176
|
+
const walk = (raw, depth) => {
|
|
177
|
+
const text = String(raw || '').normalize('NFKC').trim()
|
|
178
|
+
if (!text || depth > maxDepth) return
|
|
179
|
+
const psDecoded = decodeEncodedPowerShell(text)
|
|
180
|
+
if (psDecoded) walk(psDecoded, depth + 1)
|
|
181
|
+
|
|
182
|
+
const segs = splitTopLevelShellSegments(text)
|
|
183
|
+
for (const seg of segs) {
|
|
184
|
+
if (!seen.has(seg)) {
|
|
185
|
+
seen.add(seg)
|
|
186
|
+
out.push(seg)
|
|
187
|
+
}
|
|
188
|
+
if (depth >= maxDepth) continue
|
|
189
|
+
|
|
190
|
+
const cmdMatch = seg.match(/^(?:call\s+)?cmd(?:\.exe)?\s+\/[ck]\s+([\s\S]+)$/i)
|
|
191
|
+
if (cmdMatch && cmdMatch[1]) {
|
|
192
|
+
walk(unwrapOuterQuotes(cmdMatch[1]), depth + 1)
|
|
193
|
+
}
|
|
194
|
+
const psMatch = seg.match(/^(?:powershell|pwsh)(?:\.exe)?\b(?:\s+-[a-zA-Z0-9_-]+(?:\s+(?!-)[a-zA-Z0-9_.-]+)?)*?\s+-(?:c|command)\s+([\s\S]+)$/i)
|
|
195
|
+
if (psMatch && psMatch[1]) {
|
|
196
|
+
walk(unwrapOuterQuotes(psMatch[1]), depth + 1)
|
|
197
|
+
}
|
|
198
|
+
const shMatch = seg.match(/^(?:bash|sh|zsh|dash)(?:\.exe)?\s+-[a-z]*c\s+([\s\S]+)$/i)
|
|
199
|
+
if (shMatch && shMatch[1]) {
|
|
200
|
+
walk(unwrapOuterQuotes(shMatch[1]), depth + 1)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
walk(command, 0)
|
|
206
|
+
return out
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function decodeBase64Literals(scriptText) {
|
|
210
|
+
const decoded = []
|
|
211
|
+
const s = String(scriptText || '')
|
|
212
|
+
const patterns = [
|
|
213
|
+
/Buffer\.from\s*\(\s*['"]([A-Za-z0-9+/=]{4,})['"]\s*,\s*['"]base64['"]\s*\)/gi,
|
|
214
|
+
/\batob\s*\(\s*['"]([A-Za-z0-9+/=]{4,})['"]\s*\)/gi,
|
|
215
|
+
/FromBase64String\s*\(\s*['"]([A-Za-z0-9+/=]{4,})['"]\s*\)/gi,
|
|
216
|
+
]
|
|
217
|
+
for (const re of patterns) {
|
|
218
|
+
let m
|
|
219
|
+
while ((m = re.exec(s)) !== null) {
|
|
220
|
+
try {
|
|
221
|
+
const buf = Buffer.from(m[1], 'base64')
|
|
222
|
+
for (const enc of ['utf8', 'utf16le']) {
|
|
223
|
+
const txt = buf.toString(enc).trim()
|
|
224
|
+
if (txt.length >= 2 && !/[\x00-\x08\x0e-\x1f\ufffd]/.test(txt)) {
|
|
225
|
+
decoded.push(txt)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} catch {}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return decoded
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function readQuotedOrRestArg(text, startIdx) {
|
|
235
|
+
let i = startIdx
|
|
236
|
+
while (i < text.length && /\s/.test(text[i])) i += 1
|
|
237
|
+
if (i >= text.length) return ''
|
|
238
|
+
const first = text[i]
|
|
239
|
+
if (first === '"' || first === "'" || first === '`') {
|
|
240
|
+
let escaped = false
|
|
241
|
+
let body = ''
|
|
242
|
+
for (let j = i + 1; j < text.length; j += 1) {
|
|
243
|
+
const ch = text[j]
|
|
244
|
+
if (escaped) {
|
|
245
|
+
body += ch
|
|
246
|
+
escaped = false
|
|
247
|
+
continue
|
|
248
|
+
}
|
|
249
|
+
if (ch === '\\' && first !== "'") {
|
|
250
|
+
body += ch
|
|
251
|
+
escaped = true
|
|
252
|
+
continue
|
|
253
|
+
}
|
|
254
|
+
if (ch === first) {
|
|
255
|
+
return body
|
|
256
|
+
}
|
|
257
|
+
body += ch
|
|
258
|
+
}
|
|
259
|
+
return body
|
|
260
|
+
}
|
|
261
|
+
return text.slice(i).trim()
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function extractInlineScriptPayloads(command) {
|
|
265
|
+
const segments = splitCompoundCommand(command)
|
|
266
|
+
const payloads = []
|
|
267
|
+
const seen = new Set()
|
|
268
|
+
let hasInlineExec = false
|
|
269
|
+
let hasInlineSubprocess = false
|
|
270
|
+
|
|
271
|
+
const addPayload = (rawBody) => {
|
|
272
|
+
const body = String(rawBody || '').trim()
|
|
273
|
+
if (!body) return
|
|
274
|
+
const normalized = body.replace(/(['"`])\s*\+\s*\1/g, '').replace(/\\(["'`])/g, '$1')
|
|
275
|
+
for (const candidate of [body, normalized, ...decodeBase64Literals(body), ...decodeBase64Literals(normalized)]) {
|
|
276
|
+
const c = String(candidate || '').trim()
|
|
277
|
+
if (c && !seen.has(c)) {
|
|
278
|
+
seen.add(c)
|
|
279
|
+
payloads.push(c)
|
|
280
|
+
}
|
|
281
|
+
if (/\b(?:child_process|execSync|execFileSync|spawnSync|os\.system|os\.popen|subprocess\.|Start-Process|ShellExecute)\b/i.test(c)) {
|
|
282
|
+
hasInlineSubprocess = true
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
for (const seg of segments) {
|
|
288
|
+
const inlineMatchers = [
|
|
289
|
+
/\bnode(?:\.exe)?\b[^\n]*?\s(?:--eval|--print|-e|-p)\s+/gi,
|
|
290
|
+
/\b(?:python3?|py)(?:\.exe)?\b[^\n]*?\s-c\s+/gi,
|
|
291
|
+
/\b(?:powershell|pwsh)(?:\.exe)?\b[^\n]*?\s-(?:c|command)\s+/gi,
|
|
292
|
+
/\b(?:deno\s+eval|bun\s+-e|ruby\s+-e|perl\s+-e|php\s+-r)\s+/gi,
|
|
293
|
+
]
|
|
294
|
+
for (const re of inlineMatchers) {
|
|
295
|
+
re.lastIndex = 0
|
|
296
|
+
let m
|
|
297
|
+
while ((m = re.exec(seg)) !== null) {
|
|
298
|
+
hasInlineExec = true
|
|
299
|
+
const argBody = readQuotedOrRestArg(seg, m.index + m[0].length)
|
|
300
|
+
addPayload(argBody)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const iexRe = /\b(?:Invoke-Expression|iex)\b\s*([\s\S]+)$/i
|
|
305
|
+
const iexM = seg.match(iexRe)
|
|
306
|
+
if (iexM && iexM[1]) {
|
|
307
|
+
hasInlineExec = true
|
|
308
|
+
addPayload(unwrapOuterQuotes(iexM[1]))
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
payloads,
|
|
314
|
+
hasInlineExec,
|
|
315
|
+
hasInlineSubprocess,
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function analyzeCommand(command) {
|
|
320
|
+
const raw = String(command || '')
|
|
321
|
+
const segments = splitCompoundCommand(raw)
|
|
322
|
+
const inline = extractInlineScriptPayloads(raw)
|
|
323
|
+
const normalizedSegments = segments.map(seg => normalizeCommandForSecurity(seg)).filter(Boolean)
|
|
324
|
+
const normalizedInline = inline.payloads.map(p => normalizeCommandForSecurity(p)).filter(Boolean)
|
|
325
|
+
const normalized = [
|
|
326
|
+
normalizeCommandForSecurity(raw),
|
|
327
|
+
...normalizedSegments,
|
|
328
|
+
...normalizedInline,
|
|
329
|
+
].filter(Boolean).join(' \n ')
|
|
330
|
+
|
|
331
|
+
return {
|
|
332
|
+
raw,
|
|
333
|
+
normalized,
|
|
334
|
+
segments,
|
|
335
|
+
inlinePayloads: inline.payloads,
|
|
336
|
+
hasInlineExec: inline.hasInlineExec,
|
|
337
|
+
hasInlineSubprocess: inline.hasInlineSubprocess,
|
|
338
|
+
}
|
|
339
|
+
}
|