@longarc/mdash 3.1.2 → 3.1.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.
Files changed (172) hide show
  1. package/README.md +86 -23
  2. package/SECURITY.md +254 -0
  3. package/dist/accountability/engine.d.ts +27 -0
  4. package/dist/accountability/engine.d.ts.map +1 -0
  5. package/dist/accountability/engine.js +148 -0
  6. package/dist/accountability/engine.js.map +1 -0
  7. package/dist/accountability/types.d.ts +46 -0
  8. package/dist/accountability/types.d.ts.map +1 -0
  9. package/dist/accountability/types.js +8 -0
  10. package/dist/accountability/types.js.map +1 -0
  11. package/dist/checkpoint/engine.d.ts.map +1 -1
  12. package/dist/checkpoint/engine.js +4 -0
  13. package/dist/checkpoint/engine.js.map +1 -1
  14. package/dist/context/compose.d.ts +62 -0
  15. package/dist/context/compose.d.ts.map +1 -0
  16. package/dist/context/compose.js +286 -0
  17. package/dist/context/compose.js.map +1 -0
  18. package/dist/context/crypto/hash.d.ts +100 -0
  19. package/dist/context/crypto/hash.d.ts.map +1 -0
  20. package/dist/context/crypto/hash.js +248 -0
  21. package/dist/context/crypto/hash.js.map +1 -0
  22. package/dist/context/crypto/hmac.d.ts +80 -0
  23. package/dist/context/crypto/hmac.d.ts.map +1 -0
  24. package/dist/context/crypto/hmac.js +192 -0
  25. package/dist/context/crypto/hmac.js.map +1 -0
  26. package/dist/context/crypto/index.d.ts +7 -0
  27. package/dist/context/crypto/index.d.ts.map +1 -0
  28. package/dist/context/crypto/index.js +7 -0
  29. package/dist/context/crypto/index.js.map +1 -0
  30. package/dist/context/engine-v3.0-backup.d.ts +197 -0
  31. package/dist/context/engine-v3.0-backup.d.ts.map +1 -0
  32. package/dist/context/engine-v3.0-backup.js +392 -0
  33. package/dist/context/engine-v3.0-backup.js.map +1 -0
  34. package/dist/context/fragment.d.ts +99 -0
  35. package/dist/context/fragment.d.ts.map +1 -0
  36. package/dist/context/fragment.js +316 -0
  37. package/dist/context/fragment.js.map +1 -0
  38. package/dist/context/index.d.ts +99 -0
  39. package/dist/context/index.d.ts.map +1 -0
  40. package/dist/context/index.js +180 -0
  41. package/dist/context/index.js.map +1 -0
  42. package/dist/context/provenance.d.ts +80 -0
  43. package/dist/context/provenance.d.ts.map +1 -0
  44. package/dist/context/provenance.js +294 -0
  45. package/dist/context/provenance.js.map +1 -0
  46. package/dist/context/resolve.d.ts +106 -0
  47. package/dist/context/resolve.d.ts.map +1 -0
  48. package/dist/context/resolve.js +440 -0
  49. package/dist/context/resolve.js.map +1 -0
  50. package/dist/context/store.d.ts +156 -0
  51. package/dist/context/store.d.ts.map +1 -0
  52. package/dist/context/store.js +396 -0
  53. package/dist/context/store.js.map +1 -0
  54. package/dist/context/types.d.ts +463 -0
  55. package/dist/context/types.d.ts.map +1 -0
  56. package/dist/context/types.js +94 -0
  57. package/dist/context/types.js.map +1 -0
  58. package/dist/context/utils/atomic.d.ts +76 -0
  59. package/dist/context/utils/atomic.d.ts.map +1 -0
  60. package/dist/context/utils/atomic.js +159 -0
  61. package/dist/context/utils/atomic.js.map +1 -0
  62. package/dist/context/utils/credit.d.ts +65 -0
  63. package/dist/context/utils/credit.d.ts.map +1 -0
  64. package/dist/context/utils/credit.js +164 -0
  65. package/dist/context/utils/credit.js.map +1 -0
  66. package/dist/context/utils/index.d.ts +13 -0
  67. package/dist/context/utils/index.d.ts.map +1 -0
  68. package/dist/context/utils/index.js +13 -0
  69. package/dist/context/utils/index.js.map +1 -0
  70. package/dist/context/utils/utility.d.ts +63 -0
  71. package/dist/context/utils/utility.d.ts.map +1 -0
  72. package/dist/context/utils/utility.js +141 -0
  73. package/dist/context/utils/utility.js.map +1 -0
  74. package/dist/core/commitment.d.ts +25 -2
  75. package/dist/core/commitment.d.ts.map +1 -1
  76. package/dist/core/commitment.js +44 -6
  77. package/dist/core/commitment.js.map +1 -1
  78. package/dist/core/crypto.d.ts +2 -0
  79. package/dist/core/crypto.d.ts.map +1 -1
  80. package/dist/core/crypto.js +12 -0
  81. package/dist/core/crypto.js.map +1 -1
  82. package/dist/index.d.ts +11 -6
  83. package/dist/index.d.ts.map +1 -1
  84. package/dist/index.js +35 -10
  85. package/dist/index.js.map +1 -1
  86. package/dist/mcca/engine.d.ts.map +1 -1
  87. package/dist/mcca/engine.js +5 -4
  88. package/dist/mcca/engine.js.map +1 -1
  89. package/dist/physics/engine.d.ts +1 -0
  90. package/dist/physics/engine.d.ts.map +1 -1
  91. package/dist/physics/engine.js +36 -2
  92. package/dist/physics/engine.js.map +1 -1
  93. package/dist/provenance/api-handler.d.ts +45 -0
  94. package/dist/provenance/api-handler.d.ts.map +1 -0
  95. package/dist/provenance/api-handler.js +223 -0
  96. package/dist/provenance/api-handler.js.map +1 -0
  97. package/dist/provenance/api-types.d.ts +108 -0
  98. package/dist/provenance/api-types.d.ts.map +1 -0
  99. package/dist/provenance/api-types.js +9 -0
  100. package/dist/provenance/api-types.js.map +1 -0
  101. package/dist/provenance/index.d.ts +6 -0
  102. package/dist/provenance/index.d.ts.map +1 -0
  103. package/dist/provenance/index.js +3 -0
  104. package/dist/provenance/index.js.map +1 -0
  105. package/dist/provenance/provenance-engine.d.ts +63 -0
  106. package/dist/provenance/provenance-engine.d.ts.map +1 -0
  107. package/dist/provenance/provenance-engine.js +311 -0
  108. package/dist/provenance/provenance-engine.js.map +1 -0
  109. package/dist/provenance/types.d.ts +193 -0
  110. package/dist/provenance/types.d.ts.map +1 -0
  111. package/dist/provenance/types.js +9 -0
  112. package/dist/provenance/types.js.map +1 -0
  113. package/dist/tee/engine.d.ts.map +1 -1
  114. package/dist/tee/engine.js +14 -0
  115. package/dist/tee/engine.js.map +1 -1
  116. package/dist/warrant/engine.d.ts +24 -1
  117. package/dist/warrant/engine.d.ts.map +1 -1
  118. package/dist/warrant/engine.js +76 -1
  119. package/dist/warrant/engine.js.map +1 -1
  120. package/dist/zk/engine.d.ts.map +1 -1
  121. package/dist/zk/engine.js +7 -4
  122. package/dist/zk/engine.js.map +1 -1
  123. package/docs/SECURITY-PATCHES.md +170 -0
  124. package/package.json +17 -5
  125. package/src/__tests__/accountability.test.ts +308 -0
  126. package/src/__tests__/l1-verification-modes.test.ts +424 -0
  127. package/src/__tests__/phase1.benchmark.test.ts +94 -0
  128. package/src/__tests__/phase1.test.ts +0 -77
  129. package/src/__tests__/phase2-4.benchmark.test.ts +60 -0
  130. package/src/__tests__/phase2-4.test.ts +1 -52
  131. package/src/__tests__/provenance/api-handler.test.ts +356 -0
  132. package/src/__tests__/provenance/provenance-engine.test.ts +628 -0
  133. package/src/__tests__/sa-2026-008.test.ts +45 -0
  134. package/src/__tests__/sa-2026-009.test.ts +86 -0
  135. package/src/__tests__/sa-2026-010.test.ts +72 -0
  136. package/src/__tests__/sa-2026-012.test.ts +65 -0
  137. package/src/__tests__/sa-2026-nfc.test.ts +40 -0
  138. package/src/__tests__/security.test.ts +786 -0
  139. package/src/accountability/engine.ts +230 -0
  140. package/src/accountability/types.ts +58 -0
  141. package/src/checkpoint/engine.ts +4 -0
  142. package/src/context/__tests__/caret-v0.2.0.test.ts +860 -0
  143. package/src/context/__tests__/integration.test.ts +356 -0
  144. package/src/context/compose.ts +388 -0
  145. package/src/context/crypto/hash.ts +277 -0
  146. package/src/context/crypto/hmac.ts +253 -0
  147. package/src/context/crypto/index.ts +29 -0
  148. package/src/context/engine-v3.0-backup.ts +598 -0
  149. package/src/context/fragment.ts +454 -0
  150. package/src/context/index.ts +427 -0
  151. package/src/context/provenance.ts +380 -0
  152. package/src/context/resolve.ts +581 -0
  153. package/src/context/store.ts +503 -0
  154. package/src/context/types.ts +679 -0
  155. package/src/context/utils/atomic.ts +207 -0
  156. package/src/context/utils/credit.ts +224 -0
  157. package/src/context/utils/index.ts +13 -0
  158. package/src/context/utils/utility.ts +200 -0
  159. package/src/core/commitment.ts +129 -67
  160. package/src/core/crypto.ts +13 -0
  161. package/src/index.ts +62 -10
  162. package/src/mcca/engine.ts +5 -4
  163. package/src/physics/engine.ts +40 -3
  164. package/src/provenance/api-handler.ts +248 -0
  165. package/src/provenance/api-types.ts +112 -0
  166. package/src/provenance/index.ts +19 -0
  167. package/src/provenance/provenance-engine.ts +387 -0
  168. package/src/provenance/types.ts +211 -0
  169. package/src/tee/engine.ts +16 -0
  170. package/src/warrant/engine.ts +89 -1
  171. package/src/zk/engine.ts +8 -4
  172. package/tsconfig.json +1 -1
@@ -19,6 +19,7 @@ import {
19
19
  generateTimestamp,
20
20
  sha256Object,
21
21
  hmacSeal,
22
+ hmacVerify,
22
23
  deriveKey,
23
24
  } from '../core/crypto.js';
24
25
 
@@ -65,7 +66,7 @@ export interface Warrant {
65
66
  policy_id: string;
66
67
  /** Current state */
67
68
  state: WarrantState;
68
- /** Accountability tier */
69
+ /** Liability tier */
69
70
  tier: WarrantTier;
70
71
  /** Operational constraints */
71
72
  constraints: WarrantConstraints;
@@ -125,10 +126,18 @@ export class WarrantCache {
125
126
  private cache: Map<WarrantId, CacheEntry> = new Map();
126
127
  private speculative: Map<string, WarrantId[]> = new Map(); // agent_id -> warrant_ids
127
128
  private revocations: Set<WarrantId> = new Set();
129
+ private verifyKey: CryptoKey | null = null;
128
130
 
129
131
  private readonly DEFAULT_TTL = 5 * 60 * 1000; // 5 minutes
130
132
  private readonly SPECULATIVE_TTL = 60 * 1000; // 60 seconds (WARRANT-INV-001)
131
133
 
134
+ /**
135
+ * P2 SECURITY: Set verification key for seal checks on retrieval
136
+ */
137
+ setVerificationKey(key: CryptoKey): void {
138
+ this.verifyKey = key;
139
+ }
140
+
132
141
  /**
133
142
  * Store a warrant in cache
134
143
  */
@@ -175,6 +184,45 @@ export class WarrantCache {
175
184
  return entry.warrant;
176
185
  }
177
186
 
187
+ /**
188
+ * P2 SECURITY: Get warrant with seal verification
189
+ * Use for security-critical operations to prevent cache poisoning
190
+ */
191
+ async getVerified(id: WarrantId): Promise<Warrant | SpeculativeWarrant | null> {
192
+ const warrant = this.get(id);
193
+ if (!warrant) return null;
194
+
195
+ // If no verification key set, fall back to unverified (log warning)
196
+ if (!this.verifyKey) {
197
+ console.warn('WarrantCache.getVerified called without verification key');
198
+ return warrant;
199
+ }
200
+
201
+ // Verify seal integrity
202
+ const warrantData = {
203
+ _v: 1,
204
+ id: warrant.id,
205
+ agent_id: warrant.agent_id,
206
+ policy_id: warrant.policy_id,
207
+ state: warrant.state === 'SPECULATIVE' ? 'SPECULATIVE' : warrant.state,
208
+ tier: warrant.tier,
209
+ constraints: warrant.constraints,
210
+ created_at: warrant.created_at,
211
+ expires_at: warrant.expires_at,
212
+ issued_by: warrant.issued_by,
213
+ };
214
+
215
+ const isValid = await hmacVerify(warrantData, warrant.seal, this.verifyKey);
216
+ if (!isValid) {
217
+ // P2 SECURITY: Cache poisoning detected - remove and return null
218
+ console.error(`SECURITY: Warrant seal verification failed for ${id}`);
219
+ this.cache.delete(id);
220
+ return null;
221
+ }
222
+
223
+ return warrant;
224
+ }
225
+
178
226
  /**
179
227
  * Get speculative warrants for an agent
180
228
  * For pre-staged activation
@@ -261,6 +309,11 @@ export class WarrantEngine {
261
309
  private eventLog: WarrantEvent[] = [];
262
310
  private lastEventHash: Hash | null = null;
263
311
 
312
+ // P3 SECURITY: Rate limiting for warrant creation
313
+ private creationRateLimits: Map<string, { count: number; windowStart: number }> = new Map();
314
+ private readonly CREATION_RATE_LIMIT = 100; // Max warrants per issuer per minute
315
+ private readonly CREATION_RATE_WINDOW_MS = 60 * 1000;
316
+
264
317
  constructor(commitmentEngine: CommitmentEngine) {
265
318
  this.cache = new WarrantCache();
266
319
  this.commitmentEngine = commitmentEngine;
@@ -271,6 +324,8 @@ export class WarrantEngine {
271
324
  */
272
325
  async initialize(sealKey: string): Promise<void> {
273
326
  this.key = await deriveKey(sealKey);
327
+ // P2 SECURITY: Enable seal verification on cache retrieval
328
+ this.cache.setVerificationKey(this.key);
274
329
  }
275
330
 
276
331
  /**
@@ -289,6 +344,9 @@ export class WarrantEngine {
289
344
  throw new Error('Engine not initialized. Call initialize() first.');
290
345
  }
291
346
 
347
+ // P3 SECURITY: Rate limit warrant creation per issuer
348
+ this.checkCreationRateLimit(params.issued_by);
349
+
292
350
  const startTime = performance.now();
293
351
 
294
352
  const id = generateWarrantId();
@@ -545,6 +603,28 @@ export class WarrantEngine {
545
603
  return true;
546
604
  }
547
605
 
606
+ /**
607
+ * P3 SECURITY: Check rate limit for warrant creation
608
+ * Prevents DoS via cache flooding
609
+ */
610
+ private checkCreationRateLimit(issuerId: string): void {
611
+ const now = Date.now();
612
+ let limiter = this.creationRateLimits.get(issuerId);
613
+
614
+ // Reset window if expired
615
+ if (!limiter || now - limiter.windowStart > this.CREATION_RATE_WINDOW_MS) {
616
+ limiter = { count: 0, windowStart: now };
617
+ }
618
+
619
+ limiter.count++;
620
+ this.creationRateLimits.set(issuerId, limiter);
621
+
622
+ if (limiter.count > this.CREATION_RATE_LIMIT) {
623
+ console.warn(`[WARRANT] Rate limit exceeded for issuer: ${issuerId}`);
624
+ throw new Error('Warrant creation rate limit exceeded');
625
+ }
626
+ }
627
+
548
628
  /**
549
629
  * Log a warrant event with hash chain
550
630
  */
@@ -590,6 +670,14 @@ export class WarrantEngine {
590
670
  return [...this.eventLog];
591
671
  }
592
672
 
673
+ /**
674
+ * Check if a warrant has been revoked
675
+ * P1 SECURITY: Used for TOCTOU protection in execute flow
676
+ */
677
+ isRevoked(id: WarrantId): boolean {
678
+ return this.cache.isRevoked(id);
679
+ }
680
+
593
681
  /**
594
682
  * Get cache statistics
595
683
  */
package/src/zk/engine.ts CHANGED
@@ -25,6 +25,7 @@ import {
25
25
  sha256Object,
26
26
  hmacSeal,
27
27
  deriveKey,
28
+ constantTimeEqual,
28
29
  } from '../core/crypto.js';
29
30
 
30
31
  import { CommitmentEngine, Commitment } from '../core/commitment.js';
@@ -395,7 +396,8 @@ export class ZKProofsEngine {
395
396
  verifier: 'mdash-zk-engine-v3',
396
397
  };
397
398
 
398
- // Re-seal with proof included
399
+ // Re-seal with proof included (SA-2026-009: zero seal before computing)
400
+ item.document.seal = '' as Seal;
399
401
  item.document.seal = await hmacSeal(item.document, this.key!);
400
402
 
401
403
  // Commit to L1
@@ -537,12 +539,14 @@ export class ZKProofsEngine {
537
539
  return { valid: false, errors };
538
540
  }
539
541
 
540
- // 3. Verify seal
542
+ // 3. Verify seal (SA-2026-009: constant-time comparison closes verification gap)
541
543
  const docForSeal = { ...document };
542
544
  docForSeal.seal = '' as Seal;
543
545
  const expectedSeal = await hmacSeal(docForSeal, this.key);
544
- // Note: In production, seal verification would be more robust
545
-
546
+ if (!constantTimeEqual(expectedSeal, document.seal)) {
547
+ errors.push('Seal verification failed');
548
+ }
549
+
546
550
  // 4. Verify proof structure
547
551
  if (!document.proof.verifier_key_hash) {
548
552
  errors.push('Missing verifier key hash');
package/tsconfig.json CHANGED
@@ -17,5 +17,5 @@
17
17
  "resolveJsonModule": true
18
18
  },
19
19
  "include": ["src/**/*"],
20
- "exclude": ["node_modules", "dist", "src/__tests__"]
20
+ "exclude": ["node_modules", "dist", "src/**/__tests__"]
21
21
  }