-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
648 lines (588 loc) · 25.3 KB
/
Copy pathcli.js
File metadata and controls
648 lines (588 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#!/usr/bin/env node
/**
* secondsight -- command line
*
* npx secondsight README.md
* cat suspicious.txt | npx secondsight
* npx secondsight . --fail-on high # for CI
* npx secondsight --staged # for a pre-commit hook
* npx secondsight --compare mine.md leaked.md
*
* Same engine as the web page, no network, no dependencies.
*/
import { readFileSync, writeFileSync, statSync, lstatSync, readdirSync } from 'node:fs';
import { execFileSync } from 'node:child_process';
import { join, relative, resolve, extname, basename } from 'node:path';
import { analyze, verdictFor, SEVERITY } from './src/detect.js';
import { sanitize } from './src/sanitize.js';
import { buildSarif } from './src/sarif.js';
import { compare, markSummary } from './src/compare.js';
import { buildAnnotations } from './src/annotate.js';
const VERSION = '1.7.0';
const USAGE = `
secondsight -- find the text you cannot see
secondsight [options] [file|directory ...]
With no path, reads standard input.
Options
--json machine-readable output
--sarif [file] SARIF 2.1.0 for GitHub code scanning (stdout, or to <file>)
--fix rewrite files with hidden characters removed
--staged scan only the files staged in git -- for a pre-commit hook
--compare <a> <b> two copies of one document: find the invisible difference
--fail-on <level> exit 1 at or above this severity
(info|low|medium|high|critical; default: high)
--ignore <ids> comma-separated finding ids to drop (for corpora of
deliberate samples: test fixtures, security write-ups)
--exclude <path> skip a file or directory; repeatable
--all report every file, not just the ones with findings
--no-gitignore also scan untracked files that .gitignore excludes
--no-annotations inside GitHub Actions, do not mark findings on the PR
--no-color plain output
-h, --help this
--ignore is a command-line flag on purpose. Nothing written inside the text
being scanned can silence a finding, because the text being scanned is
exactly the thing you do not trust.
`;
// ---------------------------------------------------------------------------
const args = process.argv.slice(2);
const opts = {
json: false, sarif: false, sarifPath: null,
fix: false, all: false, color: true, staged: false, compare: false,
failOn: 3, failOnExplicit: false, paths: [], gitignore: true, annotations: true,
ignore: new Set(), exclude: [],
};
for (let i = 0; i < args.length; i++) {
const a = args[i];
if (a === '--json') opts.json = true;
else if (a === '--sarif') {
opts.sarif = true;
// Optional filename: the next arg, unless it is another flag or a path
// that exists (which would be a scan target, not the output file).
const next = args[i + 1];
if (next && !next.startsWith('-') && !existsAsPath(next)) {
opts.sarifPath = next;
i++;
}
} else if (a === '--fix') opts.fix = true;
else if (a === '--staged') opts.staged = true;
else if (a === '--compare') opts.compare = true;
else if (a === '--ignore') {
for (const id of String(args[++i] || '').split(',')) {
if (id.trim()) opts.ignore.add(id.trim());
}
} else if (a === '--exclude') {
const p = String(args[++i] || '').trim();
if (p) opts.exclude.push(slash(p));
} else if (a === '--all') opts.all = true;
else if (a === '--no-gitignore') opts.gitignore = false;
else if (a === '--no-annotations') opts.annotations = false;
else if (a === '--no-color') opts.color = false;
else if (a === '--fail-on') {
const level = String(args[++i] || '').toUpperCase();
const idx = SEVERITY.indexOf(level);
if (idx < 0) { fail('unknown severity: ' + level); }
opts.failOn = idx;
opts.failOnExplicit = true;
} else if (a === '-h' || a === '--help') { process.stdout.write(USAGE); process.exit(0); }
else if (a === '--version' || a === '-v') { process.stdout.write(VERSION + '\n'); process.exit(0); }
else if (a.startsWith('-')) fail('unknown option: ' + a);
else opts.paths.push(a);
}
function existsAsPath(p) {
try { statSync(p); return true; } catch { return false; }
}
/** One path spelling, so a Windows backslash and a POSIX slash compare equal. */
function slash(p) {
return String(p).replace(/\\/g, '/').replace(/^\.\//, '').replace(/\/+$/, '');
}
/**
* A path as it is safe to print.
*
* A filename is attacker-controlled text too. On Linux it can hold an escape
* sequence, which would repaint the terminal this report is printed to, or a
* newline, or start with `::`, which the GitHub Actions runner reads as a
* workflow command. So control characters are shown as escapes, and a path
* that would open a line with `::` is written with a leading `./`.
*/
function shown(path) {
const s = String(path).replace(/[\x00-\x1f\x7f-\x9f]/g,
(c) => '\\x' + c.charCodeAt(0).toString(16).padStart(2, '0'));
return /^\s*::/.test(s) ? './' + s : s;
}
function fail(message) {
process.stderr.write('secondsight: ' + message + '\n' + USAGE);
process.exit(2);
}
// ---------------------------------------------------------------------------
const useColor = opts.color && process.stdout.isTTY && !process.env.NO_COLOR;
const paint = (code, s) => (useColor ? '\x1b[' + code + 'm' + s + '\x1b[0m' : s);
const dim = (s) => paint('2', s);
const bold = (s) => paint('1', s);
const SEVERITY_COLOR = ['2', '2', '33', '31', '1;31'];
// Files an agent, a build or a reviewer is likely to read as text.
const TEXT_EXTENSIONS = new Set([
'.md', '.markdown', '.mdx', '.mdc', '.txt', '.rst', '.adoc', '.tex',
'.json', '.jsonc', '.json5', '.yaml', '.yml', '.toml', '.ini', '.cfg', '.conf',
'.properties', '.env', '.xml', '.svg', '.csv', '.tsv', '.ipynb', '.lock',
'.js', '.mjs', '.cjs', '.ts', '.mts', '.cts', '.tsx', '.jsx', '.vue', '.svelte',
'.py', '.rb', '.go', '.rs', '.java', '.kt', '.kts', '.scala', '.swift', '.dart',
'.c', '.h', '.cc', '.cpp', '.hpp', '.cs', '.fs', '.php', '.pl', '.lua', '.r',
'.ex', '.exs', '.erl', '.hs', '.clj', '.el', '.vim', '.zig', '.nix',
'.sh', '.bash', '.zsh', '.fish', '.ps1', '.psm1', '.bat', '.cmd',
'.html', '.htm', '.css', '.scss', '.less', '.sql', '.graphql', '.gql', '.proto',
'.tf', '.hcl', '.gradle', '.cmake', '.mk',
]);
// Read by name: build files with no extension, and the instruction files an
// agent loads on startup -- the one place a hidden sentence is sure of a reader.
const ALWAYS_READ = new Set([
'CLAUDE.md', 'AGENTS.md', 'GEMINI.md', 'SKILL.md', 'README.md',
'Dockerfile', 'Containerfile', 'Makefile', 'Jenkinsfile', 'Procfile', 'Vagrantfile',
'Gemfile', 'Rakefile', 'Brewfile', 'CODEOWNERS', 'LICENSE',
'.cursorrules', '.windsurfrules', '.clinerules', '.roorules', '.goosehints',
'.gitattributes', '.gitignore', '.gitmodules', '.npmrc', '.yarnrc', '.editorconfig',
'.env', '.envrc',
]);
// Oversized files are reported, never skipped in silence: padding a file past
// a scanner's size limit is the cheapest way around the scanner.
const MAX_BYTES = 8 * 1024 * 1024;
const SKIP_DIRS = new Set([
'node_modules', '.git', 'dist', 'build', 'vendor', 'target',
'.next', '.venv', '__pycache__', 'coverage',
]);
function isReadable(path) {
const name = basename(path);
// extname('.env') is '' and extname('.env.production') is '.production', so
// the whole family of environment files has to be named outright.
return ALWAYS_READ.has(name) || /^\.env(?:\.|$)/i.test(name)
|| TEXT_EXTENSIONS.has(extname(name).toLowerCase());
}
/** Files that were not scanned, and why. Leaving them out of the report would be lying by omission. */
const skipped = [];
function skip(path, reason) {
skipped.push({ path, reason });
}
/**
* A file's text, whatever it was saved as.
*
* A UTF-16 file read as UTF-8 comes out as a NUL between every letter: still
* flagged, but its payload is never decoded, and the decode is the half that
* matters. A byte-order mark says which it is; without one, NUL bytes in the
* first 8 KB mean binary. Returns null for binary.
*/
function readText(path) {
return decodeBytes(readFileSync(path));
}
function decodeBytes(bytes) {
if (bytes[0] === 0xff && bytes[1] === 0xfe) {
return { text: bytes.subarray(2).toString('utf16le'), encoding: 'utf16le' };
}
if (bytes[0] === 0xfe && bytes[1] === 0xff) {
const body = Buffer.from(bytes.subarray(2, 2 + ((bytes.length - 2) & ~1)));
return { text: body.swap16().toString('utf16le'), encoding: 'utf16be' };
}
if (bytes.subarray(0, 8192).includes(0)) return null;
return { text: bytes.toString('utf8'), encoding: 'utf8' };
}
/** The inverse of readText, so --fix leaves a file in the encoding it found it in. */
function encodeAs(text, encoding) {
if (encoding === 'utf8') return Buffer.from(text, 'utf8');
const body = Buffer.from(text, 'utf16le');
if (encoding === 'utf16be') body.swap16();
const bom = Buffer.from(encoding === 'utf16be' ? [0xfe, 0xff] : [0xff, 0xfe]);
return Buffer.concat([bom, body]);
}
function isExcluded(path) {
const here = slash(relative(process.cwd(), path) || path);
const there = slash(path);
return opts.exclude.some((p) => here === p || there === p
|| here.startsWith(p + '/') || there.startsWith(p + '/'));
}
/**
* Drop the findings the caller asked to ignore, and restate the verdict from
* what is left. A filtered report has to be honest about its own severity --
* silently keeping the old CRITICAL banner over an empty list would be worse
* than not filtering at all.
*/
function withIgnores(result) {
if (!opts.ignore.size) return result;
const findings = result.findings.filter((f) => !opts.ignore.has(f.id));
if (findings.length === result.findings.length) return result;
const worstLeft = findings.length ? Math.max(...findings.map((f) => f.severity)) : -1;
return { ...result, findings, verdict: verdictFor(worstLeft) };
}
// ---------------------------------------------------------------------------
// .gitignore
// ---------------------------------------------------------------------------
/**
* Files an agent loads by name whether or not git tracks them. CLAUDE.local.md
* exists to be gitignored, and a rules file nobody committed is still the
* first thing an agent in that checkout reads -- so ignoring it would skip the
* one file most certain to have a machine reader.
*/
const AGENT_FILES = new Set([
'CLAUDE.md', 'CLAUDE.local.md', 'AGENTS.md', 'AGENTS.override.md', 'GEMINI.md', 'SKILL.md',
'copilot-instructions.md', '.cursorrules', '.windsurfrules', '.clinerules', '.roorules', '.goosehints',
]);
function isAgentFile(path) {
const name = basename(path);
return AGENT_FILES.has(name) || extname(name).toLowerCase() === '.mdc';
}
/** Paths git would not commit, as absolute slash-paths; directories end in '/'. */
const gitIgnored = new Set();
/** How many ignored paths the walk actually stepped over, for the report. */
let ignoredCount = 0;
/**
* Ask git which untracked paths under a directory its ignore rules exclude.
*
* Only untracked paths can be ignored: a tracked file is scanned whatever
* .gitignore says, so an ignore rule can hide a build directory from the scan
* but never a file that is in the history. Outside a repository, or without
* git, nothing is ignored and the walk is exactly what it was.
*/
function loadGitIgnored(dir) {
let out;
try {
out = execFileSync('git', ['ls-files', '-z', '--others', '--ignored', '--exclude-standard', '--directory'], {
cwd: dir, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], maxBuffer: 64 * 1024 * 1024,
});
} catch {
return;
}
for (const entry of out.split('\0')) {
if (!entry) continue;
const abs = slash(resolve(dir, entry));
gitIgnored.add(entry.endsWith('/') ? abs + '/' : abs);
}
}
function isGitIgnored(path, isDir) {
if (!gitIgnored.size) return false;
const abs = slash(resolve(path));
return gitIgnored.has(abs) || (isDir && gitIgnored.has(abs + '/'));
}
function collect(path, out = [], named = true) {
let st;
try { st = named ? statSync(path) : lstatSync(path); } catch { fail('cannot read ' + path); }
if (isExcluded(path)) return out;
if (named && st.isDirectory() && opts.gitignore) loadGitIgnored(path);
if (!named && isGitIgnored(path, st.isDirectory()) && !(st.isFile() && isAgentFile(path))) {
ignoredCount++;
return out;
}
// A path named on the command line is followed wherever it points. A link
// met while walking is not: it can lead out of the tree, or round in a loop.
if (st.isSymbolicLink()) return out;
if (st.isDirectory()) {
if (!named && SKIP_DIRS.has(basename(path))) return out;
for (const entry of readdirSync(path)) collect(join(path, entry), out, false);
return out;
}
// A file someone named is read whatever its extension; they asked for it.
if (!named && !isReadable(path)) return out;
if (st.size > MAX_BYTES) {
skip(path, 'larger than ' + (MAX_BYTES >> 20) + ' MB');
return out;
}
out.push(path);
return out;
}
function readStdin() {
try { return readFileSync(0, 'utf8'); } catch { return ''; }
}
// ---------------------------------------------------------------------------
function reportText(label, result) {
const sev = result.verdict.severity;
if (sev < 0 && !opts.all) return;
const tag = sev < 0 ? paint('32', 'CLEAN') : paint(SEVERITY_COLOR[sev], result.verdict.label);
process.stdout.write('\n' + bold(shown(label)) + ' ' + tag + '\n');
if (sev < 0) return;
process.stdout.write(dim(' ' + result.verdict.line) + '\n');
process.stdout.write(dim(
' ' + result.stats.visible + ' visible, ' + result.stats.hidden + ' hidden'
+ (result.stats.benignHidden ? ', ' + result.stats.benignHidden + ' expected' : '')
+ ' (' + result.stats.codepoints + ' codepoints)',
) + '\n');
for (const f of result.findings) {
const level = SEVERITY[f.severity].padEnd(10);
process.stdout.write(
' ' + paint(SEVERITY_COLOR[f.severity], level) + f.title
+ dim(' x' + f.count) + '\n',
);
if (f.decoded) {
const preview = f.decoded.replace(/\s+/g, ' ').slice(0, 160);
process.stdout.write(' ' + paint('36', 'decoded: ') + JSON.stringify(preview) + '\n');
}
if (f.intents.length) {
process.stdout.write(' ' + dim('reads as: ' + f.intents.map((i) => i.label).join(', ')) + '\n');
}
if (f.samples.length) {
process.stdout.write(' ' + dim(f.samples.slice(0, 4).join(' ')) + '\n');
}
if (f.reference) {
process.stdout.write(' ' + dim(f.reference) + '\n');
}
}
}
// ---------------------------------------------------------------------------
// --compare: two copies of one document
// ---------------------------------------------------------------------------
function wrapText(text, width, indent) {
const lines = [];
let line = '';
for (const word of text.split(/\s+/)) {
if (line && (line + ' ' + word).length > width) { lines.push(line); line = word; }
else line = line ? line + ' ' + word : word;
}
if (line) lines.push(line);
return lines.map((l) => indent + l).join('\n');
}
function runCompare(pathA, pathB) {
let a;
let b;
try { a = readFileSync(pathA, 'utf8'); } catch { fail('cannot read ' + pathA); }
try { b = readFileSync(pathB, 'utf8'); } catch { fail('cannot read ' + pathB); }
const cmp = compare(a, b);
if (opts.json) {
process.stdout.write(JSON.stringify({
version: 1,
relation: cmp.relation,
headline: cmp.headline,
detail: cmp.detail,
sameVisible: cmp.sameVisible,
sameBytes: cmp.sameBytes,
differingCodepoints: cmp.differingCodepoints,
copies: cmp.copies.map((copy, i) => ({
path: i === 0 ? pathA : pathB,
hidden: copy.hidden,
signature: copy.signature,
payloads: copy.payloads.map((p) => ({ id: p.id, decoded: p.decoded })),
})),
differences: cmp.differences.slice(0, 200).map((d) => ({
at: d.at,
context: d.context,
a: d.a ? d.a.abbrs : [],
b: d.b ? d.b.abbrs : [],
})),
}, null, 2) + '\n');
return cmp;
}
const TONE = { empty: '2', identical: '32', marked: '1;31', edited: '33' };
process.stdout.write('\n' + bold(shown(pathA)) + dim(' vs ') + bold(shown(pathB)) + '\n');
process.stdout.write(paint(TONE[cmp.relation], cmp.headline) + '\n');
process.stdout.write(dim(wrapText(cmp.detail, 76, ' ')) + '\n');
for (const copy of cmp.copies) {
const where = copy.label === 'A' ? pathA : pathB;
process.stdout.write(
'\n ' + bold('copy ' + copy.label) + dim(' ' + shown(where)) + '\n'
+ dim(' ' + copy.hidden + ' hidden character' + (copy.hidden === 1 ? '' : 's'))
+ (copy.signature ? dim(' fingerprint ') + paint('36', copy.signature) : '') + '\n',
);
for (const payload of copy.payloads) {
process.stdout.write(
' ' + paint('36', 'says: ')
+ JSON.stringify(payload.decoded.replace(/\s+/g, ' ').slice(0, 160)) + '\n',
);
}
}
if (cmp.differences.length) {
process.stdout.write('\n ' + bold('where they differ') + '\n');
for (const d of cmp.differences.slice(0, 8)) {
const context = d.context ? d.context.replace(/\s+/g, ' ').slice(-24) : '';
process.stdout.write(
dim(' after ' + d.at + ' visible characters')
+ (context ? dim(' ...' + context) : '') + '\n'
+ ' A ' + markSummary(d.a, 10, d.divergeAt) + '\n'
+ ' B ' + markSummary(d.b, 10, d.divergeAt) + '\n',
);
}
if (cmp.differences.length > 8) {
process.stdout.write(dim(' ... and ' + (cmp.differences.length - 8) + ' more') + '\n');
}
}
process.stdout.write('\n');
return cmp;
}
// ---------------------------------------------------------------------------
// --staged: the files git is about to commit
// ---------------------------------------------------------------------------
/** The bytes git is about to commit, keyed by absolute path. Filled by stagedFiles. */
const stagedBlobs = new Map();
/**
* The files staged for commit, and their staged content.
*
* The content comes from the index, not from the working tree. A pre-commit
* hook that reads the working copy can be walked past: stage a file with a
* payload in it, tidy the working copy, commit -- the hook sees the tidy one,
* and the payload goes into history. What is scanned has to be what is
* committed, byte for byte.
*
* `--no-renames` makes a renamed file an added one, so moving a file while
* slipping something into it is scanned like any other change. Symlinks and
* submodules are staged as a link target and a commit id, not as text.
*/
function stagedFiles() {
const git = (args, input) => execFileSync('git', args, {
input, maxBuffer: 256 * 1024 * 1024, stdio: [input === undefined ? 'ignore' : 'pipe', 'pipe', 'ignore'],
});
let top;
let raw;
try {
top = git(['rev-parse', '--show-toplevel']).toString('utf8').trim();
raw = git(['diff', '--cached', '--raw', '-z', '--no-renames', '--diff-filter=ACMT']).toString('utf8');
} catch {
fail('--staged needs to run inside a git repository');
}
// -z --raw: ":<old mode> <new mode> <old sha> <new sha> <status>" NUL <path> NUL
const wanted = [];
const fields = raw.split('\0');
for (let i = 0; i + 1 < fields.length; i += 2) {
const meta = fields[i].replace(/^:/, '').split(' ');
const [, mode, , sha] = meta;
const path = resolve(top, fields[i + 1]);
if (!sha || mode === '120000' || mode === '160000') continue;
if (!isReadable(path) || isExcluded(path)) continue;
wanted.push({ path, sha });
}
if (!wanted.length) return [];
// One process for every blob: "<sha> blob <size>" LF <bytes> LF, per request.
const batch = git(['cat-file', '--batch'], wanted.map((w) => w.sha).join('\n') + '\n');
let at = 0;
const out = [];
for (const { path } of wanted) {
const eol = batch.indexOf(0x0a, at);
const size = Number(batch.subarray(at, eol).toString('utf8').split(' ')[2]);
const bytes = batch.subarray(eol + 1, eol + 1 + size);
at = eol + 1 + size + 1;
if (size > MAX_BYTES) { skip(path, 'larger than ' + (MAX_BYTES >> 20) + ' MB'); continue; }
stagedBlobs.set(path, bytes);
out.push(path);
}
return out;
}
// ---------------------------------------------------------------------------
if (opts.compare) {
if (opts.paths.length !== 2) fail('--compare takes exactly two files');
const cmp = runCompare(opts.paths[0], opts.paths[1]);
// A watermark difference is a fact about the pair, not about either file on
// its own, so it gets its own exit code: 1 when the two copies are separable.
process.exit(cmp.relation === 'marked' ? 1 : 0);
}
const reading = opts.staged || opts.paths.length > 0;
const targets = opts.staged ? stagedFiles() : opts.paths.flatMap((p) => collect(p));
const reports = [];
let worst = -1;
if (!reading) {
const text = readStdin();
const result = withIgnores(analyze(text));
worst = result.verdict.severity;
reports.push({ path: '<stdin>', result });
if (!opts.json && !opts.sarif) reportText('<stdin>', result);
} else {
for (const path of targets) {
let read;
try {
read = stagedBlobs.has(path) ? decodeBytes(stagedBlobs.get(path)) : readText(path);
} catch { skip(path, 'unreadable'); continue; }
if (!read) { skip(path, 'binary'); continue; }
const { text, encoding } = read;
const result = withIgnores(analyze(text));
worst = Math.max(worst, result.verdict.severity);
reports.push({ path, result });
if (!opts.json && !opts.sarif) reportText(relative(process.cwd(), path) || path, result);
if (opts.fix && result.verdict.severity >= 0) {
const cleaned = sanitize(text);
// A staged fix is written to the working copy, which is only safe when
// the working copy is what was staged. Otherwise the fix would rewrite a
// file nobody scanned, and the payload would still be in the index.
const staged = stagedBlobs.get(path);
let current = null;
try { current = staged ? readFileSync(path) : null; } catch { /* deleted since staging */ }
if (staged && !(current && current.equals(staged))) {
process.stderr.write(
'secondsight: not fixed, the working copy differs from what is staged: '
+ shown(relative(process.cwd(), path) || path) + '\n',
);
} else if (cleaned.text !== text) {
writeFileSync(path, encodeAs(cleaned.text, encoding));
if (!opts.json && !opts.sarif) {
process.stdout.write(' ' + paint('32', 'fixed') + dim(
' removed ' + cleaned.removed + ' character' + (cleaned.removed === 1 ? '' : 's')
+ (staged ? ' -- git add it to stage the fix' : ''),
) + '\n');
}
}
}
}
}
if (opts.sarif) {
const sarif = buildSarif(
reports.map(({ path, result }) => ({
path: reading ? relative(process.cwd(), path) || path : path,
result,
})),
{ version: VERSION },
);
const json = JSON.stringify(sarif, null, 2) + '\n';
if (opts.sarifPath) {
writeFileSync(opts.sarifPath, json, 'utf8');
process.stderr.write(
'secondsight: wrote ' + sarif.runs[0].results.length
+ ' result(s) to ' + opts.sarifPath + '\n',
);
} else {
process.stdout.write(json);
}
} else if (opts.json) {
process.stdout.write(JSON.stringify({
version: 1,
worst: worst < 0 ? 'CLEAN' : SEVERITY[worst],
skipped,
gitignored: ignoredCount,
files: reports.map(({ path, result }) => ({
path,
verdict: result.verdict.severity < 0 ? 'CLEAN' : result.verdict.label,
stats: result.stats,
findings: result.findings.map((f) => ({
id: f.id,
title: f.title,
severity: SEVERITY[f.severity],
count: f.count,
positions: f.positions.slice(0, 200),
decoded: f.decoded,
intents: f.intents.map((i) => i.label),
reference: f.reference,
})),
})),
}, null, 2) + '\n');
} else if (reading) {
// Inside GitHub Actions, each finding is also marked on its line of the pull
// request. Paths are relative to the checkout, which is what GitHub matches.
if (opts.annotations && process.env.GITHUB_ACTIONS === 'true') {
const root = process.env.GITHUB_WORKSPACE || process.cwd();
process.stdout.write('\n' + buildAnnotations(
reports.map(({ path, result }) => ({ path: relative(root, resolve(path)), result })),
));
}
const flagged = reports.filter((r) => r.result.verdict.severity >= 0).length;
process.stdout.write('\n' + dim(
reports.length + ' file' + (reports.length === 1 ? '' : 's') + ' scanned, '
+ flagged + ' with findings',
) + '\n');
}
if (ignoredCount && !opts.json && !opts.sarif) {
process.stderr.write(
'secondsight: ' + ignoredCount + (ignoredCount === 1 ? ' path ignored by git was' : ' paths ignored by git were')
+ ' not scanned (--no-gitignore to include them)\n',
);
}
// On stderr in every mode, so it survives --json and --sarif on stdout. Only
// files a scan would have read land here; a directory of images is not news.
for (const { path, reason } of skipped) {
process.stderr.write(
'secondsight: not scanned (' + reason + '): ' + shown(relative(process.cwd(), path) || path) + '\n',
);
}
// With --sarif, GitHub's code scanning decides how to surface and gate the
// findings, so the scan step itself must succeed for the upload to run -- unless
// the caller explicitly asked for a --fail-on gate as well.
const gate = opts.sarif && !opts.failOnExplicit ? Infinity : opts.failOn;
process.exit(worst >= gate ? 1 : 0);