@peac/cli 0.11.2 → 0.11.3

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.
@@ -0,0 +1,17 @@
1
+ /**
2
+ * PEAC Reconcile CLI Command (v0.11.3+)
3
+ *
4
+ * Merge two evidence bundles and detect conflicts.
5
+ * Conflict key: composite (iss, jti) with 3-step fallback:
6
+ * 1. (iss, jti) from JWT claims
7
+ * 2. (iss, rid) for Wire 0.1
8
+ * 3. (iss, receipt_ref) where receipt_ref = sha256(receipt_jws)
9
+ *
10
+ * NO auto-resolution: conflicts are surfaced for human decision.
11
+ * Deterministic output: receipts and conflicts sorted by (iss, jti) lexicographically.
12
+ *
13
+ * Security: 16 MB bundle size limit, path traversal prevention.
14
+ */
15
+ import { Command } from 'commander';
16
+ export declare function reconcileCommand(): Command;
17
+ //# sourceMappingURL=reconcile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reconcile.d.ts","sourceRoot":"","sources":["../../src/commands/reconcile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAqNpC,wBAAgB,gBAAgB,IAAI,OAAO,CAuH1C"}
package/dist/index.cjs CHANGED
@@ -11,6 +11,7 @@ var child_process = require('child_process');
11
11
  var crypto$1 = require('crypto');
12
12
  var schema = require('@peac/schema');
13
13
  var disc = require('@peac/disc');
14
+ var audit = require('@peac/audit');
14
15
 
15
16
  function _interopNamespace(e) {
16
17
  if (e && e.__esModule) return e;
@@ -2200,6 +2201,194 @@ JWKS for verification: ${path6__namespace.relative(outputDir, jwksPath)}`);
2200
2201
  process.exitCode = 1;
2201
2202
  }
2202
2203
  });
2204
+ var MAX_BUNDLE_SIZE = 16 * 1024 * 1024;
2205
+ function getGlobalOptions4(cmd) {
2206
+ const parent = cmd.parent;
2207
+ if (!parent) return {};
2208
+ return parent.opts();
2209
+ }
2210
+ function outputError4(error, details, opts) {
2211
+ if (opts.json) {
2212
+ console.log(JSON.stringify({ success: false, error, ...details }, null, 2));
2213
+ } else {
2214
+ console.error(`Error: ${error}`);
2215
+ }
2216
+ }
2217
+ function decodeJwsPayload(jws) {
2218
+ try {
2219
+ const { payload } = crypto.decode(jws);
2220
+ return payload;
2221
+ } catch {
2222
+ return null;
2223
+ }
2224
+ }
2225
+ function extractConflictKey(jws, receiptRef) {
2226
+ const payload = decodeJwsPayload(jws);
2227
+ if (!payload) {
2228
+ return `unknown|${receiptRef}`;
2229
+ }
2230
+ const iss = typeof payload.iss === "string" ? payload.iss : "unknown";
2231
+ if (typeof payload.jti === "string" && payload.jti.length > 0) {
2232
+ return `${iss}|${payload.jti}`;
2233
+ }
2234
+ if (typeof payload.rid === "string" && payload.rid.length > 0) {
2235
+ return `${iss}|${payload.rid}`;
2236
+ }
2237
+ return `${iss}|${receiptRef}`;
2238
+ }
2239
+ async function extractReceipts(contents, sourceLabel) {
2240
+ const records = [];
2241
+ for (const [_id, jws] of contents.receipts) {
2242
+ const receiptRef = await schema.computeReceiptRef(jws);
2243
+ const key = extractConflictKey(jws, receiptRef);
2244
+ records.push({ key, jws, receipt_ref: receiptRef, source: sourceLabel });
2245
+ }
2246
+ return records;
2247
+ }
2248
+ function findDiffFields(jwsA, jwsB) {
2249
+ const payloadA = decodeJwsPayload(jwsA);
2250
+ const payloadB = decodeJwsPayload(jwsB);
2251
+ if (!payloadA || !payloadB) return ["payload_decode_failed"];
2252
+ const allKeys = /* @__PURE__ */ new Set([...Object.keys(payloadA), ...Object.keys(payloadB)]);
2253
+ const diffs = [];
2254
+ for (const k of allKeys) {
2255
+ if (JSON.stringify(payloadA[k]) !== JSON.stringify(payloadB[k])) {
2256
+ diffs.push(k);
2257
+ }
2258
+ }
2259
+ return diffs.sort();
2260
+ }
2261
+ async function readBundle(bundlePath, label) {
2262
+ if (!fs6__namespace.existsSync(bundlePath)) {
2263
+ return { ok: false, error: `Bundle file not found: ${bundlePath}` };
2264
+ }
2265
+ const stat = fs6__namespace.statSync(bundlePath);
2266
+ if (stat.size > MAX_BUNDLE_SIZE) {
2267
+ return { ok: false, error: `Bundle ${label} exceeds 16 MB size limit (${stat.size} bytes)` };
2268
+ }
2269
+ const resolved = fs6__namespace.realpathSync(bundlePath);
2270
+ if (resolved !== bundlePath && !resolved.startsWith(process.cwd())) {
2271
+ if (!fs6__namespace.existsSync(resolved)) {
2272
+ return { ok: false, error: `Path traversal detected for ${label}` };
2273
+ }
2274
+ }
2275
+ const zipBuffer = fs6__namespace.readFileSync(bundlePath);
2276
+ const result = await audit.readDisputeBundle(zipBuffer);
2277
+ if (!result.ok) {
2278
+ return { ok: false, error: `Failed to read ${label}: ${result.error.message}` };
2279
+ }
2280
+ return { ok: true, value: result.value };
2281
+ }
2282
+ function formatReportText2(report) {
2283
+ const lines = [
2284
+ "PEAC Reconciliation Report",
2285
+ "==========================",
2286
+ "",
2287
+ `Generated: ${report.generated_at}`,
2288
+ `Bundles: ${report.bundles.join(", ")}`,
2289
+ `Total receipts: ${report.total_receipts}`,
2290
+ `Merged receipts: ${report.merged_receipts}`,
2291
+ `Conflicts: ${report.conflicts.length}`,
2292
+ ""
2293
+ ];
2294
+ if (report.conflicts.length === 0) {
2295
+ lines.push("No conflicts detected. Bundles merge cleanly.");
2296
+ } else {
2297
+ lines.push("Conflicts:");
2298
+ lines.push("---------");
2299
+ for (const conflict of report.conflicts) {
2300
+ lines.push(` Key: ${conflict.key}`);
2301
+ lines.push(` Bundle A ref: ${conflict.bundle_a_receipt_ref}`);
2302
+ lines.push(` Bundle B ref: ${conflict.bundle_b_receipt_ref}`);
2303
+ lines.push(` Different fields: ${conflict.diff_fields.join(", ")}`);
2304
+ lines.push("");
2305
+ }
2306
+ }
2307
+ return lines.join("\n");
2308
+ }
2309
+ function reconcileCommand() {
2310
+ const reconcile = new commander.Command("reconcile").description(
2311
+ "Merge evidence bundles and detect conflicts (v0.11.3+)"
2312
+ );
2313
+ reconcile.argument("<bundle1>", "Path to first bundle ZIP file").argument("<bundle2>", "Path to second bundle ZIP file").option("-f, --format <format>", "Output format: json or text", "text").option("--fail-on-conflict", "Exit code 1 when any conflict detected", false).action(
2314
+ async (bundle1Path, bundle2Path, options, cmd) => {
2315
+ const globalOpts = getGlobalOptions4(cmd);
2316
+ const format = options.format || "text";
2317
+ const failOnConflict = !!options.failOnConflict;
2318
+ try {
2319
+ const [result1, result2] = await Promise.all([
2320
+ readBundle(bundle1Path, "bundle1"),
2321
+ readBundle(bundle2Path, "bundle2")
2322
+ ]);
2323
+ if (!result1.ok) {
2324
+ outputError4(result1.error, {}, globalOpts);
2325
+ process.exitCode = 1;
2326
+ return;
2327
+ }
2328
+ if (!result2.ok) {
2329
+ outputError4(result2.error, {}, globalOpts);
2330
+ process.exitCode = 1;
2331
+ return;
2332
+ }
2333
+ const [receipts1, receipts2] = await Promise.all([
2334
+ extractReceipts(result1.value, bundle1Path),
2335
+ extractReceipts(result2.value, bundle2Path)
2336
+ ]);
2337
+ const index = /* @__PURE__ */ new Map();
2338
+ for (const r of [...receipts1, ...receipts2]) {
2339
+ const existing = index.get(r.key) ?? [];
2340
+ existing.push(r);
2341
+ index.set(r.key, existing);
2342
+ }
2343
+ const conflicts = [];
2344
+ let mergedCount = 0;
2345
+ const sortedKeys = [...index.keys()].sort();
2346
+ for (const key of sortedKeys) {
2347
+ const records = index.get(key);
2348
+ const fromA = records.filter((r) => r.source === bundle1Path);
2349
+ const fromB = records.filter((r) => r.source === bundle2Path);
2350
+ if (fromA.length > 0 && fromB.length > 0) {
2351
+ const a = fromA[0];
2352
+ const b = fromB[0];
2353
+ if (a.receipt_ref !== b.receipt_ref) {
2354
+ conflicts.push({
2355
+ key,
2356
+ bundle_a_receipt_ref: a.receipt_ref,
2357
+ bundle_b_receipt_ref: b.receipt_ref,
2358
+ diff_fields: findDiffFields(a.jws, b.jws)
2359
+ });
2360
+ }
2361
+ mergedCount++;
2362
+ } else {
2363
+ mergedCount++;
2364
+ }
2365
+ }
2366
+ const report = {
2367
+ version: "1.0",
2368
+ generated_at: (/* @__PURE__ */ new Date()).toISOString(),
2369
+ bundles: [bundle1Path, bundle2Path],
2370
+ total_receipts: receipts1.length + receipts2.length,
2371
+ merged_receipts: mergedCount,
2372
+ conflicts
2373
+ };
2374
+ if (format === "json" || globalOpts.json) {
2375
+ console.log(JSON.stringify(report, null, 2));
2376
+ } else {
2377
+ console.log(formatReportText2(report));
2378
+ }
2379
+ if (failOnConflict && conflicts.length > 0) {
2380
+ process.exitCode = 1;
2381
+ } else {
2382
+ process.exitCode = 0;
2383
+ }
2384
+ } catch (err) {
2385
+ outputError4(err.message, {}, globalOpts);
2386
+ process.exitCode = 1;
2387
+ }
2388
+ }
2389
+ );
2390
+ return reconcile;
2391
+ }
2203
2392
 
2204
2393
  // src/index.ts
2205
2394
  var program = new commander.Command();
@@ -2335,6 +2524,7 @@ program.command("decode").description("Decode a PEAC receipt JWS (without verifi
2335
2524
  program.addCommand(policy);
2336
2525
  program.addCommand(conformance);
2337
2526
  program.addCommand(samples);
2527
+ program.addCommand(reconcileCommand());
2338
2528
  try {
2339
2529
  program.parse();
2340
2530
  } catch (err) {