@conarium-ai/core 0.2.9 → 0.2.11

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 (101) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/LIMITATIONS.md +70 -0
  3. package/LIMITATIONS.tr.md +70 -0
  4. package/README.md +20 -8
  5. package/SECURITY.md +14 -0
  6. package/bin/conarium-anchor-upgrade.mjs +10 -43
  7. package/bin/conarium-doctor.mjs +221 -24
  8. package/bin/conarium-stamp.mjs +4 -13
  9. package/bin/conarium-verify.mjs +13 -62
  10. package/bin/doctor-sql.mjs +47 -0
  11. package/deploy/anchor-service/README.md +3 -4
  12. package/dist/anchor-service.d.ts.map +1 -1
  13. package/dist/anchor-service.js +11 -26
  14. package/dist/anchor-service.js.map +1 -1
  15. package/dist/anchor.d.ts +6 -31
  16. package/dist/anchor.d.ts.map +1 -1
  17. package/dist/anchor.js +15 -65
  18. package/dist/anchor.js.map +1 -1
  19. package/dist/config.d.ts +14 -6
  20. package/dist/config.d.ts.map +1 -1
  21. package/dist/config.js +6 -0
  22. package/dist/config.js.map +1 -1
  23. package/dist/console.d.ts.map +1 -1
  24. package/dist/console.js +14 -3
  25. package/dist/console.js.map +1 -1
  26. package/dist/governance.d.ts +8 -0
  27. package/dist/governance.d.ts.map +1 -1
  28. package/dist/governance.js +29 -78
  29. package/dist/governance.js.map +1 -1
  30. package/dist/masking-cost.d.ts +16 -0
  31. package/dist/masking-cost.d.ts.map +1 -0
  32. package/dist/masking-cost.js +31 -0
  33. package/dist/masking-cost.js.map +1 -0
  34. package/dist/ots/calendar.d.ts +11 -0
  35. package/dist/ots/calendar.d.ts.map +1 -0
  36. package/dist/ots/calendar.js +93 -0
  37. package/dist/ots/calendar.js.map +1 -0
  38. package/dist/ots/client.d.ts +29 -0
  39. package/dist/ots/client.d.ts.map +1 -0
  40. package/dist/ots/client.js +102 -0
  41. package/dist/ots/client.js.map +1 -0
  42. package/dist/ots/codec.d.ts +26 -0
  43. package/dist/ots/codec.d.ts.map +1 -0
  44. package/dist/ots/codec.js +92 -0
  45. package/dist/ots/codec.js.map +1 -0
  46. package/dist/ots/format.d.ts +60 -0
  47. package/dist/ots/format.d.ts.map +1 -0
  48. package/dist/ots/format.js +232 -0
  49. package/dist/ots/format.js.map +1 -0
  50. package/dist/server.d.ts.map +1 -1
  51. package/dist/server.js +3 -1
  52. package/dist/server.js.map +1 -1
  53. package/dist/sql-gate/dispatch.d.ts +17 -0
  54. package/dist/sql-gate/dispatch.d.ts.map +1 -0
  55. package/dist/sql-gate/dispatch.js +30 -0
  56. package/dist/sql-gate/dispatch.js.map +1 -0
  57. package/dist/sql-gate/mssql.d.ts +11 -0
  58. package/dist/sql-gate/mssql.d.ts.map +1 -0
  59. package/dist/sql-gate/mssql.js +287 -0
  60. package/dist/sql-gate/mssql.js.map +1 -0
  61. package/dist/sql-gate/oracle.d.ts +11 -0
  62. package/dist/sql-gate/oracle.d.ts.map +1 -0
  63. package/dist/sql-gate/oracle.js +227 -0
  64. package/dist/sql-gate/oracle.js.map +1 -0
  65. package/dist/sql-gate/postgres.d.ts +12 -0
  66. package/dist/sql-gate/postgres.d.ts.map +1 -0
  67. package/dist/sql-gate/postgres.js +64 -0
  68. package/dist/sql-gate/postgres.js.map +1 -0
  69. package/dist/sql-gate/rules.d.ts +14 -0
  70. package/dist/sql-gate/rules.d.ts.map +1 -0
  71. package/dist/sql-gate/rules.js +73 -0
  72. package/dist/sql-gate/rules.js.map +1 -0
  73. package/dist/sql-gate/types.d.ts +31 -0
  74. package/dist/sql-gate/types.d.ts.map +1 -0
  75. package/dist/sql-gate/types.js +9 -0
  76. package/dist/sql-gate/types.js.map +1 -0
  77. package/dist/types.d.ts +6 -0
  78. package/dist/types.d.ts.map +1 -1
  79. package/docs/API-STABILITY.md +174 -0
  80. package/docs/BENCHMARK.md +124 -0
  81. package/docs/audit/2026-08-14-e2e-health.md +69 -0
  82. package/docs/audit/2026-08-14-security-scan.md +48 -0
  83. package/docs/audit/2026-08-14-surface-sync.md +34 -0
  84. package/docs/benchmarks/latest.json +2310 -0
  85. package/docs/benchmarks/masking-cost-threshold.json +6 -0
  86. package/docs/benchmarks/overhead-20260814T1238Z-win32.json +402 -0
  87. package/docs/benchmarks/overhead-20260814T1346Z-linux.json +2310 -0
  88. package/docs/benchmarks/overhead-20260814T1644Z-win32.json +2310 -0
  89. package/docs/releases/v0.2.10.md +24 -0
  90. package/docs/releases/v0.2.8.md +23 -0
  91. package/docs/releases/v0.2.9.md +26 -0
  92. package/docs/security/PENTEST-SCOPE.md +90 -0
  93. package/docs/security/THREAT-MODEL.md +118 -0
  94. package/docs/specs/2026-08-14-mssql-bypass-inventory.md +37 -0
  95. package/docs/specs/2026-08-14-oracle-bypass-inventory.md +28 -0
  96. package/package.json +9 -13
  97. package/scripts/benchmark-overhead.mjs +492 -0
  98. package/test-vectors/README.md +3 -0
  99. package/test-vectors/sql-gate/LAST-RUN.json +11 -0
  100. package/test-vectors/sql-gate/README.md +19 -0
  101. package/test-vectors/sql-gate/vectors.json +331 -0
package/CHANGELOG.md CHANGED
@@ -2,6 +2,36 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ - **Postgres overhead measured.** Same SELECT, default `maxRows` 100: about
6
+ 5 ms added when email is masked. 500 → ~87 ms. 5 000 → ~22 s. Cost
7
+ follows distinct masked values. Doctor and boot warn above 100; they
8
+ do not reject the query. See `docs/BENCHMARK.md`.
9
+ - **Carry-over corpus lock.** `src/governance.carryover-diff.test.ts`.
10
+ A single-pass rewrite disagreed on sequential longest-first overlap
11
+ and was reverted.
12
+
13
+ - **Overhead benchmark script.** `scripts/benchmark-overhead.mjs` measures
14
+ the same SELECT direct vs through the gate (p50/p95/p99). Without a DSN
15
+ the comparison is recorded as koşulamadı — no invented numbers.
16
+ See `docs/BENCHMARK.md`.
17
+ - **Generated SQL-gate attacks.** `test/property_sql_gate.mjs` (property +
18
+ fuzz). A real bypass stays red and is written under
19
+ `test-vectors/sql-gate/`.
20
+ - **API stability inventory** (draft, not a 1.0): `docs/API-STABILITY.md`.
21
+
22
+ - **Built-in OpenTimestamps client.** Stamp / upgrade / verify no longer
23
+ load `javascript-opentimestamps`. The `web3` / `request` / `bitcore` tree
24
+ is not installed. Old proofs still verify (pending fixture + dogfood
25
+ block 960327).
26
+ - **Threat model and pentest scope** in `docs/security/`.
27
+
28
+ - **LIMITATIONS.md** (and `LIMITATIONS.tr.md`) lists what this repository has
29
+ not done. README links it in the body, not a footnote. Comparison stays on
30
+ the site only.
31
+ - **`test:checks` runs every check even if one fails.** The old `&&` chain
32
+ went silent after the first failure. `test/init.mjs` resolves temp paths
33
+ with `fs.realpathSync` (`/var` vs `/private/var` on macOS).
34
+
5
35
  - **c1 launcher sets `CONARIUM_AUDIT_SIGNING_KEY`** from
6
36
  `~/.conarium/audit-ed25519.pem` when `receiptSink` is set. Cursor MCP env
7
37
  did not; the process could not boot receipts. The sink file is still not
package/LIMITATIONS.md ADDED
@@ -0,0 +1,70 @@
1
+ # Limitations
2
+
3
+ What Conarium has not done. Measured. No dates.
4
+
5
+ ## No certification
6
+
7
+ No SOC 2. No ISO. No independent penetration test. On the roadmap.
8
+
9
+ ## Not 1.0
10
+
11
+ Version is 0.2.x. The API can break.
12
+
13
+ ## SQL is Postgres, Microsoft SQL Server, and Oracle
14
+
15
+ MySQL is not implemented.
16
+ A dialect is listed here only when the shared SQL-gate vector set is green against that dialect, unparseable input is denied, and a live engine run applied the row cap.
17
+ This is a second parser layer, not a connector. The shipped `query` tool selects the gate from `policy.dialect` (`postgres` when omitted, or `mssql`, or `oracle`). The dialect is the operator's declaration — it is not inferred from the SQL. An unknown dialect rejects the config; it does not fall back to postgres.
18
+ Oracle does not resolve synonyms: an allow-listed name is the name the parser sees, not the base table. Database links (`table@dblink`) are denied. `ROWNUM` is denied (it is not a row cap).
19
+ There is no MSSQL or Oracle connector. The gate rewrites SQL; a connector that can speak that engine is still the operator's.
20
+ Parsers: Postgres `pgsql-ast-parser` · MSSQL `node-sql-parser` (transactsql) · Oracle `@guanmingchiu/sqlparser-ts`.
21
+
22
+ ## Names in free text are not guaranteed
23
+
24
+ Structured columns: deterministic (`maskColumns`).
25
+ Free text: best effort (carry-over of values this policy already masks; labelled names).
26
+ A bare name in running prose is not detected.
27
+
28
+ ## One production install
29
+
30
+ The only production deployment is the author's own company.
31
+ There is no external reference customer.
32
+ The figure 121,366 identities masked comes from that company's ERP. It cannot be verified from outside.
33
+
34
+ ## One-person team
35
+
36
+ Bus factor 1.
37
+
38
+ ## Anchors may stay pending
39
+
40
+ An OpenTimestamps stamp can take hours to confirm on Bitcoin. Receipts already show `pending`.
41
+
42
+ ## Cryptography is not independently audited
43
+
44
+ The Ed25519 implementation has not had a formal audit.
45
+
46
+ ## Masking cost grows with distinct masked values
47
+
48
+ Carry-over builds one matcher per unique value this policy already
49
+ masked. `maxRows` bounds that set. When the policy leaves it unset the code falls
50
+ back to 100; the `conarium.config.json` shipped with the package sets 50, so a
51
+ fresh install runs at 50. The figures below use 100 as the conservative case.
52
+
53
+ Measured (same SELECT, same row count, Postgres 16.14, WSL2, see
54
+ [`docs/BENCHMARK.md`](docs/BENCHMARK.md)):
55
+
56
+ | maxRows | overhead p50 (masked) |
57
+ |---|---|
58
+ | 100 (default) | 5.0 ms |
59
+ | 500 | 87 ms |
60
+ | 5 000 | 22 s |
61
+
62
+ Raising the cap is allowed. The doctor and boot log warn above 100.
63
+ The query is not rejected.
64
+
65
+ ## OpenTimestamps client
66
+
67
+ Stamping uses a built-in calendar client (Node `crypto` + HTTPS to the public calendars).
68
+ `javascript-opentimestamps` is not a dependency. The `web3` / `elliptic` / `crypto-js` / `request` / `lodash` tree is not installed.
69
+ Bitcoin confirmation still takes hours; receipts still show `pending` until upgrade.
70
+ Bitcoin-block verification talks to `blockstream.info`. If that host is unreachable the verifier reports "could not check", not "valid".
@@ -0,0 +1,70 @@
1
+ # Sınırlar
2
+
3
+ Conarium'un yapmadıkları. Ölçülmüş. Tarih yok.
4
+
5
+ ## Sertifikasyon yok
6
+
7
+ SOC 2 yok. ISO yok. Bağımsız sızma testi yok. Yol haritasında.
8
+
9
+ ## 1.0 değil
10
+
11
+ Sürüm 0.2.x. API kırılabilir.
12
+
13
+ ## SQL: Postgres, Microsoft SQL Server ve Oracle
14
+
15
+ MySQL yok.
16
+ Bir lehçe ancak paylaşılan SQL-kapısı vektör seti o lehçede yeşilse, ayrıştırılamayan girdi reddediliyorsa ve canlı motorda satır tavanı uygulanıyorsa burada yazılır.
17
+ Bu bir konnektör değil, ikinci ayrıştırıcı katmanı. Dağıtılan `query` aracı kapıyı `policy.dialect` ile seçer (yoksa `postgres`; `mssql`; `oracle`). Lehçe operatörün beyanıdır — SQL’den tahmin edilmez. Bilinmeyen lehçe yapılandırmayı reddeder; sessizce postgres’e düşmez.
18
+ Oracle synonym hedefini çözmez: allow-list’teki ad, parser’ın gördüğü addır, taban tablo değil. Veritabanı bağlantısı (`table@dblink`) reddedilir. `ROWNUM` reddedilir (satır tavanı değildir).
19
+ MSSQL veya Oracle konnektörü yok. Kapı SQL’i yeniden yazar; o motora konuşan konnektör hâlâ operatörün.
20
+ Ayrıştırıcılar: Postgres `pgsql-ast-parser` · MSSQL `node-sql-parser` (transactsql) · Oracle `@guanmingchiu/sqlparser-ts`.
21
+
22
+ ## Serbest metinde isim garantisi yok
23
+
24
+ Yapılandırılmış kolonlar: deterministik (`maskColumns`).
25
+ Serbest metin: en iyi çaba (bu politikanın zaten maskelediği değerlerin taşınması; etiketli isimler).
26
+ Düz cümledeki çıplak isim yakalanmaz.
27
+
28
+ ## Tek üretim kurulumu
29
+
30
+ Tek üretim kurulumu yazarın kendi şirketi.
31
+ Dış referans müşteri yok.
32
+ 121.366 kimlik maskelendi rakamı o şirketin ERP'sinden gelir. Dışarıdan doğrulanamaz.
33
+
34
+ ## Tek kişilik ekip
35
+
36
+ Bus factor 1.
37
+
38
+ ## Çıpa beklemede kalabilir
39
+
40
+ OpenTimestamps damgasının Bitcoin'de onaylanması saatler sürebilir. Makbuzda `pending` zaten görünür.
41
+
42
+ ## Denetlenmemiş kriptografi
43
+
44
+ Ed25519 uygulaması resmî denetimden geçmedi.
45
+
46
+ ## Maskeleme maliyeti satırla değil, benzersiz değerle büyür
47
+
48
+ Taşıma eşleştiricisi, politikanın maskelediği her benzersiz değer için
49
+ bir tarama kurar. `maxRows` o kümeyi sınırlar. Politika boş bırakırsa kod 100'e
50
+ düşer; pakette gelen `conarium.config.json` 50 yazar. Taze kurulum 50'de çalışır.
51
+ Aşağıdaki ölçüm ihtiyatlı olsun diye 100 üzerinden.
52
+
53
+ Ölçülmüş (aynı SELECT, aynı satır sayısı, Postgres 16.14, WSL2, bakınız
54
+ [`docs/BENCHMARK.md`](docs/BENCHMARK.md)):
55
+
56
+ | maxRows | ek yük p50 (maskeli) |
57
+ |---|---|
58
+ | 100 (varsayılan) | 5,0 ms |
59
+ | 500 | 87 ms |
60
+ | 5 000 | 22 s |
61
+
62
+ Tavanı yükseltmek serbest. 100'ün üstünde doctor ve boot uyarır.
63
+ Sorgu reddedilmez.
64
+
65
+ ## OpenTimestamps istemcisi
66
+
67
+ Damgalama yerleşik takvim istemcisiyle yapılır (Node `crypto` + herkese açık takvimlere HTTPS).
68
+ `javascript-opentimestamps` bağımlılık değil. `web3` / `elliptic` / `crypto-js` / `request` / `lodash` ağacı kurulmaz.
69
+ Bitcoin onayı yine saatler sürebilir; makbuz `pending` gösterir.
70
+ Bitcoin blok doğrulaması `blockstream.info`'ya sorar. O host yoksa doğrulayıcı "kontrol edemedim" der, "geçerli" demez.
package/README.md CHANGED
@@ -15,6 +15,13 @@
15
15
 
16
16
  The site lives at [conarium.dev](https://conarium.dev); this repository is the product.
17
17
 
18
+ ## Limitations
19
+
20
+ What this repository has **not** done is in [LIMITATIONS.md](LIMITATIONS.md)
21
+ ([Türkçe](LIMITATIONS.tr.md)). The dated comparison page is
22
+ [conarium.dev/compare.html](https://conarium.dev/compare.html) — that is the
23
+ only copy; this repo does not keep a second one.
24
+
18
25
  ## 👁️ The Problem
19
26
 
20
27
  Point Cursor or Copilot at a production database and it drinks the raw stream—SSNs, credit cards, salaries, and live keys. One rogue prompt can expose your most sensitive tables. Security teams simply can't allow that.
@@ -77,6 +84,8 @@ npx conarium-verify ./receipts.jsonl --pubkey ./audit-ed25519.pub.pem --anchor-c
77
84
 
78
85
  Opt-in anchoring: `CONARIUM_ANCHOR_SINK=opentimestamps`. Upgrade pending proofs later with
79
86
  `npx conarium-anchor-upgrade ./audit.jsonl.anchors.jsonl`.
87
+ The client is in-tree (Node `crypto` + calendar HTTPS). It does not install
88
+ `javascript-opentimestamps`. See [LIMITATIONS.md](LIMITATIONS.md).
80
89
 
81
90
  ### Per-person masking profiles
82
91
 
@@ -320,7 +329,7 @@ Conarium operates on a strict tripartite architecture, balancing power between t
320
329
  graph LR
321
330
  A([AI Assistant\nCursor / Copilot]) -- "MCP Query" --> B{The Gateway\nConarium Proxy};
322
331
  B -- "Intercept & Parse" --> C[The Engine\nGovernance & Regex];
323
- C -- "Execute Query" --> D[(Your Database\nPostgres)];
332
+ C -- "Execute Query" --> D[(Your Database\nPostgres / SQL Server / Oracle)];
324
333
  D -- "Raw Data" --> C;
325
334
  C -- "Mask & Cap" --> B;
326
335
  B -- "Sanitized Data" --> A;
@@ -463,6 +472,8 @@ Control access using a simple `conarium.json` policy file:
463
472
 
464
473
  Anything not in `allowTables` is denied by default; matched `maskColumns` are redacted to `[MASKED_PII]` before the data ever reaches the model.
465
474
 
475
+ `policy.dialect` selects the SQL gate the `query` tool uses: `postgres` (omitted default), `mssql`, or `oracle`. It is the operator's declaration — Conarium does not guess the dialect from the statement. A typo or `mysql` rejects the config.
476
+
466
477
  > **Connectors are fail-closed.** `allowConnectors` is a strict allow-list:
467
478
  > if it is missing or empty, **no** connector is permitted (previously an empty
468
479
  > list meant "allow all"). If you configure connectors, you must list them here —
@@ -529,14 +540,13 @@ including labelled names in free text · allow/deny + row caps · per-person mas
529
540
  profiles · immutable hash-chained audit ledger · Ed25519-signed receipt per access
530
541
  with an offline verifier · signed coverage declarations · two-sided reconciliation
531
542
  against the database's own counters · OpenTimestamps anchoring and an optional
532
- anchoring service · conformance vectors · Postgres, Supabase, docs, OpenAPI, Jira
533
- and Slack connectors · `conarium-init` / `conarium-doctor` from source (npm
534
- publish is the remaining step before `npx` works).
543
+ anchoring service · conformance vectors · SQL gate: Postgres, Microsoft SQL Server, Oracle
544
+ (MySQL is not implemented; Oracle synonyms and database links are not resolved — see LIMITATIONS) · Postgres, Supabase, docs, OpenAPI, Jira
545
+ and Slack connectors · `conarium-init` / `conarium-doctor` via `npx` (`@conarium-ai/core`).
535
546
 
536
547
  **Next:** consent binding ([spec published](docs/CONSENT-BINDING-SPEC.md), no code —
537
548
  patent review first) · a second independent implementation of the receipt format ·
538
- per-user identity bound to an identity provider rather than an operator token map ·
539
- publishing `@conarium-ai/core` to npm so `npx conarium-init` works without a clone.
549
+ per-user identity bound to an identity provider rather than an operator token map.
540
550
 
541
551
  **Deliberately not planned**, so nobody waits for it:
542
552
 
@@ -547,8 +557,10 @@ publishing `@conarium-ai/core` to npm so `npx conarium-init` works without a clo
547
557
  - **SOC 2.** It audits organisations that hold customer data. We never receive
548
558
  yours. If that ever stops being true, this line changes first.
549
559
 
550
- Known gaps are listed in the README above and in
551
- [`docs/RECEIPT-SPEC.md`](docs/RECEIPT-SPEC.md) rather than hidden here.
560
+ Known gaps: [LIMITATIONS.md](LIMITATIONS.md), the README above,
561
+ [`docs/RECEIPT-SPEC.md`](docs/RECEIPT-SPEC.md),
562
+ [`docs/BENCHMARK.md`](docs/BENCHMARK.md), and
563
+ [`docs/API-STABILITY.md`](docs/API-STABILITY.md).
552
564
 
553
565
  ## 📜 License
554
566
 
package/SECURITY.md CHANGED
@@ -29,6 +29,20 @@ a 2-second timeout, never blocks startup, and never throws.
29
29
  it issues no network request at all. Everything else — connectors, receipts, audit
30
30
  log, verification — is local to your infrastructure by construction.
31
31
 
32
+ ## OpenTimestamps client
33
+
34
+ Anchoring is implemented in-tree (`src/ots/`). It talks to the public
35
+ OpenTimestamps calendars over HTTPS and hashes with Node `crypto`.
36
+ `javascript-opentimestamps` is not a dependency. Enabling
37
+ `CONARIUM_ANCHOR_SINK=opentimestamps` does not install `web3`, `elliptic`,
38
+ `crypto-js`, `request`, or `lodash`.
39
+ Bitcoin-block checks use `blockstream.info`. Unreachable explorer → exit 15
40
+ ("could not check"), never a silent pass.
41
+
42
+ The tester pack is [`docs/security/THREAT-MODEL.md`](docs/security/THREAT-MODEL.md)
43
+ and [`docs/security/PENTEST-SCOPE.md`](docs/security/PENTEST-SCOPE.md).
44
+ Those documents are not an audit.
45
+
32
46
  ## Threat model
33
47
 
34
48
  **Conarium protects against**
@@ -4,23 +4,12 @@
4
4
  *
5
5
  * Usage: conarium-anchor-upgrade <anchors.jsonl>
6
6
  *
7
- * For each state:"pending" row, runs OpenTimestamps.upgrade(). On success refreshes
8
- * ots/state/bitcoinBlock/upgradedAt. If nothing changed, leaves the file untouched.
7
+ * For each state:"pending" row, asks the calendars for a fuller proof.
8
+ * On success refreshes ots/state/bitcoinBlock/upgradedAt.
9
+ * If nothing changed, leaves the file untouched.
9
10
  */
10
11
  import { readFileSync, writeFileSync, existsSync } from 'fs'
11
- import { createRequire } from 'module'
12
-
13
- const require = createRequire(import.meta.url)
14
-
15
- function hashPrefixToBuffer(hash) {
16
- const hex = hash.startsWith('sha256:') ? hash.slice('sha256:'.length) : hash
17
- if (!/^[0-9a-fA-F]{64}$/.test(hex)) {
18
- throw new Error(`bad hash: ${hash}`)
19
- }
20
- const buf = Buffer.from(hex, 'hex')
21
- if (buf.length !== 32) throw new Error('hash must be 32 bytes')
22
- return buf
23
- }
12
+ import { upgradeProof } from '../dist/ots/client.js'
24
13
 
25
14
  function loadRows(path) {
26
15
  if (!existsSync(path)) {
@@ -39,15 +28,6 @@ async function main() {
39
28
  process.exit(2)
40
29
  }
41
30
 
42
- let OpenTimestamps
43
- try {
44
- OpenTimestamps = require('javascript-opentimestamps')
45
- if (OpenTimestamps && OpenTimestamps.default) OpenTimestamps = OpenTimestamps.default
46
- } catch {
47
- console.error('javascript-opentimestamps is required')
48
- process.exit(2)
49
- }
50
-
51
31
  const rows = loadRows(path)
52
32
  let changedAny = false
53
33
  const out = []
@@ -58,34 +38,21 @@ async function main() {
58
38
  continue
59
39
  }
60
40
  try {
61
- const hashBuf = hashPrefixToBuffer(row.hash)
62
- const detached = OpenTimestamps.DetachedTimestampFile.fromHash(
63
- new OpenTimestamps.Ops.OpSHA256(),
64
- hashBuf,
65
- )
66
- const detachedOts = OpenTimestamps.DetachedTimestampFile.deserialize(
67
- Buffer.from(row.ots, 'base64'),
68
- )
69
- const changed = await OpenTimestamps.upgrade(detachedOts)
70
- if (!changed) {
41
+ const up = await upgradeProof(Buffer.from(row.ots, 'base64'))
42
+ if (!up.changed) {
71
43
  out.push(row)
72
44
  continue
73
45
  }
74
- const verified = await OpenTimestamps.verify(detachedOts, detached, {
75
- ignoreBitcoinNode: true,
76
- timeout: 15000,
77
- })
78
- const height = verified?.bitcoin?.height
79
46
  const next = {
80
47
  ...row,
81
- ots: Buffer.from(detachedOts.serializeToBytes()).toString('base64'),
82
- state: height != null ? 'bitcoin' : 'pending',
83
- bitcoinBlock: height ?? null,
48
+ ots: up.otsBytes.toString('base64'),
49
+ state: up.bitcoinBlock != null ? 'bitcoin' : 'pending',
50
+ bitcoinBlock: up.bitcoinBlock,
84
51
  upgradedAt: new Date().toISOString(),
85
52
  }
86
53
  out.push(next)
87
54
  changedAny = true
88
- console.error(`[upgrade] seq=${row.seq} → ${next.state}${height != null ? ` block=${height}` : ''}`)
55
+ console.error(`[upgrade] seq=${row.seq} → ${next.state}${up.bitcoinBlock != null ? ` block=${up.bitcoinBlock}` : ''}`)
89
56
  } catch (err) {
90
57
  console.error(`[upgrade] seq=${row.seq} failed: ${err.message || err}`)
91
58
  out.push(row)
@@ -30,9 +30,16 @@
30
30
  */
31
31
  import { existsSync, readFileSync, statSync, accessSync, constants } from 'node:fs'
32
32
  import { resolve, dirname, join } from 'node:path'
33
- import { fileURLToPath } from 'node:url'
33
+ import { fileURLToPath, pathToFileURL } from 'node:url'
34
34
  import net from 'node:net'
35
35
  import { createPublicKey, createPrivateKey } from 'node:crypto'
36
+ import {
37
+ dialectAgrees,
38
+ dialectLine,
39
+ familyFromBanner,
40
+ familyFromScheme,
41
+ resolveDoctorDialect,
42
+ } from './doctor-sql.mjs'
36
43
 
37
44
  const ARGS = process.argv.slice(2)
38
45
  const has = (f) => ARGS.includes(f)
@@ -60,6 +67,8 @@ const results = []
60
67
  const ok = (label, detail) => results.push({ level: 'ok', label, detail })
61
68
  const warn = (label, detail, fix) => results.push({ level: 'warn', label, detail, fix })
62
69
  const fail = (label, detail, fix) => results.push({ level: 'fail', label, detail, fix })
70
+ /** Could not complete the check. Not a pass. Not a silent skip. */
71
+ const unseen = (label, detail, fix) => results.push({ level: 'unseen', label, detail, fix })
63
72
 
64
73
  /* ---------------------------------------------------------------- helpers */
65
74
 
@@ -108,6 +117,28 @@ function tcpProbe(host, port, timeoutMs = 4000) {
108
117
  })
109
118
  }
110
119
 
120
+ /** Ask a reachable engine who it is. Never log the DSN. */
121
+ async function identifyEngine(t) {
122
+ const schemeFamily = familyFromScheme(t.scheme)
123
+ const looksPostgres = schemeFamily === 'postgres' || t.type === 'postgres' || t.type === 'supabase'
124
+ if (!looksPostgres) return null
125
+ try {
126
+ const postgres = (await import('postgres')).default
127
+ const sql = postgres(t.raw, { max: 1, connect_timeout: 3, idle_timeout: 1 })
128
+ try {
129
+ const rows = await sql`select version() as v`
130
+ const banner = String(rows?.[0]?.v || '')
131
+ const family = familyFromBanner(banner)
132
+ if (!family) return null
133
+ return { family, summary: banner.split(',')[0].replace(/\s+/g, ' ').slice(0, 72) }
134
+ } finally {
135
+ await sql.end({ timeout: 2 })
136
+ }
137
+ } catch {
138
+ return null
139
+ }
140
+ }
141
+
111
142
  function envSet(name) {
112
143
  const v = process.env[name]
113
144
  return typeof v === 'string' && v.trim() !== '' ? v : null
@@ -254,6 +285,105 @@ if (config) {
254
285
  ok('Policy surface', [hasAllowTables && 'allowTables', hasDeny && 'denyTables', hasMask && 'maskColumns'].filter(Boolean).join(' + '))
255
286
  }
256
287
 
288
+ // 4b. Raised maxRows — performance warning, not a deny.
289
+ // Threshold is measured (docs/benchmarks/masking-cost-threshold.json).
290
+ // Doctor does not import dist/; the number is read from that file, or 100.
291
+ const thresholdPath = join(dirname(fileURLToPath(import.meta.url)), '..', 'docs', 'benchmarks', 'masking-cost-threshold.json')
292
+ let warnAbove = 100
293
+ try {
294
+ if (existsSync(thresholdPath)) {
295
+ const t = JSON.parse(readFileSync(thresholdPath, 'utf8'))
296
+ if (Number.isFinite(t.warnAbove)) warnAbove = t.warnAbove
297
+ }
298
+ } catch { /* keep 100 */ }
299
+ const maxRows = pol.maxRows
300
+ if (typeof maxRows === 'number' && maxRows > warnAbove) {
301
+ warn(
302
+ 'policy.maxRows',
303
+ `maxRows is ${maxRows} (measured warning above ${warnAbove}). Masking cost grows with the number of distinct values this policy already masked, not with table size. The row cap bounds that set. The query is not rejected.`,
304
+ 'Leave the default if you do not need more rows. If you raise it, expect carry-over cost to grow with unique masked values.',
305
+ )
306
+ } else if (typeof maxRows === 'number') {
307
+ ok('policy.maxRows', `maxRows is ${maxRows} (at or below the measured threshold ${warnAbove})`)
308
+ } else {
309
+ ok('policy.maxRows', `maxRows is 100 (default — policy.maxRows omitted; threshold ${warnAbove})`)
310
+ }
311
+ for (const [name, profile] of Object.entries(pol.profiles || {})) {
312
+ const pMax = profile && typeof profile === 'object' ? profile.maxRows : undefined
313
+ if (typeof pMax === 'number' && pMax > warnAbove) {
314
+ warn(
315
+ `policy.profiles.${name}.maxRows`,
316
+ `profile maxRows is ${pMax} (measured warning above ${warnAbove}). Same cost: distinct masked values, not table size. Not rejected.`,
317
+ 'A profile can only raise the cap for one identified person. The cost still grows with unique masked values.',
318
+ )
319
+ }
320
+ }
321
+
322
+ // D2 — which SQL gate the query tool will use. Silence here was the bug.
323
+ const pkgRoot = join(dirname(fileURLToPath(import.meta.url)), '..')
324
+ let dialectInfo = null
325
+ try {
326
+ dialectInfo = resolveDoctorDialect(pol.dialect)
327
+ ok('SQL dialect', dialectLine(dialectInfo.dialect, dialectInfo.source))
328
+ } catch (e) {
329
+ fail('SQL dialect', e.message, 'Set policy.dialect to postgres, mssql, or oracle. A typo does not become postgres.')
330
+ }
331
+ if (dialectInfo) {
332
+ if (dialectInfo.dialect === 'postgres') {
333
+ ok('SQL gate', 'postgres gate is built-in (Governance.guardQuery)')
334
+ } else {
335
+ const gateFile = join(pkgRoot, 'dist', 'sql-gate', `${dialectInfo.dialect}.js`)
336
+ if (!existsSync(gateFile)) {
337
+ fail(
338
+ 'SQL gate',
339
+ `${dialectInfo.dialect} gate file is missing (dist/sql-gate/${dialectInfo.dialect}.js)`,
340
+ 'Run npm run build in the install. The query tool cannot load this dialect without that file.',
341
+ )
342
+ } else {
343
+ try {
344
+ await import(pathToFileURL(gateFile).href)
345
+ ok('SQL gate', `${dialectInfo.dialect} gate loaded from dist/sql-gate/${dialectInfo.dialect}.js`)
346
+ } catch (e) {
347
+ fail(
348
+ 'SQL gate',
349
+ `${dialectInfo.dialect} gate failed to load: ${e.name}`,
350
+ 'Rebuild the package. The query tool will fail closed on this dialect until the gate loads.',
351
+ )
352
+ }
353
+ }
354
+ }
355
+ }
356
+
357
+ // D3 — custom PII: names only. Pattern text must never reach stdout.
358
+ const patterns = Array.isArray(pol.customPatterns) ? pol.customPatterns : []
359
+ const patternNames = patterns
360
+ .map((p) => (p && typeof p.name === 'string' ? p.name : null))
361
+ .filter(Boolean)
362
+ if (patterns.length === 0) {
363
+ ok('policy.customPatterns', '0 custom patterns')
364
+ } else {
365
+ const compilerPath = join(pkgRoot, 'dist', 'custom_patterns.js')
366
+ if (!existsSync(compilerPath)) {
367
+ unseen(
368
+ 'policy.customPatterns',
369
+ `${patterns.length} named: ${patternNames.join(', ') || '(unnamed)'} — compile not checked (dist/custom_patterns.js missing)`,
370
+ 'Run npm run build. The gateway rejects a broken pattern at load; the doctor could not confirm that here.',
371
+ )
372
+ } else {
373
+ try {
374
+ const { compileCustomPatterns } = await import(pathToFileURL(compilerPath).href)
375
+ compileCustomPatterns(patterns)
376
+ ok('policy.customPatterns', `${patterns.length} compiled: ${patternNames.join(', ')}`)
377
+ } catch (e) {
378
+ fail(
379
+ 'policy.customPatterns',
380
+ `${patterns.length} named: ${patternNames.join(', ') || '(unnamed)'} — compile failed (${e.name})`,
381
+ 'The gateway will refuse this config. Fix or remove the broken rule. Pattern text is not printed.',
382
+ )
383
+ }
384
+ }
385
+ }
386
+
257
387
  // 5. Per-person profiles need per-user credentials
258
388
  const profiles = pol.profiles || pol.actorProfiles
259
389
  if (profiles && Object.keys(profiles).length > 0) {
@@ -369,25 +499,21 @@ if (config) {
369
499
  }
370
500
  }
371
501
 
372
- // 10. Anchoring is an optional peer dependency since 0.1.0.
502
+ // 10. Anchoring uses the built-in calendar client (no extra package).
373
503
  {
374
- const anchoring = Boolean(envSet('CONARIUM_ANCHOR_BASE_URL') || envSet('CONARIUM_ANCHOR_STORE') || config?.audit?.anchor)
504
+ const sink = (process.env.CONARIUM_ANCHOR_SINK || '').toLowerCase()
505
+ const anchoring = Boolean(
506
+ sink === 'opentimestamps' ||
507
+ sink === 'ots' ||
508
+ envSet('CONARIUM_ANCHOR_BASE_URL') ||
509
+ envSet('CONARIUM_ANCHOR_STORE') ||
510
+ config?.audit?.anchor,
511
+ )
375
512
  if (anchoring) {
376
- let present = false
377
- try {
378
- await import('javascript-opentimestamps')
379
- present = true
380
- } catch {
381
- present = false
382
- }
383
- if (present) ok('Anchoring', 'configured, javascript-opentimestamps present')
384
- else
385
- fail(
386
- 'Anchoring',
387
- 'configured, but javascript-opentimestamps is not installed',
388
- 'It is an OPTIONAL peer dependency (its transitive tree carried known vulnerabilities, so it is not installed by default). ' +
389
- 'Run: npm install javascript-opentimestamps — or turn anchoring off. Until then the verifier reports exit 15 (could not check).',
390
- )
513
+ ok(
514
+ 'Anchoring',
515
+ 'configured — built-in OpenTimestamps client (Node crypto + calendar HTTPS). No javascript-opentimestamps.',
516
+ )
391
517
  }
392
518
  }
393
519
 
@@ -418,18 +544,87 @@ if (config) {
418
544
  } else {
419
545
  ok('Connector DSN', `${c.name}: ${d.text}${d.hasPassword ? ' (password set, not shown)' : ''}`)
420
546
  }
421
- if (d.host && d.port) targets.push({ name: c.name, ...d })
547
+ if (d.host && d.port) targets.push({ name: c.name, type: c.type, raw, ...d })
422
548
  }
549
+ const reachable = []
423
550
  for (const t of SKIP_NET ? [] : targets) {
424
551
  const r = await tcpProbe(t.host, t.port)
425
- if (r.reachable) ok('Reachability', `${t.name}: ${t.host}:${t.port} answers`)
426
- else
552
+ if (r.reachable) {
553
+ ok('Reachability', `${t.name}: ${t.host}:${t.port} answers`)
554
+ reachable.push(t)
555
+ } else
427
556
  fail(
428
557
  'Reachability',
429
558
  `${t.name}: ${t.host}:${t.port} unreachable (${r.why})`,
430
559
  'The gateway logs connector failures and keeps running with zero connectors — it will look healthy and serve nothing.',
431
560
  )
432
561
  }
562
+
563
+ // D1 — declared dialect vs the engine that actually answers.
564
+ // Silence here meant Oracle rules on a Postgres DSN. Mismatch is FAIL.
565
+ // Cannot ask → unseen. Never "probably matches".
566
+ let dialectForEngine = null
567
+ try {
568
+ dialectForEngine = resolveDoctorDialect(config.policy?.dialect).dialect
569
+ } catch {
570
+ dialectForEngine = null
571
+ }
572
+ if (dialectForEngine) {
573
+ let schemeConflict = false
574
+ for (const t of targets) {
575
+ const fromScheme = familyFromScheme(t.scheme)
576
+ if (fromScheme && !dialectAgrees(dialectForEngine, fromScheme)) {
577
+ schemeConflict = true
578
+ fail(
579
+ 'Engine identity',
580
+ `${t.name}: policy.dialect is ${dialectForEngine} but the DSN scheme is ${t.scheme} (${fromScheme})`,
581
+ 'The query tool will parse with one grammar and send the SQL to another engine. Point the DSN at the declared dialect, or change policy.dialect.',
582
+ )
583
+ }
584
+ }
585
+ if (schemeConflict) {
586
+ // Already FAIL. Do not add a "not checked" line that reads like a second opinion.
587
+ } else if (SKIP_NET) {
588
+ unseen(
589
+ 'Engine identity',
590
+ `not checked (--no-net). Declared dialect ${dialectForEngine} was not compared to a live engine.`,
591
+ 'Rerun without --no-net when the database is reachable. Do not assume the dialect matches.',
592
+ )
593
+ } else if (targets.length === 0) {
594
+ unseen(
595
+ 'Engine identity',
596
+ `not checked (no SQL DSN). Declared dialect ${dialectForEngine}.`,
597
+ 'Add a postgres / SQL connector URL, or ignore this if this install has no SQL engine.',
598
+ )
599
+ } else if (reachable.length === 0) {
600
+ unseen(
601
+ 'Engine identity',
602
+ `not checked (host unreachable). Declared dialect ${dialectForEngine}.`,
603
+ 'Bring the engine up, then rerun. Unreachable is not a match.',
604
+ )
605
+ } else {
606
+ for (const t of reachable) {
607
+ const id = await identifyEngine(t)
608
+ if (!id) {
609
+ unseen(
610
+ 'Engine identity',
611
+ `${t.name}: ${t.host}:${t.port} answers TCP but the engine family could not be read. Declared ${dialectForEngine}.`,
612
+ 'Doctor asked the engine and got no version banner. Not a pass.',
613
+ )
614
+ continue
615
+ }
616
+ if (!dialectAgrees(dialectForEngine, id.family)) {
617
+ fail(
618
+ 'Engine identity',
619
+ `${t.name}: policy.dialect is ${dialectForEngine} but the engine identified as ${id.family} (${id.summary})`,
620
+ 'The query tool will enforce the wrong grammar against this engine. Fix policy.dialect or the DSN.',
621
+ )
622
+ } else {
623
+ ok('Engine identity', `${t.name}: engine is ${id.family} — matches policy.dialect ${dialectForEngine}`)
624
+ }
625
+ }
626
+ }
627
+ }
433
628
  }
434
629
 
435
630
  // 12. License pack — offline, never fail-closed. A broken license file must
@@ -470,7 +665,7 @@ if (config) {
470
665
 
471
666
  /* ------------------------------------------------------------------ report */
472
667
 
473
- const ICON = { ok: ' ok ', warn: ' warn ', fail: ' FAIL ' }
668
+ const ICON = { ok: ' ok ', warn: ' warn ', fail: ' FAIL ', unseen: 'unseen' }
474
669
  console.log('')
475
670
  console.log('conarium doctor')
476
671
  console.log('─'.repeat(66))
@@ -480,9 +675,11 @@ for (const r of results) {
480
675
  }
481
676
  const fails = results.filter((r) => r.level === 'fail').length
482
677
  const warns = results.filter((r) => r.level === 'warn').length
678
+ const unseenN = results.filter((r) => r.level === 'unseen').length
483
679
  console.log('─'.repeat(66))
484
- console.log(`${results.length} checks · ${fails} failed · ${warns} warning(s)`)
485
- if (fails === 0 && warns === 0) console.log('This install is ready.')
680
+ console.log(`${results.length} checks · ${fails} failed · ${warns} warning(s) · ${unseenN} unseen`)
681
+ if (fails === 0 && warns === 0 && unseenN === 0) console.log('This install is ready.')
682
+ if (fails === 0 && unseenN > 0) console.log('No failures. Unseen lines were not verified — not a pass.')
486
683
  if (fails > 0) console.log('Fix the FAIL lines above; the gateway will not govern correctly until you do.')
487
684
  console.log('')
488
685
  // Set the code and let the loop drain. process.exit() here raced the closing