tribunal-kit 4.4.1 → 4.4.2

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 (44) hide show
  1. package/.agent/history/architecture-graph.yaml +140 -0
  2. package/.agent/history/graph-cache.json +262 -0
  3. package/.agent/history/snapshots/bin__tribunal-kit.js.json +19 -0
  4. package/.agent/history/snapshots/eslint.config.js.json +9 -0
  5. package/.agent/history/snapshots/migrate_refs.js.json +11 -0
  6. package/.agent/history/snapshots/scripts__changelog.js.json +13 -0
  7. package/.agent/history/snapshots/scripts__sync-version.js.json +12 -0
  8. package/.agent/history/snapshots/scripts__validate-payload.js.json +12 -0
  9. package/.agent/history/snapshots/test__integration__bridges.test.js.json +14 -0
  10. package/.agent/history/snapshots/test__integration__init.test.js.json +14 -0
  11. package/.agent/history/snapshots/test__integration__routing.test.js.json +12 -0
  12. package/.agent/history/snapshots/test__integration__swarm_dispatcher.test.js.json +14 -0
  13. package/.agent/history/snapshots/test__integration__wave2.test.js.json +14 -0
  14. package/.agent/history/snapshots/test__unit__args.test.js.json +20 -0
  15. package/.agent/history/snapshots/test__unit__case_law_manager.test.js.json +11 -0
  16. package/.agent/history/snapshots/test__unit__context_broker.test.js.json +11 -0
  17. package/.agent/history/snapshots/test__unit__copyDir.test.js.json +23 -0
  18. package/.agent/history/snapshots/test__unit__graph_tools.test.js.json +12 -0
  19. package/.agent/history/snapshots/test__unit__inner_loop_validator.test.js.json +11 -0
  20. package/.agent/history/snapshots/test__unit__selfInstall.test.js.json +23 -0
  21. package/.agent/history/snapshots/test__unit__semver.test.js.json +20 -0
  22. package/.agent/history/snapshots/test__unit__swarm_dispatcher.test.js.json +12 -0
  23. package/.agent/scripts/_colors.js +170 -18
  24. package/.agent/scripts/_utils.js +244 -42
  25. package/.agent/scripts/bundle_analyzer.js +261 -290
  26. package/.agent/scripts/case_law_manager.js +1 -7
  27. package/.agent/scripts/checklist.js +278 -266
  28. package/.agent/scripts/colors.js +11 -17
  29. package/.agent/scripts/context_broker.js +1 -7
  30. package/.agent/scripts/dependency_analyzer.js +234 -272
  31. package/.agent/scripts/graph_builder.js +46 -18
  32. package/.agent/scripts/graph_visualizer.js +10 -4
  33. package/.agent/scripts/graph_zoom.js +6 -4
  34. package/.agent/scripts/inner_loop_validator.js +2 -8
  35. package/.agent/scripts/lint_runner.js +186 -187
  36. package/.agent/scripts/schema_validator.js +8 -25
  37. package/.agent/scripts/security_scan.js +276 -303
  38. package/.agent/scripts/session_manager.js +1 -7
  39. package/.agent/scripts/skill_evolution.js +1 -8
  40. package/.agent/scripts/skill_integrator.js +1 -7
  41. package/.agent/scripts/test_runner.js +186 -193
  42. package/.agent/scripts/utils.js +17 -32
  43. package/.agent/scripts/verify_all.js +248 -257
  44. package/package.json +1 -1
@@ -1,303 +1,276 @@
1
- #!/usr/bin/env node
2
- /**
3
- * security_scan.js — Deep security scanner for the Tribunal Agent Kit.
4
- *
5
- * Checks for OWASP Top 10 patterns in source code:
6
- * - Hardcoded secrets and credentials
7
- * - SQL injection patterns (string concatenation in queries)
8
- * - XSS-prone code (innerHTML, dangerouslySetInnerHTML)
9
- * - Insecure eval() usage
10
- * - Missing auth patterns
11
- * - Insecure crypto usage
12
- *
13
- * ⚠️ DISCLAIMER: This is a heuristic regex-based pattern scanner, NOT a
14
- * full static analysis tool. It may produce false positives (e.g., test
15
- * fixtures containing 'password') and cannot detect indirect vulnerabilities
16
- * (e.g., SQL injection via variable indirection). For production security
17
- * auditing, supplement with dedicated tools like Semgrep, CodeQL, or Snyk.
18
- *
19
- * Usage:
20
- * node .agent/scripts/security_scan.js .
21
- * node .agent/scripts/security_scan.js . --severity high
22
- * node .agent/scripts/security_scan.js . --files src/auth.ts src/db.ts
23
- */
24
-
25
- 'use strict';
26
-
27
- const fs = require('fs');
28
- const path = require('path');
29
-
30
- // ━━━ ANSI colors ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
31
- const RED = '\x1b[91m';
32
- const GREEN = '\x1b[92m';
33
- const YELLOW = '\x1b[93m';
34
- const BLUE = '\x1b[94m';
35
- const MAGENTA = '\x1b[95m';
36
- const BOLD = '\x1b[1m';
37
- const RESET = '\x1b[0m';
38
-
39
- const SOURCE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx', '.py', '.go', '.java', '.rb']);
40
- const SKIP_DIRS = new Set([
41
- 'node_modules', '.git', 'dist', 'build', '__pycache__', '.agent',
42
- '.next', 'vendor', 'coverage', 'lcov-report', '.nyc_output',
43
- 'test-results', '.jest-cache',
44
- ]);
45
-
46
- const SEVERITY_COLORS = {
47
- critical: RED + BOLD,
48
- high: RED,
49
- medium: YELLOW,
50
- low: BLUE,
51
- };
52
-
53
- const SEVERITY_RANK = { critical: 0, high: 1, medium: 2, low: 3 };
54
-
55
- // Pattern definitions: [regex, severity, category, message]
56
- const PATTERNS = [
57
- // Secrets
58
- [/(?:password|passwd|pwd)\s*=\s*["'][^"']+["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded password detected'],
59
- [/(?:api_key|apikey|api_secret)\s*=\s*["'][^"']+["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded API key detected'],
60
- [/(?:secret|token|auth_token)\s*=\s*["'][A-Za-z0-9+/=]{16,}["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded secret/token detected'],
61
- [/(?:PRIVATE_KEY|private_key)\s*=\s*["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded private key detected'],
62
-
63
- // SQL Injection
64
- [/(?:query|execute|raw)\s*\(\s*[`"'].*\$\{/i, 'high', 'SQL Injection', 'String interpolation in SQL query use parameterized queries'],
65
- [/(?:query|execute|raw)\s*\(\s*["'].*\+\s*(?:req|input|params|body)/i, 'high', 'SQL Injection', 'String concatenation with user input in SQL'],
66
- [/\.raw\s*\(\s*`/, 'medium', 'SQL Injection', 'Raw query with template literal — verify inputs are sanitized'],
67
-
68
- // XSS
69
- [/\.innerHTML\s*=/, 'high', 'XSS', 'Direct innerHTML assignment use textContent or a sanitizer'],
70
- [/dangerouslySetInnerHTML/, 'medium', 'XSS', 'dangerouslySetInnerHTML used ensure input is sanitized'],
71
- [/document\.write\s*\(/, 'high', 'XSS', 'document.write() is an XSS vector'],
72
-
73
- // Insecure Functions
74
- [/\beval\s*\(/, 'high', 'Code Injection', 'eval() is a code injection vector avoid entirely'],
75
- [/new\s+Function\s*\(/, 'high', 'Code Injection', 'new Function() is equivalent to eval()'],
76
- [/child_process\.exec\s*\(/, 'medium', 'Command Injection', 'exec() with unsanitized input is a command injection vector'],
77
- [/subprocess\.call\s*\(\s*[^,\]]*\bshell\s*=\s*True/, 'high', 'Command Injection', 'subprocess with shell=True — use shell=False and pass args as list'],
78
-
79
- // Crypto
80
- [/createHash\s*\(\s*["']md5["']/, 'medium', 'Weak Crypto', 'MD5 is cryptographically broken — use SHA-256+'],
81
- [/createHash\s*\(\s*["']sha1["']/, 'medium', 'Weak Crypto', 'SHA-1 is deprecated — use SHA-256+'],
82
- [/Math\.random\s*\(/, 'low', 'Weak Randomness', 'Math.random() is not cryptographically secure — use crypto.randomBytes()'],
83
-
84
- // Auth Issues
85
- [/algorithms\s*:\s*\[\s*["']none["']/, 'critical', 'Auth Bypass', "JWT 'none' algorithm allows auth bypass"],
86
- [/verify\s*:\s*false/, 'high', 'Auth Bypass', 'SSL/TLS verification disabled'],
87
- [/rejectUnauthorized\s*:\s*false/, 'high', 'Auth Bypass', 'TLS certificate validation disabled'],
88
-
89
- // Information Disclosure
90
- [/console\.log\s*\(.*(?:password|secret|token|key)/i, 'medium', 'Info Disclosure', 'Sensitive data logged to console'],
91
- [/\.env(?:\.local|\.production)/, 'low', 'Info Disclosure', 'Env file reference ensure not committed to git'],
92
- ];
93
-
94
-
95
- /**
96
- * Scan a single file for security patterns.
97
- * @param {string} filepath - Absolute path to the file.
98
- * @param {string} projectRoot - Project root for relative path computation.
99
- * @returns {Array<{severity:string, category:string, file:string, line:number, message:string, snippet:string}>}
100
- */
101
- function scanFile(filepath, projectRoot) {
102
- const findings = [];
103
- const relPath = path.relative(projectRoot, filepath);
104
-
105
- let content;
106
- try {
107
- content = fs.readFileSync(filepath, 'utf8');
108
- } catch {
109
- return findings;
110
- }
111
-
112
- const lines = content.split('\n');
113
- for (let i = 0; i < lines.length; i++) {
114
- const stripped = lines[i].trim();
115
- // Skip comments
116
- if (stripped.startsWith('//') || stripped.startsWith('#') || stripped.startsWith('*')) {
117
- continue;
118
- }
119
-
120
- for (const [pattern, severity, category, message] of PATTERNS) {
121
- if (pattern.test(stripped)) {
122
- findings.push({
123
- severity,
124
- category,
125
- file: relPath,
126
- line: i + 1,
127
- message,
128
- snippet: stripped.slice(0, 120),
129
- });
130
- }
131
- }
132
- }
133
-
134
- return findings;
135
- }
136
-
137
-
138
- /**
139
- * Recursively walk a directory yield file paths.
140
- * @param {string} dir - Directory to walk.
141
- * @param {Set<string>} skipDirs - Directory names to skip.
142
- * @returns {string[]} Array of file paths.
143
- */
144
- function walkDir(dir, skipDirs) {
145
- const results = [];
146
- let entries;
147
- try {
148
- entries = fs.readdirSync(dir, { withFileTypes: true });
149
- } catch {
150
- return results;
151
- }
152
-
153
- for (const entry of entries) {
154
- if (entry.isDirectory()) {
155
- if (!skipDirs.has(entry.name)) {
156
- results.push(...walkDir(path.join(dir, entry.name), skipDirs));
157
- }
158
- } else if (entry.isFile()) {
159
- results.push(path.join(dir, entry.name));
160
- }
161
- }
162
- return results;
163
- }
164
-
165
-
166
- /**
167
- * Scan all source files in a directory.
168
- * @param {string} projectRoot - Root directory to scan.
169
- * @param {string[]|null} targetFiles - Specific files to scan, or null for full scan.
170
- * @returns {Array} Array of finding objects.
171
- */
172
- function scanDirectory(projectRoot, targetFiles) {
173
- const allFindings = [];
174
-
175
- if (targetFiles && targetFiles.length > 0) {
176
- for (const fpath of targetFiles) {
177
- const absPath = path.isAbsolute(fpath) ? fpath : path.join(projectRoot, fpath);
178
- if (fs.existsSync(absPath) && fs.statSync(absPath).isFile()) {
179
- allFindings.push(...scanFile(absPath, projectRoot));
180
- }
181
- }
182
- return allFindings;
183
- }
184
-
185
- const files = walkDir(projectRoot, SKIP_DIRS);
186
- for (const filepath of files) {
187
- const ext = path.extname(filepath);
188
- if (!SOURCE_EXTENSIONS.has(ext)) continue;
189
- allFindings.push(...scanFile(filepath, projectRoot));
190
- }
191
-
192
- return allFindings;
193
- }
194
-
195
-
196
- /**
197
- * Print findings filtered by minimum severity. Returns count of displayed findings.
198
- */
199
- function printFindings(findings, minSeverity) {
200
- const minRank = SEVERITY_RANK[minSeverity] ?? 3;
201
- const filtered = findings
202
- .filter(f => (SEVERITY_RANK[f.severity] ?? 3) <= minRank)
203
- .sort((a, b) => (SEVERITY_RANK[a.severity] ?? 3) - (SEVERITY_RANK[b.severity] ?? 3));
204
-
205
- if (filtered.length === 0) {
206
- console.log(`\n ${GREEN}✅ No security issues found at severity '${minSeverity}' or above${RESET}`);
207
- return 0;
208
- }
209
-
210
- let currentCategory = '';
211
- for (const finding of filtered) {
212
- if (finding.category !== currentCategory) {
213
- currentCategory = finding.category;
214
- console.log(`\n ${BOLD}${currentCategory}${RESET}`);
215
- }
216
- const color = SEVERITY_COLORS[finding.severity] || '';
217
- console.log(` ${color}[${finding.severity.toUpperCase()}]${RESET} ${finding.file}:${finding.line}`);
218
- console.log(` ${finding.message}`);
219
- console.log(` ${MAGENTA}→ ${finding.snippet}${RESET}`);
220
- }
221
-
222
- return filtered.length;
223
- }
224
-
225
-
226
- /**
227
- * Parse CLI arguments manually (no external dependencies).
228
- */
229
- function parseArgs(argv) {
230
- const args = { path: null, severity: 'low', files: null };
231
- const raw = argv.slice(2);
232
-
233
- for (let i = 0; i < raw.length; i++) {
234
- if (raw[i] === '--severity' && raw[i + 1]) {
235
- args.severity = raw[++i];
236
- } else if (raw[i] === '--files') {
237
- args.files = [];
238
- while (i + 1 < raw.length && !raw[i + 1].startsWith('--')) {
239
- args.files.push(raw[++i]);
240
- }
241
- } else if (!raw[i].startsWith('--') && !args.path) {
242
- args.path = raw[i];
243
- }
244
- }
245
- return args;
246
- }
247
-
248
-
249
- function main() {
250
- const args = parseArgs(process.argv);
251
-
252
- if (!args.path) {
253
- console.error(`Usage: node security_scan.js <path> [--severity critical|high|medium|low] [--files ...]`);
254
- process.exit(1);
255
- }
256
-
257
- const projectRoot = path.resolve(args.path);
258
- if (!fs.existsSync(projectRoot) || !fs.statSync(projectRoot).isDirectory()) {
259
- console.error(` ${RED} Directory not found: ${projectRoot}${RESET}`);
260
- process.exit(1);
261
- }
262
-
263
- console.log(`${BOLD}Tribunal — security_scan.js${RESET}`);
264
- console.log(`Project: ${projectRoot}`);
265
- console.log(`Severity filter: ${args.severity}+`);
266
-
267
- const findings = scanDirectory(projectRoot, args.files);
268
- const count = printFindings(findings, args.severity);
269
-
270
- // Summary
271
- console.log(`\n${BOLD}━━━ Security Scan Summary ━━━${RESET}`);
272
- const bySeverity = {};
273
- for (const f of findings) {
274
- bySeverity[f.severity] = (bySeverity[f.severity] || 0) + 1;
275
- }
276
-
277
- for (const sev of ['critical', 'high', 'medium', 'low']) {
278
- const c = bySeverity[sev] || 0;
279
- if (c > 0) {
280
- const color = SEVERITY_COLORS[sev] || '';
281
- console.log(` ${color}${sev.toUpperCase()}: ${c}${RESET}`);
282
- }
283
- }
284
-
285
- if (count === 0) {
286
- console.log(` ${GREEN}✅ No issues found — scan passed${RESET}`);
287
- } else {
288
- const criticalHigh = (bySeverity.critical || 0) + (bySeverity.high || 0);
289
- if (criticalHigh > 0) {
290
- console.log(`\n ${RED}${BOLD}⚠️ ${criticalHigh} critical/high issue(s) require immediate attention${RESET}`);
291
- }
292
- }
293
-
294
- process.exit((bySeverity.critical || 0) > 0 ? 1 : 0);
295
- }
296
-
297
-
298
- // ━━━ Exports for testing & programmatic use ━━━
299
- module.exports = { scanFile, scanDirectory, printFindings, PATTERNS, SEVERITY_RANK };
300
-
301
- if (require.main === module) {
302
- main();
303
- }
1
+ #!/usr/bin/env node
2
+ /**
3
+ * security_scan.js — Deep security scanner for the Tribunal Agent Kit.
4
+ *
5
+ * Checks for OWASP Top 10 patterns in source code:
6
+ * - Hardcoded secrets and credentials
7
+ * - SQL injection patterns (string concatenation in queries)
8
+ * - XSS-prone code (innerHTML, dangerouslySetInnerHTML)
9
+ * - Insecure eval() usage
10
+ * - Missing auth patterns
11
+ * - Insecure crypto usage
12
+ *
13
+ * ⚠️ DISCLAIMER: This is a heuristic regex-based pattern scanner, NOT a
14
+ * full static analysis tool. It may produce false positives (e.g., test
15
+ * fixtures containing 'password') and cannot detect indirect vulnerabilities
16
+ * (e.g., SQL injection via variable indirection). For production security
17
+ * auditing, supplement with dedicated tools like Semgrep, CodeQL, or Snyk.
18
+ *
19
+ * Usage:
20
+ * node .agent/scripts/security_scan.js .
21
+ * node .agent/scripts/security_scan.js . --severity high
22
+ * node .agent/scripts/security_scan.js . --files src/auth.ts src/db.ts
23
+ */
24
+
25
+ 'use strict';
26
+
27
+ const fs = require('fs');
28
+ const path = require('path');
29
+
30
+ const {
31
+ RED, GREEN, YELLOW, BLUE, MAGENTA, BOLD, DIM, CYAN, RESET,
32
+ banner, sectionHeader, timer, formatMs,
33
+ } = require('./_colors');
34
+
35
+ const { walkDir, SOURCE_EXTENSIONS } = require('./_utils');
36
+
37
+ // ── Security-specific source extensions (broader than default) ──────────────
38
+ const SCAN_EXTENSIONS = new Set([...SOURCE_EXTENSIONS, '.py', '.go', '.java', '.rb']);
39
+
40
+ const SEVERITY_COLORS = {
41
+ critical: RED + BOLD,
42
+ high: RED,
43
+ medium: YELLOW,
44
+ low: BLUE,
45
+ };
46
+
47
+ const SEVERITY_RANK = { critical: 0, high: 1, medium: 2, low: 3 };
48
+
49
+ // Pattern definitions: [regex, severity, category, message]
50
+ const PATTERNS = [
51
+ // Secrets
52
+ [/(?:password|passwd|pwd)\s*=\s*["'][^"']+["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded password detected'],
53
+ [/(?:api_key|apikey|api_secret)\s*=\s*["'][^"']+["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded API key detected'],
54
+ [/(?:secret|token|auth_token)\s*=\s*["'][A-Za-z0-9+/=]{16,}["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded secret/token detected'],
55
+ [/(?:PRIVATE_KEY|private_key)\s*=\s*["']/i, 'critical', 'Hardcoded Secret', 'Hardcoded private key detected'],
56
+
57
+ // SQL Injection
58
+ [/(?:query|execute|raw)\s*\(\s*[`"'].*\$\{/i, 'high', 'SQL Injection', 'String interpolation in SQL query — use parameterized queries'],
59
+ [/(?:query|execute|raw)\s*\(\s*["'].*\+\s*(?:req|input|params|body)/i, 'high', 'SQL Injection', 'String concatenation with user input in SQL'],
60
+ [/\.raw\s*\(\s*`/, 'medium', 'SQL Injection', 'Raw query with template literal — verify inputs are sanitized'],
61
+
62
+ // XSS
63
+ [/\.innerHTML\s*=/, 'high', 'XSS', 'Direct innerHTML assignment — use textContent or a sanitizer'],
64
+ [/dangerouslySetInnerHTML/, 'medium', 'XSS', 'dangerouslySetInnerHTML used ensure input is sanitized'],
65
+ [/document\.write\s*\(/, 'high', 'XSS', 'document.write() is an XSS vector'],
66
+
67
+ // Insecure Functions
68
+ [/\beval\s*\(/, 'high', 'Code Injection', 'eval() is a code injection vector — avoid entirely'],
69
+ [/new\s+Function\s*\(/, 'high', 'Code Injection', 'new Function() is equivalent to eval()'],
70
+ [/child_process\.exec\s*\(/, 'medium', 'Command Injection', 'exec() with unsanitized input is a command injection vector'],
71
+ [/subprocess\.call\s*\(\s*[^,\]]*\bshell\s*=\s*True/, 'high', 'Command Injection', 'subprocess with shell=True use shell=False and pass args as list'],
72
+
73
+ // Crypto
74
+ [/createHash\s*\(\s*["']md5["']/, 'medium', 'Weak Crypto', 'MD5 is cryptographically brokenuse SHA-256+'],
75
+ [/createHash\s*\(\s*["']sha1["']/, 'medium', 'Weak Crypto', 'SHA-1 is deprecated use SHA-256+'],
76
+ [/Math\.random\s*\(/, 'low', 'Weak Randomness', 'Math.random() is not cryptographically secure use crypto.randomBytes()'],
77
+
78
+ // Auth Issues
79
+ [/algorithms\s*:\s*\[\s*["']none["']/, 'critical', 'Auth Bypass', "JWT 'none' algorithm allows auth bypass"],
80
+ [/verify\s*:\s*false/, 'high', 'Auth Bypass', 'SSL/TLS verification disabled'],
81
+ [/rejectUnauthorized\s*:\s*false/, 'high', 'Auth Bypass', 'TLS certificate validation disabled'],
82
+
83
+ // Information Disclosure
84
+ [/console\.log\s*\(.*(?:password|secret|token|key)/i, 'medium', 'Info Disclosure', 'Sensitive data logged to console'],
85
+ [/\.env(?:\.local|\.production)/, 'low', 'Info Disclosure', 'Env file reference ensure not committed to git'],
86
+ ];
87
+
88
+
89
+ /**
90
+ * Scan a single file for security patterns.
91
+ * @param {string} filepath - Absolute path to the file.
92
+ * @param {string} projectRoot - Project root for relative path computation.
93
+ * @returns {Array<{severity:string, category:string, file:string, line:number, message:string, snippet:string}>}
94
+ */
95
+ function scanFile(filepath, projectRoot) {
96
+ const findings = [];
97
+ const relPath = path.relative(projectRoot, filepath);
98
+
99
+ let content;
100
+ try {
101
+ content = fs.readFileSync(filepath, 'utf8');
102
+ } catch {
103
+ return findings;
104
+ }
105
+
106
+ const lines = content.split('\n');
107
+ for (let i = 0; i < lines.length; i++) {
108
+ const stripped = lines[i].trim();
109
+ // Skip comments
110
+ if (stripped.startsWith('//') || stripped.startsWith('#') || stripped.startsWith('*')) {
111
+ continue;
112
+ }
113
+
114
+ for (const [pattern, severity, category, message] of PATTERNS) {
115
+ if (pattern.test(stripped)) {
116
+ findings.push({
117
+ severity,
118
+ category,
119
+ file: relPath,
120
+ line: i + 1,
121
+ message,
122
+ snippet: stripped.slice(0, 120),
123
+ });
124
+ }
125
+ }
126
+ }
127
+
128
+ return findings;
129
+ }
130
+
131
+
132
+ /**
133
+ * Scan all source files in a directory.
134
+ * PERFORMANCE FIX: Uses shared walkDir from _utils.js and pushes findings
135
+ * individually instead of using spread operator (eliminates O(n²) array growth).
136
+ *
137
+ * @param {string} projectRoot - Root directory to scan.
138
+ * @param {string[]|null} targetFiles - Specific files to scan, or null for full scan.
139
+ * @returns {Array} Array of finding objects.
140
+ */
141
+ function scanDirectory(projectRoot, targetFiles) {
142
+ const allFindings = [];
143
+
144
+ if (targetFiles && targetFiles.length > 0) {
145
+ for (const fpath of targetFiles) {
146
+ const absPath = path.isAbsolute(fpath) ? fpath : path.join(projectRoot, fpath);
147
+ if (fs.existsSync(absPath) && fs.statSync(absPath).isFile()) {
148
+ // FIX: Push individually instead of spread to avoid O(n²)
149
+ const fileFindings = scanFile(absPath, projectRoot);
150
+ for (const f of fileFindings) allFindings.push(f);
151
+ }
152
+ }
153
+ return allFindings;
154
+ }
155
+
156
+ const files = walkDir(projectRoot, { extensions: SCAN_EXTENSIONS });
157
+
158
+ for (const filepath of files) {
159
+ // FIX: Push individually instead of spread to avoid O()
160
+ const fileFindings = scanFile(filepath, projectRoot);
161
+ for (const f of fileFindings) allFindings.push(f);
162
+ }
163
+
164
+ return allFindings;
165
+ }
166
+
167
+
168
+ /**
169
+ * Print findings filtered by minimum severity. Returns count of displayed findings.
170
+ */
171
+ function printFindings(findings, minSeverity) {
172
+ const minRank = SEVERITY_RANK[minSeverity] ?? 3;
173
+ const filtered = findings
174
+ .filter(f => (SEVERITY_RANK[f.severity] ?? 3) <= minRank)
175
+ .sort((a, b) => (SEVERITY_RANK[a.severity] ?? 3) - (SEVERITY_RANK[b.severity] ?? 3));
176
+
177
+ if (filtered.length === 0) {
178
+ console.log(`\n ${GREEN}✅ No security issues found at severity '${minSeverity}' or above${RESET}`);
179
+ return 0;
180
+ }
181
+
182
+ let currentCategory = '';
183
+ for (const finding of filtered) {
184
+ if (finding.category !== currentCategory) {
185
+ currentCategory = finding.category;
186
+ console.log(`\n ${BOLD}${currentCategory}${RESET}`);
187
+ }
188
+ const color = SEVERITY_COLORS[finding.severity] || '';
189
+ console.log(` ${color}[${finding.severity.toUpperCase()}]${RESET} ${finding.file}:${finding.line}`);
190
+ console.log(` ${finding.message}`);
191
+ console.log(` ${MAGENTA}→ ${finding.snippet}${RESET}`);
192
+ }
193
+
194
+ return filtered.length;
195
+ }
196
+
197
+
198
+ function main() {
199
+ const args = { path: null, severity: 'low', files: null };
200
+ const raw = process.argv.slice(2);
201
+
202
+ for (let i = 0; i < raw.length; i++) {
203
+ if (raw[i] === '--severity' && raw[i + 1]) {
204
+ args.severity = raw[++i];
205
+ } else if (raw[i] === '--files') {
206
+ args.files = [];
207
+ while (i + 1 < raw.length && !raw[i + 1].startsWith('--')) {
208
+ args.files.push(raw[++i]);
209
+ }
210
+ } else if (!raw[i].startsWith('--') && !args.path) {
211
+ args.path = raw[i];
212
+ }
213
+ }
214
+
215
+ if (!args.path) {
216
+ console.error(`Usage: node security_scan.js <path> [--severity critical|high|medium|low] [--files ...]`);
217
+ process.exit(1);
218
+ }
219
+
220
+ const projectRoot = path.resolve(args.path);
221
+ if (!fs.existsSync(projectRoot) || !fs.statSync(projectRoot).isDirectory()) {
222
+ console.error(` ${RED}❌ Directory not found: ${projectRoot}${RESET}`);
223
+ process.exit(1);
224
+ }
225
+
226
+ console.log(banner('security_scan.js', {
227
+ Project: projectRoot,
228
+ Severity: `${args.severity}+`,
229
+ }));
230
+
231
+ const elapsed = timer();
232
+ const findings = scanDirectory(projectRoot, args.files);
233
+ const scanMs = elapsed();
234
+
235
+ const count = printFindings(findings, args.severity);
236
+
237
+ // ━━━ Summary ━━━
238
+ console.log(`\n${BOLD}${CYAN}━━━ Security Scan Summary ━━━${RESET}`);
239
+
240
+ const bySeverity = {};
241
+ for (const f of findings) {
242
+ bySeverity[f.severity] = (bySeverity[f.severity] || 0) + 1;
243
+ }
244
+
245
+ const uniqueFiles = new Set(findings.map(f => f.file)).size;
246
+
247
+ for (const sev of ['critical', 'high', 'medium', 'low']) {
248
+ const c = bySeverity[sev] || 0;
249
+ if (c > 0) {
250
+ const color = SEVERITY_COLORS[sev] || '';
251
+ console.log(` ${color}${sev.toUpperCase()}: ${c}${RESET}`);
252
+ }
253
+ }
254
+
255
+ console.log(`\n ${DIM}Scanned in ${formatMs(scanMs)} — ${findings.length} findings across ${uniqueFiles} file(s)${RESET}`);
256
+
257
+ if (count === 0) {
258
+ console.log(` ${GREEN}✅ No issues found — scan passed${RESET}`);
259
+ } else {
260
+ const criticalHigh = (bySeverity.critical || 0) + (bySeverity.high || 0);
261
+ if (criticalHigh > 0) {
262
+ console.log(`\n ${RED}${BOLD}⚠️ ${criticalHigh} critical/high issue(s) require immediate attention${RESET}`);
263
+ }
264
+ }
265
+ console.log();
266
+
267
+ process.exit((bySeverity.critical || 0) > 0 ? 1 : 0);
268
+ }
269
+
270
+
271
+ // ━━━ Exports for testing & programmatic use ━━━
272
+ module.exports = { scanFile, scanDirectory, printFindings, PATTERNS, SEVERITY_RANK };
273
+
274
+ if (require.main === module) {
275
+ main();
276
+ }
@@ -20,13 +20,7 @@ const path = require('path');
20
20
 
21
21
  const STATE_FILE = ".agent_session.json";
22
22
 
23
- const GREEN = "\x1b[92m";
24
- const YELLOW = "\x1b[93m";
25
- const BLUE = "\x1b[94m";
26
- const CYAN = "\x1b[96m";
27
- const RED = "\x1b[91m";
28
- const BOLD = "\x1b[1m";
29
- const RESET = "\x1b[0m";
23
+ const { GREEN, YELLOW, BLUE, CYAN, RED, BOLD, RESET } = require('./_colors');
30
24
 
31
25
  const VALID_COMMANDS = new Set(["save", "load", "show", "clear", "status", "tag", "list", "export"]);
32
26
  const LIST_PAGE_SIZE = 10;
@@ -31,14 +31,7 @@ const { execSync } = require('child_process');
31
31
  const readline = require('readline');
32
32
 
33
33
  // ── Colours ──────────────────────────────────────────────────────────────────
34
- const GREEN = '\x1b[92m';
35
- const YELLOW = '\x1b[93m';
36
- const CYAN = '\x1b[96m';
37
- const RED = '\x1b[91m';
38
- const BLUE = '\x1b[94m';
39
- const BOLD = '\x1b[1m';
40
- const DIM = '\x1b[2m';
41
- const RESET = '\x1b[0m';
34
+ const { GREEN, YELLOW, CYAN, RED, BLUE, BOLD, DIM, RESET } = require('./_colors');
42
35
 
43
36
  // ── Find .agent directory ─────────────────────────────────────────────────────
44
37
  function findAgentDir() {
@@ -20,13 +20,7 @@ const fs = require('fs');
20
20
  const path = require('path');
21
21
  const { execFileSync } = require('child_process');
22
22
 
23
- // Colors for terminal output
24
- const CYAN = '\x1b[96m';
25
- const GREEN = '\x1b[92m';
26
- const YELLOW = '\x1b[93m';
27
- const RED = '\x1b[91m';
28
- const BOLD = '\x1b[1m';
29
- const RESET = '\x1b[0m';
23
+ const { CYAN, GREEN, YELLOW, RED, BOLD, RESET } = require('./_colors');
30
24
 
31
25
  const REPORT_FILE = 'skill-integration-report.md';
32
26