@abdulr7man/pensieve 0.0.0-stage → 1.2.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.
- package/LICENSE +684 -0
- package/README.md +180 -2
- package/VERSION +1 -0
- package/agents/README.md +16 -0
- package/agents/pensieve-code-auditor.md +58 -0
- package/agents/pensieve-mapper.md +46 -0
- package/agents/pensieve-planner.md +67 -0
- package/agents/pensieve-reconner.md +76 -0
- package/agents/pensieve-reporter.md +61 -0
- package/agents/pensieve-roe-checker.md +46 -0
- package/agents/pensieve-scoper.md +77 -0
- package/agents/pensieve-tester.md +83 -0
- package/agents/pensieve-validator.md +62 -0
- package/bin/pensieve +452 -0
- package/bin/pensieve-installer.cjs +5 -0
- package/commands/pensieve-close.md +6 -0
- package/commands/pensieve-map.md +6 -0
- package/commands/pensieve-new-engagement.md +6 -0
- package/commands/pensieve-pause.md +15 -0
- package/commands/pensieve-plan.md +6 -0
- package/commands/pensieve-progress.md +34 -0
- package/commands/pensieve-quick.md +8 -0
- package/commands/pensieve-recon.md +6 -0
- package/commands/pensieve-report.md +6 -0
- package/commands/pensieve-resume.md +6 -0
- package/commands/pensieve-retest.md +10 -0
- package/commands/pensieve-scope.md +49 -0
- package/commands/pensieve-surface.md +8 -0
- package/commands/pensieve-test.md +6 -0
- package/commands/pensieve-validate.md +6 -0
- package/docker/Dockerfile.pensieve-toolbox +75 -0
- package/docker/compose.pensieve-net.yml +37 -0
- package/docker/squid.conf.template +21 -0
- package/package.json +39 -4
- package/packs/ad/ad-killchain/checklist.md +64 -0
- package/packs/ad/ad-killchain/pack.yaml +19 -0
- package/packs/cloud/cloud-cis/checklist.md +66 -0
- package/packs/cloud/cloud-cis/pack.yaml +20 -0
- package/packs/fintech/practice-core/checklist.md +33 -0
- package/packs/fintech/practice-core/learnings.md +5 -0
- package/packs/fintech/practice-core/pack.yaml +18 -0
- package/packs/infra/ptes-infra/checklist.md +22 -0
- package/packs/infra/ptes-infra/pack.yaml +19 -0
- package/packs/mobile/owasp-masvs/checklist.md +80 -0
- package/packs/mobile/owasp-masvs/pack.yaml +20 -0
- package/packs/owasp-api-v4/checklist.md +85 -0
- package/packs/owasp-api-v4/learnings.md +6 -0
- package/packs/owasp-api-v4/pack.yaml +23 -0
- package/packs/owasp-api-v4/runbooks/API1/SKILL.md +66 -0
- package/packs/owasp-api-v4/runbooks/API10/SKILL.md +67 -0
- package/packs/owasp-api-v4/runbooks/API2/SKILL.md +129 -0
- package/packs/owasp-api-v4/runbooks/API3/SKILL.md +55 -0
- package/packs/owasp-api-v4/runbooks/API4/SKILL.md +59 -0
- package/packs/owasp-api-v4/runbooks/API5/SKILL.md +60 -0
- package/packs/owasp-api-v4/runbooks/API6/SKILL.md +63 -0
- package/packs/owasp-api-v4/runbooks/API7/SKILL.md +70 -0
- package/packs/owasp-api-v4/runbooks/API8/SKILL.md +72 -0
- package/packs/owasp-api-v4/runbooks/API9/SKILL.md +66 -0
- package/packs/thickclient/thickclient-core/checklist.md +60 -0
- package/packs/thickclient/thickclient-core/pack.yaml +24 -0
- package/packs/web/asvs-l2/checklist.md +50 -0
- package/packs/web/asvs-l2/learnings.md +6 -0
- package/packs/web/asvs-l2/pack.yaml +19 -0
- package/packs/web/asvs-l2/runbooks/V2-AUTH-ENUM/SKILL.md +75 -0
- package/packs/web/asvs-l2/runbooks/V2-AUTH-LIVE/SKILL.md +78 -0
- package/packs/web/asvs-l2/runbooks/V2-AUTH-MFA/SKILL.md +76 -0
- package/packs/web/asvs-l2/runbooks/V2-AUTH-RATE/SKILL.md +72 -0
- package/packs/web/owasp-testing-checklist/checklist.md +52 -0
- package/packs/web/owasp-testing-checklist/learnings.md +47 -0
- package/packs/web/owasp-testing-checklist/pack.yaml +21 -0
- package/packs/web/unauth-perimeter/checklist.md +72 -0
- package/packs/web/unauth-perimeter/pack.yaml +18 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-01/SKILL.md +60 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-02/SKILL.md +63 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-03/SKILL.md +62 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-04/SKILL.md +75 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-05/SKILL.md +66 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-06/SKILL.md +59 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-07/SKILL.md +56 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-08/SKILL.md +57 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-09/SKILL.md +62 -0
- package/packs/web/unauth-perimeter/runbooks/PERIM-10/SKILL.md +60 -0
- package/packs/whitebox/code-audit-core/checklist.md +36 -0
- package/packs/whitebox/code-audit-core/learnings.md +6 -0
- package/packs/whitebox/code-audit-core/pack.yaml +14 -0
- package/prompts/orchestrator-core.md +52 -0
- package/reference/attack-playbooks.md +57 -0
- package/reference/otp-relay.md +98 -0
- package/reference/param-profiles.md +48 -0
- package/reference/templates/FINDING.md +97 -0
- package/reference/tool-selection.md +64 -0
- package/reference/vulnerability-finding-standard.md +80 -0
- package/runtimes/hermes/hooks/injection-scan.py +290 -0
- package/runtimes/hermes/hooks/scope-guard.py +136 -0
- package/skills/pensieve/SKILL.md +134 -0
- package/skills/pensieve-close/SKILL.md +31 -0
- package/skills/pensieve-map/SKILL.md +25 -0
- package/skills/pensieve-new-engagement/SKILL.md +28 -0
- package/skills/pensieve-pause/SKILL.md +22 -0
- package/skills/pensieve-plan/SKILL.md +39 -0
- package/skills/pensieve-progress/SKILL.md +35 -0
- package/skills/pensieve-quick/SKILL.md +47 -0
- package/skills/pensieve-recon/SKILL.md +30 -0
- package/skills/pensieve-report/SKILL.md +46 -0
- package/skills/pensieve-resume/SKILL.md +33 -0
- package/skills/pensieve-retest/SKILL.md +35 -0
- package/skills/pensieve-scope/SKILL.md +35 -0
- package/skills/pensieve-surface/SKILL.md +36 -0
- package/skills/pensieve-test/SKILL.md +35 -0
- package/skills/pensieve-validate/SKILL.md +28 -0
- package/skills/pensieve-wiki/SKILL.md +39 -0
- package/src/installer/cli.cjs +209 -0
- package/src/installer/home.cjs +155 -0
- package/src/installer/runtimes.cjs +276 -0
- package/src/installer/uninstall.cjs +342 -0
- package/src/pensieve-sdk/bin/pensieve-sdk.cjs +1045 -0
- package/src/pensieve-sdk/lib/agent-translate.cjs +50 -0
- package/src/pensieve-sdk/lib/approvals.cjs +408 -0
- package/src/pensieve-sdk/lib/audit.cjs +290 -0
- package/src/pensieve-sdk/lib/checklist.cjs +337 -0
- package/src/pensieve-sdk/lib/clientlist.cjs +814 -0
- package/src/pensieve-sdk/lib/close.cjs +133 -0
- package/src/pensieve-sdk/lib/config.cjs +156 -0
- package/src/pensieve-sdk/lib/cvss.cjs +91 -0
- package/src/pensieve-sdk/lib/env.cjs +272 -0
- package/src/pensieve-sdk/lib/evidence.cjs +217 -0
- package/src/pensieve-sdk/lib/findings.cjs +1096 -0
- package/src/pensieve-sdk/lib/frontmatter.cjs +178 -0
- package/src/pensieve-sdk/lib/graph.cjs +607 -0
- package/src/pensieve-sdk/lib/ids.cjs +63 -0
- package/src/pensieve-sdk/lib/init.cjs +79 -0
- package/src/pensieve-sdk/lib/liveness.cjs +176 -0
- package/src/pensieve-sdk/lib/loop.cjs +619 -0
- package/src/pensieve-sdk/lib/migrate.cjs +57 -0
- package/src/pensieve-sdk/lib/monitor.cjs +69 -0
- package/src/pensieve-sdk/lib/pack.cjs +607 -0
- package/src/pensieve-sdk/lib/paths.cjs +73 -0
- package/src/pensieve-sdk/lib/pdf.cjs +291 -0
- package/src/pensieve-sdk/lib/recon.cjs +296 -0
- package/src/pensieve-sdk/lib/renderers/__pycache__/formal_report.cpython-314.pyc +0 -0
- package/src/pensieve-sdk/lib/renderers/formal_report.py +608 -0
- package/src/pensieve-sdk/lib/report.cjs +1135 -0
- package/src/pensieve-sdk/lib/runbook.cjs +124 -0
- package/src/pensieve-sdk/lib/sarif.cjs +154 -0
- package/src/pensieve-sdk/lib/scope.cjs +621 -0
- package/src/pensieve-sdk/lib/security.cjs +61 -0
- package/src/pensieve-sdk/lib/skills.cjs +193 -0
- package/src/pensieve-sdk/lib/state.cjs +610 -0
- package/src/pensieve-sdk/lib/subagent.cjs +658 -0
- package/src/pensieve-sdk/lib/target.cjs +149 -0
- package/src/pensieve-sdk/lib/tools.cjs +66 -0
- package/src/pensieve-sdk/lib/vault.cjs +122 -0
- package/src/pensieve-sdk/lib/vendor/cvss40.LICENSE +22 -0
- package/src/pensieve-sdk/lib/vendor/cvss40.js +1208 -0
- package/src/pensieve-sdk/package.json +11 -0
- package/templates/ACCOUNTS.md +35 -0
- package/templates/ATTACK-SURFACE.md +30 -0
- package/templates/FINDING.md +43 -0
- package/templates/PLAN.md +33 -0
- package/templates/PROJECT.md +29 -0
- package/templates/ROE.md +50 -0
- package/templates/RUN-LOG.md +19 -0
- package/templates/SCOPE.md +32 -0
- package/templates/VALIDATION.md +62 -0
- package/templates/report/REPORT.md +49 -0
- package/workflows/close.md +34 -0
- package/workflows/map.md +39 -0
- package/workflows/new-engagement.md +37 -0
- package/workflows/plan.md +45 -0
- package/workflows/progress.md +28 -0
- package/workflows/quick.md +67 -0
- package/workflows/recon.md +87 -0
- package/workflows/report.md +51 -0
- package/workflows/resume.md +26 -0
- package/workflows/retest.md +53 -0
- package/workflows/scope.md +47 -0
- package/workflows/surface.md +29 -0
- package/workflows/test.md +95 -0
- package/workflows/validate.md +34 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* evidence.cjs — hash-indexed evidence store (ADR-004/006).
|
|
4
|
+
* store(): copy into evidence/raw/<ts>-<safe-name>, sha256, JSONL index.
|
|
5
|
+
* verify(): recompute hash, compare against the index entry.
|
|
6
|
+
* Flat layout (v0.6 01-01): paths resolve via lib/paths.cjs at the engagement root.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const fs = require('node:fs');
|
|
10
|
+
const path = require('node:path');
|
|
11
|
+
const crypto = require('node:crypto');
|
|
12
|
+
const paths = require('./paths.cjs');
|
|
13
|
+
const vault = require('./vault.cjs');
|
|
14
|
+
|
|
15
|
+
function evDir(engagementDir) { return paths.evidenceDir(engagementDir); }
|
|
16
|
+
function indexFile(engagementDir) { return paths.evidenceIndex(engagementDir); }
|
|
17
|
+
|
|
18
|
+
function sha256File(file) {
|
|
19
|
+
return crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function safeName(name) {
|
|
23
|
+
return name.toLowerCase().replace(/[^a-z0-9.]+/g, '-').replace(/-{2,}/g, '-').replace(/^-|-$/g, '');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function readIndex(engagementDir) {
|
|
27
|
+
try {
|
|
28
|
+
return fs.readFileSync(indexFile(engagementDir), 'utf8')
|
|
29
|
+
.split('\n').filter((l) => l.startsWith('{')).map((l) => JSON.parse(l));
|
|
30
|
+
} catch {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// withIndexLock — serialize the read-compute-seq-append critical section of the
|
|
36
|
+
// hash-chained ledger across PROCESSES (CWE-362/345, Phase 17 BLOCKER fix). The
|
|
37
|
+
// test swarm can fire many `evidence.put` processes at once; without a lock two
|
|
38
|
+
// of them read the same `prior.length`, compute the same seq, and fork the chain.
|
|
39
|
+
// Lock = an atomic `mkdir` of `<INDEX>.lock` (fails EEXIST if held), bounded wait
|
|
40
|
+
// then a busy-throw, and a stale-steal so a crashed holder can't deadlock us.
|
|
41
|
+
// ponytail: short synchronous spin — store() is sync + each append is sub-ms, so a
|
|
42
|
+
// real blocking wait (async/flock) isn't worth it until contention actually hurts.
|
|
43
|
+
const LOCK_WAIT_MS = 5000;
|
|
44
|
+
const LOCK_STALE_MS = 10000;
|
|
45
|
+
function withIndexLock(engagementDir, fn) {
|
|
46
|
+
const idx = indexFile(engagementDir);
|
|
47
|
+
fs.mkdirSync(path.dirname(idx), { recursive: true });
|
|
48
|
+
const lock = idx + '.lock';
|
|
49
|
+
const deadline = Date.now() + LOCK_WAIT_MS;
|
|
50
|
+
for (;;) {
|
|
51
|
+
try { fs.mkdirSync(lock); break; } // atomic acquire
|
|
52
|
+
catch (e) {
|
|
53
|
+
if (e.code !== 'EEXIST') throw e;
|
|
54
|
+
try {
|
|
55
|
+
if (Date.now() - fs.statSync(lock).mtimeMs > LOCK_STALE_MS) { fs.rmdirSync(lock); continue; }
|
|
56
|
+
} catch { continue; } // lock vanished between stat and now — retry acquire
|
|
57
|
+
if (Date.now() >= deadline) {
|
|
58
|
+
throw new Error(`evidence ledger busy: could not acquire append lock within ${LOCK_WAIT_MS}ms (${lock})`);
|
|
59
|
+
}
|
|
60
|
+
const until = Date.now() + 20; while (Date.now() < until) { /* spin */ }
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
try { return fn(); } finally { try { fs.rmdirSync(lock); } catch { /* already released */ } }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* vaultKeyLeakWarning — Phase 23 (VAULT-01): evidence text (note + raw-file
|
|
68
|
+
* basename) is scanned for vault KEY NAMES. A match means the operator may be
|
|
69
|
+
* one step from pasting the credential itself into the ledger — so the put
|
|
70
|
+
* WARNS, naming the matched key(s) ONLY. It never refuses (operator judgment)
|
|
71
|
+
* and never mentions a value. Returns the warning string or null. Advisory by
|
|
72
|
+
* design: an unreadable vault file silences the check (the vault verbs are
|
|
73
|
+
* the loud path for vault problems — the evidence store must not break on it).
|
|
74
|
+
*/
|
|
75
|
+
function vaultKeyLeakWarning(engagementDir, texts) {
|
|
76
|
+
let keyNames;
|
|
77
|
+
try {
|
|
78
|
+
keyNames = vault.keys(engagementDir); // NAMES only — values never leave vault.cjs masked
|
|
79
|
+
} catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
if (!keyNames.length) return null;
|
|
83
|
+
const haystack = (Array.isArray(texts) ? texts : []).filter((t) => typeof t === 'string' && t).join('\n');
|
|
84
|
+
if (!haystack) return null;
|
|
85
|
+
const matched = keyNames.filter((k) => haystack.includes(k));
|
|
86
|
+
if (!matched.length) return null;
|
|
87
|
+
return `vault key name in evidence text: ${matched.join(', ')} — evidence naming a vault key may invite the value itself `
|
|
88
|
+
+ 'into the ledger; the vault is never printed (masked read-back only, values stay in <engagement>/.env)';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function store(engagementDir, srcPath, opts = {}) {
|
|
92
|
+
if (!fs.existsSync(srcPath)) throw new Error(`evidence source not found: ${srcPath}`);
|
|
93
|
+
// Phase 20 (EVID-01): --screenshot points at a figure file anywhere on disk.
|
|
94
|
+
// It must exist NOW — fail-closed: the ledger never records a dangling figure
|
|
95
|
+
// reference the report renderer would later link to nothing.
|
|
96
|
+
if (opts.screenshot && !fs.existsSync(path.resolve(opts.screenshot))) {
|
|
97
|
+
throw new Error(`screenshot figure not found: ${opts.screenshot} — evidence.put --screenshot requires an existing image file`);
|
|
98
|
+
}
|
|
99
|
+
const sha = sha256File(srcPath);
|
|
100
|
+
const id = `E-${sha.slice(0, 8)}`;
|
|
101
|
+
// The whole dedup-read → seq-compute → append runs inside the exclusive lock so
|
|
102
|
+
// concurrent stores can never read a stale index and fork the chain.
|
|
103
|
+
return withIndexLock(engagementDir, () => {
|
|
104
|
+
const existing = readIndex(engagementDir).find((r) => r.id === id);
|
|
105
|
+
if (existing) {
|
|
106
|
+
// identical content re-store: idempotent, no ledger growth — but only if
|
|
107
|
+
// the stored copy is still intact; a corrupted copy means tampering
|
|
108
|
+
const abs = paths.resolve(engagementDir, existing.path);
|
|
109
|
+
const actual = fs.existsSync(abs) ? sha256File(abs) : null;
|
|
110
|
+
if (actual === existing.sha256) return existing;
|
|
111
|
+
throw new Error(`integrity failure: stored copy of ${id} is missing or tampered — refusing to overwrite (investigate, don't repair)`);
|
|
112
|
+
}
|
|
113
|
+
const ts = new Date().toISOString().replace(/[:.]/g, '-');
|
|
114
|
+
const destRel = path.join('evidence', 'raw', `${ts}-${safeName(path.basename(srcPath))}`);
|
|
115
|
+
const destAbs = paths.resolve(engagementDir, destRel);
|
|
116
|
+
fs.mkdirSync(path.dirname(destAbs), { recursive: true });
|
|
117
|
+
fs.copyFileSync(srcPath, destAbs);
|
|
118
|
+
|
|
119
|
+
// Phase 20 (EVID-01): figures are REFERENCED, never inlined — the image is
|
|
120
|
+
// copied into the engagement's evidence tree (evidence/figures/) and the
|
|
121
|
+
// record carries the engagement-relative path + caption. The report
|
|
122
|
+
// renderer links it relative to the emitted report location.
|
|
123
|
+
let screenshotRel = null;
|
|
124
|
+
if (opts.screenshot) {
|
|
125
|
+
screenshotRel = path.join('evidence', 'figures', `${ts}-${safeName(path.basename(opts.screenshot))}`);
|
|
126
|
+
const figAbs = paths.resolve(engagementDir, screenshotRel);
|
|
127
|
+
fs.mkdirSync(path.dirname(figAbs), { recursive: true });
|
|
128
|
+
fs.copyFileSync(path.resolve(opts.screenshot), figAbs);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// M2 (ADR-016): append-only hash-chained ledger — seq + prev_hash +
|
|
132
|
+
// entry_hash. Editing, deleting, or reordering any historical record
|
|
133
|
+
// breaks every subsequent link; verifyChain walks it.
|
|
134
|
+
const prior = readIndex(engagementDir);
|
|
135
|
+
const seq = prior.length + 1;
|
|
136
|
+
const prev_hash = prior.length ? prior[prior.length - 1].entry_hash || null : null;
|
|
137
|
+
const rec = {
|
|
138
|
+
seq,
|
|
139
|
+
id,
|
|
140
|
+
sha256: sha,
|
|
141
|
+
path: destRel,
|
|
142
|
+
finding_id: opts.finding_id || null,
|
|
143
|
+
note: opts.note || null,
|
|
144
|
+
screenshot: screenshotRel,
|
|
145
|
+
caption: opts.caption || null,
|
|
146
|
+
stored_at: new Date().toISOString(),
|
|
147
|
+
prev_hash,
|
|
148
|
+
};
|
|
149
|
+
rec.entry_hash = crypto.createHash('sha256').update(hashedPayload(rec)).digest('hex');
|
|
150
|
+
const idx = indexFile(engagementDir);
|
|
151
|
+
if (!fs.existsSync(idx)) fs.writeFileSync(idx, '# Evidence Index (JSONL — one record per line)\n');
|
|
152
|
+
fs.appendFileSync(idx, JSON.stringify(rec) + '\n');
|
|
153
|
+
return rec;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function verify(engagementDir, id) {
|
|
158
|
+
const rec = readIndex(engagementDir).find((r) => r.id === id);
|
|
159
|
+
if (!rec) return { ok: false, reason: `unknown evidence id: ${id}` };
|
|
160
|
+
const abs = paths.resolve(engagementDir, rec.path);
|
|
161
|
+
if (!fs.existsSync(abs)) return { ok: false, reason: `evidence file missing: ${rec.path}` };
|
|
162
|
+
const actual = sha256File(abs);
|
|
163
|
+
if (actual !== rec.sha256) {
|
|
164
|
+
return { ok: false, reason: `hash mismatch: index=${rec.sha256} actual=${actual}` };
|
|
165
|
+
}
|
|
166
|
+
return { ok: true, record: rec };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* hashedPayload — the canonical string covered by entry_hash. Phase 20
|
|
171
|
+
* (EVID-01): the figure fields are APPENDED (never spliced into the middle),
|
|
172
|
+
* so a record written before they existed hashes EXACTLY as it always did —
|
|
173
|
+
* extending the payload cannot rewrite history. Once present, screenshot and
|
|
174
|
+
* caption are covered like every other field: editing either after append
|
|
175
|
+
* breaks entry_hash recompute. Key=value suffixes keep the fields
|
|
176
|
+
* unambiguous (a caption can never masquerade as a figure path).
|
|
177
|
+
*/
|
|
178
|
+
function hashedPayload(rec) {
|
|
179
|
+
let s = `${rec.prev_hash || 'GENESIS'}|${rec.seq}|${rec.id}|${rec.sha256}|${rec.path}|${rec.stored_at}`;
|
|
180
|
+
if (rec.screenshot != null) s += `|screenshot=${rec.screenshot}`;
|
|
181
|
+
if (rec.caption != null) s += `|caption=${rec.caption}`;
|
|
182
|
+
return s;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function entryHashFor(rec) {
|
|
186
|
+
return crypto.createHash('sha256').update(hashedPayload(rec)).digest('hex');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* verifyChain — walk the ledger: seq monotonic, prev_hash linkage intact,
|
|
191
|
+
* entry_hash recomputes. Legacy pre-M2 records (no entry_hash) are counted
|
|
192
|
+
* but chain-exempt (they still get per-id file-hash verify). Returns the
|
|
193
|
+
* first record where verification breaks.
|
|
194
|
+
*/
|
|
195
|
+
function verifyChain(engagementDir) {
|
|
196
|
+
const records = readIndex(engagementDir);
|
|
197
|
+
let chained = 0;
|
|
198
|
+
let legacy = 0;
|
|
199
|
+
let prev = null;
|
|
200
|
+
for (let i = 0; i < records.length; i++) {
|
|
201
|
+
const r = records[i];
|
|
202
|
+
if (!r.entry_hash) { legacy++; prev = r; continue; }
|
|
203
|
+
chained++;
|
|
204
|
+
if (r.seq !== i + 1) return { ok: false, broken_at: r.seq ?? i + 1, reason: `seq gap at ledger position ${i + 1}`, records: records.length, chained, legacy };
|
|
205
|
+
const expectedPrev = i === 0 ? null : records[i - 1].entry_hash || null;
|
|
206
|
+
if ((r.prev_hash || null) !== expectedPrev) {
|
|
207
|
+
return { ok: false, broken_at: r.seq, reason: 'prev_hash linkage broken', records: records.length, chained, legacy };
|
|
208
|
+
}
|
|
209
|
+
if (entryHashFor(r) !== r.entry_hash) {
|
|
210
|
+
return { ok: false, broken_at: r.seq, reason: 'entry_hash mismatch — record fields were edited after append', records: records.length, chained, legacy };
|
|
211
|
+
}
|
|
212
|
+
prev = r;
|
|
213
|
+
}
|
|
214
|
+
return { ok: true, records: records.length, chained, legacy };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
module.exports = { store, verify, verifyChain, readIndex, vaultKeyLeakWarning };
|