ship-safe 3.1.0 → 4.0.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/README.md +200 -307
- package/cli/agents/api-fuzzer.js +224 -0
- package/cli/agents/auth-bypass-agent.js +326 -0
- package/cli/agents/base-agent.js +240 -0
- package/cli/agents/cicd-scanner.js +200 -0
- package/cli/agents/config-auditor.js +413 -0
- package/cli/agents/git-history-scanner.js +167 -0
- package/cli/agents/html-reporter.js +363 -0
- package/cli/agents/index.js +56 -0
- package/cli/agents/injection-tester.js +401 -0
- package/cli/agents/llm-redteam.js +251 -0
- package/cli/agents/mobile-scanner.js +225 -0
- package/cli/agents/orchestrator.js +152 -0
- package/cli/agents/policy-engine.js +149 -0
- package/cli/agents/recon-agent.js +196 -0
- package/cli/agents/sbom-generator.js +176 -0
- package/cli/agents/scoring-engine.js +207 -0
- package/cli/agents/ssrf-prober.js +130 -0
- package/cli/agents/supply-chain-agent.js +274 -0
- package/cli/bin/ship-safe.js +119 -2
- package/cli/commands/agent.js +606 -0
- package/cli/commands/audit.js +565 -0
- package/cli/commands/deps.js +447 -0
- package/cli/commands/fix.js +3 -3
- package/cli/commands/init.js +86 -3
- package/cli/commands/mcp.js +2 -2
- package/cli/commands/red-team.js +315 -0
- package/cli/commands/remediate.js +4 -4
- package/cli/commands/rotate.js +6 -6
- package/cli/commands/scan.js +64 -23
- package/cli/commands/score.js +446 -0
- package/cli/commands/watch.js +160 -0
- package/cli/index.js +40 -2
- package/cli/providers/llm-provider.js +288 -0
- package/cli/utils/entropy.js +6 -0
- package/cli/utils/output.js +42 -2
- package/cli/utils/patterns.js +393 -1
- package/package.json +19 -15
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Score Command
|
|
3
|
+
* =============
|
|
4
|
+
*
|
|
5
|
+
* Compute a 0-100 security health score for your project.
|
|
6
|
+
* Combines secret detection, code vulnerability detection, and dependency auditing.
|
|
7
|
+
*
|
|
8
|
+
* USAGE:
|
|
9
|
+
* npx ship-safe score [path] Score the project in the current directory
|
|
10
|
+
* npx ship-safe score . --no-deps Skip dependency audit (faster)
|
|
11
|
+
*
|
|
12
|
+
* SCORING ALGORITHM (starts at 100):
|
|
13
|
+
* Secrets: critical −25, high −15, medium −5 (capped at −40)
|
|
14
|
+
* Code Vulns: critical −20, high −10, medium −3 (capped at −30)
|
|
15
|
+
* Dependencies: critical −20, high −10, moderate −5 (capped at −30)
|
|
16
|
+
*
|
|
17
|
+
* GRADES:
|
|
18
|
+
* A 90–100 Ship it!
|
|
19
|
+
* B 75–89 Minor issues to review
|
|
20
|
+
* C 60–74 Fix before shipping
|
|
21
|
+
* D 40–59 Significant security risks
|
|
22
|
+
* F 0–39 Not safe to ship
|
|
23
|
+
*
|
|
24
|
+
* EXIT CODES:
|
|
25
|
+
* 0 - Score is A or B (90+/75+)
|
|
26
|
+
* 1 - Score is C or below (< 75)
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import fs from 'fs';
|
|
30
|
+
import path from 'path';
|
|
31
|
+
import fg from 'fast-glob';
|
|
32
|
+
import chalk from 'chalk';
|
|
33
|
+
import ora from 'ora';
|
|
34
|
+
import {
|
|
35
|
+
SECRET_PATTERNS,
|
|
36
|
+
SECURITY_PATTERNS,
|
|
37
|
+
SKIP_DIRS,
|
|
38
|
+
SKIP_EXTENSIONS,
|
|
39
|
+
TEST_FILE_PATTERNS,
|
|
40
|
+
MAX_FILE_SIZE
|
|
41
|
+
} from '../utils/patterns.js';
|
|
42
|
+
import { isHighEntropyMatch } from '../utils/entropy.js';
|
|
43
|
+
import { runDepsAudit } from './deps.js';
|
|
44
|
+
import * as output from '../utils/output.js';
|
|
45
|
+
|
|
46
|
+
// =============================================================================
|
|
47
|
+
// SCORING CONSTANTS
|
|
48
|
+
// =============================================================================
|
|
49
|
+
|
|
50
|
+
const SECRET_DEDUCTIONS = { critical: 25, high: 15, medium: 5 };
|
|
51
|
+
const SECRET_CAP = 40;
|
|
52
|
+
|
|
53
|
+
const VULN_DEDUCTIONS = { critical: 20, high: 10, medium: 3 };
|
|
54
|
+
const VULN_CAP = 30;
|
|
55
|
+
|
|
56
|
+
const DEP_DEDUCTIONS = { critical: 20, high: 10, moderate: 5, medium: 5 };
|
|
57
|
+
const DEP_CAP = 30;
|
|
58
|
+
|
|
59
|
+
const GRADES = [
|
|
60
|
+
{ min: 90, letter: 'A', label: 'Ship it!' },
|
|
61
|
+
{ min: 75, letter: 'B', label: 'Minor issues to review' },
|
|
62
|
+
{ min: 60, letter: 'C', label: 'Fix before shipping' },
|
|
63
|
+
{ min: 40, letter: 'D', label: 'Significant security risks' },
|
|
64
|
+
{ min: 0, letter: 'F', label: 'Not safe to ship' },
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
// =============================================================================
|
|
68
|
+
// MAIN COMMAND
|
|
69
|
+
// =============================================================================
|
|
70
|
+
|
|
71
|
+
export async function scoreCommand(targetPath = '.', options = {}) {
|
|
72
|
+
const absolutePath = path.resolve(targetPath);
|
|
73
|
+
|
|
74
|
+
if (!fs.existsSync(absolutePath)) {
|
|
75
|
+
output.error(`Path does not exist: ${absolutePath}`);
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
console.log();
|
|
80
|
+
output.header('Security Health Score');
|
|
81
|
+
console.log();
|
|
82
|
+
|
|
83
|
+
const runDeps = options.deps !== false; // --no-deps sets options.deps = false
|
|
84
|
+
|
|
85
|
+
// ── 1. Scan for secrets and code vulns ──────────────────────────────────────
|
|
86
|
+
const spinner = ora({ text: 'Scanning for secrets and vulnerabilities...', color: 'cyan' }).start();
|
|
87
|
+
|
|
88
|
+
let findings = [];
|
|
89
|
+
let filesScanned = 0;
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
const files = await findFiles(absolutePath);
|
|
93
|
+
filesScanned = files.length;
|
|
94
|
+
spinner.text = `Scanning ${files.length} files...`;
|
|
95
|
+
|
|
96
|
+
for (const file of files) {
|
|
97
|
+
const fileFindings = scanFile(file);
|
|
98
|
+
findings = findings.concat(fileFindings);
|
|
99
|
+
}
|
|
100
|
+
spinner.stop();
|
|
101
|
+
} catch (err) {
|
|
102
|
+
spinner.fail('Scan failed');
|
|
103
|
+
output.error(err.message);
|
|
104
|
+
process.exit(1);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const secretFindings = findings.filter(f => f.category !== 'vulnerability');
|
|
108
|
+
const vulnFindings = findings.filter(f => f.category === 'vulnerability');
|
|
109
|
+
|
|
110
|
+
// ── 2. Dependency audit ──────────────────────────────────────────────────────
|
|
111
|
+
let depVulns = [];
|
|
112
|
+
let pm = null;
|
|
113
|
+
|
|
114
|
+
if (runDeps) {
|
|
115
|
+
const depSpinner = ora({ text: 'Auditing dependencies...', color: 'cyan' }).start();
|
|
116
|
+
try {
|
|
117
|
+
const result = await runDepsAudit(absolutePath);
|
|
118
|
+
pm = result.pm;
|
|
119
|
+
depVulns = result.vulns;
|
|
120
|
+
depSpinner.stop();
|
|
121
|
+
} catch {
|
|
122
|
+
depSpinner.stop();
|
|
123
|
+
// Dep audit failure doesn't block scoring — just skip
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ── 3. Compute score ─────────────────────────────────────────────────────────
|
|
128
|
+
const { score, secretDeduction, vulnDeduction, depDeduction, secretCounts, vulnCounts, depCounts } =
|
|
129
|
+
computeScore(secretFindings, vulnFindings, depVulns);
|
|
130
|
+
|
|
131
|
+
const grade = GRADES.find(g => score >= g.min);
|
|
132
|
+
|
|
133
|
+
// ── 4. Print results ─────────────────────────────────────────────────────────
|
|
134
|
+
printScore(score, grade, {
|
|
135
|
+
secretDeduction, vulnDeduction, depDeduction,
|
|
136
|
+
secretCounts, vulnCounts, depCounts,
|
|
137
|
+
filesScanned, pm, runDeps
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Exit 0 for A/B, exit 1 for C/D/F
|
|
141
|
+
process.exit(score >= 75 ? 0 : 1);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// =============================================================================
|
|
145
|
+
// SCORE COMPUTATION
|
|
146
|
+
// =============================================================================
|
|
147
|
+
|
|
148
|
+
function computeScore(secretFindings, vulnFindings, depVulns) {
|
|
149
|
+
// ── Count by severity ────────────────────────────────────────────────────────
|
|
150
|
+
const secretCounts = countBySeverity(secretFindings);
|
|
151
|
+
const vulnCounts = countBySeverity(vulnFindings);
|
|
152
|
+
const depCounts = countBySeverity(depVulns);
|
|
153
|
+
|
|
154
|
+
// ── Compute deductions ───────────────────────────────────────────────────────
|
|
155
|
+
let secretDeduction = 0;
|
|
156
|
+
for (const [sev, pts] of Object.entries(SECRET_DEDUCTIONS)) {
|
|
157
|
+
secretDeduction += (secretCounts[sev] || 0) * pts;
|
|
158
|
+
}
|
|
159
|
+
secretDeduction = Math.min(secretDeduction, SECRET_CAP);
|
|
160
|
+
|
|
161
|
+
let vulnDeduction = 0;
|
|
162
|
+
for (const [sev, pts] of Object.entries(VULN_DEDUCTIONS)) {
|
|
163
|
+
vulnDeduction += (vulnCounts[sev] || 0) * pts;
|
|
164
|
+
}
|
|
165
|
+
vulnDeduction = Math.min(vulnDeduction, VULN_CAP);
|
|
166
|
+
|
|
167
|
+
let depDeduction = 0;
|
|
168
|
+
for (const [sev, pts] of Object.entries(DEP_DEDUCTIONS)) {
|
|
169
|
+
depDeduction += (depCounts[sev] || 0) * pts;
|
|
170
|
+
}
|
|
171
|
+
depDeduction = Math.min(depDeduction, DEP_CAP);
|
|
172
|
+
|
|
173
|
+
const score = Math.max(0, 100 - secretDeduction - vulnDeduction - depDeduction);
|
|
174
|
+
|
|
175
|
+
return { score, secretDeduction, vulnDeduction, depDeduction, secretCounts, vulnCounts, depCounts };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function countBySeverity(findings) {
|
|
179
|
+
const counts = {};
|
|
180
|
+
for (const f of findings) {
|
|
181
|
+
const sev = f.severity || 'unknown';
|
|
182
|
+
counts[sev] = (counts[sev] || 0) + 1;
|
|
183
|
+
}
|
|
184
|
+
return counts;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// =============================================================================
|
|
188
|
+
// OUTPUT
|
|
189
|
+
// =============================================================================
|
|
190
|
+
|
|
191
|
+
const GRADE_COLOR = {
|
|
192
|
+
A: chalk.green.bold,
|
|
193
|
+
B: chalk.cyan.bold,
|
|
194
|
+
C: chalk.yellow.bold,
|
|
195
|
+
D: chalk.red,
|
|
196
|
+
F: chalk.red.bold,
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
function printScore(score, grade, ctx) {
|
|
200
|
+
const gradeColor = GRADE_COLOR[grade.letter] || chalk.white;
|
|
201
|
+
const scoreColor = score >= 75 ? chalk.green.bold : score >= 60 ? chalk.yellow.bold : chalk.red.bold;
|
|
202
|
+
|
|
203
|
+
// ── Score headline ───────────────────────────────────────────────────────────
|
|
204
|
+
console.log(
|
|
205
|
+
chalk.white.bold(' Ship Safe Score: ') +
|
|
206
|
+
scoreColor(`${score}/100`) +
|
|
207
|
+
chalk.gray(' ') +
|
|
208
|
+
gradeColor(`${grade.letter}`) +
|
|
209
|
+
chalk.gray(` — ${grade.label}`)
|
|
210
|
+
);
|
|
211
|
+
console.log(chalk.cyan(' ' + '─'.repeat(58)));
|
|
212
|
+
console.log();
|
|
213
|
+
|
|
214
|
+
// ── Row: Secrets ─────────────────────────────────────────────────────────────
|
|
215
|
+
const secretCount = Object.values(ctx.secretCounts).reduce((a, b) => a + b, 0);
|
|
216
|
+
const secretIcon = secretCount === 0 ? chalk.green('✔') : chalk.red('✘');
|
|
217
|
+
const secretStatus = secretCount === 0
|
|
218
|
+
? chalk.green('0 found')
|
|
219
|
+
: chalk.red(`${secretCount} found`);
|
|
220
|
+
const secretDeductStr = ctx.secretDeduction === 0
|
|
221
|
+
? chalk.gray('+0 deductions')
|
|
222
|
+
: chalk.red(`−${ctx.secretDeduction} points`) + chalk.gray(` (${formatCounts(ctx.secretCounts)})`);
|
|
223
|
+
|
|
224
|
+
console.log(
|
|
225
|
+
` ${secretIcon} ${chalk.white.bold('Secrets ')} ${secretStatus.padEnd(18)} ${secretDeductStr}`
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
// ── Row: Code Vulns ───────────────────────────────────────────────────────────
|
|
229
|
+
const vulnCount = Object.values(ctx.vulnCounts).reduce((a, b) => a + b, 0);
|
|
230
|
+
const vulnIcon = vulnCount === 0 ? chalk.green('✔') : chalk.yellow('✘');
|
|
231
|
+
const vulnStatus = vulnCount === 0
|
|
232
|
+
? chalk.green('0 found')
|
|
233
|
+
: chalk.yellow(`${vulnCount} found`);
|
|
234
|
+
const vulnDeductStr = ctx.vulnDeduction === 0
|
|
235
|
+
? chalk.gray('+0 deductions')
|
|
236
|
+
: chalk.yellow(`−${ctx.vulnDeduction} points`) + chalk.gray(` (${formatCounts(ctx.vulnCounts)})`);
|
|
237
|
+
|
|
238
|
+
console.log(
|
|
239
|
+
` ${vulnIcon} ${chalk.white.bold('Code Vulns ')} ${vulnStatus.padEnd(18)} ${vulnDeductStr}`
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
// ── Row: Dependencies ─────────────────────────────────────────────────────────
|
|
243
|
+
if (ctx.runDeps) {
|
|
244
|
+
const depCount = Object.values(ctx.depCounts).reduce((a, b) => a + b, 0);
|
|
245
|
+
const depIcon = depCount === 0 ? chalk.green('✔') : chalk.red('✘');
|
|
246
|
+
const depLabel = ctx.pm ? `Dependencies ` : 'Dependencies ';
|
|
247
|
+
|
|
248
|
+
let depStatus, depDeductStr;
|
|
249
|
+
|
|
250
|
+
if (!ctx.pm) {
|
|
251
|
+
depStatus = chalk.gray('no manifest');
|
|
252
|
+
depDeductStr = chalk.gray('+0 deductions');
|
|
253
|
+
} else if (depCount === 0) {
|
|
254
|
+
depStatus = chalk.green('0 CVEs');
|
|
255
|
+
depDeductStr = chalk.gray('+0 deductions');
|
|
256
|
+
} else {
|
|
257
|
+
depStatus = chalk.red(`${depCount} CVEs`);
|
|
258
|
+
depDeductStr = chalk.red(`−${ctx.depDeduction} points`) + chalk.gray(` (${formatCounts(ctx.depCounts)})`);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
console.log(
|
|
262
|
+
` ${depIcon} ${chalk.white.bold(depLabel)} ${depStatus.padEnd(18)} ${depDeductStr}`
|
|
263
|
+
);
|
|
264
|
+
} else {
|
|
265
|
+
console.log(
|
|
266
|
+
` ${chalk.gray('–')} ${chalk.gray('Dependencies skipped (--no-deps)')}`
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
console.log();
|
|
271
|
+
console.log(chalk.cyan(' ' + '─'.repeat(58)));
|
|
272
|
+
console.log(chalk.gray(` Files scanned: ${ctx.filesScanned}`));
|
|
273
|
+
|
|
274
|
+
// ── Next steps ────────────────────────────────────────────────────────────────
|
|
275
|
+
if (score < 100) {
|
|
276
|
+
console.log();
|
|
277
|
+
const actions = [];
|
|
278
|
+
if (Object.values(ctx.secretCounts).some(n => n > 0)) {
|
|
279
|
+
actions.push(chalk.white(' npx ship-safe agent .') + chalk.gray(' # AI audit: classify + auto-fix secrets'));
|
|
280
|
+
}
|
|
281
|
+
if (Object.values(ctx.vulnCounts).some(n => n > 0)) {
|
|
282
|
+
actions.push(chalk.white(' npx ship-safe agent .') + chalk.gray(' # AI audit: classify + fix suggestions'));
|
|
283
|
+
}
|
|
284
|
+
if (ctx.runDeps && Object.values(ctx.depCounts).some(n => n > 0) && ctx.pm) {
|
|
285
|
+
actions.push(chalk.white(` npx ship-safe deps .`) + chalk.gray(' # See full dependency CVE details'));
|
|
286
|
+
}
|
|
287
|
+
if (actions.length > 0) {
|
|
288
|
+
console.log(chalk.gray(' Fix issues:'));
|
|
289
|
+
// Deduplicate (agent appears for both secrets and vulns)
|
|
290
|
+
const seen = new Set();
|
|
291
|
+
for (const a of actions) {
|
|
292
|
+
if (!seen.has(a)) { console.log(a); seen.add(a); }
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
} else {
|
|
296
|
+
console.log();
|
|
297
|
+
console.log(chalk.green(' All clear — safe to ship!'));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
console.log(chalk.cyan('='.repeat(60)));
|
|
301
|
+
console.log();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function formatCounts(counts) {
|
|
305
|
+
const SEV_ORDER = ['critical', 'high', 'moderate', 'medium', 'low'];
|
|
306
|
+
return SEV_ORDER
|
|
307
|
+
.filter(s => counts[s] > 0)
|
|
308
|
+
.map(s => `${counts[s]} ${s}`)
|
|
309
|
+
.join(', ');
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// =============================================================================
|
|
313
|
+
// INTERNAL SCAN (no subprocess — import patterns directly)
|
|
314
|
+
// =============================================================================
|
|
315
|
+
|
|
316
|
+
const ALL_PATTERNS = [...SECRET_PATTERNS, ...SECURITY_PATTERNS];
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Find all scannable files (same logic as scan.js, without test-exclusion
|
|
320
|
+
* and without .ship-safeignore loading — score is a quick overview).
|
|
321
|
+
*/
|
|
322
|
+
async function findFiles(rootPath) {
|
|
323
|
+
const globIgnore = Array.from(SKIP_DIRS).map(dir => `**/${dir}/**`);
|
|
324
|
+
|
|
325
|
+
const files = await fg('**/*', {
|
|
326
|
+
cwd: rootPath,
|
|
327
|
+
absolute: true,
|
|
328
|
+
onlyFiles: true,
|
|
329
|
+
ignore: globIgnore,
|
|
330
|
+
dot: true
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
const filtered = [];
|
|
334
|
+
|
|
335
|
+
for (const file of files) {
|
|
336
|
+
const ext = path.extname(file).toLowerCase();
|
|
337
|
+
if (SKIP_EXTENSIONS.has(ext)) continue;
|
|
338
|
+
|
|
339
|
+
const basename = path.basename(file);
|
|
340
|
+
if (basename.endsWith('.min.js') || basename.endsWith('.min.css')) continue;
|
|
341
|
+
|
|
342
|
+
// Load and respect .ship-safeignore
|
|
343
|
+
if (isIgnoredByFile(file, rootPath)) continue;
|
|
344
|
+
|
|
345
|
+
try {
|
|
346
|
+
const stats = fs.statSync(file);
|
|
347
|
+
if (stats.size > MAX_FILE_SIZE) continue;
|
|
348
|
+
} catch {
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
filtered.push(file);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return filtered;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Cache ignore patterns per root to avoid re-reading the file thousands of times
|
|
359
|
+
const _ignoreCache = new Map();
|
|
360
|
+
|
|
361
|
+
function loadIgnorePatterns(rootPath) {
|
|
362
|
+
if (_ignoreCache.has(rootPath)) return _ignoreCache.get(rootPath);
|
|
363
|
+
|
|
364
|
+
const ignorePath = path.join(rootPath, '.ship-safeignore');
|
|
365
|
+
let patterns = [];
|
|
366
|
+
|
|
367
|
+
if (fs.existsSync(ignorePath)) {
|
|
368
|
+
try {
|
|
369
|
+
patterns = fs.readFileSync(ignorePath, 'utf-8')
|
|
370
|
+
.split('\n')
|
|
371
|
+
.map(l => l.trim())
|
|
372
|
+
.filter(l => l && !l.startsWith('#'));
|
|
373
|
+
} catch {
|
|
374
|
+
// ignore read error
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
_ignoreCache.set(rootPath, patterns);
|
|
379
|
+
return patterns;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function isIgnoredByFile(filePath, rootPath) {
|
|
383
|
+
const patterns = loadIgnorePatterns(rootPath);
|
|
384
|
+
if (patterns.length === 0) return false;
|
|
385
|
+
|
|
386
|
+
const relPath = path.relative(rootPath, filePath).replace(/\\/g, '/');
|
|
387
|
+
|
|
388
|
+
return patterns.some(pattern => {
|
|
389
|
+
if (pattern.endsWith('/')) {
|
|
390
|
+
return relPath.startsWith(pattern) || relPath.includes('/' + pattern);
|
|
391
|
+
}
|
|
392
|
+
const escaped = pattern
|
|
393
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
|
|
394
|
+
.replace(/\*/g, '[^/]*')
|
|
395
|
+
.replace(/\?/g, '[^/]');
|
|
396
|
+
return new RegExp(`(^|/)${escaped}($|/)`).test(relPath);
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Scan a single file and return normalized findings.
|
|
402
|
+
* Same algorithm as scan.js — inline here to avoid circular dependency
|
|
403
|
+
* (scan.js has process.exit() side effects).
|
|
404
|
+
*/
|
|
405
|
+
function scanFile(filePath) {
|
|
406
|
+
const findings = [];
|
|
407
|
+
|
|
408
|
+
try {
|
|
409
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
410
|
+
const lines = content.split('\n');
|
|
411
|
+
|
|
412
|
+
for (let lineNum = 0; lineNum < lines.length; lineNum++) {
|
|
413
|
+
const line = lines[lineNum];
|
|
414
|
+
|
|
415
|
+
if (/ship-safe-ignore/i.test(line)) continue;
|
|
416
|
+
|
|
417
|
+
for (const pattern of ALL_PATTERNS) {
|
|
418
|
+
pattern.pattern.lastIndex = 0;
|
|
419
|
+
|
|
420
|
+
let match;
|
|
421
|
+
while ((match = pattern.pattern.exec(line)) !== null) {
|
|
422
|
+
if (pattern.requiresEntropyCheck && !isHighEntropyMatch(match[0])) {
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
findings.push({
|
|
427
|
+
line: lineNum + 1,
|
|
428
|
+
severity: pattern.severity,
|
|
429
|
+
category: pattern.category || 'secret',
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
} catch {
|
|
435
|
+
// Skip unreadable files
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// Deduplicate: same (line, severity, category)
|
|
439
|
+
const seen = new Set();
|
|
440
|
+
return findings.filter(f => {
|
|
441
|
+
const key = `${f.line}:${f.severity}:${f.category}`;
|
|
442
|
+
if (seen.has(key)) return false;
|
|
443
|
+
seen.add(key);
|
|
444
|
+
return true;
|
|
445
|
+
});
|
|
446
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Watch Command
|
|
3
|
+
* ==============
|
|
4
|
+
*
|
|
5
|
+
* Continuous file monitoring mode. Watches for file changes
|
|
6
|
+
* and incrementally scans modified files.
|
|
7
|
+
*
|
|
8
|
+
* USAGE:
|
|
9
|
+
* npx ship-safe watch [path] Start watching for changes
|
|
10
|
+
* npx ship-safe watch . --poll Use polling (for network drives)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import fs from 'fs';
|
|
14
|
+
import path from 'path';
|
|
15
|
+
import chalk from 'chalk';
|
|
16
|
+
import { SKIP_DIRS, SKIP_EXTENSIONS, SECRET_PATTERNS, SECURITY_PATTERNS } from '../utils/patterns.js';
|
|
17
|
+
import { isHighEntropyMatch, getConfidence } from '../utils/entropy.js';
|
|
18
|
+
import * as output from '../utils/output.js';
|
|
19
|
+
|
|
20
|
+
export async function watchCommand(targetPath = '.', options = {}) {
|
|
21
|
+
const absolutePath = path.resolve(targetPath);
|
|
22
|
+
|
|
23
|
+
if (!fs.existsSync(absolutePath)) {
|
|
24
|
+
output.error(`Path does not exist: ${absolutePath}`);
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
console.log();
|
|
29
|
+
output.header('Ship Safe — Watch Mode');
|
|
30
|
+
console.log();
|
|
31
|
+
console.log(chalk.cyan(' Watching for file changes...'));
|
|
32
|
+
console.log(chalk.gray(' Press Ctrl+C to stop'));
|
|
33
|
+
console.log();
|
|
34
|
+
|
|
35
|
+
const allPatterns = [...SECRET_PATTERNS, ...SECURITY_PATTERNS];
|
|
36
|
+
const skipDirSet = SKIP_DIRS;
|
|
37
|
+
let debounceTimer = null;
|
|
38
|
+
const pendingFiles = new Set();
|
|
39
|
+
|
|
40
|
+
// Use fs.watch recursively
|
|
41
|
+
try {
|
|
42
|
+
const watcher = fs.watch(absolutePath, { recursive: true }, (eventType, filename) => {
|
|
43
|
+
if (!filename) return;
|
|
44
|
+
|
|
45
|
+
const fullPath = path.join(absolutePath, filename); // ship-safe-ignore — filename from fs.watch, not user input
|
|
46
|
+
const relPath = filename.replace(/\\/g, '/');
|
|
47
|
+
|
|
48
|
+
// Skip directories we don't care about
|
|
49
|
+
for (const skipDir of skipDirSet) {
|
|
50
|
+
if (relPath.includes(`${skipDir}/`) || relPath.startsWith(`${skipDir}/`)) return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Skip non-code files
|
|
54
|
+
const ext = path.extname(filename).toLowerCase();
|
|
55
|
+
if (SKIP_EXTENSIONS.has(ext)) return;
|
|
56
|
+
if (filename.endsWith('.min.js') || filename.endsWith('.min.css')) return;
|
|
57
|
+
|
|
58
|
+
// Add to pending and debounce
|
|
59
|
+
pendingFiles.add(fullPath);
|
|
60
|
+
|
|
61
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
62
|
+
debounceTimer = setTimeout(() => {
|
|
63
|
+
const filesToScan = [...pendingFiles];
|
|
64
|
+
pendingFiles.clear();
|
|
65
|
+
scanChangedFiles(filesToScan, allPatterns, absolutePath);
|
|
66
|
+
}, 300);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Keep the process alive
|
|
70
|
+
process.on('SIGINT', () => {
|
|
71
|
+
watcher.close();
|
|
72
|
+
console.log();
|
|
73
|
+
output.info('Watch mode stopped.');
|
|
74
|
+
process.exit(0);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Prevent Node from exiting
|
|
78
|
+
setInterval(() => {}, 1000 * 60 * 60);
|
|
79
|
+
|
|
80
|
+
} catch (err) {
|
|
81
|
+
output.error(`Watch failed: ${err.message}`);
|
|
82
|
+
console.log(chalk.gray(' Try: npx ship-safe watch . --poll'));
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function scanChangedFiles(files, patterns, rootPath) {
|
|
88
|
+
const timestamp = new Date().toLocaleTimeString();
|
|
89
|
+
let totalFindings = 0;
|
|
90
|
+
|
|
91
|
+
for (const filePath of files) {
|
|
92
|
+
if (!fs.existsSync(filePath)) continue;
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
const stats = fs.statSync(filePath);
|
|
96
|
+
if (stats.size > 1_000_000) continue;
|
|
97
|
+
} catch {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const findings = scanFile(filePath, patterns);
|
|
102
|
+
if (findings.length > 0) {
|
|
103
|
+
totalFindings += findings.length;
|
|
104
|
+
const relPath = path.relative(rootPath, filePath);
|
|
105
|
+
|
|
106
|
+
for (const f of findings) {
|
|
107
|
+
const sevColor = f.severity === 'critical' ? chalk.red.bold
|
|
108
|
+
: f.severity === 'high' ? chalk.yellow
|
|
109
|
+
: chalk.blue;
|
|
110
|
+
|
|
111
|
+
console.log(
|
|
112
|
+
chalk.gray(` [${timestamp}] `) +
|
|
113
|
+
sevColor(`[${f.severity.toUpperCase()}]`) +
|
|
114
|
+
chalk.white(` ${relPath}:${f.line} `) +
|
|
115
|
+
chalk.gray(f.patternName)
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (totalFindings === 0 && files.length > 0) {
|
|
122
|
+
console.log(chalk.gray(` [${timestamp}] ${files.length} file(s) scanned — clean`));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function scanFile(filePath, patterns) {
|
|
127
|
+
const findings = [];
|
|
128
|
+
try {
|
|
129
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
130
|
+
const lines = content.split('\n');
|
|
131
|
+
|
|
132
|
+
for (let i = 0; i < lines.length; i++) {
|
|
133
|
+
const line = lines[i];
|
|
134
|
+
if (/ship-safe-ignore/i.test(line)) continue;
|
|
135
|
+
|
|
136
|
+
for (const pattern of patterns) {
|
|
137
|
+
pattern.pattern.lastIndex = 0;
|
|
138
|
+
let match;
|
|
139
|
+
while ((match = pattern.pattern.exec(line)) !== null) {
|
|
140
|
+
if (pattern.requiresEntropyCheck && !isHighEntropyMatch(match[0])) continue;
|
|
141
|
+
findings.push({
|
|
142
|
+
line: i + 1,
|
|
143
|
+
patternName: pattern.name,
|
|
144
|
+
severity: pattern.severity,
|
|
145
|
+
matched: match[0],
|
|
146
|
+
category: pattern.category || 'secret',
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
} catch { /* skip */ }
|
|
152
|
+
|
|
153
|
+
const seen = new Set();
|
|
154
|
+
return findings.filter(f => {
|
|
155
|
+
const key = `${f.line}:${f.matched}`;
|
|
156
|
+
if (seen.has(key)) return false;
|
|
157
|
+
seen.add(key);
|
|
158
|
+
return true;
|
|
159
|
+
});
|
|
160
|
+
}
|
package/cli/index.js
CHANGED
|
@@ -2,11 +2,49 @@
|
|
|
2
2
|
* Ship Safe CLI - Module Entry Point
|
|
3
3
|
* ===================================
|
|
4
4
|
*
|
|
5
|
-
* This file exports the CLI commands for programmatic use.
|
|
5
|
+
* This file exports the CLI commands and agents for programmatic use.
|
|
6
6
|
* For normal CLI usage, run: npx ship-safe
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
// ── Core Commands ─────────────────────────────────────────────────────────────
|
|
9
10
|
export { scanCommand } from './commands/scan.js';
|
|
10
11
|
export { checklistCommand } from './commands/checklist.js';
|
|
11
12
|
export { initCommand } from './commands/init.js';
|
|
12
|
-
export {
|
|
13
|
+
export { agentCommand } from './commands/agent.js';
|
|
14
|
+
export { depsCommand, runDepsAudit } from './commands/deps.js';
|
|
15
|
+
export { scoreCommand } from './commands/score.js';
|
|
16
|
+
|
|
17
|
+
// ── v4.0 Commands ─────────────────────────────────────────────────────────────
|
|
18
|
+
export { auditCommand } from './commands/audit.js';
|
|
19
|
+
export { redTeamCommand } from './commands/red-team.js';
|
|
20
|
+
export { watchCommand } from './commands/watch.js';
|
|
21
|
+
|
|
22
|
+
// ── Patterns ──────────────────────────────────────────────────────────────────
|
|
23
|
+
export { SECRET_PATTERNS, SECURITY_PATTERNS, SKIP_DIRS, SKIP_EXTENSIONS } from './utils/patterns.js';
|
|
24
|
+
|
|
25
|
+
// ── Agent Framework ───────────────────────────────────────────────────────────
|
|
26
|
+
export { BaseAgent, createFinding } from './agents/base-agent.js';
|
|
27
|
+
export { Orchestrator } from './agents/orchestrator.js';
|
|
28
|
+
export { buildOrchestrator } from './agents/index.js';
|
|
29
|
+
|
|
30
|
+
// ── Individual Agents ─────────────────────────────────────────────────────────
|
|
31
|
+
export { ReconAgent } from './agents/recon-agent.js';
|
|
32
|
+
export { InjectionTester } from './agents/injection-tester.js';
|
|
33
|
+
export { AuthBypassAgent } from './agents/auth-bypass-agent.js';
|
|
34
|
+
export { SSRFProber } from './agents/ssrf-prober.js';
|
|
35
|
+
export { SupplyChainAudit } from './agents/supply-chain-agent.js';
|
|
36
|
+
export { ConfigAuditor } from './agents/config-auditor.js';
|
|
37
|
+
export { LLMRedTeam } from './agents/llm-redteam.js';
|
|
38
|
+
export { MobileScanner } from './agents/mobile-scanner.js';
|
|
39
|
+
export { GitHistoryScanner } from './agents/git-history-scanner.js';
|
|
40
|
+
export { CICDScanner } from './agents/cicd-scanner.js';
|
|
41
|
+
export { APIFuzzer } from './agents/api-fuzzer.js';
|
|
42
|
+
|
|
43
|
+
// ── Supporting Modules ────────────────────────────────────────────────────────
|
|
44
|
+
export { ScoringEngine, GRADES, CATEGORIES } from './agents/scoring-engine.js';
|
|
45
|
+
export { SBOMGenerator } from './agents/sbom-generator.js';
|
|
46
|
+
export { PolicyEngine } from './agents/policy-engine.js';
|
|
47
|
+
export { HTMLReporter } from './agents/html-reporter.js';
|
|
48
|
+
|
|
49
|
+
// ── LLM Providers ─────────────────────────────────────────────────────────────
|
|
50
|
+
export { createProvider, autoDetectProvider } from './providers/llm-provider.js';
|