@conarium-ai/core 0.2.3 → 0.2.5

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,95 @@
1
+ /**
2
+ * Content-scanner normalisation — runs BEFORE IBAN / digit / email detectors.
3
+ *
4
+ * Invisible and look-alike characters were splitting identifiers so the IBAN
5
+ * scanner never fired, then the digit scanner ate the tail and left
6
+ * `TR33<ZWSP>000610051[MASKED_PII]` — which looks like protection, increments
7
+ * `maskedCount`, and still hands the prefix to the model. That is the failure
8
+ * this pass exists to prevent.
9
+ *
10
+ * Conscious output change: the string that leaves the scanner is the
11
+ * normalised form even when nothing was PII. Zero-width / format characters
12
+ * are stripped; fullwidth digits and `@` become ASCII; unicode dashes become
13
+ * ASCII hyphen. The model and the operator see the same bytes. A ZWSP that
14
+ * was only there to hide an IBAN is gone; a ZWSP that was only there as
15
+ * decoration is also gone. Documented, tested, not silent.
16
+ */
17
+ export function normalizePiiText(input) {
18
+ let s = input;
19
+ // ZWSP, ZWNJ, ZWJ, BOM, soft hyphen
20
+ s = s.replace(/[\u200B\u200C\u200D\uFEFF\u00AD]/g, '');
21
+ s = s.replace(/\uFF20/g, '@'); // fullwidth commercial at
22
+ s = s.replace(/[\uFF10-\uFF19]/g, (ch) => String.fromCharCode(ch.charCodeAt(0) - 0xff10 + 0x30));
23
+ s = s.replace(/[\u2010-\u2015\u2212]/g, '-');
24
+ return s;
25
+ }
26
+ /**
27
+ * Backstop: any leftover alphanumeric prefix glued to a mask
28
+ * (`411[MASKED_PII]`, `TR33000610051[MASKED_PII]`) is a partial mask.
29
+ * Collapse it to a full mask so maskedCount cannot claim protection
30
+ * while the prefix is still in the clear. The detectors must not produce
31
+ * this; this exists so a future mid-run match still cannot lie.
32
+ */
33
+ export function collapsePartialMask(text) {
34
+ // The literal is required. Without this guard `[A-Za-z0-9]+` eats a
35
+ // 12k-digit field, fails to find `[MASKED_PII]`, and backtracks O(n²).
36
+ if (!text.includes('[MASKED_PII]'))
37
+ return text;
38
+ return text.replace(/[A-Za-z0-9]+\[MASKED_PII\]/g, '[MASKED_PII]');
39
+ }
40
+ /** @deprecated name — same function, kept so existing imports compile during the cut */
41
+ export const collapsePartialIbanMask = collapsePartialMask;
42
+ export function decodedLooksLikePii(decoded) {
43
+ const n = normalizePiiText(decoded);
44
+ if (/[a-zA-Z0-9._%+-]{1,64}@[a-zA-Z0-9.-]{1,253}\.[a-zA-Z]{2,24}/.test(n))
45
+ return true;
46
+ if (/^[1-9][0-9]{10}$/.test(n) || /\b[1-9][0-9]{10}\b/.test(n))
47
+ return true;
48
+ return false;
49
+ }
50
+ /**
51
+ * Mask *tokens inside a field* that decode to PII. Whole-field base64 is
52
+ * handled separately. Length-capped so a 20 kB image blob is not scanned.
53
+ * Non-PII payloads (hashes, greetings, signatures) are left alone.
54
+ */
55
+ export function maskEmbeddedEncodedPii(text, ibanHit) {
56
+ // A maximal digit run longer than a hex/b64 token cannot contain a
57
+ // bounded token with word boundaries on both sides. Skipping avoids
58
+ // `{24,80}` backtracking across 12k hex-looking digits.
59
+ if (text.length > 80 && /^\d+$/.test(text))
60
+ return { text, count: 0 };
61
+ let count = 0;
62
+ let out = text;
63
+ out = out.replace(/(?<![A-Za-z0-9+/])([A-Za-z0-9+/]{16,80}={0,2})(?![A-Za-z0-9+/=])/g, (token) => {
64
+ if (token.includes('[MASKED'))
65
+ return token;
66
+ try {
67
+ const decoded = Buffer.from(token, 'base64').toString('utf8');
68
+ if (!decoded || decoded.includes('\u0000'))
69
+ return token;
70
+ if (decodedLooksLikePii(decoded) || ibanHit(decoded)) {
71
+ count++;
72
+ return '[MASKED_PII]';
73
+ }
74
+ }
75
+ catch { /* not base64 */ }
76
+ return token;
77
+ });
78
+ out = out.replace(/\b([0-9A-Fa-f]{24,80})\b/g, (token) => {
79
+ if (token.length % 2 !== 0)
80
+ return token;
81
+ try {
82
+ const decoded = Buffer.from(token, 'hex').toString('utf8');
83
+ if (!decoded || decoded.includes('\u0000'))
84
+ return token;
85
+ if (decodedLooksLikePii(decoded) || ibanHit(decoded)) {
86
+ count++;
87
+ return '[MASKED_PII]';
88
+ }
89
+ }
90
+ catch { /* not hex */ }
91
+ return token;
92
+ });
93
+ return { text: out, count };
94
+ }
95
+ //# sourceMappingURL=pii_normalize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pii_normalize.js","sourceRoot":"","sources":["../src/pii_normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,IAAI,CAAC,GAAG,KAAK,CAAA;IACb,oCAAoC;IACpC,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,mCAAmC,EAAE,EAAE,CAAC,CAAA;IACtD,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAA,CAAC,0BAA0B;IACxD,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,MAAM,GAAG,IAAI,CAAC,CAAC,CAAA;IAChG,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,wBAAwB,EAAE,GAAG,CAAC,CAAA;IAC5C,OAAO,CAAC,CAAA;AACV,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAC,IAAY;IAC9C,oEAAoE;IACpE,uEAAuE;IACvE,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC;QAAE,OAAO,IAAI,CAAA;IAC/C,OAAO,IAAI,CAAC,OAAO,CAAC,6BAA6B,EAAE,cAAc,CAAC,CAAA;AACpE,CAAC;AAED,wFAAwF;AACxF,MAAM,CAAC,MAAM,uBAAuB,GAAG,mBAAmB,CAAA;AAE1D,MAAM,UAAU,mBAAmB,CAAC,OAAe;IACjD,MAAM,CA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,GAAG,CAAC,OAAO,CAAC,2BAA2B,EAAE,CAAC,KAAK,EAAE,EAAE;QACvD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC;YAAE,OAAO,KAAK,CAAA;QACxC,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;YAC1D,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC;gBAAE,OAAO,KAAK,CAAA;YACxD,IAAI,mBAAmB,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;gBACrD,KAAK,EAAE,CAAA;gBACP,OAAO,cAAc,CAAA;YACvB,CAAC;QACH,CAAC;QAAC,MAAM,CAAC,CAAC,aAAa,CAAC,CAAC;QACzB,OAAO,KAAK,CAAA;IACd,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,CAAA;AAC7B,CAAC"}
@@ -12,13 +12,19 @@ Design source: `docs/superpowers/specs/2026-07-29-conarium-receipt-design.md`.
12
12
 
13
13
  ## Official claim (do not widen)
14
14
 
15
- > A Conarium Receipt proves that records have **not been altered, deleted,
16
- > reordered, or backdated after they were created**. It does **not** prove they
17
- > were correct at the moment of creation.
18
-
19
- *(TR)* Conarium Makbuzu, kayıtların **oluşturulduktan sonra değiştirilmediğini,
20
- silinmediğini, yeniden sıralanmadığını ve geriye dönük tarihlenmediğini**
21
- kanıtlar. **Oluşturma anında doğru olduğunu kanıtlamaz.**
15
+ > A Conarium Receipt proves that the records **still in the file** have not been
16
+ > altered, reordered, or backdated after they were created, and that none were
17
+ > **removed from the middle** of the chain (`prevHash` / `seq`). It does **not**
18
+ > prove they were correct at the moment of creation. It cannot, by itself, prove
19
+ > that records were not **dropped from the end**: a shorter leftover chain is
20
+ > still internally consistent. Catching tail truncation needs a pin from outside
21
+ > the file (`--expect-count`, `--expect-last-hash`, an OpenTimestamps anchor, or
22
+ > `conarium-reconcile`).
23
+
24
+ *(TR)* Conarium Makbuzu, dosyada **hâlâ duran** kayıtların oluşturulduktan sonra
25
+ değiştirilmediğini, **ortadan** silinmediğini, yeniden sıralanmadığını ve geriye
26
+ dönük tarihlenmediğini kanıtlar. **Oluşturma anında doğru olduğunu kanıtlamaz.**
27
+ Sondan kesmeyi tek başına göremez.
22
28
 
23
29
  ## Media type
24
30
 
@@ -86,14 +92,20 @@ merely *who* it is, and the coverage declaration's rule that absence is reported
86
92
  ## Verifier
87
93
 
88
94
  ```
89
- conarium-verify <file|dir> --pubkey <path> [--pubkey <path2>] [--anchor-check] [--expect-seq-from N] [--json]
95
+ conarium-verify <file|dir> --pubkey <path> [--pubkey <path2>] [--anchor-check] [--expect-seq-from N] [--expect-count N] [--expect-last-hash sha256:…] [--json]
90
96
  ```
91
97
 
98
+ `--expect-count` / `--expect-last-hash` are **opt-in tail pins**. Without them,
99
+ a chain that had its last receipts deleted still exits 0 — the remainder is a
100
+ valid shorter chain. With them, a count or last-hash miss is exit **11** (same
101
+ code as a `prevHash` break: the chain as presented is not the chain you pinned).
102
+ Default behaviour and the exit-code list are unchanged.
103
+
92
104
  | Exit | Meaning |
93
105
  |---|---|
94
106
  | 0 | Chain intact, signatures valid |
95
107
  | 10 | Hash mismatch — record altered |
96
- | 11 | `prevHash` break — deleted or inserted |
108
+ | 11 | `prevHash` break — deleted or inserted; also `--expect-count` / `--expect-last-hash` mismatch |
97
109
  | 12 | `seq` gap / non-increasing — missing or reordered |
98
110
  | 13 | Signature invalid / pubkey missing (fail-closed) |
99
111
  | 14 | Anchor missing/invalid under `--anchor-check` |
@@ -250,6 +262,7 @@ revision history is itself timestamped.
250
262
  3. **Creation-time truth is not proven.** Without hardware attestation, an operator can still write false-but-well-formed receipts *before* anchoring.
251
263
  4. **In-file `sig` stripping + HMAC/`anchor` reduction.** Content `hash` is computed with `{hash, sig, anchor}` (and audit `signature`/`sig`) excluded, so an operator who controls the file can drop or thin those fields without invalidating the content hash itself. Contiguity and the trust store catch some boot-time cases, but full protection against in-file strip/reduce games is **not solvable in-file** (*in-file çözülemez*) — it needs an external transparency-log anchor and/or out-of-band key ceremony. **Mitigation available today, measured:** keep `CONARIUM_AUDIT_HMAC_KEY` enabled alongside Ed25519. HMAC is keyed, so an actor who strips `sig` and recomputes the unkeyed hashes still fails the HMAC check (`entry signature mismatch`). Verified by test: Ed25519 alone → strip-all passes the boot check; Ed25519 + HMAC → caught. `conarium-verify --pubkey` also catches it (exit 13, `missing sig`) because it is told to expect signatures. **Anchor is available** (`CONARIUM_ANCHOR_SINK=opentimestamps`) but starts as `pending` — Bitcoin finality is delayed (hours); see §Anchoring.
252
264
  5. **`argsHash` hurts debugging.** Support cases need the customer's own logs to correlate.
265
+ 6. **Tail truncation is invisible to `conarium-verify` unless pinned.** Deleting the last N receipts leaves a consistent leftover chain (exit 0). `--expect-count` / `--expect-last-hash` exist for operators who have an external length or last-hash; they are opt-in so a verifier that saw the same file yesterday still exits 0 today. Anchoring the head, or `conarium-reconcile` against the database's own counters, are the other pins.
253
266
 
254
267
  ## Anchoring (OpenTimestamps)
255
268
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@conarium-ai/core",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "//files-artefakt": "examples/ altindaki uretilmis dosyalar (ozel anahtar, makbuz/denetim JSONL) ELENIR. .gitignore npm'i baglamaz: izin listesinde 'examples' oldugu icin demoyu kosturmus bir makinede 'npm publish' _keys/audit-ed25519.pem'i (OZEL ANAHTAR) ve makbuzlari yayinliyordu. test/pack_artefakt.mjs bunu kilitler. Elemeler examples/ ile SINIRLI: test-vectors/ altindaki receipts.jsonl ve .pub.pem YAYINLANMAK ZORUNDA, yoksa ucuncu taraf konformans vektorlerini kosturamaz.",
5
5
  "//files": "Pakete NE gireceginin izin listesi. Liste olmadan npm her seyi gonderiyordu: 296 dosya / 1.5 MB — CI workflowlari, gitleaks ayari, 137 KB pazarlama gorseli, sitenin api/chat.js dosyasi. Bir bankanin guvenlik ekibi tarball'i acip bunlari gorur. scripts/ BILEREK var: conarium-reconcile calisirken scripts/pg-snapshot.sql'e atif yapiyor, olmazsa kullanici destek maili yazar.",
6
6
  "files": [
@@ -69,7 +69,7 @@
69
69
  "build": "tsc",
70
70
  "test": "vitest run",
71
71
  "test:anchor-verify": "npm run build && node test/anchor_verify.mjs",
72
- "test:checks": "npm run build && node test/doctor.mjs && node test/console_cli.mjs && node test/console_policy.mjs && node test/init.mjs && node test/demo_bank_seed.mjs && node test/pack_artefakt.mjs && node test/connectors.mjs && node test/governance_lineage_adversarial.mjs && node test/governance_security_regression.mjs && node test/security_hardening_14.mjs && node test/anchor_verify.mjs && node test/spec_exitcode_drift.mjs && node test/vectors_run.mjs && node test/http_gateway.e2e.mjs",
72
+ "test:checks": "npm run build && node test/doctor.mjs && node test/console_cli.mjs && node test/console_policy.mjs && node test/init.mjs && node test/demo_bank_seed.mjs && node test/pack_artefakt.mjs && node test/connectors.mjs && node test/governance_lineage_adversarial.mjs && node test/governance_security_regression.mjs && node test/security_hardening_14.mjs && node test/anchor_verify.mjs && node test/spec_exitcode_drift.mjs && node test/encoding_evasion_mask.mjs && node test/verify_tail_pin.mjs && node test/partial_mask_root.mjs && node test/pii_regression_matrix.mjs && node test/scanner_perf.mjs && node test/vectors_run.mjs && node test/http_gateway.e2e.mjs",
73
73
  "test:all": "npm run lint && npm run test && npm run test:checks",
74
74
  "dev": "tsx watch src/index.ts",
75
75
  "start": "node dist/index.js",