sentinelayer-cli 0.1.2 → 0.3.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.
Files changed (127) hide show
  1. package/README.md +996 -996
  2. package/bin/create-sentinelayer.js +5 -5
  3. package/bin/sentinelayer-cli.js +4 -4
  4. package/bin/sl.js +5 -5
  5. package/package.json +62 -54
  6. package/src/agents/jules/config/definition.js +209 -209
  7. package/src/agents/jules/config/system-prompt.js +175 -175
  8. package/src/agents/jules/error-intake.js +51 -51
  9. package/src/agents/jules/fix-cycle.js +377 -377
  10. package/src/agents/jules/loop.js +367 -367
  11. package/src/agents/jules/pulse.js +319 -319
  12. package/src/agents/jules/stream.js +186 -186
  13. package/src/agents/jules/swarm/file-scanner.js +74 -74
  14. package/src/agents/jules/swarm/index.js +11 -11
  15. package/src/agents/jules/swarm/orchestrator.js +362 -362
  16. package/src/agents/jules/swarm/pattern-hunter.js +123 -123
  17. package/src/agents/jules/swarm/sub-agent.js +308 -308
  18. package/src/agents/jules/tools/auth-audit.js +226 -222
  19. package/src/agents/jules/tools/dispatch.js +327 -327
  20. package/src/agents/jules/tools/file-edit.js +180 -180
  21. package/src/agents/jules/tools/file-read.js +100 -100
  22. package/src/agents/jules/tools/frontend-analyze.js +570 -570
  23. package/src/agents/jules/tools/glob.js +168 -168
  24. package/src/agents/jules/tools/grep.js +228 -228
  25. package/src/agents/jules/tools/index.js +29 -29
  26. package/src/agents/jules/tools/path-guards.js +161 -161
  27. package/src/agents/jules/tools/runtime-audit.js +493 -493
  28. package/src/agents/jules/tools/shell.js +383 -383
  29. package/src/ai/aidenid.js +972 -945
  30. package/src/ai/client.js +508 -508
  31. package/src/ai/domain-target-store.js +268 -268
  32. package/src/ai/identity-store.js +270 -270
  33. package/src/ai/site-store.js +145 -145
  34. package/src/audit/agents/architecture.js +180 -180
  35. package/src/audit/agents/compliance.js +179 -179
  36. package/src/audit/agents/documentation.js +165 -165
  37. package/src/audit/agents/performance.js +145 -145
  38. package/src/audit/agents/security.js +215 -215
  39. package/src/audit/agents/testing.js +172 -172
  40. package/src/audit/orchestrator.js +557 -557
  41. package/src/audit/package.js +204 -204
  42. package/src/audit/registry.js +284 -284
  43. package/src/audit/replay.js +103 -103
  44. package/src/auth/http.js +113 -113
  45. package/src/auth/service.js +891 -848
  46. package/src/auth/session-store.js +359 -345
  47. package/src/cli.js +252 -252
  48. package/src/commands/ai/identity-lifecycle.js +1338 -1337
  49. package/src/commands/ai/provision-governance.js +1272 -1246
  50. package/src/commands/ai/shared.js +147 -147
  51. package/src/commands/ai.js +11 -11
  52. package/src/commands/apply.js +12 -12
  53. package/src/commands/audit.js +1166 -1166
  54. package/src/commands/auth.js +375 -366
  55. package/src/commands/chat.js +191 -191
  56. package/src/commands/config.js +184 -184
  57. package/src/commands/cost.js +311 -311
  58. package/src/commands/daemon/core.js +850 -850
  59. package/src/commands/daemon/extended.js +1048 -1048
  60. package/src/commands/daemon/shared.js +213 -213
  61. package/src/commands/daemon.js +11 -11
  62. package/src/commands/guide.js +174 -174
  63. package/src/commands/ingest.js +58 -58
  64. package/src/commands/init.js +55 -55
  65. package/src/commands/legacy-args.js +10 -10
  66. package/src/commands/mcp.js +461 -404
  67. package/src/commands/omargate.js +15 -15
  68. package/src/commands/persona.js +20 -20
  69. package/src/commands/plugin.js +260 -260
  70. package/src/commands/policy.js +132 -132
  71. package/src/commands/prompt.js +238 -238
  72. package/src/commands/review.js +704 -704
  73. package/src/commands/scan.js +866 -788
  74. package/src/commands/spec.js +716 -716
  75. package/src/commands/swarm.js +651 -651
  76. package/src/commands/telemetry.js +202 -202
  77. package/src/commands/watch.js +510 -510
  78. package/src/config/agent-dictionary.js +182 -182
  79. package/src/config/io.js +56 -56
  80. package/src/config/paths.js +18 -18
  81. package/src/config/schema.js +55 -55
  82. package/src/config/service.js +184 -184
  83. package/src/cost/budget.js +235 -235
  84. package/src/cost/history.js +188 -188
  85. package/src/cost/tracker.js +171 -171
  86. package/src/daemon/artifact-lineage.js +534 -534
  87. package/src/daemon/assignment-ledger.js +770 -770
  88. package/src/daemon/ast-parser-layer.js +258 -258
  89. package/src/daemon/budget-governor.js +633 -633
  90. package/src/daemon/callgraph-overlay.js +646 -646
  91. package/src/daemon/error-worker.js +626 -626
  92. package/src/daemon/hybrid-mapper.js +929 -929
  93. package/src/daemon/jira-lifecycle.js +632 -632
  94. package/src/daemon/operator-control.js +657 -657
  95. package/src/daemon/reliability-lane.js +471 -471
  96. package/src/daemon/watchdog.js +971 -971
  97. package/src/guide/generator.js +316 -316
  98. package/src/ingest/engine.js +918 -918
  99. package/src/legacy-cli.js +2548 -2435
  100. package/src/mcp/registry.js +695 -695
  101. package/src/memory/blackboard.js +301 -301
  102. package/src/memory/retrieval.js +581 -581
  103. package/src/plugin/manifest.js +553 -553
  104. package/src/policy/packs.js +144 -144
  105. package/src/prompt/generator.js +118 -106
  106. package/src/review/ai-review.js +669 -669
  107. package/src/review/local-review.js +1284 -1284
  108. package/src/review/replay.js +235 -235
  109. package/src/review/report.js +664 -664
  110. package/src/review/spec-binding.js +487 -487
  111. package/src/scaffold/generator.js +67 -0
  112. package/src/scaffold/templates.js +150 -0
  113. package/src/scan/generator.js +418 -351
  114. package/src/scan/gh-secrets.js +107 -0
  115. package/src/spec/generator.js +519 -519
  116. package/src/spec/regenerate.js +237 -237
  117. package/src/spec/templates.js +91 -91
  118. package/src/swarm/dashboard.js +247 -247
  119. package/src/swarm/factory.js +363 -363
  120. package/src/swarm/pentest.js +934 -934
  121. package/src/swarm/registry.js +419 -419
  122. package/src/swarm/report.js +158 -158
  123. package/src/swarm/runtime.js +576 -576
  124. package/src/swarm/scenario-dsl.js +272 -272
  125. package/src/telemetry/ledger.js +302 -302
  126. package/src/telemetry/sync.js +96 -59
  127. package/src/ui/markdown.js +220 -220
@@ -1,383 +1,383 @@
1
- import { execSync } from "node:child_process";
2
- import path from "node:path";
3
-
4
- const DEFAULT_TIMEOUT_MS = 120_000;
5
- const MAX_OUTPUT_CHARS = 30_000;
6
- const DEFAULT_ALLOWED_FETCH_HOSTS = [
7
- "github.com",
8
- "api.github.com",
9
- "raw.githubusercontent.com",
10
- "*.githubusercontent.com",
11
- "registry.npmjs.org",
12
- "registry.yarnpkg.com",
13
- "pypi.org",
14
- "files.pythonhosted.org",
15
- "rubygems.org",
16
- "crates.io",
17
- "static.crates.io",
18
- ];
19
-
20
- /**
21
- * Patterns that are BLOCKED unconditionally.
22
- * Sourced from review/local-review.js security rules + src bash security patterns.
23
- */
24
- const BLOCKED_PATTERNS = [
25
- { pattern: /rm\s+(-[rR]f?|--recursive)\s+\/($|\s)/, desc: "rm -rf /" },
26
- { pattern: /:\(\)\s*\{[^}]*\}\s*;?\s*:/, desc: "fork bomb" },
27
- { pattern: /\beval\s*\(/, desc: "eval injection" },
28
- { pattern: /curl[^|]*\|\s*(ba)?sh/, desc: "pipe to shell" },
29
- { pattern: /wget[^|]*\|\s*(ba)?sh/, desc: "wget pipe to shell" },
30
- { pattern: />\s*\/dev\/sd[a-z]/, desc: "write to raw device" },
31
- { pattern: /mkfs\./, desc: "filesystem format" },
32
- { pattern: /dd\s+.*of=\/dev\//, desc: "dd to device" },
33
- { pattern: /chmod\s+777\s+\//, desc: "chmod 777 root" },
34
- { pattern: />\s*\/etc\//, desc: "write to /etc" },
35
- { pattern: /rm\s+-rf?\s+~/, desc: "rm home directory" },
36
- { pattern: /git\s+push\s+.*--force\s+.*main/, desc: "force push to main" },
37
- { pattern: /git\s+reset\s+--hard/, desc: "git reset hard" },
38
- { pattern: /DROP\s+TABLE|DROP\s+DATABASE/i, desc: "SQL drop" },
39
- { pattern: /TRUNCATE\s+TABLE/i, desc: "SQL truncate" },
40
- ];
41
-
42
- /**
43
- * Patterns that trigger a WARNING but are allowed.
44
- */
45
- const WARN_PATTERNS = [
46
- { pattern: /npm\s+install|yarn\s+add|pnpm\s+add/, desc: "package install" },
47
- { pattern: /git\s+push/, desc: "git push" },
48
- { pattern: /curl\s|wget\s|fetch\(/, desc: "network request" },
49
- { pattern: /rm\s+-/, desc: "file deletion" },
50
- { pattern: /sudo\s/, desc: "elevated privileges" },
51
- ];
52
-
53
- /**
54
- * Environment variables to strip from child process env.
55
- * Prevents credential leakage to spawned commands.
56
- */
57
- const EXACT_ENV_KEYS_TO_STRIP = new Set([
58
- "ANTHROPIC_API_KEY",
59
- "OPENAI_API_KEY",
60
- "GOOGLE_API_KEY",
61
- "GEMINI_API_KEY",
62
- "SENTINELAYER_TOKEN",
63
- "AIDENID_API_KEY",
64
- "AIDENID_TOKEN",
65
- "AWS_ACCESS_KEY_ID",
66
- "AWS_SECRET_ACCESS_KEY",
67
- "AWS_SESSION_TOKEN",
68
- "AWS_WEB_IDENTITY_TOKEN_FILE",
69
- "AZURE_CLIENT_SECRET",
70
- "GOOGLE_APPLICATION_CREDENTIALS",
71
- "GITHUB_TOKEN",
72
- "GH_TOKEN",
73
- "GITLAB_TOKEN",
74
- "BITBUCKET_TOKEN",
75
- "NPM_TOKEN",
76
- "NODE_AUTH_TOKEN",
77
- "PYPI_API_TOKEN",
78
- "RUBYGEMS_API_KEY",
79
- "CARGO_REGISTRY_TOKEN",
80
- "ACTIONS_ID_TOKEN_REQUEST_TOKEN",
81
- "ACTIONS_RUNTIME_TOKEN",
82
- "CI_JOB_TOKEN",
83
- "CI_JOB_JWT",
84
- "STRIPE_SECRET_KEY",
85
- "SLACK_BOT_TOKEN",
86
- "TELEGRAM_BOT_TOKEN",
87
- "DISCORD_BOT_TOKEN",
88
- "TWILIO_AUTH_TOKEN",
89
- "SENDGRID_API_KEY",
90
- "RESEND_API_KEY",
91
- "DATABASE_URL",
92
- "REDIS_URL",
93
- "MONGODB_URI",
94
- "SSH_SIGNING_KEY",
95
- "SSH_PRIVATE_KEY",
96
- "KUBECONFIG",
97
- "KUBE_CONFIG_DATA",
98
- ]);
99
-
100
- const ENV_KEY_PREFIX_PATTERNS = [
101
- /^AIDENID_/i,
102
- /^SENTINELAYER_/i,
103
- /^OPENAI_/i,
104
- /^ANTHROPIC_/i,
105
- /^GOOGLE_/i,
106
- /^GITHUB_/i,
107
- /^GH_/i,
108
- /^AWS_/i,
109
- /^AZURE_/i,
110
- /^SLACK_/i,
111
- /^STRIPE_/i,
112
- /^TELEGRAM_/i,
113
- /^DISCORD_/i,
114
- /^TWILIO_/i,
115
- /^SENDGRID_/i,
116
- /^RESEND_/i,
117
- ];
118
-
119
- const ENV_KEY_SUFFIX_PATTERNS = [
120
- /_TOKEN$/i,
121
- /_API_KEY$/i,
122
- /_SECRET$/i,
123
- /_SECRET_KEY$/i,
124
- /_PASSWORD$/i,
125
- /_PRIVATE_KEY$/i,
126
- /_ACCESS_KEY$/i,
127
- /_AUTH_TOKEN$/i,
128
- /_SESSION_TOKEN$/i,
129
- ];
130
-
131
- /**
132
- * Execute a shell command with security analysis, timeout, and env scrubbing.
133
- *
134
- * @param {object} input
135
- * @param {string} input.command - The shell command to execute.
136
- * @param {string} [input.cwd] - Working directory (default: process.cwd()).
137
- * @param {number} [input.timeout] - Timeout in ms (default: 120000).
138
- * @returns {{ stdout, stderr, exitCode, durationMs, command, security }}
139
- */
140
- export function shell(input) {
141
- if (!input.command || typeof input.command !== "string") {
142
- throw new ShellError("command is required and must be a non-empty string.");
143
- }
144
-
145
- const command = input.command.trim();
146
- const cwd = input.cwd ? path.resolve(input.cwd) : process.cwd();
147
- const timeout = input.timeout ?? DEFAULT_TIMEOUT_MS;
148
-
149
- // Security analysis
150
- const security = analyzeCommand(command);
151
- if (security.risk === "blocked") {
152
- throw new ShellBlockedError(
153
- `Command blocked: ${security.patterns[0].desc}`,
154
- security,
155
- );
156
- }
157
-
158
- // Build scrubbed environment
159
- const env = buildScrubbedEnv();
160
-
161
- const startMs = Date.now();
162
- let stdout = "";
163
- let stderr = "";
164
- let exitCode = 0;
165
-
166
- try {
167
- stdout = execSync(command, {
168
- cwd,
169
- encoding: "utf-8",
170
- timeout,
171
- env,
172
- maxBuffer: 10 * 1024 * 1024,
173
- stdio: ["pipe", "pipe", "pipe"],
174
- });
175
- } catch (err) {
176
- exitCode = err.status ?? 1;
177
- stdout = err.stdout ?? "";
178
- stderr = err.stderr ?? "";
179
- if (err.killed) {
180
- stderr += `\n[Process killed: timeout after ${timeout}ms]`;
181
- }
182
- }
183
-
184
- const durationMs = Date.now() - startMs;
185
-
186
- // Truncate large outputs
187
- if (stdout.length > MAX_OUTPUT_CHARS) {
188
- stdout = stdout.slice(0, MAX_OUTPUT_CHARS) + "\n[... truncated]";
189
- }
190
- if (stderr.length > MAX_OUTPUT_CHARS) {
191
- stderr = stderr.slice(0, MAX_OUTPUT_CHARS) + "\n[... truncated]";
192
- }
193
-
194
- return { stdout, stderr, exitCode, durationMs, command, security };
195
- }
196
-
197
- /**
198
- * Analyze a command for security risks.
199
- * @param {object} [options]
200
- * @param {Record<string, string|undefined>} [options.env]
201
- * @returns {{ risk: "safe"|"warn"|"blocked", patterns: Array<{desc}>, networkPolicy?: object }}
202
- */
203
- export function analyzeCommand(command, options = {}) {
204
- const networkPolicy = evaluateNetworkPolicy(command, options.env);
205
- if (networkPolicy.blocking) {
206
- return {
207
- risk: "blocked",
208
- patterns: [{ desc: networkPolicy.reason }],
209
- networkPolicy,
210
- };
211
- }
212
-
213
- for (const rule of BLOCKED_PATTERNS) {
214
- if (rule.pattern.test(command)) {
215
- return { risk: "blocked", patterns: [rule], networkPolicy };
216
- }
217
- }
218
-
219
- const warnings = [];
220
- for (const rule of WARN_PATTERNS) {
221
- if (rule.pattern.test(command)) {
222
- warnings.push(rule);
223
- }
224
- }
225
-
226
- if (warnings.length > 0) {
227
- return { risk: "warn", patterns: warnings, networkPolicy };
228
- }
229
-
230
- return { risk: "safe", patterns: [], networkPolicy };
231
- }
232
-
233
- export function buildScrubbedEnv(sourceEnv = process.env) {
234
- const env = { ...sourceEnv };
235
- for (const key of Object.keys(env)) {
236
- if (shouldStripEnvKey(key)) {
237
- delete env[key];
238
- }
239
- }
240
- return env;
241
- }
242
-
243
- function shouldStripEnvKey(key) {
244
- if (!key) {
245
- return false;
246
- }
247
-
248
- const upperKey = String(key).toUpperCase();
249
- if (EXACT_ENV_KEYS_TO_STRIP.has(upperKey)) {
250
- return true;
251
- }
252
-
253
- if (upperKey.startsWith("INPUT_")) {
254
- const baseKey = upperKey.slice("INPUT_".length);
255
- if (EXACT_ENV_KEYS_TO_STRIP.has(baseKey)) {
256
- return true;
257
- }
258
- if (ENV_KEY_PREFIX_PATTERNS.some((pattern) => pattern.test(baseKey))) {
259
- return true;
260
- }
261
- if (ENV_KEY_SUFFIX_PATTERNS.some((pattern) => pattern.test(baseKey))) {
262
- return true;
263
- }
264
- }
265
-
266
- if (ENV_KEY_PREFIX_PATTERNS.some((pattern) => pattern.test(upperKey))) {
267
- return true;
268
- }
269
-
270
- return ENV_KEY_SUFFIX_PATTERNS.some((pattern) => pattern.test(upperKey));
271
- }
272
-
273
- function evaluateNetworkPolicy(command, sourceEnv = process.env) {
274
- if (!/\b(curl|wget)\b/i.test(command)) {
275
- return {
276
- blocking: false,
277
- hosts: [],
278
- deniedHosts: [],
279
- };
280
- }
281
-
282
- const hosts = extractNetworkHosts(command);
283
- if (hosts.length === 0) {
284
- return {
285
- blocking: true,
286
- hosts: [],
287
- deniedHosts: [],
288
- reason: "network command requires explicit URL host",
289
- };
290
- }
291
-
292
- const allowlist = resolveAllowedFetchHosts(sourceEnv);
293
- const deniedHosts = hosts.filter((host) => !isAllowedHost(host, allowlist));
294
- if (deniedHosts.length > 0) {
295
- return {
296
- blocking: true,
297
- hosts,
298
- deniedHosts,
299
- reason: `network host not allowlisted: ${deniedHosts.join(", ")}`,
300
- };
301
- }
302
-
303
- return {
304
- blocking: false,
305
- hosts,
306
- deniedHosts: [],
307
- };
308
- }
309
-
310
- function resolveAllowedFetchHosts(sourceEnv = process.env) {
311
- const configured = String(sourceEnv.SENTINELAYER_ALLOWED_FETCH_HOSTS || "")
312
- .split(",")
313
- .map((item) => normalizeHostPattern(item))
314
- .filter(Boolean);
315
-
316
- const allPatterns = [...DEFAULT_ALLOWED_FETCH_HOSTS, ...configured]
317
- .map((item) => normalizeHostPattern(item))
318
- .filter(Boolean);
319
- return Array.from(new Set(allPatterns));
320
- }
321
-
322
- function extractNetworkHosts(command) {
323
- const matches = command.matchAll(/\bhttps?:\/\/([^\s"'`]+)/gi);
324
- const hosts = new Set();
325
- for (const match of matches) {
326
- const candidate = match?.[0];
327
- if (!candidate) {
328
- continue;
329
- }
330
- try {
331
- const parsed = new URL(candidate);
332
- const host = normalizeHostPattern(parsed.hostname);
333
- if (host) {
334
- hosts.add(host);
335
- }
336
- } catch {
337
- // Skip malformed URL segments.
338
- }
339
- }
340
- return Array.from(hosts);
341
- }
342
-
343
- function isAllowedHost(host, allowlist) {
344
- const normalizedHost = normalizeHostPattern(host);
345
- if (!normalizedHost) {
346
- return false;
347
- }
348
-
349
- return allowlist.some((pattern) => {
350
- if (!pattern) {
351
- return false;
352
- }
353
-
354
- if (pattern.startsWith("*.")) {
355
- const suffix = pattern.slice(2);
356
- return normalizedHost === suffix || normalizedHost.endsWith(`.${suffix}`);
357
- }
358
-
359
- return normalizedHost === pattern;
360
- });
361
- }
362
-
363
- function normalizeHostPattern(value) {
364
- return String(value || "")
365
- .trim()
366
- .toLowerCase()
367
- .replace(/\.$/, "");
368
- }
369
-
370
- export class ShellError extends Error {
371
- constructor(message) {
372
- super(message);
373
- this.name = "ShellError";
374
- }
375
- }
376
-
377
- export class ShellBlockedError extends ShellError {
378
- constructor(message, security) {
379
- super(message);
380
- this.name = "ShellBlockedError";
381
- this.security = security;
382
- }
383
- }
1
+ import { execSync } from "node:child_process";
2
+ import path from "node:path";
3
+
4
+ const DEFAULT_TIMEOUT_MS = 120_000;
5
+ const MAX_OUTPUT_CHARS = 30_000;
6
+ const DEFAULT_ALLOWED_FETCH_HOSTS = [
7
+ "github.com",
8
+ "api.github.com",
9
+ "raw.githubusercontent.com",
10
+ "*.githubusercontent.com",
11
+ "registry.npmjs.org",
12
+ "registry.yarnpkg.com",
13
+ "pypi.org",
14
+ "files.pythonhosted.org",
15
+ "rubygems.org",
16
+ "crates.io",
17
+ "static.crates.io",
18
+ ];
19
+
20
+ /**
21
+ * Patterns that are BLOCKED unconditionally.
22
+ * Sourced from review/local-review.js security rules + src bash security patterns.
23
+ */
24
+ const BLOCKED_PATTERNS = [
25
+ { pattern: /rm\s+(-[rR]f?|--recursive)\s+\/($|\s)/, desc: "rm -rf /" },
26
+ { pattern: /:\(\)\s*\{[^}]*\}\s*;?\s*:/, desc: "fork bomb" },
27
+ { pattern: /\beval\s*\(/, desc: "eval injection" },
28
+ { pattern: /curl[^|]*\|\s*(ba)?sh/, desc: "pipe to shell" },
29
+ { pattern: /wget[^|]*\|\s*(ba)?sh/, desc: "wget pipe to shell" },
30
+ { pattern: />\s*\/dev\/sd[a-z]/, desc: "write to raw device" },
31
+ { pattern: /mkfs\./, desc: "filesystem format" },
32
+ { pattern: /dd\s+.*of=\/dev\//, desc: "dd to device" },
33
+ { pattern: /chmod\s+777\s+\//, desc: "chmod 777 root" },
34
+ { pattern: />\s*\/etc\//, desc: "write to /etc" },
35
+ { pattern: /rm\s+-rf?\s+~/, desc: "rm home directory" },
36
+ { pattern: /git\s+push\s+.*--force\s+.*main/, desc: "force push to main" },
37
+ { pattern: /git\s+reset\s+--hard/, desc: "git reset hard" },
38
+ { pattern: /DROP\s+TABLE|DROP\s+DATABASE/i, desc: "SQL drop" },
39
+ { pattern: /TRUNCATE\s+TABLE/i, desc: "SQL truncate" },
40
+ ];
41
+
42
+ /**
43
+ * Patterns that trigger a WARNING but are allowed.
44
+ */
45
+ const WARN_PATTERNS = [
46
+ { pattern: /npm\s+install|yarn\s+add|pnpm\s+add/, desc: "package install" },
47
+ { pattern: /git\s+push/, desc: "git push" },
48
+ { pattern: /curl\s|wget\s|fetch\(/, desc: "network request" },
49
+ { pattern: /rm\s+-/, desc: "file deletion" },
50
+ { pattern: /sudo\s/, desc: "elevated privileges" },
51
+ ];
52
+
53
+ /**
54
+ * Environment variables to strip from child process env.
55
+ * Prevents credential leakage to spawned commands.
56
+ */
57
+ const EXACT_ENV_KEYS_TO_STRIP = new Set([
58
+ "ANTHROPIC_API_KEY",
59
+ "OPENAI_API_KEY",
60
+ "GOOGLE_API_KEY",
61
+ "GEMINI_API_KEY",
62
+ "SENTINELAYER_TOKEN",
63
+ "AIDENID_API_KEY",
64
+ "AIDENID_TOKEN",
65
+ "AWS_ACCESS_KEY_ID",
66
+ "AWS_SECRET_ACCESS_KEY",
67
+ "AWS_SESSION_TOKEN",
68
+ "AWS_WEB_IDENTITY_TOKEN_FILE",
69
+ "AZURE_CLIENT_SECRET",
70
+ "GOOGLE_APPLICATION_CREDENTIALS",
71
+ "GITHUB_TOKEN",
72
+ "GH_TOKEN",
73
+ "GITLAB_TOKEN",
74
+ "BITBUCKET_TOKEN",
75
+ "NPM_TOKEN",
76
+ "NODE_AUTH_TOKEN",
77
+ "PYPI_API_TOKEN",
78
+ "RUBYGEMS_API_KEY",
79
+ "CARGO_REGISTRY_TOKEN",
80
+ "ACTIONS_ID_TOKEN_REQUEST_TOKEN",
81
+ "ACTIONS_RUNTIME_TOKEN",
82
+ "CI_JOB_TOKEN",
83
+ "CI_JOB_JWT",
84
+ "STRIPE_SECRET_KEY",
85
+ "SLACK_BOT_TOKEN",
86
+ "TELEGRAM_BOT_TOKEN",
87
+ "DISCORD_BOT_TOKEN",
88
+ "TWILIO_AUTH_TOKEN",
89
+ "SENDGRID_API_KEY",
90
+ "RESEND_API_KEY",
91
+ "DATABASE_URL",
92
+ "REDIS_URL",
93
+ "MONGODB_URI",
94
+ "SSH_SIGNING_KEY",
95
+ "SSH_PRIVATE_KEY",
96
+ "KUBECONFIG",
97
+ "KUBE_CONFIG_DATA",
98
+ ]);
99
+
100
+ const ENV_KEY_PREFIX_PATTERNS = [
101
+ /^AIDENID_/i,
102
+ /^SENTINELAYER_/i,
103
+ /^OPENAI_/i,
104
+ /^ANTHROPIC_/i,
105
+ /^GOOGLE_/i,
106
+ /^GITHUB_/i,
107
+ /^GH_/i,
108
+ /^AWS_/i,
109
+ /^AZURE_/i,
110
+ /^SLACK_/i,
111
+ /^STRIPE_/i,
112
+ /^TELEGRAM_/i,
113
+ /^DISCORD_/i,
114
+ /^TWILIO_/i,
115
+ /^SENDGRID_/i,
116
+ /^RESEND_/i,
117
+ ];
118
+
119
+ const ENV_KEY_SUFFIX_PATTERNS = [
120
+ /_TOKEN$/i,
121
+ /_API_KEY$/i,
122
+ /_SECRET$/i,
123
+ /_SECRET_KEY$/i,
124
+ /_PASSWORD$/i,
125
+ /_PRIVATE_KEY$/i,
126
+ /_ACCESS_KEY$/i,
127
+ /_AUTH_TOKEN$/i,
128
+ /_SESSION_TOKEN$/i,
129
+ ];
130
+
131
+ /**
132
+ * Execute a shell command with security analysis, timeout, and env scrubbing.
133
+ *
134
+ * @param {object} input
135
+ * @param {string} input.command - The shell command to execute.
136
+ * @param {string} [input.cwd] - Working directory (default: process.cwd()).
137
+ * @param {number} [input.timeout] - Timeout in ms (default: 120000).
138
+ * @returns {{ stdout, stderr, exitCode, durationMs, command, security }}
139
+ */
140
+ export function shell(input) {
141
+ if (!input.command || typeof input.command !== "string") {
142
+ throw new ShellError("command is required and must be a non-empty string.");
143
+ }
144
+
145
+ const command = input.command.trim();
146
+ const cwd = input.cwd ? path.resolve(input.cwd) : process.cwd();
147
+ const timeout = input.timeout ?? DEFAULT_TIMEOUT_MS;
148
+
149
+ // Security analysis
150
+ const security = analyzeCommand(command);
151
+ if (security.risk === "blocked") {
152
+ throw new ShellBlockedError(
153
+ `Command blocked: ${security.patterns[0].desc}`,
154
+ security,
155
+ );
156
+ }
157
+
158
+ // Build scrubbed environment
159
+ const env = buildScrubbedEnv();
160
+
161
+ const startMs = Date.now();
162
+ let stdout = "";
163
+ let stderr = "";
164
+ let exitCode = 0;
165
+
166
+ try {
167
+ stdout = execSync(command, {
168
+ cwd,
169
+ encoding: "utf-8",
170
+ timeout,
171
+ env,
172
+ maxBuffer: 10 * 1024 * 1024,
173
+ stdio: ["pipe", "pipe", "pipe"],
174
+ });
175
+ } catch (err) {
176
+ exitCode = err.status ?? 1;
177
+ stdout = err.stdout ?? "";
178
+ stderr = err.stderr ?? "";
179
+ if (err.killed) {
180
+ stderr += `\n[Process killed: timeout after ${timeout}ms]`;
181
+ }
182
+ }
183
+
184
+ const durationMs = Date.now() - startMs;
185
+
186
+ // Truncate large outputs
187
+ if (stdout.length > MAX_OUTPUT_CHARS) {
188
+ stdout = stdout.slice(0, MAX_OUTPUT_CHARS) + "\n[... truncated]";
189
+ }
190
+ if (stderr.length > MAX_OUTPUT_CHARS) {
191
+ stderr = stderr.slice(0, MAX_OUTPUT_CHARS) + "\n[... truncated]";
192
+ }
193
+
194
+ return { stdout, stderr, exitCode, durationMs, command, security };
195
+ }
196
+
197
+ /**
198
+ * Analyze a command for security risks.
199
+ * @param {object} [options]
200
+ * @param {Record<string, string|undefined>} [options.env]
201
+ * @returns {{ risk: "safe"|"warn"|"blocked", patterns: Array<{desc}>, networkPolicy?: object }}
202
+ */
203
+ export function analyzeCommand(command, options = {}) {
204
+ const networkPolicy = evaluateNetworkPolicy(command, options.env);
205
+ if (networkPolicy.blocking) {
206
+ return {
207
+ risk: "blocked",
208
+ patterns: [{ desc: networkPolicy.reason }],
209
+ networkPolicy,
210
+ };
211
+ }
212
+
213
+ for (const rule of BLOCKED_PATTERNS) {
214
+ if (rule.pattern.test(command)) {
215
+ return { risk: "blocked", patterns: [rule], networkPolicy };
216
+ }
217
+ }
218
+
219
+ const warnings = [];
220
+ for (const rule of WARN_PATTERNS) {
221
+ if (rule.pattern.test(command)) {
222
+ warnings.push(rule);
223
+ }
224
+ }
225
+
226
+ if (warnings.length > 0) {
227
+ return { risk: "warn", patterns: warnings, networkPolicy };
228
+ }
229
+
230
+ return { risk: "safe", patterns: [], networkPolicy };
231
+ }
232
+
233
+ export function buildScrubbedEnv(sourceEnv = process.env) {
234
+ const env = { ...sourceEnv };
235
+ for (const key of Object.keys(env)) {
236
+ if (shouldStripEnvKey(key)) {
237
+ delete env[key];
238
+ }
239
+ }
240
+ return env;
241
+ }
242
+
243
+ function shouldStripEnvKey(key) {
244
+ if (!key) {
245
+ return false;
246
+ }
247
+
248
+ const upperKey = String(key).toUpperCase();
249
+ if (EXACT_ENV_KEYS_TO_STRIP.has(upperKey)) {
250
+ return true;
251
+ }
252
+
253
+ if (upperKey.startsWith("INPUT_")) {
254
+ const baseKey = upperKey.slice("INPUT_".length);
255
+ if (EXACT_ENV_KEYS_TO_STRIP.has(baseKey)) {
256
+ return true;
257
+ }
258
+ if (ENV_KEY_PREFIX_PATTERNS.some((pattern) => pattern.test(baseKey))) {
259
+ return true;
260
+ }
261
+ if (ENV_KEY_SUFFIX_PATTERNS.some((pattern) => pattern.test(baseKey))) {
262
+ return true;
263
+ }
264
+ }
265
+
266
+ if (ENV_KEY_PREFIX_PATTERNS.some((pattern) => pattern.test(upperKey))) {
267
+ return true;
268
+ }
269
+
270
+ return ENV_KEY_SUFFIX_PATTERNS.some((pattern) => pattern.test(upperKey));
271
+ }
272
+
273
+ function evaluateNetworkPolicy(command, sourceEnv = process.env) {
274
+ if (!/\b(curl|wget)\b/i.test(command)) {
275
+ return {
276
+ blocking: false,
277
+ hosts: [],
278
+ deniedHosts: [],
279
+ };
280
+ }
281
+
282
+ const hosts = extractNetworkHosts(command);
283
+ if (hosts.length === 0) {
284
+ return {
285
+ blocking: true,
286
+ hosts: [],
287
+ deniedHosts: [],
288
+ reason: "network command requires explicit URL host",
289
+ };
290
+ }
291
+
292
+ const allowlist = resolveAllowedFetchHosts(sourceEnv);
293
+ const deniedHosts = hosts.filter((host) => !isAllowedHost(host, allowlist));
294
+ if (deniedHosts.length > 0) {
295
+ return {
296
+ blocking: true,
297
+ hosts,
298
+ deniedHosts,
299
+ reason: `network host not allowlisted: ${deniedHosts.join(", ")}`,
300
+ };
301
+ }
302
+
303
+ return {
304
+ blocking: false,
305
+ hosts,
306
+ deniedHosts: [],
307
+ };
308
+ }
309
+
310
+ function resolveAllowedFetchHosts(sourceEnv = process.env) {
311
+ const configured = String(sourceEnv.SENTINELAYER_ALLOWED_FETCH_HOSTS || "")
312
+ .split(",")
313
+ .map((item) => normalizeHostPattern(item))
314
+ .filter(Boolean);
315
+
316
+ const allPatterns = [...DEFAULT_ALLOWED_FETCH_HOSTS, ...configured]
317
+ .map((item) => normalizeHostPattern(item))
318
+ .filter(Boolean);
319
+ return Array.from(new Set(allPatterns));
320
+ }
321
+
322
+ function extractNetworkHosts(command) {
323
+ const matches = command.matchAll(/\bhttps?:\/\/([^\s"'`]+)/gi);
324
+ const hosts = new Set();
325
+ for (const match of matches) {
326
+ const candidate = match?.[0];
327
+ if (!candidate) {
328
+ continue;
329
+ }
330
+ try {
331
+ const parsed = new URL(candidate);
332
+ const host = normalizeHostPattern(parsed.hostname);
333
+ if (host) {
334
+ hosts.add(host);
335
+ }
336
+ } catch {
337
+ // Skip malformed URL segments.
338
+ }
339
+ }
340
+ return Array.from(hosts);
341
+ }
342
+
343
+ function isAllowedHost(host, allowlist) {
344
+ const normalizedHost = normalizeHostPattern(host);
345
+ if (!normalizedHost) {
346
+ return false;
347
+ }
348
+
349
+ return allowlist.some((pattern) => {
350
+ if (!pattern) {
351
+ return false;
352
+ }
353
+
354
+ if (pattern.startsWith("*.")) {
355
+ const suffix = pattern.slice(2);
356
+ return normalizedHost === suffix || normalizedHost.endsWith(`.${suffix}`);
357
+ }
358
+
359
+ return normalizedHost === pattern;
360
+ });
361
+ }
362
+
363
+ function normalizeHostPattern(value) {
364
+ return String(value || "")
365
+ .trim()
366
+ .toLowerCase()
367
+ .replace(/\.$/, "");
368
+ }
369
+
370
+ export class ShellError extends Error {
371
+ constructor(message) {
372
+ super(message);
373
+ this.name = "ShellError";
374
+ }
375
+ }
376
+
377
+ export class ShellBlockedError extends ShellError {
378
+ constructor(message, security) {
379
+ super(message);
380
+ this.name = "ShellBlockedError";
381
+ this.security = security;
382
+ }
383
+ }