vigilsutra 0.2.3 → 0.4.1

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 (51) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +34 -28
  3. package/advisories/db.json.gz +0 -0
  4. package/agents/agents-registry.json +14 -2
  5. package/agents/agentshield/SKILL.md +12 -0
  6. package/agents/apishield/SKILL.md +12 -0
  7. package/agents/codeguard/SKILL.md +1 -1
  8. package/agents/compliancemap/SKILL.md +12 -0
  9. package/agents/datashield/SKILL.md +12 -0
  10. package/agents/identityguard/SKILL.md +12 -0
  11. package/agents/mobileshield/SKILL.md +12 -0
  12. package/agents/pipelineguard/SKILL.md +12 -0
  13. package/agents/remedy/SKILL.md +12 -0
  14. package/agents/riskrank/SKILL.md +12 -0
  15. package/agents/secretwatch/SKILL.md +1 -1
  16. package/agents/supplywatch/SKILL.md +12 -0
  17. package/agents/threatmap/SKILL.md +12 -0
  18. package/agents/webshield/SKILL.md +12 -0
  19. package/dist/adapters.js +35 -19
  20. package/dist/advisories.js +83 -0
  21. package/dist/cli.js +96 -18
  22. package/dist/components.js +116 -0
  23. package/dist/engine.js +99 -13
  24. package/dist/fix.js +85 -0
  25. package/dist/git-index.js +17 -6
  26. package/dist/git-objects.js +558 -0
  27. package/dist/policy.js +21 -2
  28. package/dist/redact.js +7 -1
  29. package/dist/rules/code.js +23 -6
  30. package/dist/rules/index.js +3 -2
  31. package/dist/rules/secrets.js +72 -0
  32. package/dist/rules/supply.js +103 -0
  33. package/dist/rules/util.js +29 -16
  34. package/dist/scope.js +3 -4
  35. package/dist/taint.js +83 -0
  36. package/docs/agent-contract.md +1 -1
  37. package/docs/assurance/benchmark.md +2 -0
  38. package/docs/assurance/coverage-raw.txt +151 -111
  39. package/docs/assurance/coverage.md +26 -18
  40. package/docs/assurance/fuzzing.md +1 -1
  41. package/docs/assurance/internal-review.md +3 -1
  42. package/docs/ci-cd.md +13 -4
  43. package/docs/compatibility.md +4 -4
  44. package/docs/index.html +76 -0
  45. package/docs/quickstart.md +3 -2
  46. package/docs/rules.md +92 -2
  47. package/docs/security-model.md +1 -1
  48. package/llms.txt +2 -2
  49. package/package.json +7 -3
  50. package/policies/safe-default.yaml +3 -0
  51. package/templates/pre-commit +7 -0
@@ -0,0 +1,558 @@
1
+ import { createHash } from "node:crypto";
2
+ import { lstatSync, readdirSync, readFileSync } from "node:fs";
3
+ import { join, relative, resolve, extname, basename, isAbsolute } from "node:path";
4
+ import { inflateSync } from "node:zlib";
5
+ import { parseGitIndexEntries } from "./git-index.js";
6
+ import { makeIgnoreMatcher } from "./policy.js";
7
+ /** Blobs larger than this are not loaded from history. The worktree scan uses the same 1 MiB default. */
8
+ export const HISTORY_MAX_BLOB_BYTES = 1024 * 1024;
9
+ /**
10
+ * Finds the .git directory for `start` by walking parents. A symlink `.git` is ignored.
11
+ * A worktree `gitdir:` file is followed only when it points at a real directory.
12
+ */
13
+ export function locateGit(start) {
14
+ let dir = resolve(start);
15
+ for (;;) {
16
+ const gitPath = join(dir, ".git");
17
+ let st;
18
+ try {
19
+ st = lstatSync(gitPath);
20
+ }
21
+ catch {
22
+ const parent = resolve(dir, "..");
23
+ if (parent === dir)
24
+ return null;
25
+ dir = parent;
26
+ continue;
27
+ }
28
+ if (st.isSymbolicLink())
29
+ return null;
30
+ if (st.isDirectory()) {
31
+ if (!isFile(join(gitPath, "HEAD")))
32
+ return null;
33
+ return { workTree: dir, gitDir: gitPath };
34
+ }
35
+ if (!st.isFile() || st.size > 4096 || st.size === 0)
36
+ return null;
37
+ const text = readFileSync(gitPath, "utf8");
38
+ const m = /^gitdir:[ \t]*([^\r\n]+)$/m.exec(text);
39
+ if (!m)
40
+ return null;
41
+ const gitDir = resolve(dir, m[1].trim());
42
+ let gst;
43
+ try {
44
+ gst = lstatSync(gitDir);
45
+ }
46
+ catch {
47
+ return null;
48
+ }
49
+ if (!gst.isDirectory() || gst.isSymbolicLink() || !isFile(join(gitDir, "HEAD")))
50
+ return null;
51
+ return { workTree: dir, gitDir };
52
+ }
53
+ }
54
+ function isFile(p) {
55
+ try {
56
+ const st = lstatSync(p);
57
+ return st.isFile() && !st.isSymbolicLink();
58
+ }
59
+ catch {
60
+ return false;
61
+ }
62
+ }
63
+ /** HEAD, a full object id, or a ref name such as `main` or `refs/heads/main`. */
64
+ export function resolveRevision(gitDir, rev) {
65
+ const name = rev.trim();
66
+ if (/^[0-9a-f]{40}$|^[0-9a-f]{64}$/i.test(name))
67
+ return name.toLowerCase();
68
+ if (name === "HEAD")
69
+ return peel(gitDir, readText(join(gitDir, "HEAD")));
70
+ if (name.startsWith("refs/"))
71
+ return peel(gitDir, readText(join(gitDir, name))) ?? packed(gitDir, name);
72
+ return peel(gitDir, readText(join(gitDir, "refs", "heads", name))) ?? packed(gitDir, `refs/heads/${name}`);
73
+ }
74
+ function peel(gitDir, text) {
75
+ if (!text)
76
+ return null;
77
+ const line = text.trim();
78
+ const ref = /^ref:\s*(\S+)/.exec(line);
79
+ if (ref)
80
+ return resolveRevision(gitDir, ref[1]);
81
+ return /^[0-9a-f]{40}$|^[0-9a-f]{64}$/i.test(line) ? line.toLowerCase() : null;
82
+ }
83
+ function packed(gitDir, ref) {
84
+ const text = readText(join(gitDir, "packed-refs"));
85
+ if (!text)
86
+ return null;
87
+ for (const line of text.split(/\n/)) {
88
+ if (line.startsWith("#") || line.startsWith("^"))
89
+ continue;
90
+ const m = /^([0-9a-f]{40}|[0-9a-f]{64})\s+(\S+)/i.exec(line);
91
+ if (m && m[2] === ref)
92
+ return m[1].toLowerCase();
93
+ }
94
+ return null;
95
+ }
96
+ function readText(p) {
97
+ try {
98
+ const st = lstatSync(p);
99
+ if (!st.isFile() || st.isSymbolicLink() || st.size > 1024 * 1024)
100
+ return null;
101
+ return readFileSync(p, "utf8");
102
+ }
103
+ catch {
104
+ return null;
105
+ }
106
+ }
107
+ class Store {
108
+ gitDir;
109
+ bySha = new Map();
110
+ resolving = new Set();
111
+ packs = [];
112
+ notes = [];
113
+ hashLen;
114
+ hashAlgo;
115
+ constructor(gitDir) {
116
+ this.gitDir = gitDir;
117
+ this.hashLen = /objectformat\s*=\s*sha256/i.test(readText(join(gitDir, "config")) ?? "") ? 32 : 20;
118
+ this.hashAlgo = this.hashLen === 32 ? "sha256" : "sha1";
119
+ this.loadPacks();
120
+ }
121
+ get(sha) {
122
+ const key = sha.toLowerCase();
123
+ if (this.bySha.has(key))
124
+ return this.bySha.get(key) ?? null;
125
+ if (this.resolving.has(key))
126
+ return null;
127
+ this.resolving.add(key);
128
+ const found = this.load(key);
129
+ this.resolving.delete(key);
130
+ this.bySha.set(key, found);
131
+ return found;
132
+ }
133
+ load(key) {
134
+ const loose = this.loose(key);
135
+ if (loose)
136
+ return loose;
137
+ for (const pack of this.packs) {
138
+ const off = offsetOf(pack.shas, pack.offsets, key, pack.hashLen);
139
+ if (off === null)
140
+ continue;
141
+ return this.at(pack.pack, off, 0);
142
+ }
143
+ return null;
144
+ }
145
+ loose(sha) {
146
+ const p = join(this.gitDir, "objects", sha.slice(0, 2), sha.slice(2));
147
+ try {
148
+ const st = lstatSync(p);
149
+ if (!st.isFile() || st.isSymbolicLink() || st.size > 8 * 1024 * 1024)
150
+ return null;
151
+ return parseLoose(inflateSync(readFileSync(p)));
152
+ }
153
+ catch {
154
+ return null;
155
+ }
156
+ }
157
+ loadPacks() {
158
+ const dir = join(this.gitDir, "objects", "pack");
159
+ let names = [];
160
+ try {
161
+ names = readdirSync(dir);
162
+ }
163
+ catch {
164
+ return;
165
+ }
166
+ for (const name of names) {
167
+ if (!name.endsWith(".idx"))
168
+ continue;
169
+ const idxPath = join(dir, name);
170
+ const packPath = idxPath.slice(0, -4) + ".pack";
171
+ try {
172
+ const idx = readFileSync(idxPath);
173
+ const pack = readFileSync(packPath);
174
+ const parsed = parseIdx(idx, this.hashLen);
175
+ if (!parsed) {
176
+ this.notes.push(`Pack index ${name} was skipped (version 2 SHA-1 or SHA-256 indexes are read).`);
177
+ continue;
178
+ }
179
+ this.packs.push({ pack, shas: parsed.shas, offsets: parsed.offsets, hashLen: this.hashLen });
180
+ }
181
+ catch {
182
+ this.notes.push(`Pack ${name} could not be read.`);
183
+ }
184
+ }
185
+ }
186
+ at(pack, offset, depth) {
187
+ if (depth > 40 || offset < 0 || offset >= pack.length)
188
+ return null;
189
+ const header = packHeader(pack, offset, this.hashLen);
190
+ if (!header)
191
+ return null;
192
+ let payload;
193
+ try {
194
+ payload = inflateSync(pack.subarray(header.dataAt));
195
+ }
196
+ catch {
197
+ return null;
198
+ }
199
+ if (header.type === "ofs") {
200
+ const base = this.at(pack, offset - header.distance, depth + 1);
201
+ if (!base)
202
+ return null;
203
+ const data = applyDelta(base.data, payload);
204
+ return data ? { type: base.type, data } : null;
205
+ }
206
+ if (header.type === "ref") {
207
+ const base = this.get(header.base);
208
+ if (!base)
209
+ return null;
210
+ const data = applyDelta(base.data, payload);
211
+ return data ? { type: base.type, data } : null;
212
+ }
213
+ if (payload.length !== header.size)
214
+ return null;
215
+ return { type: header.type, data: payload };
216
+ }
217
+ }
218
+ function parseLoose(raw) {
219
+ const nul = raw.indexOf(0);
220
+ if (nul < 0)
221
+ return null;
222
+ const head = raw.toString("utf8", 0, nul);
223
+ const m = /^(commit|tree|blob|tag) (\d+)$/.exec(head);
224
+ if (!m || Number(m[2]) !== raw.length - nul - 1)
225
+ return null;
226
+ return { type: m[1], data: raw.subarray(nul + 1) };
227
+ }
228
+ function parseIdx(buf, hashLen) {
229
+ if (buf.length < 8 + 256 * 4 || buf.readUInt32BE(0) !== 0xff744f63 || buf.readUInt32BE(4) !== 2)
230
+ return null;
231
+ const n = buf.readUInt32BE(8 + 255 * 4);
232
+ const shaAt = 8 + 256 * 4;
233
+ const crcAt = shaAt + n * hashLen;
234
+ const offAt = crcAt + n * 4;
235
+ if (offAt + n * 4 > buf.length)
236
+ return null;
237
+ const offsets = [];
238
+ let large = 0;
239
+ for (let i = 0; i < n; i++) {
240
+ const v = buf.readUInt32BE(offAt + i * 4);
241
+ if (v & 0x80000000)
242
+ large++;
243
+ offsets.push(v);
244
+ }
245
+ const largeAt = offAt + n * 4;
246
+ if (large && largeAt + large * 8 > buf.length)
247
+ return null;
248
+ for (let i = 0; i < n; i++) {
249
+ const v = offsets[i];
250
+ if (v & 0x80000000) {
251
+ const idx = v & 0x7fffffff;
252
+ if (idx >= large)
253
+ return null;
254
+ const hi = buf.readUInt32BE(largeAt + idx * 8);
255
+ const lo = buf.readUInt32BE(largeAt + idx * 8 + 4);
256
+ if (hi !== 0)
257
+ return null;
258
+ offsets[i] = lo;
259
+ }
260
+ }
261
+ return { shas: buf.subarray(shaAt, shaAt + n * hashLen), offsets };
262
+ }
263
+ function offsetOf(shas, offsets, sha, hashLen) {
264
+ const want = Buffer.from(sha, "hex");
265
+ if (want.length !== hashLen)
266
+ return null;
267
+ let lo = 0;
268
+ let hi = offsets.length - 1;
269
+ while (lo <= hi) {
270
+ const mid = (lo + hi) >> 1;
271
+ const cmp = want.compare(shas, mid * hashLen, mid * hashLen + hashLen);
272
+ if (cmp === 0)
273
+ return offsets[mid];
274
+ if (cmp < 0)
275
+ hi = mid - 1;
276
+ else
277
+ lo = mid + 1;
278
+ }
279
+ return null;
280
+ }
281
+ function packHeader(pack, offset, hashLen) {
282
+ if (offset + 1 >= pack.length)
283
+ return null;
284
+ let p = offset;
285
+ let c = pack[p++];
286
+ const typeN = (c >> 4) & 7;
287
+ let size = c & 15;
288
+ let shift = 4;
289
+ while (c & 0x80) {
290
+ if (p >= pack.length || shift > 32)
291
+ return null;
292
+ c = pack[p++];
293
+ size |= (c & 0x7f) << shift;
294
+ shift += 7;
295
+ }
296
+ let distance = 0;
297
+ let base = "";
298
+ if (typeN === 6) {
299
+ if (p >= pack.length)
300
+ return null;
301
+ c = pack[p++];
302
+ distance = c & 0x7f;
303
+ while (c & 0x80) {
304
+ if (p >= pack.length)
305
+ return null;
306
+ c = pack[p++];
307
+ distance = ((distance + 1) << 7) | (c & 0x7f);
308
+ }
309
+ }
310
+ else if (typeN === 7) {
311
+ if (p + hashLen > pack.length)
312
+ return null;
313
+ base = pack.subarray(p, p + hashLen).toString("hex");
314
+ p += hashLen;
315
+ }
316
+ const type = typeN === 1 ? "commit" : typeN === 2 ? "tree" : typeN === 3 ? "blob" : typeN === 4 ? "tag" : typeN === 6 ? "ofs" : typeN === 7 ? "ref" : null;
317
+ if (!type)
318
+ return null;
319
+ return { type, size, dataAt: p, distance, base };
320
+ }
321
+ function applyDelta(base, delta) {
322
+ let p = 0;
323
+ const readSize = () => {
324
+ let size = 0;
325
+ let shift = 0;
326
+ while (p < delta.length) {
327
+ const c = delta[p++];
328
+ size |= (c & 127) << shift;
329
+ if (!(c & 128))
330
+ return size;
331
+ shift += 7;
332
+ if (shift > 32)
333
+ return null;
334
+ }
335
+ return null;
336
+ };
337
+ const srcSize = readSize();
338
+ const dstSize = readSize();
339
+ if (srcSize === null || dstSize === null || srcSize !== base.length || dstSize > HISTORY_MAX_BLOB_BYTES)
340
+ return null;
341
+ const out = Buffer.alloc(dstSize);
342
+ let o = 0;
343
+ while (p < delta.length) {
344
+ const cmd = delta[p++];
345
+ if (cmd === 0)
346
+ return null;
347
+ if (cmd & 0x80) {
348
+ let off = 0;
349
+ let size = 0;
350
+ for (let i = 0; i < 4; i++)
351
+ if (cmd & (1 << i))
352
+ off |= delta[p++] << (8 * i);
353
+ for (let i = 0; i < 3; i++)
354
+ if (cmd & (1 << (4 + i)))
355
+ size |= delta[p++] << (8 * i);
356
+ if (size === 0)
357
+ size = 0x10000;
358
+ if (off + size > base.length || o + size > dstSize)
359
+ return null;
360
+ base.copy(out, o, off, off + size);
361
+ o += size;
362
+ }
363
+ else {
364
+ if (p + cmd > delta.length || o + cmd > dstSize)
365
+ return null;
366
+ delta.copy(out, o, p, p + cmd);
367
+ p += cmd;
368
+ o += cmd;
369
+ }
370
+ }
371
+ return o === dstSize ? out : null;
372
+ }
373
+ function commitTree(obj) {
374
+ const m = /^tree ([0-9a-f]{64}|[0-9a-f]{40})/m.exec(obj.data.toString("utf8"));
375
+ return m ? m[1] : null;
376
+ }
377
+ function commitParents(obj) {
378
+ const out = [];
379
+ for (const line of obj.data.toString("utf8").split("\n")) {
380
+ if (line.startsWith("parent "))
381
+ out.push(line.slice(7).trim());
382
+ else if (line === "")
383
+ break;
384
+ }
385
+ return out;
386
+ }
387
+ function parseTree(data, hashLen) {
388
+ const out = [];
389
+ let p = 0;
390
+ while (p < data.length) {
391
+ const sp = data.indexOf(0x20, p);
392
+ const nul = data.indexOf(0, sp + 1);
393
+ if (sp < 0 || nul < 0 || nul + 1 + hashLen > data.length)
394
+ return null;
395
+ out.push({ mode: data.toString("utf8", p, sp), name: data.toString("utf8", sp + 1, nul), sha: data.subarray(nul + 1, nul + 1 + hashLen).toString("hex") });
396
+ p = nul + 1 + hashLen;
397
+ }
398
+ return out;
399
+ }
400
+ /** Blobs reachable from HEAD, newest commits first, skipping paths still identical in the worktree. */
401
+ export function scanHistory(scanRoot, ignore, current, deadline) {
402
+ const located = locateGit(scanRoot);
403
+ const empty = { files: [], commitsSeen: 0, blobsRead: 0, capped: false, notes: [] };
404
+ if (!located) {
405
+ empty.notes.push("No git directory was found, so git history was not read.");
406
+ return empty;
407
+ }
408
+ const store = new Store(located.gitDir);
409
+ const head = resolveRevision(located.gitDir, "HEAD");
410
+ if (!head) {
411
+ empty.notes.push("HEAD did not resolve, so git history was not read.");
412
+ empty.notes.push(...store.notes);
413
+ return empty;
414
+ }
415
+ const ignored = makeIgnoreMatcher(ignore);
416
+ const scanRel = relative(located.workTree, resolve(scanRoot)).split("\\").join("/");
417
+ const prefix = scanRel && scanRel !== "." ? scanRel.replace(/\/$/, "") + "/" : "";
418
+ if (prefix.startsWith("..")) {
419
+ empty.notes.push("Scan root is outside the git work tree, so history was not read.");
420
+ return empty;
421
+ }
422
+ const seenCommits = new Set();
423
+ const seenBlobs = new Set();
424
+ const queue = [head];
425
+ const files = [];
426
+ let capped = false;
427
+ while (queue.length) {
428
+ if (Date.now() > deadline) {
429
+ capped = true;
430
+ break;
431
+ }
432
+ const sha = queue.shift();
433
+ if (seenCommits.has(sha))
434
+ continue;
435
+ seenCommits.add(sha);
436
+ const commit = store.get(sha);
437
+ if (!commit || commit.type !== "commit")
438
+ continue;
439
+ for (const parent of commitParents(commit))
440
+ if (!seenCommits.has(parent))
441
+ queue.push(parent);
442
+ const tree = commitTree(commit);
443
+ if (!tree)
444
+ continue;
445
+ const blobs = flatten(store, tree, "");
446
+ for (const [path, blobSha] of blobs) {
447
+ if (prefix && !path.startsWith(prefix))
448
+ continue;
449
+ const rel = prefix ? path.slice(prefix.length) : path;
450
+ if (!rel || ignored(rel))
451
+ continue;
452
+ const key = `${rel}\0${blobSha}`;
453
+ if (seenBlobs.has(key))
454
+ continue;
455
+ seenBlobs.add(key);
456
+ if (current.get(rel) !== undefined && createHash(store.hashAlgo).update(`blob ${Buffer.byteLength(current.get(rel))}\0${current.get(rel)}`).digest("hex") === blobSha)
457
+ continue;
458
+ const obj = store.get(blobSha);
459
+ if (!obj || obj.type !== "blob" || obj.data.length > HISTORY_MAX_BLOB_BYTES)
460
+ continue;
461
+ if (obj.data.subarray(0, 8192).includes(0))
462
+ continue;
463
+ const text = obj.data.toString("utf8");
464
+ files.push(virtualFile(`git-history/${sha.slice(0, 12)}/${rel}`, text));
465
+ }
466
+ }
467
+ const notes = [...store.notes];
468
+ if (capped)
469
+ notes.push(`Git history stopped at the time limit after ${seenCommits.size} commit(s). Older commits were not read.`);
470
+ else
471
+ notes.push(`Git history read ${seenCommits.size} commit(s) from HEAD without running git (${store.hashAlgo}). Objects still identical to the worktree were not reported twice.`);
472
+ return { files, commitsSeen: seenCommits.size, blobsRead: files.length, capped, notes };
473
+ }
474
+ function flatten(store, sha, prefix) {
475
+ const out = new Map();
476
+ const obj = store.get(sha);
477
+ if (!obj || obj.type !== "tree")
478
+ return out;
479
+ const entries = parseTree(obj.data, store.hashLen);
480
+ if (!entries)
481
+ return out;
482
+ for (const e of entries) {
483
+ if (e.name === ".." || e.name.includes("\\") || e.name.includes("/"))
484
+ continue;
485
+ const path = prefix ? `${prefix}/${e.name}` : e.name;
486
+ if (e.mode === "40000" || e.mode === "040000") {
487
+ for (const [k, v] of flatten(store, e.sha, path))
488
+ out.set(k, v);
489
+ }
490
+ else if (e.mode === "100644" || e.mode === "100755")
491
+ out.set(path, e.sha);
492
+ }
493
+ return out;
494
+ }
495
+ export function blobSha1(text) {
496
+ return blobHash(text, "sha1");
497
+ }
498
+ export function blobHash(text, algo) {
499
+ return createHash(algo).update(`blob ${Buffer.byteLength(text)}\0${text}`).digest("hex");
500
+ }
501
+ /** Blob ids in `rev`, keyed by path. `note` is set when the revision cannot be read. */
502
+ export function revisionTree(scanRoot, rev) {
503
+ const located = locateGit(scanRoot);
504
+ if (!located)
505
+ return { blobs: null, note: `No git directory; --since ${rev} cannot be resolved.`, algo: "sha1" };
506
+ const sha = resolveRevision(located.gitDir, rev);
507
+ if (!sha)
508
+ return { blobs: null, note: `Revision "${rev}" did not resolve.`, algo: "sha1" };
509
+ const store = new Store(located.gitDir);
510
+ const commit = store.get(sha);
511
+ const tree = commit && commit.type === "commit" ? commitTree(commit) : null;
512
+ if (!tree)
513
+ return { blobs: null, note: `Revision "${rev}" has no tree.`, algo: "sha1" };
514
+ return { blobs: flatten(store, tree, ""), note: "", algo: store.hashAlgo };
515
+ }
516
+ export function stagedBlobs(scanRoot) {
517
+ const located = locateGit(scanRoot);
518
+ if (!located)
519
+ return null;
520
+ const idx = join(located.gitDir, "index");
521
+ let buf;
522
+ try {
523
+ const st = lstatSync(idx);
524
+ if (!st.isFile() || st.isSymbolicLink() || st.size > 32 * 1024 * 1024)
525
+ return null;
526
+ buf = readFileSync(idx);
527
+ }
528
+ catch {
529
+ return null;
530
+ }
531
+ const entries = parseGitIndexEntries(buf);
532
+ if (!entries)
533
+ return null;
534
+ const store = new Store(located.gitDir);
535
+ const out = [];
536
+ for (const e of entries) {
537
+ if (e.path.includes(".."))
538
+ continue;
539
+ const obj = store.get(e.sha);
540
+ if (!obj || obj.type !== "blob" || obj.data.length > HISTORY_MAX_BLOB_BYTES)
541
+ continue;
542
+ if (obj.data.subarray(0, 8192).includes(0))
543
+ continue;
544
+ out.push({ path: e.path, text: obj.data.toString("utf8") });
545
+ }
546
+ return out;
547
+ }
548
+ function virtualFile(path, text) {
549
+ const name = basename(path).toLowerCase();
550
+ return { path, name, ext: extname(name), text, lines: text.split(/\r\n|\n|\r/) };
551
+ }
552
+ export function insideWorkTree(scanRoot, abs) {
553
+ const located = locateGit(scanRoot);
554
+ if (!located)
555
+ return false;
556
+ const rel = relative(located.workTree, abs);
557
+ return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
558
+ }
package/dist/policy.js CHANGED
@@ -24,7 +24,8 @@ export const DEFAULT_POLICY = {
24
24
  maxFileSize: 1024 * 1024,
25
25
  maxFiles: 20000,
26
26
  timeLimitMs: 60000,
27
- disableRules: []
27
+ disableRules: [],
28
+ waivers: []
28
29
  };
29
30
  /**
30
31
  * Parses the small YAML subset used by .vigilsutra.yml:
@@ -56,7 +57,7 @@ export function parsePolicy(text) {
56
57
  out[kv[1]] = stripQuotes(kv[2].trim());
57
58
  }
58
59
  }
59
- const known = new Set(["ignore", "maxFileSize", "maxFiles", "timeLimitMs", "failOn", "disableRules", "version"]);
60
+ const known = new Set(["ignore", "maxFileSize", "maxFiles", "timeLimitMs", "failOn", "disableRules", "waivers", "version"]);
60
61
  for (const k of Object.keys(out))
61
62
  if (!known.has(k))
62
63
  throw new Error(`.vigilsutra.yml: unknown key "${k}"`);
@@ -65,6 +66,8 @@ export function parsePolicy(text) {
65
66
  p.ignore = out.ignore;
66
67
  if (Array.isArray(out.disableRules))
67
68
  p.disableRules = out.disableRules;
69
+ if (Array.isArray(out.waivers))
70
+ p.waivers = out.waivers.map(parseWaiver);
68
71
  for (const k of ["maxFileSize", "maxFiles", "timeLimitMs"]) {
69
72
  if (typeof out[k] === "string") {
70
73
  const n = Number(out[k]);
@@ -80,6 +83,21 @@ export function parsePolicy(text) {
80
83
  }
81
84
  return p;
82
85
  }
86
+ function parseWaiver(item) {
87
+ const parts = item.split("|").map((s) => s.trim());
88
+ if (parts.length !== 4)
89
+ throw new Error(`.vigilsutra.yml: waiver must be ruleId|path|owner|YYYY-MM-DD`);
90
+ const [ruleId, path, owner, until] = parts;
91
+ if (!/^VS-[A-Z]+-\d{3}$/.test(ruleId))
92
+ throw new Error(`.vigilsutra.yml: waiver rule id "${ruleId}" is not a rule id`);
93
+ if (!path || path.startsWith("/") || path.split("/").includes(".."))
94
+ throw new Error(`.vigilsutra.yml: waiver path must stay inside the project`);
95
+ if (!owner || owner.length > 80)
96
+ throw new Error(`.vigilsutra.yml: waiver needs an owner`);
97
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(until) || Number.isNaN(Date.parse(until + "T00:00:00Z")))
98
+ throw new Error(`.vigilsutra.yml: waiver until must be YYYY-MM-DD`);
99
+ return { ruleId, path, owner, until };
100
+ }
83
101
  function stripQuotes(s) {
84
102
  return /^(['"]).*\1$/.test(s) ? s.slice(1, -1) : s;
85
103
  }
@@ -100,6 +118,7 @@ export function loadPolicy(root, opts = {}) {
100
118
  ...user,
101
119
  // User ignores extend, never replace, the safe defaults.
102
120
  ignore: [...DEFAULT_IGNORE, ...(user.ignore ?? [])],
121
+ waivers: user.waivers ?? [],
103
122
  source: opts.explicitFile ? "explicit --policy file" : "project .vigilsutra.yml",
104
123
  userIgnores: user.ignore?.length ?? 0
105
124
  };
package/dist/redact.js CHANGED
@@ -16,7 +16,13 @@ const TOKEN_SHAPES = [
16
16
  /gh[pousr]_[A-Za-z0-9]{30,}/g,
17
17
  /github_pat_[A-Za-z0-9_]{30,}/g,
18
18
  /xox[baprs]-[A-Za-z0-9-]{10,}/g,
19
- /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*/g
19
+ /-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*/g,
20
+ /eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g,
21
+ /npm_[A-Za-z0-9]{36,}/g,
22
+ /(?:sk|rk)_(?:live|test)_[0-9A-Za-z]{16,}/g,
23
+ /AIza[0-9A-Za-z_-]{35}/g,
24
+ /AccountKey=[A-Za-z0-9+/]{40,}={0,2}/g,
25
+ /glpat-[A-Za-z0-9_-]{20,}/g
20
26
  ];
21
27
  export function scrubTokens(s) {
22
28
  let out = s;
@@ -9,16 +9,33 @@ export const codeRules = [
9
9
  confidence: "medium",
10
10
  status: "potential",
11
11
  cwe: ["CWE-95"],
12
- description: "Strings are compiled and executed as code. If any part is attacker-influenced this is remote code execution. This rule matches the call, not the data flow.",
12
+ description: "Strings are compiled and executed as code. If any part is attacker-influenced this is remote code execution. Confidence is raised when the argument name traces to a request-like assignment in this project, including through a relative import. Object fields, dynamic imports, and function calls are not tracked.",
13
13
  remediation: "Replace with a data-driven approach (lookup table, JSON.parse, ast.literal_eval). Never pass user input to an evaluator.",
14
14
  falsePositives: "Constant strings, build tooling, or sandboxed evaluators. Trace the argument to confirm.",
15
15
  verification: "The call is removed or its argument is proven constant; add a test with a malicious input.",
16
16
  applies: isCode,
17
- check: (f) => lineRule(f, isPy(f) ? /(?<![\w.])(?:eval|exec)\s*\(\s*(?!["'][^"']*["']\s*\))/ : /(?<![\w.])(?:eval\s*\(|new\s+Function\s*\()/, {
18
- skipComments: true,
19
- // Not a call when the text sits inside a string literal, and eval of a constant string is not injectable.
20
- filter: (m, line) => !insideString(line, m.index) && !/\beval\s*\(\s*(?:"[^"]*"|'[^']*'|`[^`$]*`)\s*\)/.test(line)
21
- })
17
+ check: (f) => {
18
+ const hits = lineRule(f, isPy(f) ? /(?<![\w.])(?:eval|exec)\s*\(\s*(?!["'][^"']*["']\s*\))/ : /(?<![\w.])(?:eval\s*\(|new\s+Function\s*\()/, {
19
+ skipComments: true,
20
+ // Not a call when the text sits inside a string literal, and eval of a constant string is not injectable.
21
+ filter: (m, line) => !insideString(line, m.index) && !/\beval\s*\(\s*(?:"[^"]*"|'[^']*'|`[^`$]*`)\s*\)/.test(line)
22
+ });
23
+ const sources = new Set(f.tainted ?? []);
24
+ for (const l of f.lines) {
25
+ if (!/\b(req|request|params|query|body|input|event)\b/.test(l))
26
+ continue;
27
+ const assigned = /(?:^|[^\w$])([A-Za-z_$][\w$]*)\s*=/.exec(l);
28
+ if (assigned)
29
+ sources.add(assigned[1]);
30
+ }
31
+ return hits.map((h) => {
32
+ const line = f.lines[h.line - 1] ?? "";
33
+ const arg = /(?:eval|exec|Function)\s*\(\s*([A-Za-z_$][\w$]*)/.exec(line);
34
+ return arg && sources.has(arg[1])
35
+ ? { ...h, confidence: "high", note: "Name tracked from a request-like assignment in this project. Object fields, dynamic imports, and function calls are not tracked." }
36
+ : h;
37
+ });
38
+ }
22
39
  },
23
40
  {
24
41
  ...b,
@@ -2,8 +2,9 @@ import { secretRules } from "./secrets.js";
2
2
  import { infraRules } from "./infra.js";
3
3
  import { codeRules } from "./code.js";
4
4
  import { agentPrivacyRules, processRules } from "./agents-privacy.js";
5
- export const RULES_VERSION = "1.0.0";
6
- export const ALL_RULES = [...secretRules, ...infraRules, ...codeRules, ...agentPrivacyRules, ...processRules];
5
+ import { supplyRules } from "./supply.js";
6
+ export const RULES_VERSION = "1.2.0";
7
+ export const ALL_RULES = [...secretRules, ...infraRules, ...codeRules, ...agentPrivacyRules, ...processRules, ...supplyRules];
7
8
  export const CWE_NOTE = "CWE ids are orientation only, taken from the MITRE CWE list; no OWASP/NIST control mapping is asserted (mapping unverified).";
8
9
  export function findRule(id) {
9
10
  return ALL_RULES.find((r) => r.id.toLowerCase() === id.toLowerCase());