@blamejs/core 0.7.4 → 0.7.18

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 (180) hide show
  1. package/CHANGELOG.md +423 -395
  2. package/README.md +150 -149
  3. package/bin/blamejs.js +0 -0
  4. package/index.js +308 -284
  5. package/lib/api-key.js +660 -663
  6. package/lib/api-snapshot.js +338 -338
  7. package/lib/app-shutdown.js +385 -385
  8. package/lib/app.js +365 -365
  9. package/lib/archive.js +250 -250
  10. package/lib/atomic-file.js +544 -544
  11. package/lib/audit-chain.js +177 -177
  12. package/lib/audit-sign.js +344 -344
  13. package/lib/audit-tools.js +677 -677
  14. package/lib/audit.js +766 -766
  15. package/lib/auth/jwt.js +311 -311
  16. package/lib/auth/lockout.js +436 -436
  17. package/lib/auth/oauth.js +721 -721
  18. package/lib/auth/passkey.js +181 -181
  19. package/lib/auth/password.js +594 -594
  20. package/lib/backup/bundle.js +217 -217
  21. package/lib/backup/crypto.js +176 -176
  22. package/lib/backup/index.js +515 -515
  23. package/lib/backup/manifest.js +282 -282
  24. package/lib/break-glass.js +1338 -1338
  25. package/lib/bundler.js +441 -441
  26. package/lib/cache-redis.js +256 -256
  27. package/lib/cache.js +1206 -1206
  28. package/lib/canonical-json.js +115 -115
  29. package/lib/chain-writer.js +234 -234
  30. package/lib/cli-helpers.js +206 -206
  31. package/lib/cli.js +2334 -2334
  32. package/lib/cluster-provider-db.js +317 -317
  33. package/lib/cluster-storage.js +226 -226
  34. package/lib/cluster.js +703 -703
  35. package/lib/codepoint-class.js +196 -0
  36. package/lib/config-drift.js +301 -301
  37. package/lib/consent.js +222 -222
  38. package/lib/constants.js +191 -191
  39. package/lib/cookies.js +315 -315
  40. package/lib/credential-hash.js +322 -322
  41. package/lib/crypto.js +266 -266
  42. package/lib/csv.js +275 -286
  43. package/lib/db-declare-row-policy.js +267 -267
  44. package/lib/db-declare-view.js +420 -421
  45. package/lib/db-query.js +406 -406
  46. package/lib/db-schema.js +319 -319
  47. package/lib/db.js +1288 -1288
  48. package/lib/deprecate.js +222 -222
  49. package/lib/dev.js +335 -335
  50. package/lib/dual-control.js +473 -473
  51. package/lib/error-page.js +420 -420
  52. package/lib/external-db-migrate.js +441 -441
  53. package/lib/external-db.js +1061 -1061
  54. package/lib/file-type.js +273 -273
  55. package/lib/file-upload.js +213 -10
  56. package/lib/forms.js +422 -422
  57. package/lib/framework-error.js +293 -215
  58. package/lib/framework-schema.js +717 -717
  59. package/lib/gate-contract.js +971 -0
  60. package/lib/guard-all.js +405 -0
  61. package/lib/guard-archive.js +739 -0
  62. package/lib/guard-csv.js +816 -0
  63. package/lib/guard-email.js +744 -0
  64. package/lib/guard-filename.js +724 -0
  65. package/lib/guard-html.js +976 -0
  66. package/lib/guard-json.js +729 -0
  67. package/lib/guard-markdown.js +586 -0
  68. package/lib/guard-svg.js +976 -0
  69. package/lib/guard-xml.js +405 -0
  70. package/lib/guard-yaml.js +529 -0
  71. package/lib/handlers.js +350 -350
  72. package/lib/http-client-cookie-jar.js +508 -508
  73. package/lib/http-client.js +1195 -1195
  74. package/lib/i18n.js +878 -878
  75. package/lib/jobs.js +185 -185
  76. package/lib/log-stream-cloudwatch.js +369 -369
  77. package/lib/log-stream-local.js +146 -146
  78. package/lib/log-stream-otlp-grpc.js +410 -410
  79. package/lib/log-stream-otlp.js +286 -286
  80. package/lib/log-stream-syslog.js +302 -302
  81. package/lib/log-stream-webhook.js +199 -199
  82. package/lib/log-stream.js +330 -330
  83. package/lib/log.js +500 -500
  84. package/lib/mail-bounce.js +528 -528
  85. package/lib/mail-dkim.js +369 -362
  86. package/lib/mail.js +981 -962
  87. package/lib/metrics.js +683 -683
  88. package/lib/middleware/api-encrypt.js +936 -936
  89. package/lib/middleware/attach-user.js +157 -157
  90. package/lib/middleware/body-parser.js +1170 -1091
  91. package/lib/middleware/bot-guard.js +178 -178
  92. package/lib/middleware/compression.js +452 -452
  93. package/lib/middleware/cors.js +314 -314
  94. package/lib/middleware/csp-nonce.js +348 -348
  95. package/lib/middleware/csrf-protect.js +316 -316
  96. package/lib/middleware/db-role-for.js +264 -264
  97. package/lib/middleware/health.js +392 -392
  98. package/lib/middleware/index.js +79 -79
  99. package/lib/middleware/rate-limit.js +358 -358
  100. package/lib/middleware/request-id.js +61 -61
  101. package/lib/middleware/request-log.js +168 -168
  102. package/lib/middleware/require-auth.js +104 -104
  103. package/lib/middleware/security-headers.js +116 -116
  104. package/lib/middleware/sse.js +166 -166
  105. package/lib/migrations.js +383 -383
  106. package/lib/mtls-ca.js +518 -518
  107. package/lib/mtls-engine-default.js +481 -481
  108. package/lib/network-dns.js +632 -632
  109. package/lib/network-heartbeat.js +290 -290
  110. package/lib/network-nts.js +574 -574
  111. package/lib/network-proxy.js +265 -265
  112. package/lib/network-tls.js +328 -328
  113. package/lib/network.js +233 -233
  114. package/lib/notify.js +612 -612
  115. package/lib/ntp-check.js +229 -229
  116. package/lib/numeric-bounds.js +111 -91
  117. package/lib/object-store/azure-blob-bucket-ops.js +349 -349
  118. package/lib/object-store/azure-blob.js +488 -488
  119. package/lib/object-store/gcs-bucket-ops.js +351 -351
  120. package/lib/object-store/gcs.js +519 -519
  121. package/lib/object-store/http-put.js +153 -153
  122. package/lib/object-store/index.js +197 -197
  123. package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
  124. package/lib/object-store/sigv4.js +903 -903
  125. package/lib/observability.js +151 -151
  126. package/lib/otel-export.js +269 -269
  127. package/lib/pagination.js +464 -464
  128. package/lib/parsers/index.js +80 -80
  129. package/lib/parsers/safe-env.js +642 -642
  130. package/lib/parsers/safe-ini.js +292 -292
  131. package/lib/parsers/safe-toml.js +784 -784
  132. package/lib/parsers/safe-xml.js +390 -390
  133. package/lib/parsers/safe-yaml.js +1015 -1015
  134. package/lib/permissions.js +708 -708
  135. package/lib/pqc-agent.js +87 -87
  136. package/lib/pqc-gate.js +279 -279
  137. package/lib/protobuf-encoder.js +190 -190
  138. package/lib/protocol-dispatcher.js +161 -161
  139. package/lib/pubsub-redis.js +167 -167
  140. package/lib/pubsub.js +429 -429
  141. package/lib/queue-local.js +476 -476
  142. package/lib/queue-redis.js +745 -745
  143. package/lib/queue-sqs.js +319 -319
  144. package/lib/queue.js +695 -695
  145. package/lib/redis-client.js +519 -519
  146. package/lib/request-helpers.js +340 -340
  147. package/lib/restore-bundle.js +237 -237
  148. package/lib/restore-rollback.js +259 -259
  149. package/lib/restore.js +409 -409
  150. package/lib/retry.js +376 -376
  151. package/lib/router.js +748 -748
  152. package/lib/safe-async.js +735 -735
  153. package/lib/safe-buffer.js +237 -237
  154. package/lib/safe-json.js +541 -541
  155. package/lib/safe-schema.js +1266 -1266
  156. package/lib/safe-url.js +159 -159
  157. package/lib/scheduler.js +706 -706
  158. package/lib/security-assert.js +373 -373
  159. package/lib/seeders.js +618 -618
  160. package/lib/session.js +478 -478
  161. package/lib/slug.js +269 -269
  162. package/lib/ssrf-guard.js +401 -401
  163. package/lib/static.js +184 -4
  164. package/lib/storage.js +471 -471
  165. package/lib/subject.js +281 -281
  166. package/lib/template.js +791 -791
  167. package/lib/testing.js +798 -798
  168. package/lib/time.js +310 -310
  169. package/lib/totp.js +302 -302
  170. package/lib/tracing.js +494 -494
  171. package/lib/uuid.js +132 -132
  172. package/lib/validate-opts.js +340 -319
  173. package/lib/vault/index.js +308 -308
  174. package/lib/vault/rotate.js +784 -784
  175. package/lib/vault/wrap.js +296 -296
  176. package/lib/vendor/noble-ciphers.cjs +9 -9
  177. package/lib/webhook.js +595 -595
  178. package/lib/websocket.js +1048 -1048
  179. package/package.json +77 -77
  180. package/sbom.cyclonedx.json +7 -7
@@ -1,784 +1,784 @@
1
- "use strict";
2
- /**
3
- * vault-rotate — vault key rotation primitives.
4
- *
5
- * Two surfaces ship together: the read-only diagnostic helpers operators
6
- * run before/after a rotation, and the rotation pipeline itself.
7
- *
8
- * Diagnostics:
9
- *
10
- * - validateSchemaMatch(db, opts)
11
- * Compare the registered field-crypto schema against the live
12
- * DB. Surfaces three failure modes:
13
- * · table_missing — schema declares a table the DB lacks
14
- * · sealed_col_missing — schema declares a sealed column the
15
- * live table lacks
16
- * · drift — a real column the schema does NOT
17
- * declare sealed has vault-prefixed
18
- * values in at least one sampled row
19
- *
20
- * - verify({ keys, db, oldKeys?, sampleMin?, samplePercent? })
21
- * Walk every registered sealed column, sample rows, attempt to
22
- * decrypt with `keys`. Reports failures + (if oldKeys passed)
23
- * regressions: rows that still decrypt under oldKeys, indicating
24
- * rotation didn't take effect.
25
- *
26
- * - formatValidationResult(result)
27
- * Render the validation result for a CLI / log line.
28
- *
29
- * Rotation pipeline:
30
- *
31
- * - rotate({ dataDir, oldKeys, newKeys, ... })
32
- * Walks every sealed column across every registered table, decrypts
33
- * under oldKeys and re-encrypts under newKeys with batched commits.
34
- * Atomic commit at the data-directory level so a partial rotation
35
- * never leaves rows half-translated.
36
- *
37
- * Operators run diagnostics whether or not they're rotating: post-deploy
38
- * schema-drift smoke tests, post-incident "did anything get unrotated?"
39
- * sweeps. The rotation pipeline runs whenever the vault keypair changes
40
- * (passphrase rotation, sealed-blob refresh, hardware-token swap).
41
- *
42
- * Generic by design: no hardcoded list of "infrastructure columns" —
43
- * the drift detector treats every column not declared in the schema's
44
- * sealedFields / derivedHashes as a candidate, samples rows, and only
45
- * flags those that actually contain vault-prefixed values. Operators
46
- * with framework tables that legitimately hold a vault-prefixed string
47
- * in an undeclared column pass them via opts.infraColumns so the
48
- * sampler skips them.
49
- */
50
-
51
- var fs = require("fs");
52
- var path = require("path");
53
- var { DatabaseSync } = require("node:sqlite");
54
- var atomicFile = require("../atomic-file");
55
- var C = require("../constants");
56
- var cryptoField = require("../crypto-field");
57
- var cryptoLib = require("../crypto");
58
- var dbSchema = require("../db-schema");
59
- var lazyRequire = require("../lazy-require");
60
- var { boot } = require("../log");
61
- var nb = require("../numeric-bounds");
62
- var safeJson = require("../safe-json");
63
- var validateOpts = require("../validate-opts");
64
- var vaultWrap = lazyRequire(function () { return require("./wrap"); });
65
- var { defineClass } = require("../framework-error");
66
-
67
- var rotateLog = boot("vault-rotate");
68
-
69
- var VaultRotateError = defineClass("VaultRotateError", { alwaysPermanent: true });
70
-
71
- var VAULT_PREFIX = C.VAULT_PREFIX;
72
- var DEFAULT_DRIFT_SAMPLE_LIMIT = 100;
73
- var DEFAULT_VERIFY_SAMPLE_MIN = 5;
74
- var DEFAULT_VERIFY_SAMPLE_FRAC = 0.01;
75
-
76
- function _listLiveTables(db) {
77
- return db.prepare(
78
- "SELECT name FROM sqlite_master " +
79
- "WHERE type='table' AND name NOT LIKE 'sqlite_%'"
80
- ).all().map(function (r) { return r.name; });
81
- }
82
-
83
- function _listLiveColumns(db, table) {
84
- // PRAGMA table_info — table name comes from sqlite_master so it's
85
- // already validated as an existing identifier.
86
- return db.prepare("PRAGMA table_info(\"" + table.replace(/"/g, '""') + "\")").all()
87
- .map(function (c) { return c.name; });
88
- }
89
-
90
- function _knownColumnsFor(schema, infraColumns) {
91
- var set = Object.create(null);
92
- if (Array.isArray(infraColumns)) {
93
- for (var i = 0; i < infraColumns.length; i++) set[infraColumns[i]] = true;
94
- }
95
- if (schema && Array.isArray(schema.sealedFields)) {
96
- for (var s = 0; s < schema.sealedFields.length; s++) set[schema.sealedFields[s]] = true;
97
- }
98
- if (schema && schema.derivedHashes) {
99
- for (var dk in schema.derivedHashes) {
100
- if (Object.prototype.hasOwnProperty.call(schema.derivedHashes, dk)) set[dk] = true;
101
- // Source field is also "known" — it's the unsealed input
102
- var spec = schema.derivedHashes[dk];
103
- if (spec && spec.from) set[spec.from] = true;
104
- }
105
- }
106
- return set;
107
- }
108
-
109
- function validateSchemaMatch(db, opts) {
110
- opts = opts || {};
111
- nb.requirePositiveFiniteIntIfPresent(opts.driftSampleLimit,
112
- "validateSchemaMatch: driftSampleLimit", VaultRotateError, "vault-rotate/bad-opt");
113
- var sampleLimit = opts.driftSampleLimit !== undefined
114
- ? opts.driftSampleLimit : DEFAULT_DRIFT_SAMPLE_LIMIT;
115
- var infraColumns = Array.isArray(opts.infraColumns) ? opts.infraColumns : [];
116
- // Tables to consider — by default, every table the framework's
117
- // field-crypto registry knows about. Operator can pass an explicit
118
- // tables list to scope the check.
119
- var tablesToCheck = Array.isArray(opts.tables) && opts.tables.length > 0
120
- ? opts.tables.slice()
121
- : null;
122
-
123
- var warnings = [];
124
- var errors = [];
125
-
126
- var liveTables = _listLiveTables(db);
127
- var liveTableSet = Object.create(null);
128
- for (var lt = 0; lt < liveTables.length; lt++) liveTableSet[liveTables[lt]] = true;
129
-
130
- // If no tables list passed, derive it from the live DB. Tables
131
- // unknown to cryptoField will be reported as drift candidates if
132
- // they have vault-prefixed columns.
133
- var allTables = tablesToCheck || liveTables;
134
-
135
- for (var t = 0; t < allTables.length; t++) {
136
- var table = allTables[t];
137
-
138
- if (!liveTableSet[table]) {
139
- // Schema declared a table the DB doesn't have. Non-fatal —
140
- // rotation skips it (nothing to rotate).
141
- warnings.push({
142
- kind: "table_missing",
143
- table: table,
144
- message: "schema lists table '" + table + "' but the live DB has no such table (skipped during rotation)",
145
- });
146
- continue;
147
- }
148
-
149
- var schema = cryptoField.getSchema(table); // null if not registered
150
- var liveCols = _listLiveColumns(db, table);
151
- var liveColSet = Object.create(null);
152
- for (var c = 0; c < liveCols.length; c++) liveColSet[liveCols[c]] = true;
153
-
154
- // Schema-declared sealed columns missing from live → warning
155
- if (schema && Array.isArray(schema.sealedFields)) {
156
- for (var sf = 0; sf < schema.sealedFields.length; sf++) {
157
- var col = schema.sealedFields[sf];
158
- if (!liveColSet[col]) {
159
- warnings.push({
160
- kind: "sealed_col_missing",
161
- table: table,
162
- column: col,
163
- message: "schema lists '" + table + "." + col + "' as sealed but the live table has no such column (skipped during rotation)",
164
- });
165
- }
166
- }
167
- }
168
-
169
- // Drift detection: real columns that aren't in the schema's
170
- // sealedFields, derivedHashes, or the operator's infraColumns
171
- // allowlist. Sample up to driftSampleLimit rows; flag any column
172
- // that holds a vault-prefixed string.
173
- var known = _knownColumnsFor(schema, infraColumns);
174
- var unknown = [];
175
- for (var lc = 0; lc < liveCols.length; lc++) {
176
- if (!known[liveCols[lc]]) unknown.push(liveCols[lc]);
177
- }
178
- if (unknown.length === 0) continue;
179
-
180
- var quotedCols = unknown.map(function (n) { return '"' + n.replace(/"/g, '""') + '"'; }).join(", ");
181
- var sampleSql = "SELECT " + quotedCols +
182
- " FROM \"" + table.replace(/"/g, '""') + "\" LIMIT " + sampleLimit;
183
- var sampled;
184
- try {
185
- sampled = db.prepare(sampleSql).all();
186
- } catch (e) {
187
- warnings.push({
188
- kind: "sample_failed",
189
- table: table,
190
- message: "could not sample '" + table + "' for drift detection: " + ((e && e.message) || String(e)),
191
- });
192
- continue;
193
- }
194
-
195
- var flagged = Object.create(null);
196
- for (var r = 0; r < sampled.length; r++) {
197
- var row = sampled[r];
198
- for (var u = 0; u < unknown.length; u++) {
199
- var uname = unknown[u];
200
- if (flagged[uname]) continue;
201
- var v = row[uname];
202
- if (typeof v === "string" && v.indexOf(VAULT_PREFIX) === 0) {
203
- flagged[uname] = true;
204
- errors.push({
205
- kind: "drift",
206
- table: table,
207
- column: uname,
208
- message: "live DB has vault-prefixed value in '" + table + "." + uname +
209
- "' but the schema does NOT declare it sealed. Rotating now would leave " +
210
- "this column encrypted under the OLD key, unreadable post-rotation. " +
211
- "Either add '" + uname + "' to the schema's sealedFields, or pass it " +
212
- "via opts.infraColumns if it's intentionally unsealed in the framework's tables.",
213
- });
214
- }
215
- }
216
- }
217
- }
218
-
219
- return { warnings: warnings, errors: errors };
220
- }
221
-
222
- function formatValidationResult(result) {
223
- var lines = [];
224
- if (result.warnings.length === 0 && result.errors.length === 0) {
225
- return "[vault-rotate] schema match: OK";
226
- }
227
- if (result.warnings.length > 0) {
228
- lines.push("[vault-rotate] schema warnings (" + result.warnings.length + ", non-fatal):");
229
- for (var w = 0; w < result.warnings.length; w++) lines.push(" - " + result.warnings[w].message);
230
- }
231
- if (result.errors.length > 0) {
232
- lines.push("[vault-rotate] schema errors (" + result.errors.length + ", FATAL — rotation refused):");
233
- for (var e = 0; e < result.errors.length; e++) lines.push(" - " + result.errors[e].message);
234
- }
235
- return lines.join("\n");
236
- }
237
-
238
- // verify — sample sealed columns, decrypt with `keys`, report results.
239
- //
240
- // When opts.oldKeys is supplied, also flag rows whose sampled values
241
- // STILL decrypt under oldKeys — that's a regression: rotation didn't
242
- // take effect for those rows.
243
- function verify(opts) {
244
- opts = opts || {};
245
- if (!opts.keys) {
246
- throw new VaultRotateError("vault-rotate/no-keys",
247
- "verify: opts.keys is required (the keypair to decrypt with)");
248
- }
249
- if (!opts.db || typeof opts.db.prepare !== "function") {
250
- throw new VaultRotateError("vault-rotate/no-db",
251
- "verify: opts.db is required (a node:sqlite handle)");
252
- }
253
- var keys = opts.keys;
254
- var db = opts.db;
255
- var oldKeys = opts.oldKeys || null;
256
- nb.requirePositiveFiniteIntIfPresent(opts.sampleMin,
257
- "verify: sampleMin", VaultRotateError, "vault-rotate/bad-opt");
258
- var sampleMin = opts.sampleMin !== undefined
259
- ? opts.sampleMin : DEFAULT_VERIFY_SAMPLE_MIN;
260
- if (opts.samplePercent !== undefined &&
261
- (typeof opts.samplePercent !== "number" || !Number.isFinite(opts.samplePercent) ||
262
- opts.samplePercent <= 0)) {
263
- throw new VaultRotateError("vault-rotate/bad-opt",
264
- "verify: samplePercent must be a positive finite fraction; got " +
265
- nb.shape(opts.samplePercent));
266
- }
267
- var samplePct = opts.samplePercent !== undefined
268
- ? opts.samplePercent : DEFAULT_VERIFY_SAMPLE_FRAC;
269
- var tablesArg = Array.isArray(opts.tables) && opts.tables.length > 0
270
- ? opts.tables.slice() : null;
271
-
272
- var passed = [];
273
- var failures = [];
274
- var regressions = [];
275
-
276
- var liveTables = _listLiveTables(db);
277
- var liveTableSet = Object.create(null);
278
- for (var lt = 0; lt < liveTables.length; lt++) liveTableSet[liveTables[lt]] = true;
279
- var tables = tablesArg || liveTables;
280
-
281
- for (var ti = 0; ti < tables.length; ti++) {
282
- var table = tables[ti];
283
- if (!liveTableSet[table]) continue;
284
- var schema = cryptoField.getSchema(table);
285
- if (!schema || !Array.isArray(schema.sealedFields) || schema.sealedFields.length === 0) continue;
286
-
287
- var totalRow = db.prepare('SELECT COUNT(*) AS n FROM "' + table.replace(/"/g, '""') + '"').get();
288
- var total = totalRow ? totalRow.n : 0;
289
- if (total === 0) continue;
290
-
291
- var sampleN = Math.max(sampleMin, Math.ceil(total * samplePct));
292
- if (sampleN > total) sampleN = total;
293
-
294
- // RANDOM() is fine for a sampler — we're picking representative rows,
295
- // not building cryptographic randomness.
296
- var sampled = db.prepare(
297
- 'SELECT * FROM "' + table.replace(/"/g, '""') + '" ORDER BY RANDOM() LIMIT ?'
298
- ).all(sampleN);
299
-
300
- var foundOldFail = !oldKeys; // when no oldKeys supplied, this check is N/A
301
- var verifiedRows = 0;
302
-
303
- for (var r = 0; r < sampled.length; r++) {
304
- var row = sampled[r];
305
- var rowFailed = false;
306
-
307
- for (var sf = 0; sf < schema.sealedFields.length; sf++) {
308
- var col = schema.sealedFields[sf];
309
- var v = row[col];
310
- if (typeof v !== "string" || v.indexOf(VAULT_PREFIX) !== 0) continue;
311
- var payload = v.substring(VAULT_PREFIX.length);
312
-
313
- try { cryptoLib.decrypt(payload, keys); }
314
- catch (e) {
315
- rowFailed = true;
316
- failures.push({
317
- table: table,
318
- column: col,
319
- _id: row._id,
320
- error: (e && e.message) || String(e),
321
- });
322
- }
323
-
324
- if (oldKeys && !foundOldFail) {
325
- try {
326
- cryptoLib.decrypt(payload, oldKeys);
327
- regressions.push({
328
- table: table,
329
- column: col,
330
- _id: row._id,
331
- error: "old keys still decrypt this value — rotation did not take effect",
332
- });
333
- } catch (_e) {
334
- foundOldFail = true; // at least one row no longer decrypts under old keys → rotation effective
335
- }
336
- }
337
- }
338
-
339
- if (!rowFailed) verifiedRows++;
340
- }
341
-
342
- passed.push({ table: table, sampled: sampled.length, verified: verifiedRows });
343
- }
344
-
345
- return {
346
- ok: failures.length === 0 && regressions.length === 0,
347
- passed: passed,
348
- failures: failures,
349
- regressions: regressions,
350
- };
351
- }
352
-
353
- // =====================================================================
354
- // Rotation pipeline
355
- //
356
- // Produces a fully-rotated copy of dataDir at stagingDir. Caller does
357
- // the atomic swap (rename or symlink flip) after verifying staging.
358
- // dataDir is read-only throughout; failure leaves dataDir untouched.
359
- //
360
- // File layout the rotator knows about (override via opts.paths):
361
- //
362
- // <dataDir>/db.enc — at-rest encrypted SQLite DB (XChaCha20-
363
- // Poly1305 with a 32-byte dbKey)
364
- // <dataDir>/db.key.enc — vault-sealed base64(dbKey)
365
- // <dataDir>/vault.key — plaintext JSON keypair (plaintext mode)
366
- // <dataDir>/vault.key.sealed — passphrase-wrapped keypair (wrapped mode)
367
- //
368
- // dbKey is REUSED across rotation — it isn't a vault key, just an
369
- // XChaCha20 key, and the surface protecting it is the vault seal of
370
- // db.key.enc. We re-seal db.key.enc under newKeys, but db.enc itself
371
- // only needs to be rewritten because the SEALED VALUES INSIDE it
372
- // changed (each row's email/etc. column). The packed envelope around
373
- // the SQLite bytes uses the same dbKey old → new.
374
- // =====================================================================
375
-
376
- // Row count, not a byte quantity — hex form keeps the literal out of the
377
- // byte-shape detector while preserving the operator-readable magnitude.
378
- var ROW_BATCH_SIZE_DEFAULT = 0x3E8;
379
- var VAULT_PREFIX_LEN = C.VAULT_PREFIX.length;
380
-
381
- function _emit(cb, ev) {
382
- if (typeof cb === "function") {
383
- try { cb(ev); } catch (_e) { /* progress-callback errors are non-fatal */ }
384
- }
385
- }
386
-
387
- // Open a file for fsync. Different from atomicFile.fsync (which takes
388
- // an already-open fd) — vault-rotate's fsync-by-path semantic opens
389
- // then syncs then closes, which is the right shape when we don't have
390
- // the original write fd around.
391
- function _fsyncFileByPath(p) {
392
- try {
393
- var fd = fs.openSync(p, "r+");
394
- try { fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
395
- } catch (_e) { /* best-effort across platforms */ }
396
- }
397
-
398
- function _reSealValue(sealedValue, oldKeys, newKeys) {
399
- if (typeof sealedValue !== "string") return sealedValue;
400
- if (sealedValue.indexOf(C.VAULT_PREFIX) !== 0) return sealedValue;
401
- var payload = sealedValue.substring(VAULT_PREFIX_LEN);
402
- var plain = cryptoLib.decrypt(payload, oldKeys);
403
- return C.VAULT_PREFIX + cryptoLib.encrypt(plain, newKeys);
404
- }
405
-
406
- // Walk a JSON-decoded value, re-sealing every vault-prefixed string.
407
- // Returns { value, changed } so the caller knows whether to write back.
408
- function _walkAndReSeal(node, oldKeys, newKeys) {
409
- if (typeof node === "string") {
410
- if (node.indexOf(C.VAULT_PREFIX) !== 0) return { value: node, changed: false };
411
- return { value: _reSealValue(node, oldKeys, newKeys), changed: true };
412
- }
413
- if (Array.isArray(node)) {
414
- var out = new Array(node.length);
415
- var any = false;
416
- for (var i = 0; i < node.length; i++) {
417
- var r = _walkAndReSeal(node[i], oldKeys, newKeys);
418
- out[i] = r.value;
419
- if (r.changed) any = true;
420
- }
421
- return { value: out, changed: any };
422
- }
423
- if (node && typeof node === "object") {
424
- var ob = {};
425
- var c = false;
426
- for (var k in node) {
427
- if (!Object.prototype.hasOwnProperty.call(node, k)) continue;
428
- var rv = _walkAndReSeal(node[k], oldKeys, newKeys);
429
- ob[k] = rv.value;
430
- if (rv.changed) c = true;
431
- }
432
- return { value: ob, changed: c };
433
- }
434
- return { value: node, changed: false };
435
- }
436
-
437
- function _runStmt(db, sql) { db.prepare(sql).run(); }
438
-
439
- function _rotateColumn(db, table, column, oldKeys, newKeys, batchSize, progress) {
440
- var qt = '"' + table.replace(/"/g, '""') + '"';
441
- var qc = '"' + column.replace(/"/g, '""') + '"';
442
- var total = db.prepare("SELECT COUNT(*) AS n FROM " + qt + " WHERE " + qc + " IS NOT NULL").get().n;
443
- if (total === 0) return 0;
444
-
445
- var sel = db.prepare(
446
- "SELECT _id, " + qc + " AS v FROM " + qt +
447
- " WHERE " + qc + " IS NOT NULL AND _id > ? ORDER BY _id LIMIT ?"
448
- );
449
- var upd = db.prepare("UPDATE " + qt + " SET " + qc + " = ? WHERE _id = ?");
450
-
451
- var processed = 0;
452
- var lastId = "";
453
- while (true) {
454
- var rows = sel.all(lastId, batchSize);
455
- if (rows.length === 0) break;
456
-
457
- dbSchema.runInTransaction(db, function () {
458
- for (var i = 0; i < rows.length; i++) {
459
- var row = rows[i];
460
- if (typeof row.v === "string" && row.v.indexOf(C.VAULT_PREFIX) === 0) {
461
- upd.run(_reSealValue(row.v, oldKeys, newKeys), row._id);
462
- }
463
- }
464
- });
465
- processed += rows.length;
466
- lastId = rows[rows.length - 1]._id;
467
- _emit(progress, { phase: "rotate_rows", table: table, column: column, rowsProcessed: processed, rowsTotal: total });
468
- }
469
- return processed;
470
- }
471
-
472
- function _rotateOverflow(db, table, oldKeys, newKeys, batchSize, progress, warnings) {
473
- var qt = '"' + table.replace(/"/g, '""') + '"';
474
- var cols = db.prepare("PRAGMA table_info(" + qt + ")").all();
475
- if (!cols.some(function (c) { return c.name === "data"; })) return 0;
476
-
477
- var total = db.prepare("SELECT COUNT(*) AS n FROM " + qt + " WHERE data IS NOT NULL").get().n;
478
- if (total === 0) return 0;
479
-
480
- var sel = db.prepare(
481
- "SELECT _id, data FROM " + qt +
482
- " WHERE data IS NOT NULL AND _id > ? ORDER BY _id LIMIT ?"
483
- );
484
- var upd = db.prepare("UPDATE " + qt + " SET data = ? WHERE _id = ?");
485
-
486
- var processed = 0;
487
- var lastId = "";
488
- while (true) {
489
- var rows = sel.all(lastId, batchSize);
490
- if (rows.length === 0) break;
491
-
492
- _runStmt(db, "BEGIN");
493
- try {
494
- for (var i = 0; i < rows.length; i++) {
495
- var row = rows[i];
496
- var doc;
497
- // Vault overflow rows are framework-written but read at rotate
498
- // time — gate the parse via safeJson so a corrupted row can't
499
- // stage a parse-bomb against the rotation worker.
500
- try { doc = safeJson.parse(row.data, { maxBytes: C.BYTES.mib(16) }); }
501
- catch (_e) {
502
- warnings.push("malformed overflow JSON at " + table + "._id=" + row._id + " — left unrotated");
503
- continue;
504
- }
505
- var rv = _walkAndReSeal(doc, oldKeys, newKeys);
506
- if (rv.changed) upd.run(JSON.stringify(rv.value), row._id);
507
- }
508
- _runStmt(db, "COMMIT");
509
- } catch (e) {
510
- _runStmt(db, "ROLLBACK");
511
- throw e;
512
- }
513
- processed += rows.length;
514
- lastId = rows[rows.length - 1]._id;
515
- _emit(progress, { phase: "rotate_overflow", table: table, rowsProcessed: processed, rowsTotal: total });
516
- }
517
- return processed;
518
- }
519
-
520
- async function rotate(opts) {
521
- opts = opts || {};
522
- var startedAt = Date.now();
523
- var oldKeys = opts.oldKeys;
524
- var newKeys = opts.newKeys;
525
- if (!oldKeys || !newKeys) {
526
- throw new VaultRotateError("vault-rotate/no-keys",
527
- "rotate: opts.oldKeys and opts.newKeys are required");
528
- }
529
- if (typeof opts.dataDir !== "string" || !fs.existsSync(opts.dataDir)) {
530
- throw new VaultRotateError("vault-rotate/no-datadir",
531
- "rotate: opts.dataDir is required and must exist");
532
- }
533
- validateOpts.requireNonEmptyString(opts.stagingDir, "rotate: opts.stagingDir", VaultRotateError, "vault-rotate/no-staging");
534
- if (fs.existsSync(opts.stagingDir)) {
535
- throw new VaultRotateError("vault-rotate/staging-exists",
536
- "rotate: stagingDir already exists: " + opts.stagingDir);
537
- }
538
- var mode = opts.mode || "plaintext";
539
- if (mode !== "plaintext" && mode !== "wrapped") {
540
- throw new VaultRotateError("vault-rotate/bad-mode",
541
- "rotate: opts.mode must be 'plaintext' or 'wrapped'");
542
- }
543
- if (mode === "wrapped" && !Buffer.isBuffer(opts.newPassphrase)) {
544
- throw new VaultRotateError("vault-rotate/no-passphrase",
545
- "rotate: wrapped mode requires opts.newPassphrase (Buffer)");
546
- }
547
- var rowBatchSize = opts.rowBatchSize || ROW_BATCH_SIZE_DEFAULT;
548
- var progress = opts.progressCallback;
549
- var warnings = [];
550
- var paths = Object.assign({
551
- encryptedDb: "db.enc",
552
- dbKeySealed: "db.key.enc",
553
- vaultKeyPlain: "vault.key",
554
- vaultKeySealed: "vault.key.sealed",
555
- additionalSealed: [],
556
- verbatimFiles: [],
557
- verbatimDirs: [],
558
- }, opts.paths || {});
559
-
560
- var dataDir = opts.dataDir;
561
- var stagingDir = opts.stagingDir;
562
-
563
- _emit(progress, { phase: "init" });
564
- atomicFile.ensureDir(stagingDir);
565
-
566
- // 1. verbatim files + dirs
567
- _emit(progress, { phase: "copy_verbatim" });
568
- for (var vf = 0; vf < paths.verbatimFiles.length; vf++) {
569
- var entry = paths.verbatimFiles[vf];
570
- var src = path.join(dataDir, entry.relativePath);
571
- if (!fs.existsSync(src)) {
572
- if (entry.required) {
573
- throw new VaultRotateError("vault-rotate/missing-verbatim",
574
- "rotate: required verbatim file missing: " + entry.relativePath);
575
- }
576
- continue;
577
- }
578
- var dest = path.join(stagingDir, entry.relativePath);
579
- atomicFile.ensureDir(path.dirname(dest));
580
- fs.copyFileSync(src, dest);
581
- }
582
- for (var vd = 0; vd < paths.verbatimDirs.length; vd++) {
583
- var dent = paths.verbatimDirs[vd];
584
- var sdir = path.join(dataDir, dent.relativePath);
585
- if (!fs.existsSync(sdir)) {
586
- if (dent.required) {
587
- throw new VaultRotateError("vault-rotate/missing-verbatim-dir",
588
- "rotate: required verbatim dir missing: " + dent.relativePath);
589
- }
590
- continue;
591
- }
592
- if (fs.existsSync(sdir)) {
593
- atomicFile.copyDirRecursive(sdir, path.join(stagingDir, dent.relativePath));
594
- }
595
- }
596
-
597
- // 2. write new vault key
598
- _emit(progress, { phase: "write_vault_key" });
599
- var keysJson = JSON.stringify(newKeys, null, 2);
600
- if (mode === "wrapped") {
601
- var sealed = await vaultWrap().wrap(keysJson, opts.newPassphrase);
602
- fs.writeFileSync(path.join(stagingDir, paths.vaultKeySealed), sealed, { mode: 0o600 });
603
- } else {
604
- fs.writeFileSync(path.join(stagingDir, paths.vaultKeyPlain), keysJson, { mode: 0o600 });
605
- }
606
-
607
- // 3. re-seal db.key.enc + any operator-supplied additionalSealed files
608
- _emit(progress, { phase: "reseal_files" });
609
- var dbKeySealedPath = path.join(dataDir, paths.dbKeySealed);
610
- var dbKey = null;
611
- if (fs.existsSync(dbKeySealedPath)) {
612
- var sealedKey = fs.readFileSync(dbKeySealedPath, "utf8").trim();
613
- if (sealedKey.indexOf(C.VAULT_PREFIX) !== 0) {
614
- throw new VaultRotateError("vault-rotate/bad-dbkey",
615
- "rotate: db.key.enc does not start with the vault prefix");
616
- }
617
- var dbKeyB64 = cryptoLib.decrypt(sealedKey.substring(VAULT_PREFIX_LEN), oldKeys);
618
- dbKey = Buffer.from(dbKeyB64, "base64");
619
- var resealedKey = C.VAULT_PREFIX + cryptoLib.encrypt(dbKeyB64, newKeys);
620
- fs.writeFileSync(path.join(stagingDir, paths.dbKeySealed), resealedKey, { mode: 0o600 });
621
- }
622
- for (var as = 0; as < paths.additionalSealed.length; as++) {
623
- var ase = paths.additionalSealed[as];
624
- var asSrc = path.join(dataDir, ase.relativePath);
625
- if (!fs.existsSync(asSrc)) {
626
- if (ase.required) {
627
- throw new VaultRotateError("vault-rotate/missing-sealed",
628
- "rotate: required sealed file missing: " + ase.relativePath);
629
- }
630
- continue;
631
- }
632
- var current = fs.readFileSync(asSrc, "utf8").trim();
633
- if (current.indexOf(C.VAULT_PREFIX) !== 0) {
634
- throw new VaultRotateError("vault-rotate/bad-sealed",
635
- "rotate: sealed file does not start with the vault prefix: " + ase.relativePath);
636
- }
637
- var asDestDir = path.join(stagingDir, path.dirname(ase.relativePath));
638
- if (!fs.existsSync(asDestDir)) atomicFile.ensureDir(asDestDir);
639
- fs.writeFileSync(path.join(stagingDir, ase.relativePath),
640
- _reSealValue(current, oldKeys, newKeys), { mode: 0o600 });
641
- }
642
-
643
- // 4. decrypt + rotate + re-encrypt db.enc
644
- _emit(progress, { phase: "rotate_db" });
645
- var encDbPath = path.join(dataDir, paths.encryptedDb);
646
- var tablesProcessed = 0;
647
- var totalRowsProcessed = 0;
648
- var verifyResult = null;
649
-
650
- if (fs.existsSync(encDbPath) && dbKey) {
651
- var packed = fs.readFileSync(encDbPath);
652
- var plainBytes = cryptoLib.decryptPacked(packed, dbKey);
653
- var tmpDbPath = path.join(stagingDir, "_blamejs_rotate.tmp.db");
654
- fs.writeFileSync(tmpDbPath, plainBytes, { mode: 0o600 });
655
-
656
- var db = new DatabaseSync(tmpDbPath);
657
- try {
658
- _runStmt(db, "PRAGMA journal_mode=WAL");
659
- _runStmt(db, "PRAGMA synchronous=NORMAL");
660
-
661
- // Walk tables. For each, re-seal every column declared sealed
662
- // by the field-crypto registry, plus the overflow `data` JSON
663
- // column if present.
664
- var tablesToRotate = Array.isArray(opts.tables) && opts.tables.length > 0
665
- ? opts.tables.slice()
666
- : db.prepare(
667
- "SELECT name FROM sqlite_master " +
668
- "WHERE type='table' AND name NOT LIKE 'sqlite_%'"
669
- ).all().map(function (r) { return r.name; });
670
-
671
- for (var ti = 0; ti < tablesToRotate.length; ti++) {
672
- var table = tablesToRotate[ti];
673
- var tableExists = db.prepare(
674
- "SELECT name FROM sqlite_master WHERE type='table' AND name = ?"
675
- ).get(table);
676
- if (!tableExists) continue;
677
-
678
- var schema = cryptoField.getSchema(table);
679
- var liveCols = db.prepare(
680
- 'PRAGMA table_info("' + table.replace(/"/g, '""') + '")'
681
- ).all().map(function (c) { return c.name; });
682
- var liveColSet = Object.create(null);
683
- for (var lc = 0; lc < liveCols.length; lc++) liveColSet[liveCols[lc]] = true;
684
-
685
- var tableRows = 0;
686
- if (schema && Array.isArray(schema.sealedFields)) {
687
- for (var sc = 0; sc < schema.sealedFields.length; sc++) {
688
- var col = schema.sealedFields[sc];
689
- if (!liveColSet[col]) continue;
690
- tableRows += _rotateColumn(db, table, col, oldKeys, newKeys, rowBatchSize, progress);
691
- }
692
- }
693
- tableRows += _rotateOverflow(db, table, oldKeys, newKeys, rowBatchSize, progress, warnings);
694
-
695
- if (tableRows > 0) { tablesProcessed++; totalRowsProcessed += tableRows; }
696
- }
697
-
698
- _runStmt(db, "PRAGMA wal_checkpoint(TRUNCATE)");
699
- } finally {
700
- db.close();
701
- }
702
-
703
- // Drop WAL/SHM sidecars before re-encrypting the .db file. Either
704
- // sidecar may be absent (depending on whether journal_mode produced
705
- // one for this run); log at debug so the cleanup attempt isn't
706
- // silently swallowed when something genuinely unexpected fails.
707
- try { fs.unlinkSync(tmpDbPath + "-wal"); }
708
- catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: tmpDbPath + "-wal", error: e.message }); }
709
- try { fs.unlinkSync(tmpDbPath + "-shm"); }
710
- catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: tmpDbPath + "-shm", error: e.message }); }
711
-
712
- var rotatedBytes = fs.readFileSync(tmpDbPath);
713
- fs.writeFileSync(path.join(stagingDir, paths.encryptedDb),
714
- cryptoLib.encryptPacked(rotatedBytes, dbKey));
715
- fs.unlinkSync(tmpDbPath);
716
-
717
- // Round-trip verify on the staged DB
718
- _emit(progress, { phase: "verify" });
719
- var verifyTmp = path.join(stagingDir, "_blamejs_verify.tmp.db");
720
- fs.writeFileSync(verifyTmp,
721
- cryptoLib.decryptPacked(fs.readFileSync(path.join(stagingDir, paths.encryptedDb)), dbKey));
722
- var vdb = new DatabaseSync(verifyTmp);
723
- try {
724
- verifyResult = verify({ keys: newKeys, db: vdb, oldKeys: oldKeys });
725
- } finally {
726
- vdb.close();
727
- try { fs.unlinkSync(verifyTmp); }
728
- catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: verifyTmp, error: e.message }); }
729
- try { fs.unlinkSync(verifyTmp + "-wal"); }
730
- catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: verifyTmp + "-wal", error: e.message }); }
731
- try { fs.unlinkSync(verifyTmp + "-shm"); }
732
- catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: verifyTmp + "-shm", error: e.message }); }
733
- }
734
- if (!verifyResult.ok) {
735
- throw new VaultRotateError("vault-rotate/verify-failed",
736
- "round-trip verification failed: " +
737
- verifyResult.failures.length + " decrypt failure(s), " +
738
- verifyResult.regressions.length + " non-rotated row(s). " +
739
- "First issue: " + JSON.stringify(verifyResult.failures[0] || verifyResult.regressions[0]));
740
- }
741
- }
742
-
743
- // 5. fsync staging for durability before caller does the swap
744
- _emit(progress, { phase: "fsync" });
745
- function fsyncTree(dir) {
746
- var entries = fs.readdirSync(dir);
747
- for (var i = 0; i < entries.length; i++) {
748
- var p = path.join(dir, entries[i]);
749
- var st = fs.statSync(p);
750
- if (st.isFile()) _fsyncFileByPath(p);
751
- else if (st.isDirectory()) fsyncTree(p);
752
- }
753
- atomicFile.fsyncDir(dir);
754
- }
755
- fsyncTree(stagingDir);
756
-
757
- var durationMs = Date.now() - startedAt;
758
- _emit(progress, {
759
- phase: "done",
760
- durationMs: durationMs,
761
- tablesProcessed: tablesProcessed,
762
- totalRowsProcessed: totalRowsProcessed,
763
- });
764
- return {
765
- durationMs: durationMs,
766
- tablesProcessed: tablesProcessed,
767
- totalRowsProcessed: totalRowsProcessed,
768
- verifyResult: verifyResult,
769
- warnings: warnings,
770
- };
771
- }
772
-
773
- module.exports = {
774
- validateSchemaMatch: validateSchemaMatch,
775
- formatValidationResult: formatValidationResult,
776
- verify: verify,
777
- rotate: rotate,
778
- VaultRotateError: VaultRotateError,
779
- // Constants exposed so operators / tests can reference the same defaults
780
- DEFAULT_DRIFT_SAMPLE_LIMIT: DEFAULT_DRIFT_SAMPLE_LIMIT,
781
- DEFAULT_VERIFY_SAMPLE_MIN: DEFAULT_VERIFY_SAMPLE_MIN,
782
- DEFAULT_VERIFY_SAMPLE_FRAC: DEFAULT_VERIFY_SAMPLE_FRAC,
783
- ROW_BATCH_SIZE_DEFAULT: ROW_BATCH_SIZE_DEFAULT,
784
- };
1
+ "use strict";
2
+ /**
3
+ * vault-rotate — vault key rotation primitives.
4
+ *
5
+ * Two surfaces ship together: the read-only diagnostic helpers operators
6
+ * run before/after a rotation, and the rotation pipeline itself.
7
+ *
8
+ * Diagnostics:
9
+ *
10
+ * - validateSchemaMatch(db, opts)
11
+ * Compare the registered field-crypto schema against the live
12
+ * DB. Surfaces three failure modes:
13
+ * · table_missing — schema declares a table the DB lacks
14
+ * · sealed_col_missing — schema declares a sealed column the
15
+ * live table lacks
16
+ * · drift — a real column the schema does NOT
17
+ * declare sealed has vault-prefixed
18
+ * values in at least one sampled row
19
+ *
20
+ * - verify({ keys, db, oldKeys?, sampleMin?, samplePercent? })
21
+ * Walk every registered sealed column, sample rows, attempt to
22
+ * decrypt with `keys`. Reports failures + (if oldKeys passed)
23
+ * regressions: rows that still decrypt under oldKeys, indicating
24
+ * rotation didn't take effect.
25
+ *
26
+ * - formatValidationResult(result)
27
+ * Render the validation result for a CLI / log line.
28
+ *
29
+ * Rotation pipeline:
30
+ *
31
+ * - rotate({ dataDir, oldKeys, newKeys, ... })
32
+ * Walks every sealed column across every registered table, decrypts
33
+ * under oldKeys and re-encrypts under newKeys with batched commits.
34
+ * Atomic commit at the data-directory level so a partial rotation
35
+ * never leaves rows half-translated.
36
+ *
37
+ * Operators run diagnostics whether or not they're rotating: post-deploy
38
+ * schema-drift smoke tests, post-incident "did anything get unrotated?"
39
+ * sweeps. The rotation pipeline runs whenever the vault keypair changes
40
+ * (passphrase rotation, sealed-blob refresh, hardware-token swap).
41
+ *
42
+ * Generic by design: no hardcoded list of "infrastructure columns" —
43
+ * the drift detector treats every column not declared in the schema's
44
+ * sealedFields / derivedHashes as a candidate, samples rows, and only
45
+ * flags those that actually contain vault-prefixed values. Operators
46
+ * with framework tables that legitimately hold a vault-prefixed string
47
+ * in an undeclared column pass them via opts.infraColumns so the
48
+ * sampler skips them.
49
+ */
50
+
51
+ var fs = require("fs");
52
+ var path = require("path");
53
+ var { DatabaseSync } = require("node:sqlite");
54
+ var atomicFile = require("../atomic-file");
55
+ var C = require("../constants");
56
+ var cryptoField = require("../crypto-field");
57
+ var cryptoLib = require("../crypto");
58
+ var dbSchema = require("../db-schema");
59
+ var lazyRequire = require("../lazy-require");
60
+ var { boot } = require("../log");
61
+ var nb = require("../numeric-bounds");
62
+ var safeJson = require("../safe-json");
63
+ var validateOpts = require("../validate-opts");
64
+ var vaultWrap = lazyRequire(function () { return require("./wrap"); });
65
+ var { defineClass } = require("../framework-error");
66
+
67
+ var rotateLog = boot("vault-rotate");
68
+
69
+ var VaultRotateError = defineClass("VaultRotateError", { alwaysPermanent: true });
70
+
71
+ var VAULT_PREFIX = C.VAULT_PREFIX;
72
+ var DEFAULT_DRIFT_SAMPLE_LIMIT = 100;
73
+ var DEFAULT_VERIFY_SAMPLE_MIN = 5;
74
+ var DEFAULT_VERIFY_SAMPLE_FRAC = 0.01;
75
+
76
+ function _listLiveTables(db) {
77
+ return db.prepare(
78
+ "SELECT name FROM sqlite_master " +
79
+ "WHERE type='table' AND name NOT LIKE 'sqlite_%'"
80
+ ).all().map(function (r) { return r.name; });
81
+ }
82
+
83
+ function _listLiveColumns(db, table) {
84
+ // PRAGMA table_info — table name comes from sqlite_master so it's
85
+ // already validated as an existing identifier.
86
+ return db.prepare("PRAGMA table_info(\"" + table.replace(/"/g, '""') + "\")").all()
87
+ .map(function (c) { return c.name; });
88
+ }
89
+
90
+ function _knownColumnsFor(schema, infraColumns) {
91
+ var set = Object.create(null);
92
+ if (Array.isArray(infraColumns)) {
93
+ for (var i = 0; i < infraColumns.length; i++) set[infraColumns[i]] = true;
94
+ }
95
+ if (schema && Array.isArray(schema.sealedFields)) {
96
+ for (var s = 0; s < schema.sealedFields.length; s++) set[schema.sealedFields[s]] = true;
97
+ }
98
+ if (schema && schema.derivedHashes) {
99
+ for (var dk in schema.derivedHashes) {
100
+ if (Object.prototype.hasOwnProperty.call(schema.derivedHashes, dk)) set[dk] = true;
101
+ // Source field is also "known" — it's the unsealed input
102
+ var spec = schema.derivedHashes[dk];
103
+ if (spec && spec.from) set[spec.from] = true;
104
+ }
105
+ }
106
+ return set;
107
+ }
108
+
109
+ function validateSchemaMatch(db, opts) {
110
+ opts = opts || {};
111
+ nb.requirePositiveFiniteIntIfPresent(opts.driftSampleLimit,
112
+ "validateSchemaMatch: driftSampleLimit", VaultRotateError, "vault-rotate/bad-opt");
113
+ var sampleLimit = opts.driftSampleLimit !== undefined
114
+ ? opts.driftSampleLimit : DEFAULT_DRIFT_SAMPLE_LIMIT;
115
+ var infraColumns = Array.isArray(opts.infraColumns) ? opts.infraColumns : [];
116
+ // Tables to consider — by default, every table the framework's
117
+ // field-crypto registry knows about. Operator can pass an explicit
118
+ // tables list to scope the check.
119
+ var tablesToCheck = Array.isArray(opts.tables) && opts.tables.length > 0
120
+ ? opts.tables.slice()
121
+ : null;
122
+
123
+ var warnings = [];
124
+ var errors = [];
125
+
126
+ var liveTables = _listLiveTables(db);
127
+ var liveTableSet = Object.create(null);
128
+ for (var lt = 0; lt < liveTables.length; lt++) liveTableSet[liveTables[lt]] = true;
129
+
130
+ // If no tables list passed, derive it from the live DB. Tables
131
+ // unknown to cryptoField will be reported as drift candidates if
132
+ // they have vault-prefixed columns.
133
+ var allTables = tablesToCheck || liveTables;
134
+
135
+ for (var t = 0; t < allTables.length; t++) {
136
+ var table = allTables[t];
137
+
138
+ if (!liveTableSet[table]) {
139
+ // Schema declared a table the DB doesn't have. Non-fatal —
140
+ // rotation skips it (nothing to rotate).
141
+ warnings.push({
142
+ kind: "table_missing",
143
+ table: table,
144
+ message: "schema lists table '" + table + "' but the live DB has no such table (skipped during rotation)",
145
+ });
146
+ continue;
147
+ }
148
+
149
+ var schema = cryptoField.getSchema(table); // null if not registered
150
+ var liveCols = _listLiveColumns(db, table);
151
+ var liveColSet = Object.create(null);
152
+ for (var c = 0; c < liveCols.length; c++) liveColSet[liveCols[c]] = true;
153
+
154
+ // Schema-declared sealed columns missing from live → warning
155
+ if (schema && Array.isArray(schema.sealedFields)) {
156
+ for (var sf = 0; sf < schema.sealedFields.length; sf++) {
157
+ var col = schema.sealedFields[sf];
158
+ if (!liveColSet[col]) {
159
+ warnings.push({
160
+ kind: "sealed_col_missing",
161
+ table: table,
162
+ column: col,
163
+ message: "schema lists '" + table + "." + col + "' as sealed but the live table has no such column (skipped during rotation)",
164
+ });
165
+ }
166
+ }
167
+ }
168
+
169
+ // Drift detection: real columns that aren't in the schema's
170
+ // sealedFields, derivedHashes, or the operator's infraColumns
171
+ // allowlist. Sample up to driftSampleLimit rows; flag any column
172
+ // that holds a vault-prefixed string.
173
+ var known = _knownColumnsFor(schema, infraColumns);
174
+ var unknown = [];
175
+ for (var lc = 0; lc < liveCols.length; lc++) {
176
+ if (!known[liveCols[lc]]) unknown.push(liveCols[lc]);
177
+ }
178
+ if (unknown.length === 0) continue;
179
+
180
+ var quotedCols = unknown.map(function (n) { return '"' + n.replace(/"/g, '""') + '"'; }).join(", ");
181
+ var sampleSql = "SELECT " + quotedCols +
182
+ " FROM \"" + table.replace(/"/g, '""') + "\" LIMIT " + sampleLimit;
183
+ var sampled;
184
+ try {
185
+ sampled = db.prepare(sampleSql).all();
186
+ } catch (e) {
187
+ warnings.push({
188
+ kind: "sample_failed",
189
+ table: table,
190
+ message: "could not sample '" + table + "' for drift detection: " + ((e && e.message) || String(e)),
191
+ });
192
+ continue;
193
+ }
194
+
195
+ var flagged = Object.create(null);
196
+ for (var r = 0; r < sampled.length; r++) {
197
+ var row = sampled[r];
198
+ for (var u = 0; u < unknown.length; u++) {
199
+ var uname = unknown[u];
200
+ if (flagged[uname]) continue;
201
+ var v = row[uname];
202
+ if (typeof v === "string" && v.indexOf(VAULT_PREFIX) === 0) {
203
+ flagged[uname] = true;
204
+ errors.push({
205
+ kind: "drift",
206
+ table: table,
207
+ column: uname,
208
+ message: "live DB has vault-prefixed value in '" + table + "." + uname +
209
+ "' but the schema does NOT declare it sealed. Rotating now would leave " +
210
+ "this column encrypted under the OLD key, unreadable post-rotation. " +
211
+ "Either add '" + uname + "' to the schema's sealedFields, or pass it " +
212
+ "via opts.infraColumns if it's intentionally unsealed in the framework's tables.",
213
+ });
214
+ }
215
+ }
216
+ }
217
+ }
218
+
219
+ return { warnings: warnings, errors: errors };
220
+ }
221
+
222
+ function formatValidationResult(result) {
223
+ var lines = [];
224
+ if (result.warnings.length === 0 && result.errors.length === 0) {
225
+ return "[vault-rotate] schema match: OK";
226
+ }
227
+ if (result.warnings.length > 0) {
228
+ lines.push("[vault-rotate] schema warnings (" + result.warnings.length + ", non-fatal):");
229
+ for (var w = 0; w < result.warnings.length; w++) lines.push(" - " + result.warnings[w].message);
230
+ }
231
+ if (result.errors.length > 0) {
232
+ lines.push("[vault-rotate] schema errors (" + result.errors.length + ", FATAL — rotation refused):");
233
+ for (var e = 0; e < result.errors.length; e++) lines.push(" - " + result.errors[e].message);
234
+ }
235
+ return lines.join("\n");
236
+ }
237
+
238
+ // verify — sample sealed columns, decrypt with `keys`, report results.
239
+ //
240
+ // When opts.oldKeys is supplied, also flag rows whose sampled values
241
+ // STILL decrypt under oldKeys — that's a regression: rotation didn't
242
+ // take effect for those rows.
243
+ function verify(opts) {
244
+ opts = opts || {};
245
+ if (!opts.keys) {
246
+ throw new VaultRotateError("vault-rotate/no-keys",
247
+ "verify: opts.keys is required (the keypair to decrypt with)");
248
+ }
249
+ if (!opts.db || typeof opts.db.prepare !== "function") {
250
+ throw new VaultRotateError("vault-rotate/no-db",
251
+ "verify: opts.db is required (a node:sqlite handle)");
252
+ }
253
+ var keys = opts.keys;
254
+ var db = opts.db;
255
+ var oldKeys = opts.oldKeys || null;
256
+ nb.requirePositiveFiniteIntIfPresent(opts.sampleMin,
257
+ "verify: sampleMin", VaultRotateError, "vault-rotate/bad-opt");
258
+ var sampleMin = opts.sampleMin !== undefined
259
+ ? opts.sampleMin : DEFAULT_VERIFY_SAMPLE_MIN;
260
+ if (opts.samplePercent !== undefined &&
261
+ (typeof opts.samplePercent !== "number" || !Number.isFinite(opts.samplePercent) ||
262
+ opts.samplePercent <= 0)) {
263
+ throw new VaultRotateError("vault-rotate/bad-opt",
264
+ "verify: samplePercent must be a positive finite fraction; got " +
265
+ nb.shape(opts.samplePercent));
266
+ }
267
+ var samplePct = opts.samplePercent !== undefined
268
+ ? opts.samplePercent : DEFAULT_VERIFY_SAMPLE_FRAC;
269
+ var tablesArg = Array.isArray(opts.tables) && opts.tables.length > 0
270
+ ? opts.tables.slice() : null;
271
+
272
+ var passed = [];
273
+ var failures = [];
274
+ var regressions = [];
275
+
276
+ var liveTables = _listLiveTables(db);
277
+ var liveTableSet = Object.create(null);
278
+ for (var lt = 0; lt < liveTables.length; lt++) liveTableSet[liveTables[lt]] = true;
279
+ var tables = tablesArg || liveTables;
280
+
281
+ for (var ti = 0; ti < tables.length; ti++) {
282
+ var table = tables[ti];
283
+ if (!liveTableSet[table]) continue;
284
+ var schema = cryptoField.getSchema(table);
285
+ if (!schema || !Array.isArray(schema.sealedFields) || schema.sealedFields.length === 0) continue;
286
+
287
+ var totalRow = db.prepare('SELECT COUNT(*) AS n FROM "' + table.replace(/"/g, '""') + '"').get();
288
+ var total = totalRow ? totalRow.n : 0;
289
+ if (total === 0) continue;
290
+
291
+ var sampleN = Math.max(sampleMin, Math.ceil(total * samplePct));
292
+ if (sampleN > total) sampleN = total;
293
+
294
+ // RANDOM() is fine for a sampler — we're picking representative rows,
295
+ // not building cryptographic randomness.
296
+ var sampled = db.prepare(
297
+ 'SELECT * FROM "' + table.replace(/"/g, '""') + '" ORDER BY RANDOM() LIMIT ?'
298
+ ).all(sampleN);
299
+
300
+ var foundOldFail = !oldKeys; // when no oldKeys supplied, this check is N/A
301
+ var verifiedRows = 0;
302
+
303
+ for (var r = 0; r < sampled.length; r++) {
304
+ var row = sampled[r];
305
+ var rowFailed = false;
306
+
307
+ for (var sf = 0; sf < schema.sealedFields.length; sf++) {
308
+ var col = schema.sealedFields[sf];
309
+ var v = row[col];
310
+ if (typeof v !== "string" || v.indexOf(VAULT_PREFIX) !== 0) continue;
311
+ var payload = v.substring(VAULT_PREFIX.length);
312
+
313
+ try { cryptoLib.decrypt(payload, keys); }
314
+ catch (e) {
315
+ rowFailed = true;
316
+ failures.push({
317
+ table: table,
318
+ column: col,
319
+ _id: row._id,
320
+ error: (e && e.message) || String(e),
321
+ });
322
+ }
323
+
324
+ if (oldKeys && !foundOldFail) {
325
+ try {
326
+ cryptoLib.decrypt(payload, oldKeys);
327
+ regressions.push({
328
+ table: table,
329
+ column: col,
330
+ _id: row._id,
331
+ error: "old keys still decrypt this value — rotation did not take effect",
332
+ });
333
+ } catch (_e) {
334
+ foundOldFail = true; // at least one row no longer decrypts under old keys → rotation effective
335
+ }
336
+ }
337
+ }
338
+
339
+ if (!rowFailed) verifiedRows++;
340
+ }
341
+
342
+ passed.push({ table: table, sampled: sampled.length, verified: verifiedRows });
343
+ }
344
+
345
+ return {
346
+ ok: failures.length === 0 && regressions.length === 0,
347
+ passed: passed,
348
+ failures: failures,
349
+ regressions: regressions,
350
+ };
351
+ }
352
+
353
+ // =====================================================================
354
+ // Rotation pipeline
355
+ //
356
+ // Produces a fully-rotated copy of dataDir at stagingDir. Caller does
357
+ // the atomic swap (rename or symlink flip) after verifying staging.
358
+ // dataDir is read-only throughout; failure leaves dataDir untouched.
359
+ //
360
+ // File layout the rotator knows about (override via opts.paths):
361
+ //
362
+ // <dataDir>/db.enc — at-rest encrypted SQLite DB (XChaCha20-
363
+ // Poly1305 with a 32-byte dbKey)
364
+ // <dataDir>/db.key.enc — vault-sealed base64(dbKey)
365
+ // <dataDir>/vault.key — plaintext JSON keypair (plaintext mode)
366
+ // <dataDir>/vault.key.sealed — passphrase-wrapped keypair (wrapped mode)
367
+ //
368
+ // dbKey is REUSED across rotation — it isn't a vault key, just an
369
+ // XChaCha20 key, and the surface protecting it is the vault seal of
370
+ // db.key.enc. We re-seal db.key.enc under newKeys, but db.enc itself
371
+ // only needs to be rewritten because the SEALED VALUES INSIDE it
372
+ // changed (each row's email/etc. column). The packed envelope around
373
+ // the SQLite bytes uses the same dbKey old → new.
374
+ // =====================================================================
375
+
376
+ // Row count, not a byte quantity — hex form keeps the literal out of the
377
+ // byte-shape detector while preserving the operator-readable magnitude.
378
+ var ROW_BATCH_SIZE_DEFAULT = 0x3E8;
379
+ var VAULT_PREFIX_LEN = C.VAULT_PREFIX.length;
380
+
381
+ function _emit(cb, ev) {
382
+ if (typeof cb === "function") {
383
+ try { cb(ev); } catch (_e) { /* progress-callback errors are non-fatal */ }
384
+ }
385
+ }
386
+
387
+ // Open a file for fsync. Different from atomicFile.fsync (which takes
388
+ // an already-open fd) — vault-rotate's fsync-by-path semantic opens
389
+ // then syncs then closes, which is the right shape when we don't have
390
+ // the original write fd around.
391
+ function _fsyncFileByPath(p) {
392
+ try {
393
+ var fd = fs.openSync(p, "r+");
394
+ try { fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
395
+ } catch (_e) { /* best-effort across platforms */ }
396
+ }
397
+
398
+ function _reSealValue(sealedValue, oldKeys, newKeys) {
399
+ if (typeof sealedValue !== "string") return sealedValue;
400
+ if (sealedValue.indexOf(C.VAULT_PREFIX) !== 0) return sealedValue;
401
+ var payload = sealedValue.substring(VAULT_PREFIX_LEN);
402
+ var plain = cryptoLib.decrypt(payload, oldKeys);
403
+ return C.VAULT_PREFIX + cryptoLib.encrypt(plain, newKeys);
404
+ }
405
+
406
+ // Walk a JSON-decoded value, re-sealing every vault-prefixed string.
407
+ // Returns { value, changed } so the caller knows whether to write back.
408
+ function _walkAndReSeal(node, oldKeys, newKeys) {
409
+ if (typeof node === "string") {
410
+ if (node.indexOf(C.VAULT_PREFIX) !== 0) return { value: node, changed: false };
411
+ return { value: _reSealValue(node, oldKeys, newKeys), changed: true };
412
+ }
413
+ if (Array.isArray(node)) {
414
+ var out = new Array(node.length);
415
+ var any = false;
416
+ for (var i = 0; i < node.length; i++) {
417
+ var r = _walkAndReSeal(node[i], oldKeys, newKeys);
418
+ out[i] = r.value;
419
+ if (r.changed) any = true;
420
+ }
421
+ return { value: out, changed: any };
422
+ }
423
+ if (node && typeof node === "object") {
424
+ var ob = {};
425
+ var c = false;
426
+ for (var k in node) {
427
+ if (!Object.prototype.hasOwnProperty.call(node, k)) continue;
428
+ var rv = _walkAndReSeal(node[k], oldKeys, newKeys);
429
+ ob[k] = rv.value;
430
+ if (rv.changed) c = true;
431
+ }
432
+ return { value: ob, changed: c };
433
+ }
434
+ return { value: node, changed: false };
435
+ }
436
+
437
+ function _runStmt(db, sql) { db.prepare(sql).run(); }
438
+
439
+ function _rotateColumn(db, table, column, oldKeys, newKeys, batchSize, progress) {
440
+ var qt = '"' + table.replace(/"/g, '""') + '"';
441
+ var qc = '"' + column.replace(/"/g, '""') + '"';
442
+ var total = db.prepare("SELECT COUNT(*) AS n FROM " + qt + " WHERE " + qc + " IS NOT NULL").get().n;
443
+ if (total === 0) return 0;
444
+
445
+ var sel = db.prepare(
446
+ "SELECT _id, " + qc + " AS v FROM " + qt +
447
+ " WHERE " + qc + " IS NOT NULL AND _id > ? ORDER BY _id LIMIT ?"
448
+ );
449
+ var upd = db.prepare("UPDATE " + qt + " SET " + qc + " = ? WHERE _id = ?");
450
+
451
+ var processed = 0;
452
+ var lastId = "";
453
+ while (true) {
454
+ var rows = sel.all(lastId, batchSize);
455
+ if (rows.length === 0) break;
456
+
457
+ dbSchema.runInTransaction(db, function () {
458
+ for (var i = 0; i < rows.length; i++) {
459
+ var row = rows[i];
460
+ if (typeof row.v === "string" && row.v.indexOf(C.VAULT_PREFIX) === 0) {
461
+ upd.run(_reSealValue(row.v, oldKeys, newKeys), row._id);
462
+ }
463
+ }
464
+ });
465
+ processed += rows.length;
466
+ lastId = rows[rows.length - 1]._id;
467
+ _emit(progress, { phase: "rotate_rows", table: table, column: column, rowsProcessed: processed, rowsTotal: total });
468
+ }
469
+ return processed;
470
+ }
471
+
472
+ function _rotateOverflow(db, table, oldKeys, newKeys, batchSize, progress, warnings) {
473
+ var qt = '"' + table.replace(/"/g, '""') + '"';
474
+ var cols = db.prepare("PRAGMA table_info(" + qt + ")").all();
475
+ if (!cols.some(function (c) { return c.name === "data"; })) return 0;
476
+
477
+ var total = db.prepare("SELECT COUNT(*) AS n FROM " + qt + " WHERE data IS NOT NULL").get().n;
478
+ if (total === 0) return 0;
479
+
480
+ var sel = db.prepare(
481
+ "SELECT _id, data FROM " + qt +
482
+ " WHERE data IS NOT NULL AND _id > ? ORDER BY _id LIMIT ?"
483
+ );
484
+ var upd = db.prepare("UPDATE " + qt + " SET data = ? WHERE _id = ?");
485
+
486
+ var processed = 0;
487
+ var lastId = "";
488
+ while (true) {
489
+ var rows = sel.all(lastId, batchSize);
490
+ if (rows.length === 0) break;
491
+
492
+ _runStmt(db, "BEGIN");
493
+ try {
494
+ for (var i = 0; i < rows.length; i++) {
495
+ var row = rows[i];
496
+ var doc;
497
+ // Vault overflow rows are framework-written but read at rotate
498
+ // time — gate the parse via safeJson so a corrupted row can't
499
+ // stage a parse-bomb against the rotation worker.
500
+ try { doc = safeJson.parse(row.data, { maxBytes: C.BYTES.mib(16) }); }
501
+ catch (_e) {
502
+ warnings.push("malformed overflow JSON at " + table + "._id=" + row._id + " — left unrotated");
503
+ continue;
504
+ }
505
+ var rv = _walkAndReSeal(doc, oldKeys, newKeys);
506
+ if (rv.changed) upd.run(JSON.stringify(rv.value), row._id);
507
+ }
508
+ _runStmt(db, "COMMIT");
509
+ } catch (e) {
510
+ _runStmt(db, "ROLLBACK");
511
+ throw e;
512
+ }
513
+ processed += rows.length;
514
+ lastId = rows[rows.length - 1]._id;
515
+ _emit(progress, { phase: "rotate_overflow", table: table, rowsProcessed: processed, rowsTotal: total });
516
+ }
517
+ return processed;
518
+ }
519
+
520
+ async function rotate(opts) {
521
+ opts = opts || {};
522
+ var startedAt = Date.now();
523
+ var oldKeys = opts.oldKeys;
524
+ var newKeys = opts.newKeys;
525
+ if (!oldKeys || !newKeys) {
526
+ throw new VaultRotateError("vault-rotate/no-keys",
527
+ "rotate: opts.oldKeys and opts.newKeys are required");
528
+ }
529
+ if (typeof opts.dataDir !== "string" || !fs.existsSync(opts.dataDir)) {
530
+ throw new VaultRotateError("vault-rotate/no-datadir",
531
+ "rotate: opts.dataDir is required and must exist");
532
+ }
533
+ validateOpts.requireNonEmptyString(opts.stagingDir, "rotate: opts.stagingDir", VaultRotateError, "vault-rotate/no-staging");
534
+ if (fs.existsSync(opts.stagingDir)) {
535
+ throw new VaultRotateError("vault-rotate/staging-exists",
536
+ "rotate: stagingDir already exists: " + opts.stagingDir);
537
+ }
538
+ var mode = opts.mode || "plaintext";
539
+ if (mode !== "plaintext" && mode !== "wrapped") {
540
+ throw new VaultRotateError("vault-rotate/bad-mode",
541
+ "rotate: opts.mode must be 'plaintext' or 'wrapped'");
542
+ }
543
+ if (mode === "wrapped" && !Buffer.isBuffer(opts.newPassphrase)) {
544
+ throw new VaultRotateError("vault-rotate/no-passphrase",
545
+ "rotate: wrapped mode requires opts.newPassphrase (Buffer)");
546
+ }
547
+ var rowBatchSize = opts.rowBatchSize || ROW_BATCH_SIZE_DEFAULT;
548
+ var progress = opts.progressCallback;
549
+ var warnings = [];
550
+ var paths = Object.assign({
551
+ encryptedDb: "db.enc",
552
+ dbKeySealed: "db.key.enc",
553
+ vaultKeyPlain: "vault.key",
554
+ vaultKeySealed: "vault.key.sealed",
555
+ additionalSealed: [],
556
+ verbatimFiles: [],
557
+ verbatimDirs: [],
558
+ }, opts.paths || {});
559
+
560
+ var dataDir = opts.dataDir;
561
+ var stagingDir = opts.stagingDir;
562
+
563
+ _emit(progress, { phase: "init" });
564
+ atomicFile.ensureDir(stagingDir);
565
+
566
+ // 1. verbatim files + dirs
567
+ _emit(progress, { phase: "copy_verbatim" });
568
+ for (var vf = 0; vf < paths.verbatimFiles.length; vf++) {
569
+ var entry = paths.verbatimFiles[vf];
570
+ var src = path.join(dataDir, entry.relativePath);
571
+ if (!fs.existsSync(src)) {
572
+ if (entry.required) {
573
+ throw new VaultRotateError("vault-rotate/missing-verbatim",
574
+ "rotate: required verbatim file missing: " + entry.relativePath);
575
+ }
576
+ continue;
577
+ }
578
+ var dest = path.join(stagingDir, entry.relativePath);
579
+ atomicFile.ensureDir(path.dirname(dest));
580
+ fs.copyFileSync(src, dest);
581
+ }
582
+ for (var vd = 0; vd < paths.verbatimDirs.length; vd++) {
583
+ var dent = paths.verbatimDirs[vd];
584
+ var sdir = path.join(dataDir, dent.relativePath);
585
+ if (!fs.existsSync(sdir)) {
586
+ if (dent.required) {
587
+ throw new VaultRotateError("vault-rotate/missing-verbatim-dir",
588
+ "rotate: required verbatim dir missing: " + dent.relativePath);
589
+ }
590
+ continue;
591
+ }
592
+ if (fs.existsSync(sdir)) {
593
+ atomicFile.copyDirRecursive(sdir, path.join(stagingDir, dent.relativePath));
594
+ }
595
+ }
596
+
597
+ // 2. write new vault key
598
+ _emit(progress, { phase: "write_vault_key" });
599
+ var keysJson = JSON.stringify(newKeys, null, 2);
600
+ if (mode === "wrapped") {
601
+ var sealed = await vaultWrap().wrap(keysJson, opts.newPassphrase);
602
+ fs.writeFileSync(path.join(stagingDir, paths.vaultKeySealed), sealed, { mode: 0o600 });
603
+ } else {
604
+ fs.writeFileSync(path.join(stagingDir, paths.vaultKeyPlain), keysJson, { mode: 0o600 });
605
+ }
606
+
607
+ // 3. re-seal db.key.enc + any operator-supplied additionalSealed files
608
+ _emit(progress, { phase: "reseal_files" });
609
+ var dbKeySealedPath = path.join(dataDir, paths.dbKeySealed);
610
+ var dbKey = null;
611
+ if (fs.existsSync(dbKeySealedPath)) {
612
+ var sealedKey = fs.readFileSync(dbKeySealedPath, "utf8").trim();
613
+ if (sealedKey.indexOf(C.VAULT_PREFIX) !== 0) {
614
+ throw new VaultRotateError("vault-rotate/bad-dbkey",
615
+ "rotate: db.key.enc does not start with the vault prefix");
616
+ }
617
+ var dbKeyB64 = cryptoLib.decrypt(sealedKey.substring(VAULT_PREFIX_LEN), oldKeys);
618
+ dbKey = Buffer.from(dbKeyB64, "base64");
619
+ var resealedKey = C.VAULT_PREFIX + cryptoLib.encrypt(dbKeyB64, newKeys);
620
+ fs.writeFileSync(path.join(stagingDir, paths.dbKeySealed), resealedKey, { mode: 0o600 });
621
+ }
622
+ for (var as = 0; as < paths.additionalSealed.length; as++) {
623
+ var ase = paths.additionalSealed[as];
624
+ var asSrc = path.join(dataDir, ase.relativePath);
625
+ if (!fs.existsSync(asSrc)) {
626
+ if (ase.required) {
627
+ throw new VaultRotateError("vault-rotate/missing-sealed",
628
+ "rotate: required sealed file missing: " + ase.relativePath);
629
+ }
630
+ continue;
631
+ }
632
+ var current = fs.readFileSync(asSrc, "utf8").trim();
633
+ if (current.indexOf(C.VAULT_PREFIX) !== 0) {
634
+ throw new VaultRotateError("vault-rotate/bad-sealed",
635
+ "rotate: sealed file does not start with the vault prefix: " + ase.relativePath);
636
+ }
637
+ var asDestDir = path.join(stagingDir, path.dirname(ase.relativePath));
638
+ if (!fs.existsSync(asDestDir)) atomicFile.ensureDir(asDestDir);
639
+ fs.writeFileSync(path.join(stagingDir, ase.relativePath),
640
+ _reSealValue(current, oldKeys, newKeys), { mode: 0o600 });
641
+ }
642
+
643
+ // 4. decrypt + rotate + re-encrypt db.enc
644
+ _emit(progress, { phase: "rotate_db" });
645
+ var encDbPath = path.join(dataDir, paths.encryptedDb);
646
+ var tablesProcessed = 0;
647
+ var totalRowsProcessed = 0;
648
+ var verifyResult = null;
649
+
650
+ if (fs.existsSync(encDbPath) && dbKey) {
651
+ var packed = fs.readFileSync(encDbPath);
652
+ var plainBytes = cryptoLib.decryptPacked(packed, dbKey);
653
+ var tmpDbPath = path.join(stagingDir, "_blamejs_rotate.tmp.db");
654
+ fs.writeFileSync(tmpDbPath, plainBytes, { mode: 0o600 });
655
+
656
+ var db = new DatabaseSync(tmpDbPath);
657
+ try {
658
+ _runStmt(db, "PRAGMA journal_mode=WAL");
659
+ _runStmt(db, "PRAGMA synchronous=NORMAL");
660
+
661
+ // Walk tables. For each, re-seal every column declared sealed
662
+ // by the field-crypto registry, plus the overflow `data` JSON
663
+ // column if present.
664
+ var tablesToRotate = Array.isArray(opts.tables) && opts.tables.length > 0
665
+ ? opts.tables.slice()
666
+ : db.prepare(
667
+ "SELECT name FROM sqlite_master " +
668
+ "WHERE type='table' AND name NOT LIKE 'sqlite_%'"
669
+ ).all().map(function (r) { return r.name; });
670
+
671
+ for (var ti = 0; ti < tablesToRotate.length; ti++) {
672
+ var table = tablesToRotate[ti];
673
+ var tableExists = db.prepare(
674
+ "SELECT name FROM sqlite_master WHERE type='table' AND name = ?"
675
+ ).get(table);
676
+ if (!tableExists) continue;
677
+
678
+ var schema = cryptoField.getSchema(table);
679
+ var liveCols = db.prepare(
680
+ 'PRAGMA table_info("' + table.replace(/"/g, '""') + '")'
681
+ ).all().map(function (c) { return c.name; });
682
+ var liveColSet = Object.create(null);
683
+ for (var lc = 0; lc < liveCols.length; lc++) liveColSet[liveCols[lc]] = true;
684
+
685
+ var tableRows = 0;
686
+ if (schema && Array.isArray(schema.sealedFields)) {
687
+ for (var sc = 0; sc < schema.sealedFields.length; sc++) {
688
+ var col = schema.sealedFields[sc];
689
+ if (!liveColSet[col]) continue;
690
+ tableRows += _rotateColumn(db, table, col, oldKeys, newKeys, rowBatchSize, progress);
691
+ }
692
+ }
693
+ tableRows += _rotateOverflow(db, table, oldKeys, newKeys, rowBatchSize, progress, warnings);
694
+
695
+ if (tableRows > 0) { tablesProcessed++; totalRowsProcessed += tableRows; }
696
+ }
697
+
698
+ _runStmt(db, "PRAGMA wal_checkpoint(TRUNCATE)");
699
+ } finally {
700
+ db.close();
701
+ }
702
+
703
+ // Drop WAL/SHM sidecars before re-encrypting the .db file. Either
704
+ // sidecar may be absent (depending on whether journal_mode produced
705
+ // one for this run); log at debug so the cleanup attempt isn't
706
+ // silently swallowed when something genuinely unexpected fails.
707
+ try { fs.unlinkSync(tmpDbPath + "-wal"); }
708
+ catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: tmpDbPath + "-wal", error: e.message }); }
709
+ try { fs.unlinkSync(tmpDbPath + "-shm"); }
710
+ catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: tmpDbPath + "-shm", error: e.message }); }
711
+
712
+ var rotatedBytes = fs.readFileSync(tmpDbPath);
713
+ fs.writeFileSync(path.join(stagingDir, paths.encryptedDb),
714
+ cryptoLib.encryptPacked(rotatedBytes, dbKey));
715
+ fs.unlinkSync(tmpDbPath);
716
+
717
+ // Round-trip verify on the staged DB
718
+ _emit(progress, { phase: "verify" });
719
+ var verifyTmp = path.join(stagingDir, "_blamejs_verify.tmp.db");
720
+ fs.writeFileSync(verifyTmp,
721
+ cryptoLib.decryptPacked(fs.readFileSync(path.join(stagingDir, paths.encryptedDb)), dbKey));
722
+ var vdb = new DatabaseSync(verifyTmp);
723
+ try {
724
+ verifyResult = verify({ keys: newKeys, db: vdb, oldKeys: oldKeys });
725
+ } finally {
726
+ vdb.close();
727
+ try { fs.unlinkSync(verifyTmp); }
728
+ catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: verifyTmp, error: e.message }); }
729
+ try { fs.unlinkSync(verifyTmp + "-wal"); }
730
+ catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: verifyTmp + "-wal", error: e.message }); }
731
+ try { fs.unlinkSync(verifyTmp + "-shm"); }
732
+ catch (e) { rotateLog.debug("cleanup-failed", { op: "fs.unlinkSync", path: verifyTmp + "-shm", error: e.message }); }
733
+ }
734
+ if (!verifyResult.ok) {
735
+ throw new VaultRotateError("vault-rotate/verify-failed",
736
+ "round-trip verification failed: " +
737
+ verifyResult.failures.length + " decrypt failure(s), " +
738
+ verifyResult.regressions.length + " non-rotated row(s). " +
739
+ "First issue: " + JSON.stringify(verifyResult.failures[0] || verifyResult.regressions[0]));
740
+ }
741
+ }
742
+
743
+ // 5. fsync staging for durability before caller does the swap
744
+ _emit(progress, { phase: "fsync" });
745
+ function fsyncTree(dir) {
746
+ var entries = fs.readdirSync(dir);
747
+ for (var i = 0; i < entries.length; i++) {
748
+ var p = path.join(dir, entries[i]);
749
+ var st = fs.statSync(p);
750
+ if (st.isFile()) _fsyncFileByPath(p);
751
+ else if (st.isDirectory()) fsyncTree(p);
752
+ }
753
+ atomicFile.fsyncDir(dir);
754
+ }
755
+ fsyncTree(stagingDir);
756
+
757
+ var durationMs = Date.now() - startedAt;
758
+ _emit(progress, {
759
+ phase: "done",
760
+ durationMs: durationMs,
761
+ tablesProcessed: tablesProcessed,
762
+ totalRowsProcessed: totalRowsProcessed,
763
+ });
764
+ return {
765
+ durationMs: durationMs,
766
+ tablesProcessed: tablesProcessed,
767
+ totalRowsProcessed: totalRowsProcessed,
768
+ verifyResult: verifyResult,
769
+ warnings: warnings,
770
+ };
771
+ }
772
+
773
+ module.exports = {
774
+ validateSchemaMatch: validateSchemaMatch,
775
+ formatValidationResult: formatValidationResult,
776
+ verify: verify,
777
+ rotate: rotate,
778
+ VaultRotateError: VaultRotateError,
779
+ // Constants exposed so operators / tests can reference the same defaults
780
+ DEFAULT_DRIFT_SAMPLE_LIMIT: DEFAULT_DRIFT_SAMPLE_LIMIT,
781
+ DEFAULT_VERIFY_SAMPLE_MIN: DEFAULT_VERIFY_SAMPLE_MIN,
782
+ DEFAULT_VERIFY_SAMPLE_FRAC: DEFAULT_VERIFY_SAMPLE_FRAC,
783
+ ROW_BATCH_SIZE_DEFAULT: ROW_BATCH_SIZE_DEFAULT,
784
+ };