@blamejs/core 0.14.26 → 0.15.0
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.
- package/CHANGELOG.md +6 -0
- package/README.md +2 -2
- package/index.js +4 -0
- package/lib/agent-envelope-mac.js +104 -0
- package/lib/agent-event-bus.js +105 -4
- package/lib/agent-posture-chain.js +8 -42
- package/lib/ai-content-detect.js +9 -10
- package/lib/api-key.js +107 -74
- package/lib/atomic-file.js +62 -4
- package/lib/audit-chain.js +47 -11
- package/lib/audit-sign.js +77 -2
- package/lib/audit-tools.js +79 -51
- package/lib/audit.js +249 -123
- package/lib/auth/openid-federation.js +108 -47
- package/lib/backup/index.js +13 -10
- package/lib/break-glass.js +202 -144
- package/lib/cache.js +174 -105
- package/lib/chain-writer.js +38 -16
- package/lib/cli.js +19 -14
- package/lib/cluster-provider-db.js +130 -104
- package/lib/cluster-storage.js +119 -22
- package/lib/cluster.js +119 -71
- package/lib/compliance.js +169 -4
- package/lib/consent.js +73 -24
- package/lib/constants.js +16 -11
- package/lib/crypto-field.js +474 -92
- package/lib/db-declare-row-policy.js +35 -22
- package/lib/db-file-lifecycle.js +3 -2
- package/lib/db-query.js +497 -255
- package/lib/db-schema.js +209 -44
- package/lib/db.js +176 -95
- package/lib/error-page.js +14 -1
- package/lib/external-db-migrate.js +229 -139
- package/lib/external-db.js +25 -15
- package/lib/file-upload.js +52 -7
- package/lib/framework-error.js +14 -1
- package/lib/framework-files.js +73 -0
- package/lib/framework-schema.js +695 -394
- package/lib/gate-contract.js +649 -1
- package/lib/guard-agent-registry.js +26 -44
- package/lib/guard-all.js +1 -0
- package/lib/guard-auth.js +42 -112
- package/lib/guard-cidr.js +33 -154
- package/lib/guard-csv.js +46 -113
- package/lib/guard-domain.js +34 -157
- package/lib/guard-dsn.js +27 -43
- package/lib/guard-email.js +47 -69
- package/lib/guard-envelope.js +19 -32
- package/lib/guard-event-bus-payload.js +24 -42
- package/lib/guard-event-bus-topic.js +25 -43
- package/lib/guard-filename.js +42 -106
- package/lib/guard-graphql.js +42 -123
- package/lib/guard-html.js +53 -108
- package/lib/guard-idempotency-key.js +24 -42
- package/lib/guard-image.js +46 -103
- package/lib/guard-imap-command.js +18 -32
- package/lib/guard-jmap.js +16 -30
- package/lib/guard-json.js +38 -108
- package/lib/guard-jsonpath.js +38 -171
- package/lib/guard-jwt.js +49 -179
- package/lib/guard-list-id.js +25 -41
- package/lib/guard-list-unsubscribe.js +27 -43
- package/lib/guard-mail-compose.js +24 -42
- package/lib/guard-mail-move.js +26 -44
- package/lib/guard-mail-query.js +28 -46
- package/lib/guard-mail-reply.js +24 -42
- package/lib/guard-mail-sieve.js +24 -42
- package/lib/guard-managesieve-command.js +17 -31
- package/lib/guard-markdown.js +37 -104
- package/lib/guard-message-id.js +26 -45
- package/lib/guard-mime.js +39 -151
- package/lib/guard-oauth.js +54 -135
- package/lib/guard-pdf.js +45 -101
- package/lib/guard-pop3-command.js +21 -31
- package/lib/guard-posture-chain.js +24 -42
- package/lib/guard-regex.js +33 -107
- package/lib/guard-saga-config.js +24 -42
- package/lib/guard-shell.js +42 -172
- package/lib/guard-smtp-command.js +48 -54
- package/lib/guard-snapshot-envelope.js +24 -42
- package/lib/guard-sql.js +1491 -0
- package/lib/guard-stream-args.js +24 -43
- package/lib/guard-svg.js +47 -65
- package/lib/guard-template.js +35 -172
- package/lib/guard-tenant-id.js +26 -45
- package/lib/guard-time.js +32 -154
- package/lib/guard-trace-context.js +25 -44
- package/lib/guard-uuid.js +32 -153
- package/lib/guard-xml.js +38 -113
- package/lib/guard-yaml.js +51 -163
- package/lib/http-client.js +37 -9
- package/lib/inbox.js +120 -107
- package/lib/legal-hold.js +107 -50
- package/lib/log-stream-cloudwatch.js +47 -31
- package/lib/log-stream-otlp.js +32 -18
- package/lib/mail-crypto-smime.js +2 -6
- package/lib/mail-greylist.js +2 -6
- package/lib/mail-helo.js +2 -6
- package/lib/mail-journal.js +85 -64
- package/lib/mail-rbl.js +2 -6
- package/lib/mail-scan.js +2 -6
- package/lib/mail-server-jmap.js +117 -12
- package/lib/mail-spam-score.js +2 -6
- package/lib/mail-store.js +287 -154
- package/lib/middleware/body-parser.js +71 -25
- package/lib/middleware/csrf-protect.js +19 -8
- package/lib/middleware/fetch-metadata.js +17 -7
- package/lib/middleware/idempotency-key.js +54 -38
- package/lib/middleware/rate-limit.js +102 -32
- package/lib/middleware/security-headers.js +21 -5
- package/lib/migrations.js +108 -66
- package/lib/network-heartbeat.js +7 -0
- package/lib/nonce-store.js +31 -9
- package/lib/object-store/azure-blob-bucket-ops.js +9 -4
- package/lib/object-store/azure-blob.js +57 -3
- package/lib/object-store/sigv4.js +10 -0
- package/lib/observability.js +87 -0
- package/lib/otel-export.js +25 -1
- package/lib/outbox.js +136 -82
- package/lib/parsers/safe-xml.js +47 -7
- package/lib/pqc-agent.js +44 -0
- package/lib/pubsub-cluster.js +42 -20
- package/lib/queue-local.js +202 -139
- package/lib/queue-redis.js +9 -1
- package/lib/queue-sqs.js +6 -0
- package/lib/redact.js +68 -11
- package/lib/redis-client.js +160 -31
- package/lib/retention.js +82 -39
- package/lib/router.js +212 -5
- package/lib/safe-dns.js +29 -45
- package/lib/safe-ical.js +18 -33
- package/lib/safe-icap.js +27 -43
- package/lib/safe-sieve.js +21 -40
- package/lib/safe-sql.js +124 -3
- package/lib/safe-vcard.js +18 -33
- package/lib/scheduler.js +35 -12
- package/lib/seeders.js +122 -74
- package/lib/session-stores.js +42 -14
- package/lib/session.js +109 -72
- package/lib/sql.js +3885 -0
- package/lib/ssrf-guard.js +51 -4
- package/lib/static.js +177 -34
- package/lib/subject.js +55 -17
- package/lib/vault/index.js +3 -2
- package/lib/vault/passphrase-ops.js +3 -2
- package/lib/vault/rotate.js +104 -64
- package/lib/vendor-data.js +2 -0
- package/lib/websocket.js +35 -5
- package/package.json +1 -1
- package/sbom.cdx.json +6 -6
package/lib/audit.js
CHANGED
|
@@ -55,7 +55,9 @@ var cluster = require("./cluster");
|
|
|
55
55
|
var clusterStorage = require("./cluster-storage");
|
|
56
56
|
var { generateToken } = require("./crypto");
|
|
57
57
|
var cryptoField = require("./crypto-field");
|
|
58
|
+
var frameworkSchema = require("./framework-schema");
|
|
58
59
|
var safeSql = require("./safe-sql");
|
|
60
|
+
var sql = require("./sql");
|
|
59
61
|
var dbRoleContext = require("./db-role-context");
|
|
60
62
|
var handlers = require("./handlers");
|
|
61
63
|
var { boot } = require("./log");
|
|
@@ -101,19 +103,36 @@ var _externalStore = null;
|
|
|
101
103
|
// the worst case.
|
|
102
104
|
var FRAMEWORK_SQL_TIMEOUT_MS = C.TIME.seconds(30);
|
|
103
105
|
|
|
106
|
+
// b.sql opts for every framework-table statement: thread the ACTIVE backend
|
|
107
|
+
// dialect (clusterStorage.dialect() — "sqlite" single-node, "postgres" |
|
|
108
|
+
// "mysql" in cluster mode) so the emitted identifier quoting + dialect
|
|
109
|
+
// idioms (ON CONFLICT vs ON DUPLICATE KEY) match the backend the SQL
|
|
110
|
+
// dispatches to. Defaulting to "sqlite" works on Postgres only by accident
|
|
111
|
+
// (both double-quote identifiers) and emits the wrong quoting on MySQL.
|
|
112
|
+
// clusterStorage.execute still rewrites table names + translates `?`
|
|
113
|
+
// placeholders at dispatch; this controls only the builder-side quoting +
|
|
114
|
+
// idiom selection.
|
|
115
|
+
function _sqlOpts() { return { dialect: clusterStorage.dialect() }; }
|
|
116
|
+
|
|
104
117
|
// ---- Resilience-wrapped SQL operations (audit-specific reads) ----
|
|
105
118
|
// Chain APPEND lives in chain-writer (race-safe via mutex, retry, timeout).
|
|
106
119
|
// The wrappers below cover audit-specific reads/writes that aren't part
|
|
107
120
|
// of the chain append: checkpoint queries, verifyCheckpoints reads,
|
|
108
121
|
// audit-tip cluster-row updates.
|
|
109
122
|
|
|
123
|
+
// Framework-state reads compose b.sql with BARE logical table names —
|
|
124
|
+
// clusterStorage rewrites the framework names to their configured-prefix
|
|
125
|
+
// form and placeholderizes per dialect; b.sql quotes the camelCase columns
|
|
126
|
+
// and runs the output validator.
|
|
110
127
|
async function _readLastCheckpointCounter() {
|
|
128
|
+
var built = sql.select("audit_checkpoints", _sqlOpts())
|
|
129
|
+
.columns(["atMonotonicCounter"])
|
|
130
|
+
.orderBy("atMonotonicCounter", "desc")
|
|
131
|
+
.limit(1)
|
|
132
|
+
.toSql();
|
|
111
133
|
return await safeAsync.withTimeout(
|
|
112
134
|
safeAsync.asyncRetry(function () {
|
|
113
|
-
return clusterStorage.executeOne(
|
|
114
|
-
"SELECT atMonotonicCounter FROM audit_checkpoints " +
|
|
115
|
-
"ORDER BY atMonotonicCounter DESC LIMIT 1"
|
|
116
|
-
);
|
|
135
|
+
return clusterStorage.executeOne(built.sql, built.params);
|
|
117
136
|
}),
|
|
118
137
|
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
119
138
|
{ name: "audit.readLastCheckpoint" }
|
|
@@ -121,11 +140,12 @@ async function _readLastCheckpointCounter() {
|
|
|
121
140
|
}
|
|
122
141
|
|
|
123
142
|
async function _readAllAuditRowsAsc() {
|
|
143
|
+
var built = sql.select("audit_log", _sqlOpts())
|
|
144
|
+
.orderBy("monotonicCounter", "asc")
|
|
145
|
+
.toSql();
|
|
124
146
|
return await safeAsync.withTimeout(
|
|
125
147
|
safeAsync.asyncRetry(function () {
|
|
126
|
-
return clusterStorage.executeAll(
|
|
127
|
-
'SELECT * FROM "audit_log" ORDER BY monotonicCounter ASC'
|
|
128
|
-
);
|
|
148
|
+
return clusterStorage.executeAll(built.sql, built.params);
|
|
129
149
|
}),
|
|
130
150
|
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
131
151
|
{ name: "audit.readAllRowsAsc" }
|
|
@@ -133,11 +153,12 @@ async function _readAllAuditRowsAsc() {
|
|
|
133
153
|
}
|
|
134
154
|
|
|
135
155
|
async function _readAllCheckpointsAsc() {
|
|
156
|
+
var built = sql.select("audit_checkpoints", _sqlOpts())
|
|
157
|
+
.orderBy("atMonotonicCounter", "asc")
|
|
158
|
+
.toSql();
|
|
136
159
|
return await safeAsync.withTimeout(
|
|
137
160
|
safeAsync.asyncRetry(function () {
|
|
138
|
-
return clusterStorage.executeAll(
|
|
139
|
-
"SELECT * FROM audit_checkpoints ORDER BY atMonotonicCounter ASC"
|
|
140
|
-
);
|
|
161
|
+
return clusterStorage.executeAll(built.sql, built.params);
|
|
141
162
|
}),
|
|
142
163
|
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
143
164
|
{ name: "audit.readAllCheckpoints" }
|
|
@@ -145,12 +166,13 @@ async function _readAllCheckpointsAsc() {
|
|
|
145
166
|
}
|
|
146
167
|
|
|
147
168
|
async function _readAuditRowHashAtCounter(counter) {
|
|
169
|
+
var built = sql.select("audit_log", _sqlOpts())
|
|
170
|
+
.columns(["rowHash"])
|
|
171
|
+
.where("monotonicCounter", counter)
|
|
172
|
+
.toSql();
|
|
148
173
|
return await safeAsync.withTimeout(
|
|
149
174
|
safeAsync.asyncRetry(function () {
|
|
150
|
-
return clusterStorage.executeOne(
|
|
151
|
-
"SELECT rowHash FROM audit_log WHERE monotonicCounter = ?",
|
|
152
|
-
[counter]
|
|
153
|
-
);
|
|
175
|
+
return clusterStorage.executeOne(built.sql, built.params);
|
|
154
176
|
}),
|
|
155
177
|
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
156
178
|
{ name: "audit.readRowHashAtCounter" }
|
|
@@ -158,26 +180,37 @@ async function _readAuditRowHashAtCounter(counter) {
|
|
|
158
180
|
}
|
|
159
181
|
|
|
160
182
|
async function _insertAuditRow(allCols, values) {
|
|
161
|
-
// No retry — non-idempotent. Timeout only.
|
|
162
|
-
|
|
163
|
-
|
|
183
|
+
// No retry — non-idempotent. Timeout only. Map each column to its
|
|
184
|
+
// positional value and bind as a row object (the unambiguous b.sql form;
|
|
185
|
+
// a flat value array whose first element is a Buffer would be misread as
|
|
186
|
+
// an array-of-rows). BARE logical table name → clusterStorage rewrites.
|
|
187
|
+
var rowObj = {};
|
|
188
|
+
for (var i = 0; i < allCols.length; i++) rowObj[allCols[i]] = values[i];
|
|
189
|
+
var built = sql.insert("audit_log", _sqlOpts())
|
|
190
|
+
.columns(allCols)
|
|
191
|
+
.values(rowObj)
|
|
192
|
+
.toSql();
|
|
164
193
|
return await safeAsync.withTimeout(
|
|
165
|
-
clusterStorage.execute(
|
|
166
|
-
"INSERT INTO audit_log (" + quoted + ") VALUES (" + placeholders + ")",
|
|
167
|
-
values
|
|
168
|
-
),
|
|
194
|
+
clusterStorage.execute(built.sql, built.params),
|
|
169
195
|
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
170
196
|
{ name: "audit.insertRow" }
|
|
171
197
|
);
|
|
172
198
|
}
|
|
173
199
|
|
|
200
|
+
var _CHECKPOINT_COLS = [
|
|
201
|
+
"_id", "createdAt", "atMonotonicCounter", "atRowHash",
|
|
202
|
+
"signature", "publicKeyFingerprint", "fencingToken",
|
|
203
|
+
];
|
|
204
|
+
|
|
174
205
|
async function _insertCheckpoint(values) {
|
|
206
|
+
var rowObj = {};
|
|
207
|
+
for (var i = 0; i < _CHECKPOINT_COLS.length; i++) rowObj[_CHECKPOINT_COLS[i]] = values[i];
|
|
208
|
+
var built = sql.insert("audit_checkpoints", _sqlOpts())
|
|
209
|
+
.columns(_CHECKPOINT_COLS)
|
|
210
|
+
.values(rowObj)
|
|
211
|
+
.toSql();
|
|
175
212
|
return await safeAsync.withTimeout(
|
|
176
|
-
clusterStorage.execute(
|
|
177
|
-
"INSERT INTO audit_checkpoints (_id, createdAt, atMonotonicCounter, atRowHash, signature, publicKeyFingerprint, fencingToken) " +
|
|
178
|
-
"VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
179
|
-
values
|
|
180
|
-
),
|
|
213
|
+
clusterStorage.execute(built.sql, built.params),
|
|
181
214
|
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
182
215
|
{ name: "audit.insertCheckpoint" }
|
|
183
216
|
);
|
|
@@ -198,24 +231,79 @@ async function _upsertAuditTip(counter, rowHash, signedAt, fencingToken) {
|
|
|
198
231
|
// as ClusterError(code='FENCED_OUT', permanent=true) so the
|
|
199
232
|
// dispatching node knows it's been superseded and should step down
|
|
200
233
|
// rather than retry.
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
)
|
|
218
|
-
|
|
234
|
+
// Single atomic INSERT … ON CONFLICT(scope) DO UPDATE … WHERE … RETURNING
|
|
235
|
+
// via b.sql. BARE logical table name (`_blamejs_audit_tip`) — clusterStorage
|
|
236
|
+
// rewrites it (and the same bare name inside the conflictWhere fence) to
|
|
237
|
+
// the configured prefix and placeholderizes. The fenced WHERE enforces the
|
|
238
|
+
// monotonic-non-decreasing fencingToken at the DB level; on rejection
|
|
239
|
+
// RETURNING produces 0 rows.
|
|
240
|
+
// The audit-tip is external-only; its LOGICAL name IS the
|
|
241
|
+
// `_blamejs_`-prefixed name (self-mapped in LOCAL_TO_EXTERNAL), passed
|
|
242
|
+
// bare to b.sql so clusterStorage rewrites it (and the same bare name
|
|
243
|
+
// inside the guarded fence) to the configured prefix.
|
|
244
|
+
//
|
|
245
|
+
// The fence `<table>.<fencingToken> <= EXCLUDED.<fencingToken>` references
|
|
246
|
+
// both the EXISTING row (table-qualified) and the PROPOSED row (EXCLUDED on
|
|
247
|
+
// Postgres/SQLite, VALUES() on MySQL ON DUPLICATE KEY UPDATE), and the
|
|
248
|
+
// identifier quoting differs per dialect — so the raw fragment is built
|
|
249
|
+
// dialect-aware: safeSql.quoteQualified for the table-qualified existing
|
|
250
|
+
// column (backticks on MySQL, double-quotes on PG/SQLite), and the
|
|
251
|
+
// proposed-row reference spelled per dialect (the EXCLUDED keyword has no
|
|
252
|
+
// MySQL equivalent — ON DUPLICATE KEY UPDATE uses VALUES(col)). guardColumn
|
|
253
|
+
// tells the MySQL upsert renderer that the fence protects `fencingToken`,
|
|
254
|
+
// so the IF(<fence>, …, col) wrap on the other SET targets evaluates
|
|
255
|
+
// against the fencingToken column's PRE-update value (the IF-eval-order
|
|
256
|
+
// hazard) and the guard column is assigned last.
|
|
257
|
+
var dialect = clusterStorage.dialect();
|
|
258
|
+
var fenceExisting = safeSql.quoteQualified(["_blamejs_audit_tip", "fencingToken"], dialect); // allow:hand-rolled-sql
|
|
259
|
+
var fenceProposed = dialect === "mysql"
|
|
260
|
+
? "VALUES(" + safeSql.quoteIdentifier("fencingToken", "mysql") + ")"
|
|
261
|
+
: "EXCLUDED." + safeSql.quoteIdentifier("fencingToken", dialect);
|
|
262
|
+
var tipFence = fenceExisting + " <= " + fenceProposed;
|
|
263
|
+
var tipBuilt = sql.upsert("_blamejs_audit_tip", { dialect: dialect }) // allow:hand-rolled-sql
|
|
264
|
+
.columns(["scope", "atMonotonicCounter", "rowHash", "signedAt", "fencingToken"])
|
|
265
|
+
.values({
|
|
266
|
+
scope: "audit",
|
|
267
|
+
atMonotonicCounter: counter,
|
|
268
|
+
rowHash: rowHash,
|
|
269
|
+
signedAt: signedAt,
|
|
270
|
+
fencingToken: fencingToken,
|
|
271
|
+
})
|
|
272
|
+
.onConflict(["scope"])
|
|
273
|
+
.doUpdateFromExcluded(["atMonotonicCounter", "rowHash", "signedAt", "fencingToken"])
|
|
274
|
+
.conflictWhere(tipFence, [], { guardColumn: "fencingToken" })
|
|
275
|
+
.returning(["fencingToken"])
|
|
276
|
+
.toSql();
|
|
277
|
+
var fenced;
|
|
278
|
+
if (dialect === "mysql") {
|
|
279
|
+
// MySQL ON DUPLICATE KEY UPDATE has no WHERE + no RETURNING — the fence
|
|
280
|
+
// becomes an IF(<guard>, VALUES(col), col) per SET target, so a fenced-out
|
|
281
|
+
// (strictly-lower) token leaves every column at its stored value and the
|
|
282
|
+
// statement still "succeeds". Detection therefore can't read RETURNING
|
|
283
|
+
// rows: run the upsert, then read the stored fencingToken back (the b.sql
|
|
284
|
+
// builder hands us the keyed readback SELECT) and compare. If the stored
|
|
285
|
+
// token is ABOVE our incoming one, a higher-token successor won and we
|
|
286
|
+
// were fenced out.
|
|
287
|
+
await safeAsync.withTimeout(
|
|
288
|
+
clusterStorage.execute(tipBuilt.sql, tipBuilt.params),
|
|
289
|
+
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
290
|
+
{ name: "audit.upsertAuditTip" }
|
|
291
|
+
);
|
|
292
|
+
var back = await safeAsync.withTimeout(
|
|
293
|
+
clusterStorage.executeOne(tipBuilt.readbackSql.sql, tipBuilt.readbackSql.params),
|
|
294
|
+
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
295
|
+
{ name: "audit.upsertAuditTip.readback" }
|
|
296
|
+
);
|
|
297
|
+
fenced = !back || Number(back.fencingToken) > Number(fencingToken);
|
|
298
|
+
} else {
|
|
299
|
+
var result = await safeAsync.withTimeout(
|
|
300
|
+
clusterStorage.execute(tipBuilt.sql, tipBuilt.params),
|
|
301
|
+
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
302
|
+
{ name: "audit.upsertAuditTip" }
|
|
303
|
+
);
|
|
304
|
+
fenced = !result.rows || result.rows.length === 0;
|
|
305
|
+
}
|
|
306
|
+
if (fenced) {
|
|
219
307
|
throw new ClusterError(
|
|
220
308
|
"FENCED_OUT",
|
|
221
309
|
"audit-tip update rejected: incoming fencingToken=" + fencingToken +
|
|
@@ -617,11 +705,18 @@ function useStore(store) {
|
|
|
617
705
|
//
|
|
618
706
|
// Self-logging (PCI DSS 10.2.3): every read of audit_log is itself recorded
|
|
619
707
|
// as an 'audit.read' event before the query runs, so an exfiltration attempt
|
|
620
|
-
// is forensically visible. The
|
|
621
|
-
// the
|
|
622
|
-
//
|
|
623
|
-
// (otherwise legitimate audit
|
|
624
|
-
|
|
708
|
+
// is forensically visible. The self-log goes through record() (which never
|
|
709
|
+
// re-enters query()), so the only re-entrancy to suppress is a query whose
|
|
710
|
+
// own criteria filters for action='audit.read' — those don't auto-log
|
|
711
|
+
// (otherwise legitimate audit-of-audits produces a Russell-set spiral, and
|
|
712
|
+
// the self-read itself would log a read). That suppression is decided PER
|
|
713
|
+
// INVOCATION from the call's own criteria.action, never from shared mutable
|
|
714
|
+
// state: a prior design used a module-global `_selfLogging` boolean toggled
|
|
715
|
+
// across record()'s await (chain mutex + SQL yield), so a CONCURRENT
|
|
716
|
+
// query() racing a mid-flight self-log observed the flag set and silently
|
|
717
|
+
// skipped emitting its own audit.read — under-logging reads exactly when
|
|
718
|
+
// load is highest. b.audit.query is reachable from concurrent request
|
|
719
|
+
// handlers, so the guard MUST be invocation-local.
|
|
625
720
|
|
|
626
721
|
/**
|
|
627
722
|
* @primitive b.audit.query
|
|
@@ -633,9 +728,10 @@ var _selfLogging = false;
|
|
|
633
728
|
* Read audit rows matching the criteria, returning unsealed rows for
|
|
634
729
|
* the auditor's view. Every call self-logs an `audit.read` event before
|
|
635
730
|
* returning (PCI DSS 10.2.3) so exfiltration attempts are forensically
|
|
636
|
-
* visible;
|
|
637
|
-
*
|
|
638
|
-
*
|
|
731
|
+
* visible; the self-log is suppressed per-invocation only for a query
|
|
732
|
+
* whose own criteria targets `action: "audit.read"`, so concurrent reads
|
|
733
|
+
* each record their own `audit.read`. Plain-field criteria translate into
|
|
734
|
+
* derived-hash equality where the column is sealed.
|
|
639
735
|
*
|
|
640
736
|
* @opts
|
|
641
737
|
* from: number | Date | string, // recordedAt >=
|
|
@@ -658,21 +754,21 @@ var _selfLogging = false;
|
|
|
658
754
|
*/
|
|
659
755
|
async function query(criteria) {
|
|
660
756
|
criteria = criteria || {};
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
}
|
|
757
|
+
// Suppress the self-log ONLY for a query whose own criteria targets
|
|
758
|
+
// action='audit.read' (the self-read's shape + audit-of-audits). This is
|
|
759
|
+
// derived from THIS call's criteria — no shared flag — so concurrent
|
|
760
|
+
// reads each emit their own audit.read instead of one racing read
|
|
761
|
+
// swallowing another's self-log.
|
|
762
|
+
if (criteria.action !== "audit.read") {
|
|
763
|
+
await record({
|
|
764
|
+
actor: criteria.actor || {},
|
|
765
|
+
action: "audit.read",
|
|
766
|
+
outcome: "success",
|
|
767
|
+
metadata: {
|
|
768
|
+
criteria: _redactCriteria(criteria),
|
|
769
|
+
traceId: criteria.traceId || null,
|
|
770
|
+
},
|
|
771
|
+
});
|
|
676
772
|
}
|
|
677
773
|
|
|
678
774
|
// In single-node mode the query builder gives us field-crypto unsealing
|
|
@@ -700,35 +796,31 @@ async function query(criteria) {
|
|
|
700
796
|
}
|
|
701
797
|
|
|
702
798
|
async function _queryCluster(criteria) {
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
if (criteria.to)
|
|
710
|
-
conds.push("recordedAt <= ?");
|
|
711
|
-
params.push(_toMs(criteria.to));
|
|
712
|
-
}
|
|
799
|
+
// Compose the criteria onto a b.sql SELECT with a BARE logical table name
|
|
800
|
+
// (clusterStorage rewrites + placeholderizes). Sealed-field criteria
|
|
801
|
+
// translate to the derived-hash column via cryptoField.lookupHash exactly
|
|
802
|
+
// as before; b.sql quotes every identifier and binds every value.
|
|
803
|
+
var qb = sql.select("audit_log", _sqlOpts());
|
|
804
|
+
if (criteria.from) qb.whereOp("recordedAt", ">=", _toMs(criteria.from));
|
|
805
|
+
if (criteria.to) qb.whereOp("recordedAt", "<=", _toMs(criteria.to));
|
|
713
806
|
if (criteria.actorUserId) {
|
|
714
807
|
var auh = cryptoField.lookupHash("audit_log", "actorUserId", criteria.actorUserId);
|
|
715
|
-
if (auh)
|
|
808
|
+
if (auh) qb.where(auh.field, auh.value);
|
|
716
809
|
}
|
|
717
810
|
if (criteria.resourceId) {
|
|
718
811
|
var rh = cryptoField.lookupHash("audit_log", "resourceId", criteria.resourceId);
|
|
719
|
-
if (rh)
|
|
812
|
+
if (rh) qb.where(rh.field, rh.value);
|
|
720
813
|
}
|
|
721
|
-
if (criteria.action)
|
|
722
|
-
if (criteria.resourceKind)
|
|
723
|
-
if (criteria.outcome)
|
|
814
|
+
if (criteria.action) qb.where("action", criteria.action);
|
|
815
|
+
if (criteria.resourceKind) qb.where("resourceKind", criteria.resourceKind);
|
|
816
|
+
if (criteria.outcome) qb.where("outcome", criteria.outcome);
|
|
724
817
|
|
|
725
|
-
|
|
726
|
-
if (
|
|
727
|
-
|
|
728
|
-
if (criteria.limit != null) { sql += " LIMIT ?"; params.push(criteria.limit); }
|
|
729
|
-
if (criteria.offset != null) { sql += " OFFSET ?"; params.push(criteria.offset); }
|
|
818
|
+
qb.orderBy("monotonicCounter", "asc");
|
|
819
|
+
if (criteria.limit != null) qb.limit(criteria.limit);
|
|
820
|
+
if (criteria.offset != null) qb.offset(criteria.offset);
|
|
730
821
|
|
|
731
|
-
var
|
|
822
|
+
var built = qb.toSql();
|
|
823
|
+
var rows = await clusterStorage.executeAll(built.sql, built.params);
|
|
732
824
|
return rows.map(function (row) { return cryptoField.unsealRow("audit_log", row); });
|
|
733
825
|
}
|
|
734
826
|
|
|
@@ -840,12 +932,14 @@ async function checkpoint(opts) {
|
|
|
840
932
|
cluster.requireLeader();
|
|
841
933
|
opts = opts || {};
|
|
842
934
|
|
|
935
|
+
var tipReadBuilt = sql.select("audit_log", _sqlOpts())
|
|
936
|
+
.columns(["_id", "monotonicCounter", "rowHash"])
|
|
937
|
+
.orderBy("monotonicCounter", "desc")
|
|
938
|
+
.limit(1)
|
|
939
|
+
.toSql();
|
|
843
940
|
var tip = await safeAsync.withTimeout(
|
|
844
941
|
safeAsync.asyncRetry(function () {
|
|
845
|
-
return clusterStorage.executeOne(
|
|
846
|
-
"SELECT _id, monotonicCounter, rowHash FROM audit_log " +
|
|
847
|
-
"ORDER BY monotonicCounter DESC LIMIT 1"
|
|
848
|
-
);
|
|
942
|
+
return clusterStorage.executeOne(tipReadBuilt.sql, tipReadBuilt.params);
|
|
849
943
|
}),
|
|
850
944
|
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
851
945
|
{ name: "audit.checkpoint.readTip" }
|
|
@@ -890,16 +984,31 @@ async function checkpoint(opts) {
|
|
|
890
984
|
if (cluster.isClusterMode()) {
|
|
891
985
|
await _upsertAuditTip(counter, tip.rowHash, String(createdAt), fencingToken);
|
|
892
986
|
} else {
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
987
|
+
// Flush the anchored rows to durable db.enc BEFORE advancing the durable
|
|
988
|
+
// tip sidecar. Otherwise a crash between this checkpoint and the next
|
|
989
|
+
// encrypt leaves the tip referencing a counter not yet on durable disk;
|
|
990
|
+
// on reboot the rollback detector reads the tip ahead of the restored
|
|
991
|
+
// db.enc and FALSELY refuses boot as a rollback/deletion, even though the
|
|
992
|
+
// chain is intact and only unflushed rows were lost in a normal crash.
|
|
993
|
+
// Rows-before-tip is the correct durability ordering. flushToDisk is a
|
|
994
|
+
// no-op outside encrypted-at-rest mode (the live file is already durable).
|
|
995
|
+
// The tip advances ONLY if the flush succeeded, so the durable tip can
|
|
996
|
+
// never get ahead of the durable rows.
|
|
997
|
+
var rowsFlushed = false;
|
|
998
|
+
try { db().flushToDisk(); rowsFlushed = true; }
|
|
999
|
+
catch (_e) { /* flush failed - do not advance the tip ahead of the rows */ }
|
|
1000
|
+
if (rowsFlushed) {
|
|
1001
|
+
try {
|
|
1002
|
+
db()._writeAuditTip({
|
|
1003
|
+
atMonotonicCounter: counter,
|
|
1004
|
+
atRowHash: tip.rowHash,
|
|
1005
|
+
anchoredAt: createdAt,
|
|
1006
|
+
checkpointId: ckptId,
|
|
1007
|
+
publicKeyFingerprint: pubFp,
|
|
1008
|
+
version: 1,
|
|
1009
|
+
});
|
|
1010
|
+
} catch (_e) { /* best effort */ }
|
|
1011
|
+
}
|
|
903
1012
|
}
|
|
904
1013
|
|
|
905
1014
|
return {
|
|
@@ -947,28 +1056,28 @@ async function verifyCheckpoints() {
|
|
|
947
1056
|
|
|
948
1057
|
if (rows.length === 0) return { ok: true, checkpointsVerified: 0 };
|
|
949
1058
|
|
|
950
|
-
var currentFp = auditSign.getPublicKeyFingerprint();
|
|
951
|
-
var currentPub = auditSign.getPublicKey();
|
|
952
|
-
|
|
953
1059
|
for (var i = 0; i < rows.length; i++) {
|
|
954
1060
|
var c = rows[i];
|
|
955
|
-
//
|
|
956
|
-
// key-
|
|
957
|
-
//
|
|
958
|
-
|
|
1061
|
+
// Resolve the public key the checkpoint was signed under: the current
|
|
1062
|
+
// key, or a rotated-out key from the unsealed public-key history. A
|
|
1063
|
+
// rotation archives the prior public key, so a checkpoint anchored
|
|
1064
|
+
// before the rotation still verifies (no re-signing required). A
|
|
1065
|
+
// fingerprint with no recorded key is the genuine break (key rotated
|
|
1066
|
+
// away with no history, or a forged fingerprint).
|
|
1067
|
+
var pub = auditSign.getPublicKeyByFingerprint(c.publicKeyFingerprint);
|
|
1068
|
+
if (!pub) {
|
|
959
1069
|
return {
|
|
960
1070
|
ok: false,
|
|
961
1071
|
checkpointsVerified: i,
|
|
962
1072
|
breakAt: i,
|
|
963
1073
|
checkpointId: c._id,
|
|
964
|
-
reason: "
|
|
965
|
-
expected: currentFp,
|
|
1074
|
+
reason: "no audit-signing key on record for this checkpoint's fingerprint (key rotated without history?)",
|
|
966
1075
|
actual: c.publicKeyFingerprint,
|
|
967
1076
|
};
|
|
968
1077
|
}
|
|
969
1078
|
var payload = _checkpointPayload(Number(c.atMonotonicCounter), c.atRowHash, Number(c.createdAt));
|
|
970
1079
|
var sigBuf = Buffer.isBuffer(c.signature) ? c.signature : Buffer.from(c.signature);
|
|
971
|
-
if (!auditSign.verify(payload, sigBuf,
|
|
1080
|
+
if (!auditSign.verify(payload, sigBuf, pub)) {
|
|
972
1081
|
return {
|
|
973
1082
|
ok: false,
|
|
974
1083
|
checkpointsVerified: i,
|
|
@@ -1515,10 +1624,15 @@ function bindActor(actorId, opts) {
|
|
|
1515
1624
|
function generateActorBindingTriggerSql(opts) {
|
|
1516
1625
|
opts = opts || {};
|
|
1517
1626
|
var columnRaw = opts.column || "actorUserId";
|
|
1518
|
-
|
|
1627
|
+
// Default resolves through frameworkSchema.tableName so the configurable
|
|
1628
|
+
// framework-table prefix flows into the operator-applied trigger DDL.
|
|
1629
|
+
var tableNameRaw = opts.tableName || frameworkSchema.tableName("audit_log");
|
|
1519
1630
|
var allowRoles = Array.isArray(opts.allowRoles) ? opts.allowRoles : [];
|
|
1520
|
-
|
|
1521
|
-
|
|
1631
|
+
// Trigger function + trigger object NAMES (not framework tables — they have
|
|
1632
|
+
// no LOCAL_TO_EXTERNAL mapping and carry no prefix). assertSegregation
|
|
1633
|
+
// looks them up under these exact names.
|
|
1634
|
+
var fnNameRaw = "_blamejs_audit_actor_binding_check"; // allow:hand-rolled-sql
|
|
1635
|
+
var trigNameRaw = "_blamejs_audit_actor_binding_trig"; // allow:hand-rolled-sql
|
|
1522
1636
|
// Quote-and-validate every identifier through safeSql.quoteIdentifier
|
|
1523
1637
|
// so operator-supplied opts.column / opts.tableName / opts.roleMappingFn
|
|
1524
1638
|
// can't reach raw concatenation. PostgreSQL + SQLite both use the
|
|
@@ -1537,8 +1651,14 @@ function generateActorBindingTriggerSql(opts) {
|
|
|
1537
1651
|
var roleMatch = qRoleMapFn
|
|
1538
1652
|
? " IF " + qRoleMapFn + "(NEW." + qColumn + ") IS DISTINCT FROM current_user THEN\n"
|
|
1539
1653
|
: " IF NEW." + qColumn + " IS DISTINCT FROM current_user THEN\n";
|
|
1654
|
+
// Operator-applied plpgsql trigger DDL — a CREATE FUNCTION body + RAISE
|
|
1655
|
+
// EXCEPTION + CREATE/DROP TRIGGER, none of which b.sql's verb builders
|
|
1656
|
+
// model. Every identifier is quoted through safeSql.quoteIdentifier above;
|
|
1657
|
+
// the table name resolves via frameworkSchema.tableName, so the prefix is
|
|
1658
|
+
// honored. allow:hand-rolled-sql — this is migration-script generation,
|
|
1659
|
+
// not a framework-state DML path.
|
|
1540
1660
|
var up =
|
|
1541
|
-
"CREATE OR REPLACE FUNCTION " + qFn + "() RETURNS trigger AS $$\n" +
|
|
1661
|
+
"CREATE OR REPLACE FUNCTION " + qFn + "() RETURNS trigger AS $$\n" + // allow:hand-rolled-sql
|
|
1542
1662
|
"BEGIN\n" +
|
|
1543
1663
|
allowList +
|
|
1544
1664
|
roleMatch +
|
|
@@ -1548,12 +1668,12 @@ function generateActorBindingTriggerSql(opts) {
|
|
|
1548
1668
|
" RETURN NEW;\n" +
|
|
1549
1669
|
"END;\n" +
|
|
1550
1670
|
"$$ LANGUAGE plpgsql;\n" +
|
|
1551
|
-
"DROP TRIGGER IF EXISTS " + qTrig + " ON " + qTable + ";\n" +
|
|
1552
|
-
"CREATE TRIGGER " + qTrig + "\n" +
|
|
1671
|
+
"DROP TRIGGER IF EXISTS " + qTrig + " ON " + qTable + ";\n" + // allow:hand-rolled-sql
|
|
1672
|
+
"CREATE TRIGGER " + qTrig + "\n" + // allow:hand-rolled-sql
|
|
1553
1673
|
" BEFORE INSERT ON " + qTable + "\n" +
|
|
1554
1674
|
" FOR EACH ROW EXECUTE FUNCTION " + qFn + "();\n";
|
|
1555
1675
|
var down =
|
|
1556
|
-
"DROP TRIGGER IF EXISTS " + qTrig + " ON " + qTable + ";\n" +
|
|
1676
|
+
"DROP TRIGGER IF EXISTS " + qTrig + " ON " + qTable + ";\n" + // allow:hand-rolled-sql
|
|
1557
1677
|
"DROP FUNCTION IF EXISTS " + qFn + "();\n";
|
|
1558
1678
|
return { up: up, down: down, functionName: fnNameRaw, triggerName: trigNameRaw };
|
|
1559
1679
|
}
|
|
@@ -1592,14 +1712,20 @@ async function assertSegregation(opts) {
|
|
|
1592
1712
|
throw new AuditSegregationError("audit/segregation-no-db",
|
|
1593
1713
|
"audit.assertSegregation: opts.db with a query() method is required");
|
|
1594
1714
|
}
|
|
1595
|
-
|
|
1596
|
-
|
|
1715
|
+
// Trigger / function object NAMES (not framework tables — they have no
|
|
1716
|
+
// LOCAL_TO_EXTERNAL mapping and carry no prefix). They must match the
|
|
1717
|
+
// names generateActorBindingTriggerSql emits.
|
|
1718
|
+
var fnName = opts.functionName || "_blamejs_audit_actor_binding_check"; // allow:hand-rolled-sql
|
|
1719
|
+
var trigName = opts.triggerName || "_blamejs_audit_actor_binding_trig"; // allow:hand-rolled-sql
|
|
1720
|
+
// Operator-DB system-catalog introspection (Postgres pg_proc / pg_trigger,
|
|
1721
|
+
// $N-native, against the operator-supplied db.query) — not a framework
|
|
1722
|
+
// table, so b.sql's verb builders don't apply.
|
|
1597
1723
|
var fnRes = await db.query(
|
|
1598
|
-
"SELECT 1 FROM pg_proc WHERE proname = $1 LIMIT 1", [fnName]
|
|
1724
|
+
"SELECT 1 FROM pg_proc WHERE proname = $1 LIMIT 1", [fnName] // allow:hand-rolled-sql
|
|
1599
1725
|
);
|
|
1600
1726
|
var fnPresent = !!(fnRes && fnRes.rows && fnRes.rows.length > 0);
|
|
1601
1727
|
var trigRes = await db.query(
|
|
1602
|
-
"SELECT 1 FROM pg_trigger WHERE tgname = $1 LIMIT 1", [trigName]
|
|
1728
|
+
"SELECT 1 FROM pg_trigger WHERE tgname = $1 LIMIT 1", [trigName] // allow:hand-rolled-sql
|
|
1603
1729
|
);
|
|
1604
1730
|
var trigPresent = !!(trigRes && trigRes.rows && trigRes.rows.length > 0);
|
|
1605
1731
|
var missing = [];
|