dsh-chief 1.3.0 → 1.5.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.
@@ -0,0 +1,95 @@
1
+ /**
2
+ * safety/audit-log.js
3
+ *
4
+ * Independent append-only audit ledger (`chief-audit.jsonl`), secret redaction,
5
+ * and structured audit record normalization for DSH Chief.
6
+ */
7
+
8
+ import fs from 'node:fs'
9
+ import os from 'node:os'
10
+ import path from 'node:path'
11
+
12
+ export const SECRET_REDACT_PATTERN = /(?:sk-[a-zA-Z0-9_-]{16,}|ghp_[a-zA-Z0-9]{20,}|AKIA[0-9A-Z]{16}|AIza[0-9A-Za-z_-]{30,}|Bearer\s+[A-Za-z0-9._~+/=-]{20,}|-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$))/g
13
+
14
+ let auditIdSeq = 0
15
+
16
+ function nextAuditId() {
17
+ auditIdSeq = (auditIdSeq + 1) % 1_000_000
18
+ return 'aud-' + Date.now().toString(36) + '-' + auditIdSeq.toString(36)
19
+ }
20
+
21
+ function clampSingleLine(value, max) {
22
+ const text = String(value == null ? '' : value).replace(/\s+/g, ' ').trim()
23
+ if (text.length <= max) return text
24
+ return text.slice(0, Math.max(0, max - 1)) + '…'
25
+ }
26
+
27
+ export function redactAuditText(text, maxLen = 220) {
28
+ const raw = String(text == null ? '' : text)
29
+ if (!raw) return ''
30
+ const redacted = raw.replace(SECRET_REDACT_PATTERN, '[REDACTED]')
31
+ return clampSingleLine(redacted, maxLen)
32
+ }
33
+
34
+ export function auditJsonlPath() {
35
+ const home = process.env.DSH_HOME || path.join(os.homedir(), '.dsh')
36
+ return path.join(home, 'chief', 'chief-audit.jsonl')
37
+ }
38
+
39
+ export function normalizeAuditRecord(raw, defaultMode = 'advisor') {
40
+ if (!raw || typeof raw !== 'object') return null
41
+ const op = clampSingleLine(raw.operation || raw.tool || '', 80)
42
+ if (!op) return null
43
+ const modeRaw = String(raw.mode || defaultMode || 'advisor').trim().toLowerCase()
44
+ const mode = ['advisor', 'assistant', 'strategist', 'chief'].includes(modeRaw) ? modeRaw : 'advisor'
45
+ const rawDuration = Number(raw.durationMs)
46
+ const durationMs = Number.isFinite(rawDuration) && rawDuration >= 0 ? Math.round(rawDuration) : 0
47
+ const reason = redactAuditText(raw.reason || '', 220)
48
+ const summary = redactAuditText(raw.summary || raw.reason || '', 220)
49
+ return {
50
+ id: typeof raw.id === 'string' && raw.id ? raw.id : nextAuditId(),
51
+ at: Number(raw.at) || Date.now(),
52
+ mode,
53
+ stage: clampSingleLine(raw.stage || 'executor', 40),
54
+ rule: clampSingleLine(raw.rule || '', 80),
55
+ decision: clampSingleLine(raw.decision || 'executed', 40),
56
+ operation: op,
57
+ target: redactAuditText(raw.target || '', 140),
58
+ summary,
59
+ reason,
60
+ outcome: clampSingleLine(raw.outcome || 'ok', 40),
61
+ durationMs,
62
+ by: clampSingleLine(raw.by || 'chief', 40),
63
+ }
64
+ }
65
+
66
+ export function appendAuditJsonlRecord(record, sessionId = '') {
67
+ try {
68
+ const file = auditJsonlPath()
69
+ fs.mkdirSync(path.dirname(file), { recursive: true })
70
+ const payload = sessionId ? { sessionId: String(sessionId), ...record } : record
71
+ fs.appendFileSync(file, JSON.stringify(payload) + '\n', 'utf8')
72
+ } catch {}
73
+ }
74
+
75
+ export function readRecentAuditJsonl({ sessionId = '', limit = 80 } = {}) {
76
+ const file = auditJsonlPath()
77
+ if (!fs.existsSync(file)) return []
78
+ try {
79
+ const raw = fs.readFileSync(file, 'utf8')
80
+ const lines = raw.split(/\r?\n/).filter(Boolean)
81
+ const cap = Math.max(1, Math.min(500, Number(limit) || 80))
82
+ const out = []
83
+ for (let i = lines.length - 1; i >= 0 && out.length < cap; i -= 1) {
84
+ try {
85
+ const parsed = JSON.parse(lines[i])
86
+ if (!parsed || typeof parsed !== 'object') continue
87
+ if (sessionId && parsed.sessionId && parsed.sessionId !== sessionId) continue
88
+ out.push(parsed)
89
+ } catch {}
90
+ }
91
+ return out.reverse()
92
+ } catch {
93
+ return []
94
+ }
95
+ }
@@ -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
+ }