@blamejs/core 0.4.1
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 +230 -0
- package/LICENSE +201 -0
- package/LTS-CALENDAR.md +29 -0
- package/MIGRATING.md +7 -0
- package/NOTICE +59 -0
- package/README.md +100 -0
- package/bin/blamejs.js +13 -0
- package/index.js +253 -0
- package/lib/api-key.js +705 -0
- package/lib/api-snapshot.js +335 -0
- package/lib/app-shutdown.js +381 -0
- package/lib/app.js +364 -0
- package/lib/atomic-file.js +525 -0
- package/lib/audit-chain.js +168 -0
- package/lib/audit-sign.js +319 -0
- package/lib/audit-tools.js +682 -0
- package/lib/audit.js +753 -0
- package/lib/auth/jwt.js +280 -0
- package/lib/auth/oauth.js +691 -0
- package/lib/auth/passkey.js +185 -0
- package/lib/auth/password.js +139 -0
- package/lib/auth/totp.js +17 -0
- package/lib/auth-header.js +81 -0
- package/lib/backup/bundle.js +219 -0
- package/lib/backup/crypto.js +174 -0
- package/lib/backup/index.js +490 -0
- package/lib/backup/manifest.js +275 -0
- package/lib/bundler.js +295 -0
- package/lib/cache.js +819 -0
- package/lib/chain-writer.js +234 -0
- package/lib/cli-helpers.js +201 -0
- package/lib/cli.js +1377 -0
- package/lib/cluster-provider-db.js +245 -0
- package/lib/cluster-storage.js +166 -0
- package/lib/cluster.js +691 -0
- package/lib/consent.js +222 -0
- package/lib/constants.js +186 -0
- package/lib/cookies.js +293 -0
- package/lib/credential-hash.js +303 -0
- package/lib/crypto-field.js +159 -0
- package/lib/crypto.js +250 -0
- package/lib/db-query.js +297 -0
- package/lib/db-schema.js +250 -0
- package/lib/db.js +1054 -0
- package/lib/deprecate.js +226 -0
- package/lib/dev.js +324 -0
- package/lib/error-page.js +424 -0
- package/lib/events.js +135 -0
- package/lib/external-db.js +422 -0
- package/lib/forms.js +378 -0
- package/lib/framework-error.js +189 -0
- package/lib/framework-schema.js +604 -0
- package/lib/handlers.js +350 -0
- package/lib/html-balance.js +227 -0
- package/lib/http-client.js +615 -0
- package/lib/i18n.js +780 -0
- package/lib/jobs.js +181 -0
- package/lib/lazy-require.js +48 -0
- package/lib/log-stream-local.js +137 -0
- package/lib/log-stream-webhook.js +170 -0
- package/lib/log-stream.js +211 -0
- package/lib/log.js +355 -0
- package/lib/mail-bounce.js +507 -0
- package/lib/mail.js +701 -0
- package/lib/metrics.js +647 -0
- package/lib/middleware/api-encrypt.js +553 -0
- package/lib/middleware/attach-user.js +156 -0
- package/lib/middleware/body-parser.js +883 -0
- package/lib/middleware/bot-guard.js +148 -0
- package/lib/middleware/compression.js +436 -0
- package/lib/middleware/cors.js +236 -0
- package/lib/middleware/csp-nonce.js +332 -0
- package/lib/middleware/csrf-protect.js +275 -0
- package/lib/middleware/error-handler.js +46 -0
- package/lib/middleware/health.js +358 -0
- package/lib/middleware/index.js +52 -0
- package/lib/middleware/rate-limit.js +319 -0
- package/lib/middleware/request-id.js +53 -0
- package/lib/middleware/require-auth.js +95 -0
- package/lib/middleware/security-headers.js +91 -0
- package/lib/migrations.js +353 -0
- package/lib/mtls-ca.js +333 -0
- package/lib/mtls-engine-default.js +285 -0
- package/lib/nonce-store.js +177 -0
- package/lib/notify.js +643 -0
- package/lib/ntp-check.js +178 -0
- package/lib/object-store/azure-blob.js +467 -0
- package/lib/object-store/gcs.js +469 -0
- package/lib/object-store/http-put.js +153 -0
- package/lib/object-store/index.js +140 -0
- package/lib/object-store/local.js +163 -0
- package/lib/object-store/retry.js +15 -0
- package/lib/object-store/sigv4.js +535 -0
- package/lib/observability.js +114 -0
- package/lib/pagination.js +371 -0
- package/lib/parsers/index.js +64 -0
- package/lib/parsers/safe-csv.js +224 -0
- package/lib/parsers/safe-env.js +614 -0
- package/lib/parsers/safe-toml.js +745 -0
- package/lib/parsers/safe-xml.js +379 -0
- package/lib/parsers/safe-yaml.js +977 -0
- package/lib/permissions.js +430 -0
- package/lib/pqc-agent.js +85 -0
- package/lib/pqc-gate.js +266 -0
- package/lib/protocol-dispatcher.js +144 -0
- package/lib/queue-local.js +327 -0
- package/lib/queue.js +430 -0
- package/lib/redact.js +192 -0
- package/lib/render.js +193 -0
- package/lib/request-helpers.js +178 -0
- package/lib/restore-bundle.js +239 -0
- package/lib/restore-rollback.js +254 -0
- package/lib/restore.js +301 -0
- package/lib/retry.js +329 -0
- package/lib/router.js +437 -0
- package/lib/safe-async.js +520 -0
- package/lib/safe-buffer.js +162 -0
- package/lib/safe-json.js +532 -0
- package/lib/safe-schema.js +1176 -0
- package/lib/safe-sql.js +157 -0
- package/lib/safe-url.js +109 -0
- package/lib/scheduler.js +680 -0
- package/lib/seeders.js +622 -0
- package/lib/session.js +304 -0
- package/lib/slug.js +243 -0
- package/lib/static.js +268 -0
- package/lib/storage.js +470 -0
- package/lib/subject.js +281 -0
- package/lib/template.js +781 -0
- package/lib/testing.js +621 -0
- package/lib/totp.js +285 -0
- package/lib/tracing.js +484 -0
- package/lib/validate-opts.js +56 -0
- package/lib/vault/index.js +299 -0
- package/lib/vault/passphrase-ops.js +311 -0
- package/lib/vault/passphrase-source.js +198 -0
- package/lib/vault/rotate.js +761 -0
- package/lib/vault/wrap.js +289 -0
- package/lib/vendor/MANIFEST.json +84 -0
- package/lib/vendor/argon2/argon2.cjs +466 -0
- package/lib/vendor/argon2/argon2.d.cts +62 -0
- package/lib/vendor/argon2/package.json +1 -0
- package/lib/vendor/argon2/prebuilds/darwin-arm64/argon2.armv8.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/darwin-x64/argon2.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/freebsd-arm64/argon2.armv8.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/freebsd-x64/argon2.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-arm/argon2.armv7.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-arm/argon2.armv7.musl.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-arm64/argon2.armv8.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-arm64/argon2.armv8.musl.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-x64/argon2.glibc.node +0 -0
- package/lib/vendor/argon2/prebuilds/linux-x64/argon2.musl.node +0 -0
- package/lib/vendor/argon2/prebuilds/win32-x64/argon2.glibc.node +0 -0
- package/lib/vendor/noble-ciphers.cjs +9 -0
- package/lib/vendor/pki.cjs +181 -0
- package/lib/vendor/simplewebauthn-server.cjs +328 -0
- package/lib/webhook.js +632 -0
- package/lib/websocket-channels.js +413 -0
- package/lib/websocket.js +833 -0
- package/package.json +39 -0
package/lib/consent.js
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Consent log — GDPR Art. 6/7 lawful-basis tracking with hash chain.
|
|
4
|
+
*
|
|
5
|
+
* consent_log is baked into db.js's schema runner alongside audit_log.
|
|
6
|
+
* Same tamper-evidence design: per-row SHA3-512 hash chain, append-only,
|
|
7
|
+
* verified at boot.
|
|
8
|
+
*
|
|
9
|
+
* Lawful-basis consistency: any audit-recorded operation declared under
|
|
10
|
+
* `lawfulBasis: 'consent'` should reference a current consent_log entry.
|
|
11
|
+
* The framework records consent grants/withdrawals here; enforcement of
|
|
12
|
+
* consent before processing is the app's responsibility — query
|
|
13
|
+
* `consent.isActive(subjectId, purpose)` at the trust boundary.
|
|
14
|
+
*
|
|
15
|
+
* Public API:
|
|
16
|
+
* consent.grant({ subjectId, purpose, lawfulBasis, scope?, channel, evidenceRef? })
|
|
17
|
+
* consent.withdraw({ subjectId, purpose, reason? })
|
|
18
|
+
* consent.isGranted({ subjectId, purpose }) → boolean
|
|
19
|
+
* consent.history(subjectId) → array of consent_log rows (decrypted)
|
|
20
|
+
* consent.verify() → { ok, rowsVerified, breakAt? }
|
|
21
|
+
*/
|
|
22
|
+
var auditChain = require("./audit-chain");
|
|
23
|
+
var cluster = require("./cluster");
|
|
24
|
+
var clusterStorage = require("./cluster-storage");
|
|
25
|
+
var chainWriter = require("./chain-writer");
|
|
26
|
+
var safeAsync = require("./safe-async");
|
|
27
|
+
var lazyRequire = require("./lazy-require");
|
|
28
|
+
var C = require("./constants");
|
|
29
|
+
var { ClusterError } = require("./framework-error");
|
|
30
|
+
|
|
31
|
+
// Wall-clock cap on the tip upsert. Same value as audit's
|
|
32
|
+
// FRAMEWORK_SQL_TIMEOUT_MS — a misbehaving external-db driver
|
|
33
|
+
// shouldn't hang a consent write forever.
|
|
34
|
+
var FRAMEWORK_SQL_TIMEOUT_MS = C.TIME.seconds(30);
|
|
35
|
+
|
|
36
|
+
var LAWFUL_BASES = ["consent", "contract", "legal_obligation", "vital_interests", "public_task", "legitimate_interests"];
|
|
37
|
+
var ACTIONS = ["granted", "withdrawn", "expired", "superseded"];
|
|
38
|
+
|
|
39
|
+
var HASHABLE_COLS = [
|
|
40
|
+
"_id", "recordedAt", "monotonicCounter",
|
|
41
|
+
"subjectId", "subjectIdHash",
|
|
42
|
+
"purpose", "lawfulBasis", "action",
|
|
43
|
+
"scope", "channel", "evidenceRef",
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
var db = lazyRequire(function () { return require("./db"); });
|
|
47
|
+
|
|
48
|
+
// Race-safe chain append for consent_log. Inherits the same Mutex /
|
|
49
|
+
// counter-primer / retry-wrapped reads / timeout-bounded writes that
|
|
50
|
+
// audit_log uses. (Before this primitive landed, consent.js had a chain-
|
|
51
|
+
// fork race identical to audit.js's pre-Mutex bug; switching to chain-
|
|
52
|
+
// writer fixes it as a side effect of consolidating the duplication.)
|
|
53
|
+
var _chainWriter = chainWriter.create({
|
|
54
|
+
table: "consent_log",
|
|
55
|
+
hashableColumns: HASHABLE_COLS,
|
|
56
|
+
columnsForInsert: [
|
|
57
|
+
"_id", "recordedAt", "monotonicCounter",
|
|
58
|
+
"subjectId", "subjectIdHash",
|
|
59
|
+
"purpose", "lawfulBasis", "action",
|
|
60
|
+
"scope", "channel", "evidenceRef",
|
|
61
|
+
"prevHash", "rowHash", "nonce", "fencingToken",
|
|
62
|
+
],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ---- Public API ----
|
|
66
|
+
|
|
67
|
+
function grant(opts) {
|
|
68
|
+
cluster.requireLeader();
|
|
69
|
+
if (!opts || !opts.subjectId || !opts.purpose || !opts.lawfulBasis || !opts.channel) {
|
|
70
|
+
throw new Error("consent.grant requires { subjectId, purpose, lawfulBasis, channel }");
|
|
71
|
+
}
|
|
72
|
+
if (LAWFUL_BASES.indexOf(opts.lawfulBasis) === -1) {
|
|
73
|
+
throw new Error("invalid lawfulBasis: '" + opts.lawfulBasis + "' (must be one of " + LAWFUL_BASES.join(", ") + ")");
|
|
74
|
+
}
|
|
75
|
+
return _appendConsentRow({
|
|
76
|
+
subjectId: opts.subjectId,
|
|
77
|
+
purpose: opts.purpose,
|
|
78
|
+
lawfulBasis: opts.lawfulBasis,
|
|
79
|
+
action: "granted",
|
|
80
|
+
scope: opts.scope ? JSON.stringify(opts.scope) : null,
|
|
81
|
+
channel: opts.channel,
|
|
82
|
+
evidenceRef: opts.evidenceRef || null,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function withdraw(opts) {
|
|
87
|
+
cluster.requireLeader();
|
|
88
|
+
if (!opts || !opts.subjectId || !opts.purpose) {
|
|
89
|
+
throw new Error("consent.withdraw requires { subjectId, purpose }");
|
|
90
|
+
}
|
|
91
|
+
return _appendConsentRow({
|
|
92
|
+
subjectId: opts.subjectId,
|
|
93
|
+
purpose: opts.purpose,
|
|
94
|
+
lawfulBasis: "consent",
|
|
95
|
+
action: "withdrawn",
|
|
96
|
+
scope: null,
|
|
97
|
+
channel: opts.channel || "api",
|
|
98
|
+
evidenceRef: opts.reason ? "reason:" + opts.reason : null,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function isGranted(opts) {
|
|
103
|
+
if (!opts || !opts.subjectId || !opts.purpose) {
|
|
104
|
+
throw new Error("consent.isGranted requires { subjectId, purpose }");
|
|
105
|
+
}
|
|
106
|
+
// Find the most recent consent row for this (subjectId, purpose).
|
|
107
|
+
// subjectId is sealed → look up via subjectIdHash (derived).
|
|
108
|
+
var hash = db().hashFor("consent_log", "subjectId", opts.subjectId);
|
|
109
|
+
if (!hash) {
|
|
110
|
+
throw new Error("consent_log subjectId is missing a derived hash — schema misconfigured");
|
|
111
|
+
}
|
|
112
|
+
var row = db().prepare(
|
|
113
|
+
"SELECT action FROM consent_log WHERE subjectIdHash = ? AND purpose = ? " +
|
|
114
|
+
"ORDER BY monotonicCounter DESC LIMIT 1"
|
|
115
|
+
).get(hash, opts.purpose);
|
|
116
|
+
if (!row) return false;
|
|
117
|
+
return row.action === "granted";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function history(subjectId) {
|
|
121
|
+
if (!subjectId) throw new Error("consent.history requires a subjectId");
|
|
122
|
+
var hash = db().hashFor("consent_log", "subjectId", subjectId);
|
|
123
|
+
if (!hash) {
|
|
124
|
+
throw new Error("consent_log subjectId is missing a derived hash — schema misconfigured");
|
|
125
|
+
}
|
|
126
|
+
var rows = db().from("consent_log")
|
|
127
|
+
.where({ subjectIdHash: hash })
|
|
128
|
+
.orderBy("monotonicCounter", "asc")
|
|
129
|
+
.all();
|
|
130
|
+
return rows;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function verify(opts) {
|
|
134
|
+
return await auditChain.verifyChain(
|
|
135
|
+
function (sql, params) { return clusterStorage.executeAll(sql, params || []); },
|
|
136
|
+
"consent_log",
|
|
137
|
+
opts
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ---- Internal: append a chain-linked row ----
|
|
142
|
+
|
|
143
|
+
async function _appendConsentRow(fields) {
|
|
144
|
+
if (ACTIONS.indexOf(fields.action) === -1) {
|
|
145
|
+
throw new Error("invalid consent action: " + fields.action);
|
|
146
|
+
}
|
|
147
|
+
var row = await _chainWriter.append({
|
|
148
|
+
subjectId: fields.subjectId,
|
|
149
|
+
purpose: fields.purpose,
|
|
150
|
+
lawfulBasis: fields.lawfulBasis,
|
|
151
|
+
action: fields.action,
|
|
152
|
+
scope: fields.scope,
|
|
153
|
+
channel: fields.channel,
|
|
154
|
+
evidenceRef: fields.evidenceRef,
|
|
155
|
+
});
|
|
156
|
+
// Cluster mode: keep _blamejs_consent_tip current so cluster.init's
|
|
157
|
+
// boot-time rollback check has an authoritative tip to compare
|
|
158
|
+
// against. Mirrors the audit-tip upsert pattern from audit.js with
|
|
159
|
+
// the same WHERE-clause fencing guard.
|
|
160
|
+
if (cluster.isClusterMode()) {
|
|
161
|
+
await _upsertConsentTip(
|
|
162
|
+
Number(row.monotonicCounter),
|
|
163
|
+
row.rowHash,
|
|
164
|
+
String(row.recordedAt),
|
|
165
|
+
cluster.fencingToken()
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return row;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function _upsertConsentTip(counter, rowHash, signedAt, fencingToken) {
|
|
172
|
+
// Single atomic INSERT … ON CONFLICT DO UPDATE … WHERE … RETURNING.
|
|
173
|
+
// Same canonical fencing-token guard as _blamejs_audit_tip: the
|
|
174
|
+
// WHERE clause enforces monotonic-non-decreasing fencingToken at
|
|
175
|
+
// the DB level so a partitioned old leader can't overwrite the tip
|
|
176
|
+
// even if its application-layer cluster.requireLeader() gate let
|
|
177
|
+
// the call through.
|
|
178
|
+
var result = await safeAsync.withTimeout(
|
|
179
|
+
clusterStorage.execute(
|
|
180
|
+
"INSERT INTO _blamejs_consent_tip " +
|
|
181
|
+
" (scope, atMonotonicCounter, rowHash, signedAt, fencingToken) " +
|
|
182
|
+
"VALUES ('consent', ?, ?, ?, ?) " +
|
|
183
|
+
"ON CONFLICT (scope) DO UPDATE SET " +
|
|
184
|
+
" atMonotonicCounter = EXCLUDED.atMonotonicCounter, " +
|
|
185
|
+
" rowHash = EXCLUDED.rowHash, " +
|
|
186
|
+
" signedAt = EXCLUDED.signedAt, " +
|
|
187
|
+
" fencingToken = EXCLUDED.fencingToken " +
|
|
188
|
+
"WHERE _blamejs_consent_tip.fencingToken <= EXCLUDED.fencingToken " +
|
|
189
|
+
"RETURNING fencingToken",
|
|
190
|
+
[counter, rowHash, signedAt, fencingToken]
|
|
191
|
+
),
|
|
192
|
+
FRAMEWORK_SQL_TIMEOUT_MS,
|
|
193
|
+
{ name: "consent.upsertConsentTip" }
|
|
194
|
+
);
|
|
195
|
+
if (!result.rows || result.rows.length === 0) {
|
|
196
|
+
throw new ClusterError(
|
|
197
|
+
"FENCED_OUT",
|
|
198
|
+
"consent-tip update rejected: incoming fencingToken=" + fencingToken +
|
|
199
|
+
" is below the stored token (this leader has been fenced out " +
|
|
200
|
+
"by a higher-token successor)",
|
|
201
|
+
true
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ---- Test helpers ----
|
|
207
|
+
|
|
208
|
+
function _resetForTest() {
|
|
209
|
+
_chainWriter._resetForTest();
|
|
210
|
+
db.reset();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
module.exports = {
|
|
214
|
+
grant: grant,
|
|
215
|
+
withdraw: withdraw,
|
|
216
|
+
isGranted: isGranted,
|
|
217
|
+
history: history,
|
|
218
|
+
verify: verify,
|
|
219
|
+
LAWFUL_BASES: LAWFUL_BASES,
|
|
220
|
+
ACTIONS: ACTIONS,
|
|
221
|
+
_resetForTest: _resetForTest,
|
|
222
|
+
};
|
package/lib/constants.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Framework constants — values fixed by blamejs design.
|
|
4
|
+
*
|
|
5
|
+
* App-specific values (paths, asset versions, theme, animation, OAuth
|
|
6
|
+
* allowlists) are supplied via createApp() configuration in the consuming
|
|
7
|
+
* app. Nothing in this file is mutable per-deployment.
|
|
8
|
+
*
|
|
9
|
+
* Naming follows the roadmap "Naming conventions" section: SCREAMING_SNAKE
|
|
10
|
+
* for constants, lowercase namespace exports.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
var pkg = require("../package.json");
|
|
14
|
+
|
|
15
|
+
// ---- Time helpers (ms) ----
|
|
16
|
+
// Functional generators instead of pre-defined discrete constants:
|
|
17
|
+
// any duration the framework or app needs is built from these, so the
|
|
18
|
+
// reader sees the unit at the call site (`C.TIME.minutes(45)` instead of
|
|
19
|
+
// adding a new FORTY_FIVE_MIN constant). All return integer milliseconds.
|
|
20
|
+
//
|
|
21
|
+
// Tier A validation: these are config-time helpers. Throw on non-finite
|
|
22
|
+
// or negative input so a typo (`C.TIME.minutes(opts.x)` where opts.x is
|
|
23
|
+
// undefined) surfaces at boot instead of silently becoming `NaN` or `0`
|
|
24
|
+
// and shipping a 0ms timeout into production.
|
|
25
|
+
function _validateDuration(unit, n) {
|
|
26
|
+
if (typeof n !== "number" || !isFinite(n) || n < 0) {
|
|
27
|
+
throw new TypeError("C.TIME." + unit + ": expected non-negative finite number, got " +
|
|
28
|
+
(typeof n) + " " + JSON.stringify(n));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
var TIME = Object.freeze({
|
|
32
|
+
seconds: function (n) { _validateDuration("seconds", n); return n * 1000; },
|
|
33
|
+
minutes: function (n) { _validateDuration("minutes", n); return n * 60000; },
|
|
34
|
+
hours: function (n) { _validateDuration("hours", n); return n * 3600000; },
|
|
35
|
+
days: function (n) { _validateDuration("days", n); return n * 86400000; },
|
|
36
|
+
weeks: function (n) { _validateDuration("weeks", n); return n * 604800000; },
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// ---- Byte helpers (binary / IEC units) ----
|
|
40
|
+
// Same pattern as TIME — units at the call site instead of pre-baked
|
|
41
|
+
// constants. Returns bytes; multiplications are 1024-based per IEC 80000-13
|
|
42
|
+
// (KiB/MiB/GiB), since every existing byte literal in the framework
|
|
43
|
+
// already uses 1024 multiplication.
|
|
44
|
+
//
|
|
45
|
+
// Tier A validation: same rationale as TIME — bad input surfaces at the
|
|
46
|
+
// call site, not as a silent NaN cap that disables size limits.
|
|
47
|
+
function _validateBytes(unit, n) {
|
|
48
|
+
if (typeof n !== "number" || !isFinite(n) || n < 0) {
|
|
49
|
+
throw new TypeError("C.BYTES." + unit + ": expected non-negative finite number, got " +
|
|
50
|
+
(typeof n) + " " + JSON.stringify(n));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
var BYTES = Object.freeze({
|
|
54
|
+
kib: function (n) { _validateBytes("kib", n); return n * 1024; },
|
|
55
|
+
mib: function (n) { _validateBytes("mib", n); return n * 1024 * 1024; },
|
|
56
|
+
gib: function (n) { _validateBytes("gib", n); return n * 1024 * 1024 * 1024; },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// ---- Crypto envelope versioning ----
|
|
60
|
+
// Every encrypted blob starts with a 4-byte header that identifies the
|
|
61
|
+
// algorithms used. This enables algorithm agility — any component can
|
|
62
|
+
// be swapped without re-encrypting existing data. Old envelopes always
|
|
63
|
+
// remain readable; new writes use ACTIVE.{KEM, CIPHER, KDF}.
|
|
64
|
+
//
|
|
65
|
+
// See roadmap "Modernity posture: highest practical bar, forward only"
|
|
66
|
+
// for the algorithm rotation policy.
|
|
67
|
+
|
|
68
|
+
var ENVELOPE_MAGIC = 0xE1;
|
|
69
|
+
|
|
70
|
+
var KEM_IDS = Object.freeze({
|
|
71
|
+
ML_KEM_1024: 0x02,
|
|
72
|
+
ML_KEM_1024_P384: 0x03,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
var CIPHER_IDS = Object.freeze({
|
|
76
|
+
XCHACHA20_POLY1305: 0x02,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
var KDF_IDS = Object.freeze({
|
|
80
|
+
SHAKE256: 0x02,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// ---- Credential hash envelope (separate from data envelope) ----
|
|
84
|
+
// Used for storing verifiable digests of credentials (API key secrets,
|
|
85
|
+
// shared bearer tokens, etc.) where the framework needs forward-
|
|
86
|
+
// compatible algorithm rotation. The credential envelope format is:
|
|
87
|
+
//
|
|
88
|
+
// byte 0: 0xC1 (CREDENTIAL_MAGIC — distinct from 0xE1 storage envelope)
|
|
89
|
+
// byte 1: <CRED_HASH_IDS algorithm ID>
|
|
90
|
+
// bytes 2..N: algorithm-specific payload
|
|
91
|
+
//
|
|
92
|
+
// Encoded base64 (URL-safe) so the column type stays TEXT. The verify
|
|
93
|
+
// path dispatches on byte 1, so historical credentials always remain
|
|
94
|
+
// verifiable regardless of what ACTIVE.CRED_HASH points at today.
|
|
95
|
+
//
|
|
96
|
+
// Why a separate magic byte from 0xE1: storage-envelope blobs and
|
|
97
|
+
// credential-envelope strings live in different columns and have
|
|
98
|
+
// different lifetimes; making the magic byte distinct prevents a
|
|
99
|
+
// confused-deputy mix-up where a corrupted credential field decodes
|
|
100
|
+
// as a storage envelope.
|
|
101
|
+
|
|
102
|
+
var CREDENTIAL_MAGIC = 0xC1;
|
|
103
|
+
|
|
104
|
+
var CRED_HASH_IDS = Object.freeze({
|
|
105
|
+
SHAKE256: 0x01, // XOF digest of operator-chosen length. Default
|
|
106
|
+
// 64 bytes; payload length itself drives the
|
|
107
|
+
// output size on verify, so a future operator
|
|
108
|
+
// can request 96 bytes without an algorithm
|
|
109
|
+
// rotation. Suitable for high-entropy secrets
|
|
110
|
+
// (>= 128 bits random) where memory-hardness
|
|
111
|
+
// buys nothing. Same family as the framework
|
|
112
|
+
// KDF, so the verify path uses one primitive.
|
|
113
|
+
ARGON2ID: 0x02, // PHC string payload. Suitable for low-entropy
|
|
114
|
+
// or paranoia-mode storage; cost ~250ms per
|
|
115
|
+
// verify. Defer to b.auth.password's PHC parser.
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
var ACTIVE = Object.freeze({
|
|
119
|
+
KEM: KEM_IDS.ML_KEM_1024_P384,
|
|
120
|
+
CIPHER: CIPHER_IDS.XCHACHA20_POLY1305,
|
|
121
|
+
KDF: KDF_IDS.SHAKE256,
|
|
122
|
+
CRED_HASH: CRED_HASH_IDS.SHAKE256,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// ---- Storage-buffer envelope marker ----
|
|
126
|
+
// Used by encryptPacked / decryptPacked for symmetric buffer encryption.
|
|
127
|
+
// Single-byte version preceding nonce + ciphertext.
|
|
128
|
+
var FORMAT = Object.freeze({
|
|
129
|
+
XCHACHA20_POLY1305: 0x02,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ---- PQC TLS group IDs (IANA TLS Supported Groups Registry) ----
|
|
133
|
+
var PQC_GROUPS = Object.freeze({
|
|
134
|
+
X25519MLKEM768: 0x11EC,
|
|
135
|
+
SecP384r1MLKEM1024: 0x11ED,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Highest-first preference list. Node TLS picks the first mutually-
|
|
139
|
+
// supported group during the handshake, so SecP384r1MLKEM1024
|
|
140
|
+
// (P-384 + ML-KEM-1024) is what we always use when the peer also
|
|
141
|
+
// advertises it. X25519MLKEM768 is the only fallback — both are
|
|
142
|
+
// PQC hybrids with current standardized parameter sets.
|
|
143
|
+
//
|
|
144
|
+
// Weaker hybrids (e.g. P-256 + ML-KEM-768) are deliberately excluded
|
|
145
|
+
// from the framework's default preference. An operator integrating
|
|
146
|
+
// with a peer that only supports a weaker PQC group constructs their
|
|
147
|
+
// own https.Agent outside lib/pqc-agent so the downgrade is visible
|
|
148
|
+
// in the diff — the framework primitive cannot be coaxed into
|
|
149
|
+
// negotiating below this list.
|
|
150
|
+
var TLS_GROUP_PREFERENCE = Object.freeze([
|
|
151
|
+
"SecP384r1MLKEM1024",
|
|
152
|
+
"X25519MLKEM768",
|
|
153
|
+
]);
|
|
154
|
+
|
|
155
|
+
var TLS_GROUP_CURVE_STR = TLS_GROUP_PREFERENCE.join(":");
|
|
156
|
+
|
|
157
|
+
// ---- Vault sealed-value prefix ----
|
|
158
|
+
var VAULT_PREFIX = "vault:";
|
|
159
|
+
|
|
160
|
+
// ---- Default hash namespaces for derived-hash indexed lookups ----
|
|
161
|
+
// Apps add their own via app-config registries. The 'bj-' namespace
|
|
162
|
+
// prevents collision between framework-derived and app-derived hashes.
|
|
163
|
+
var HASH_PREFIX = Object.freeze({
|
|
164
|
+
EMAIL: "bj-email:",
|
|
165
|
+
IP: "bj-ip:",
|
|
166
|
+
TOKEN: "bj-token:",
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
module.exports = {
|
|
170
|
+
version: pkg.version,
|
|
171
|
+
TIME: TIME,
|
|
172
|
+
BYTES: BYTES,
|
|
173
|
+
ENVELOPE_MAGIC: ENVELOPE_MAGIC,
|
|
174
|
+
CREDENTIAL_MAGIC: CREDENTIAL_MAGIC,
|
|
175
|
+
KEM_IDS: KEM_IDS,
|
|
176
|
+
CIPHER_IDS: CIPHER_IDS,
|
|
177
|
+
KDF_IDS: KDF_IDS,
|
|
178
|
+
CRED_HASH_IDS: CRED_HASH_IDS,
|
|
179
|
+
ACTIVE: ACTIVE,
|
|
180
|
+
FORMAT: FORMAT,
|
|
181
|
+
PQC_GROUPS: PQC_GROUPS,
|
|
182
|
+
TLS_GROUP_PREFERENCE: TLS_GROUP_PREFERENCE,
|
|
183
|
+
TLS_GROUP_CURVE_STR: TLS_GROUP_CURVE_STR,
|
|
184
|
+
VAULT_PREFIX: VAULT_PREFIX,
|
|
185
|
+
HASH_PREFIX: HASH_PREFIX,
|
|
186
|
+
};
|