@pikku/mongodb 0.12.12 → 0.12.13

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 CHANGED
@@ -1,5 +1,330 @@
1
1
  # @pikku/mongodb
2
2
 
3
+ ## 0.12.13
4
+
5
+ ### Patch Changes
6
+
7
+ - 6a307f0: Fix four latent correctness bugs in the function, RPC and error runtimes, and
8
+ remove dead code from the workflow service surface.
9
+
10
+ `WorkflowService.getNodesWithoutSteps` is gone. It was declared on the abstract
11
+ service and implemented by all five storage backends, and nothing ever called
12
+ it — hence the non-core packages in this changeset, which only lose that method.
13
+
14
+ **An RPC could execute twice.** `RPCService.invoke`, its addon path, and
15
+ `rpcWithWire` each wrapped the _execution_ of a resolved function in a `try`
16
+ whose `catch` treated `RPCNotFoundError` as "not found locally" and re-dispatched
17
+ the call through `deploymentService`. A nested `rpc.invoke` to a missing name,
18
+ raised from inside an already-running function, therefore re-ran that function on
19
+ a remote instance after its local side effects had already committed. Resolution
20
+ is now separated from execution on all three paths, so only a genuinely
21
+ unresolvable name reaches the fallback. What escapes to callers is unchanged.
22
+
23
+ **`addonNamespace` leaked between sibling calls.** The function runner's
24
+ middleware path restored `rpc`, `functionId`, `audit` and `addonNamespace` after
25
+ an invocation; the non-middleware path restored the first three but not the
26
+ fourth. A call into an addon function with no middleware left the addon's
27
+ namespace on the wire, so subsequent sibling calls resolved the wrong
28
+ per-instance singletons and `credentialOverrides`.
29
+
30
+ **Errors registered on a subclass never resolved.** `misc.errors` was typed
31
+ `Map<PikkuError, ErrorDetails>` — instances — while `addError` stores
32
+ constructors, a mismatch hidden by its `error: any` parameter. The instance
33
+ lookup in `getErrorResponse` was consequently dead, and lookup fell through to a
34
+ scan comparing `constructor.name`, so a subclass of a registered error got no
35
+ status mapping at all. The map is now typed `Map<PikkuErrorConstructor, …>` and
36
+ lookup walks the prototype chain first. Name matching is retained, deliberately,
37
+ as the fallback that keeps error mapping working when two copies of
38
+ `@pikku/core` are installed.
39
+
40
+ **`createWeakUID` collided across instances.** The prefix was
41
+ `Date.now().toString(36)` evaluated at module load, so any two instances loading
42
+ the module in the same millisecond emitted identical `channelId` and `requestId`
43
+ sequences — reproducibly, not just in principle. It is now seeded lazily from
44
+ `crypto.randomUUID()`.
45
+
46
+ Also: `pikkuState` keys its global map with `Symbol.for` rather than `Symbol`, so
47
+ two copies of the package share registrations instead of silently getting
48
+ disjoint state; and the local channel upgrade path no longer keys its middleware
49
+ cache on the raw request path, which grew the cache without bound while the
50
+ cached value never varied by path.
51
+
52
+ - afef587: Close eleven security weaknesses found in a review of `@pikku/core`. Most are
53
+ breaking, and two invalidate data or credentials already in the wild — read the
54
+ migration notes before upgrading.
55
+
56
+ **Breaking: AI agent thread ownership now fails closed.** Reading, listing,
57
+ resuming or approving an existing thread or run requires a resolved session
58
+ principal (`userId`, or `orgId` for `sessionScope: 'org'`), regardless of the
59
+ agent's `auth` setting. Previously a request without a session had no ownership
60
+ model at all: the caller-supplied `resourceId` was accepted as the ownership
61
+ key, so any caller could read or resume another party's thread by naming its
62
+ `resourceId`. Worse, `threadOwnerConstraint` returned `undefined` for a
63
+ sessionless caller, and `undefined` means _no filter_ rather than _no rows_ —
64
+ so `getAgentThreads` returned every thread in the deployment. It now returns
65
+ `string[]`, empty for a sessionless caller, which every storage backend already
66
+ treats as matching nothing. Sessionless agents still run one-shot conversations,
67
+ each with a fresh unguessable owner; what they lose is cross-request continuity.
68
+ Wire a session to restore it.
69
+
70
+ **Breaking: stored secrets and credentials must be re-entered.** `deriveKey` ran
71
+ a single unsalted round of SHA-256 over the passphrase and used the digest
72
+ directly as the AES-GCM key — roughly one hash per brute-force guess, with one
73
+ rainbow table working against every deployment. It is now PBKDF2-HMAC-SHA256 at
74
+ 600,000 iterations over a random salt. There is no compatibility path: every
75
+ value held by the kysely, mongodb and redis secret services and the kysely
76
+ credential service becomes undecryptable. They fail loud, naming the key and
77
+ `key_version`, so the app hard-fails on first secret read until each is re-set.
78
+
79
+ The KEK salt is scoped to the key version and stored alongside it, rather than
80
+ per secret, so a bulk read costs one derivation instead of N — `getSecrets` over
81
+ 50 secrets went from ~2.3s to ~48ms, and rotation from ~4.6s to ~94ms. This adds
82
+ a salt table (kysely), hash field (redis) or collection (mongodb), created
83
+ automatically on first use.
84
+
85
+ **Breaking: `PIKKU_REMOTE_SECRET` must be at least 32 characters.** The
86
+ remote-RPC session envelope moved from PBKDF2 to HKDF, which expands
87
+ high-entropy key material rather than stretching a low-entropy passphrase. That
88
+ took a remote hop from ~269ms to ~0.4ms — PBKDF2 was running twice per request —
89
+ but HKDF supplies no brute-force resistance, so the secret must carry the
90
+ entropy itself. A shorter secret now throws `WeakKeyMaterialError` at both ends.
91
+ Generate one with `openssl rand -base64 32` and roll it out to every service in
92
+ the mesh together: existing bearer tokens are format-incompatible, so a partial
93
+ rollout produces 401s until every instance is updated. The Cloudflare, Lambda
94
+ and Azure deployment services each hand-rolled a copy of `buildRemoteHeaders`
95
+ and now call the shared one, which is what keeps the two sides in step.
96
+
97
+ **Breaking: previously signed content URLs stop verifying.** `LocalContent`
98
+ signed only `{signedAt, expiresAt, notBefore}`, so a signature proved when a URL
99
+ was issued but never what it was issued for — any valid token was a skeleton
100
+ key, and swapping the pathname from a public thumbnail to a private document
101
+ still verified. The signature now binds the request path. Separately, the
102
+ verifier returned "valid" when no JWT service was wired, which is how
103
+ `pikku serve` ran: a forged `?signedAt=0&expiresAt=99999999999999` was accepted.
104
+ It now rejects with 403, `LocalContent` requires a `JWTService`, and
105
+ `pikku serve`/`pikku dev` mint an ephemeral per-process signing key so local
106
+ development works without shipping a fail-open path. In-flight signed URLs must
107
+ be re-issued.
108
+
109
+ **Request body size limits now apply to every adapter.** The `maxBodySize` cap
110
+ existed only in `PikkuFetchHTTPRequest`. The real hole was uWebSockets, which
111
+ drove `res.onData` itself and concatenated every chunk with no bound and nothing
112
+ downstream able to intervene; it now drops chunks past the limit and replies 413
113
+ before routing. Fastify delegates to its native `bodyLimit` (set only when
114
+ `maxBodySize` is configured, so fastify's stricter 1 MB default is never
115
+ loosened), and `PikkuExpressServer` feeds the limit into its body parsers. Two
116
+ paths can only reject rather than prevent, and are documented as such:
117
+ `express-middleware` mounted on your own app receives an already-parsed body, so
118
+ that deployment must bound its own parser; Next server actions bottom out at
119
+ `experimental.serverActions.bodySizeLimit`.
120
+
121
+ **Breaking: the console addon's privileged functions are gated by default.**
122
+ `wireAddon` gains a `scopes` option that applies to every function in the
123
+ addon's namespace, and the console scaffold now generates
124
+ `wireAddon({ name: 'console', package: '@pikku/addon-console', scopes: ['admin'] })`.
125
+ Previously the console's entire privileged surface — around 54 functions
126
+ including `credentialGet`, which returns a resolved OAuth token for an arbitrary
127
+ `userId`, `updateFunctionBody`, and `installAddon`, which shells out to a
128
+ package install — was protected only by an optional host-registered
129
+ `addGlobalPermission`. `resolveGlobalPermissions` returns `[]` when none is
130
+ registered and permission checking then no-ops, so an app that never registered
131
+ one served those functions to anyone, and with the template's default
132
+ `scaffold.rpc: "no-auth"` that meant unauthenticated. All of them now return 403
133
+ `MissingScopeError` without an `admin`, `admin:*` or `*` scope. **Regenerating
134
+ is required** — an app holding an old `console.gen.ts` stays open.
135
+ `installAddon` and `installOpenapiAddon` additionally declare their own
136
+ `auth: true, scopes: ['admin']`, and `getAgentThreads` now scopes its listing to
137
+ the session's own threads unless the caller holds admin.
138
+
139
+ Addon scopes are enforced in `runPikkuFunc` rather than at the RPC boundary,
140
+ because a wiring can reference an addon function directly — the inspector
141
+ records the addon's `packageName` on HTTP, channel, schedule, queue, CLI,
142
+ trigger, gateway and MCP wirings — and those paths never call `resolveNamespace`.
143
+ Enforcing at the RPC seam would have covered only the `namespace:function` form
144
+ while reading as complete.
145
+
146
+ **Breaking: `wireAddon`'s `auth` and `tags` now apply on direct wirings too.**
147
+ Both were read only by `resolveAddonFunction`, so they had exactly the hole
148
+ scopes had: `wireAddon({ name: 'console', package: '@pikku/addon-console', auth:
149
+ true, tags: ['admin'] })` gated `rpc('console:credentialGet')` and gated nothing
150
+ at all on an HTTP route wired straight to `credentialGet`. A consumer who
151
+ reached for the documented way to lock an addon down got a control that was
152
+ silently inert on every wiring except one. Both now resolve in `runPikkuFunc`.
153
+ `auth` merges as an OR — `auth: false` from an addon is ignored on a direct
154
+ wiring, because an addon may require a session the wiring did not but must never
155
+ waive one it did. Addon tags resolve to concrete middleware against the **root**
156
+ tag groups before the call rather than being folded into the function's
157
+ inherited middleware: `addTagMiddleware('admin', …)` is written by the consuming
158
+ app and registers under the root package, whereas `combineMiddleware` would look
159
+ the tag up under the addon's own `packageName`, where it does not exist.
160
+
161
+ One consequence worth naming: an addon that wires `auth: true` and also runs its
162
+ own sessionless internal work — a scheduled task or queue worker inside the
163
+ addon calling a sibling function — is now gated where it previously was not,
164
+ because a bare `rpc('fn')` from inside the addon reaches `runPikkuFunc` with the
165
+ addon's `packageName` like any other call. This reverses a decision that
166
+ deliberately scoped the gate to the namespaced boundary; that reasoning held
167
+ only while the boundary was real, and a direct wiring can enter an addon without
168
+ crossing it, so "already inside" is not something the runtime can infer. Such an
169
+ addon should carry authorization on the function via
170
+ `pikkuFunc({ permissions })`, which has always been enforced on every path. A
171
+ follow-up will add execution provenance so an intra-addon call can be
172
+ distinguished from an external one and skip the addon-level check; that needs a
173
+ marker no caller outside the process can set, which is its own design problem
174
+ and does not belong in a security fix.
175
+
176
+ **Codegen now warns when an exposed function has no gate (PKU574).** The
177
+ generated `POST /rpc/:rpcName` dispatcher forwards to `rpc.exposed`, which
178
+ refuses anything without `expose: true` — but nothing checked whether the
179
+ target was gated, because a dispatcher cannot know what it dispatches to. The
180
+ console shipped ~54 privileged functions through that gap and the toolchain was
181
+ silent. The inspector now reports every function that is exposed, sessionless,
182
+ and carries no `auth`, `scopes` or `permissions` of its own and none from a
183
+ governing `wireAddon`. It is a `warn`, not a critical: `expose: true` on an
184
+ ungated sessionless function is correct for a genuinely public endpoint, so it
185
+ blocks a build only under `--fail-on-warn`.
186
+
187
+ Two pieces of metadata were missing for this to be answerable statically, and
188
+ both are now recorded. A `pikkuSessionlessFunc`'s own `auth: true` was read at
189
+ runtime but never written to function meta, so a self-gated function was
190
+ indistinguishable from an ungated one — `sessionless` carries the baseline, and
191
+ `auth` now carries the tightening. And `wireAddon`'s `scopes`, `auth` and `tags`
192
+ were not parsed at all: the inspector recorded the addon's `rpcEndpoint` and its
193
+ secret, variable and credential overrides, and dropped every one of its gates.
194
+ An addon whose gates are not statically knowable is treated as gated, because a
195
+ false positive on a correctly-secured addon costs more than the one case it
196
+ would catch.
197
+
198
+ **Breaking: an application's global permissions now apply inside addons.**
199
+ `resolveGlobalPermissions` read only the bucket matching the function's own
200
+ package, but the generated `addGlobalPermission` wrapper takes no package
201
+ argument and always registers under the root. An app-wide rule like "every
202
+ request needs a signed-in user" therefore stopped at the addon boundary, and the
203
+ bucket an addon's functions did read was one no host could write to — which is
204
+ why the console addon's recommended `addGlobalPermission([isAdmin],
205
+ '@pikku/addon-console')` was never a gate anybody could actually install. A
206
+ function now resolves the root bucket and its own package's, root first.
207
+ Unioning is safe in a way nothing else here would be: globals AND, so adding the
208
+ root ones can only tighten. Package buckets stay one-way — a package's globals
209
+ never reach root functions, or an installed addon could gate the whole
210
+ application. Apps with both a root global and addon-provided functions will see
211
+ those functions gated where they previously were not.
212
+
213
+ **Codegen now records whether each HTTP route requires a session, and warns
214
+ about inert addon tags (PKU575).** Four separate things can demand a session —
215
+ the function's `sessionless`, its own `auth`, the route's (or its group's)
216
+ `auth`, and the addon it belongs to — and answering "which routes are open?"
217
+ meant joining all four by hand and knowing which wins. Each route's meta now
218
+ carries the resolved `requiresSession` alongside the route's own `auth`. Scopes
219
+ count as requiring one, since they are matched against the session's and fail
220
+ closed. Anything not statically knowable resolves to `true`, matching PKU574:
221
+ a route that looks stricter than it is costs less than one that looks open and
222
+ isn't. Separately, `wireAddon({ tags: ['admin'] })` reads like a gate and is
223
+ applied like one right up until no `addTagMiddleware('admin', …)` exists, at
224
+ which point it resolves to an empty list and gates nothing; that now warns.
225
+ Only addon tags are reported — a tag on a function is as likely to be
226
+ organizational, and warning about those would bury the case that matters.
227
+
228
+ **Object-shorthand permissions were missing from meta.** The inspector visited
229
+ `ts.PropertyAssignment` but not `ts.ShorthandPropertyAssignment`, so
230
+ `permissions: { canAdminOrg }` — enforced identically to the longhand form at
231
+ runtime, since `verifyPermissions` has a non-array branch — was recorded as _no
232
+ permissions at all_. That is the most dangerous direction for meta to be wrong
233
+ in: an audit reading it sees an open door where one is shut. It cost this review
234
+ a false IDOR report across ~35 billing and org functions before the source
235
+ contradicted the metadata.
236
+
237
+ **Functions that authorize in their own body can say so.** A webhook receiver
238
+ verifying a signature, or a handler redeeming a signed invite, is genuinely
239
+ closed while carrying no session, scope or permission — indistinguishable in
240
+ meta from one nobody remembered to gate, and so warned about forever by PKU574.
241
+ `selfAuthenticated: true` on the function config records the claim and silences
242
+ the warning for that function. It is declarative only: nothing at runtime reads
243
+ it and it grants nothing. Detection was rejected deliberately — inferring it
244
+ from the body means a function that _looks_ like it checks something silences
245
+ the warning while checking nothing, and a warning that is usually wrong stops
246
+ being read.
247
+
248
+ **Breaking: `scaffold.<feature>` is now `boolean | { auth?, path? }`, and `true`
249
+ means authenticated.** The old `'auth' | 'no-auth' | false` read like a
250
+ starter-file preference while being a live authorization decision, set three
251
+ directories from the functions it governed — the shape the console incident
252
+ took. A surface now becomes public only by writing `{ auth: false }`, so
253
+ omitting a field can never open anything: the failure mode of a forgotten flag
254
+ is a locked door. `{ path }` additionally overrides where the file is
255
+ generated, which previously could only be set for all features at once via
256
+ `pikkuDir`.
257
+
258
+ The legacy strings are **refused, not coerced**. `resolveScaffoldFeature` throws
259
+ naming the key and its replacement (`"rpc": "no-auth"` → `"rpc": { "auth":
260
+ false }`), and it does so at config load, not downstream. An earlier design used
261
+ a bare `string` for the output path, under which `"no-auth"` would have parsed
262
+ as a file named `no-auth` and every unmigrated config would have silently
263
+ produced nonsense; the object form makes any string invalid, so the failure is
264
+ loud.
265
+
266
+ The collapse is deliberately not uniform in effect. For `rpc` the flag was a
267
+ blanket "no anonymous RPC in this app" set on a dispatcher that cannot know what
268
+ it dispatches to; for `userAdmin` it was redundant, since the generated
269
+ functions are already `pikkuFunc` with `scopes: ['admin:users:list']`. But for
270
+ `agent`, `workflow`, `events` and `scenarios` it is the only gate — those
271
+ generate real endpoints the app never authors — so `true` keeps them
272
+ authenticated rather than opening them as a side effect of a config cleanup.
273
+ `webhook` and `remoteRpc` have no auth dimension at all (`serialize-remote-rpc.ts`
274
+ hardcodes `auth: false`), so an `{ auth }` on them is ignored. The three
275
+ configs in this repo are migrated preserving their current behaviour exactly.
276
+
277
+ **Queue job identities are signed.** A job carried the producer's `pikkuUserId`
278
+ as a plain string and the worker resolved a session from it with no
279
+ verification, so write access to the queue backend was act-as-any-user. The
280
+ identity is now `pq1.<claim>.<hmac>`, HMAC-SHA256 over the claim and the
281
+ canonicalized job payload, keyed by HKDF expansion of a new
282
+ `PIKKU_QUEUE_IDENTITY_SECRET`. Producers opt in by wrapping their queue service
283
+ with `SignedQueueService`. This fails safe rather than closed: with no secret
284
+ configured the identity is dropped and jobs still process, warning once per
285
+ process, so no existing deployment breaks on upgrade — it simply loses queue
286
+ identity until the secret is set. The payload rather than the job id is bound
287
+ because SQS, Cloudflare Queues, Azure and the in-memory service all mint ids
288
+ after `add` returns.
289
+
290
+ **Workflow inline state is read from the run record.** `isInline` consulted a
291
+ process-local `Map`, while `WorkflowRun.inline` is durable. Any instance that
292
+ did not start a run disagreed with the record, so one instance could dispatch a
293
+ queued job for a workflow another was already executing in-process. It is now
294
+ async and resolves through the durable identity, cached only when a context
295
+ already exists so a passive reader allocates nothing. The same `Map` also leaked:
296
+ `nextStepKey` fabricated replay state on every step, and `releaseContext`
297
+ refused to free anything carrying it, so runs whose steps executed outside a
298
+ `beginReplay` bracket — the step-worker queue path — stranded their context and
299
+ step state for the process lifetime. Contexts are now released by an explicit
300
+ execution counter. Step ordinals reset per execution rather than accumulating
301
+ across step-worker invocations in one process, which makes step naming
302
+ independent of how work was distributed.
303
+
304
+ **Secret reads fail loud in every store.** `MongoDBSecretService.getSecrets`
305
+ skipped rows that failed to decrypt, and the redis equivalent dropped every
306
+ rejection via `Promise.allSettled`, including the "No KEK available for
307
+ key_version N" configuration error. Both now throw, naming the key and its key
308
+ version, matching the kysely behaviour. This matters most alongside the KEK
309
+ change above: without it, an upgrade surfaces as a partial secrets map and an
310
+ opaque downstream failure instead of an error naming the secret to re-enter.
311
+
312
+ **A second middleware registration for a pattern no longer erases the first.**
313
+ `addHTTPMiddleware`, `addTagMiddleware` and `addChannelMiddleware` groups are
314
+ keyed by pattern or tag and held one source file each, so a second file's call
315
+ overwrote the first's. Codegen emits its imports from what is stored, so the
316
+ losing file was never imported and its middleware never registered — the
317
+ runtime composes repeated registrations for a pattern happily, and only codegen
318
+ dropped one. Adding an unrelated `addHTTPMiddleware('*', …)` to an app was
319
+ enough to silently unregister the generated better-auth session bridge, which
320
+ fails open and gives no sign until a request arrives without a session. A group
321
+ now carries every registration made for it, and all of them are imported.
322
+
323
+ - Updated dependencies [6a307f0]
324
+ - Updated dependencies [afef587]
325
+ - Updated dependencies [8075f6a]
326
+ - @pikku/core@0.12.74
327
+
3
328
  ## 0.12.12
4
329
 
5
330
  ### Patch Changes
@@ -17,9 +17,13 @@ export declare class MongoDBSecretService implements SecretService {
17
17
  private auditReads;
18
18
  private secrets;
19
19
  private secretsAudit;
20
+ private kekSaltDocs;
21
+ private kekSalts;
22
+ private keks;
20
23
  constructor(db: Db, config: MongoDBSecretServiceConfig);
21
24
  init(): Promise<void>;
22
25
  private logAudit;
26
+ private getKEKSalt;
23
27
  private getKEK;
24
28
  getSecret<T = string>(key: string): Promise<T>;
25
29
  hasSecret(key: string): Promise<boolean>;
@@ -1,4 +1,4 @@
1
- import { envelopeEncrypt, envelopeDecrypt, envelopeRewrap, } from '@pikku/core/crypto-utils';
1
+ import { deriveKEK, generateKEKSalt, envelopeEncrypt, envelopeDecrypt, envelopeRewrap, } from '@pikku/core/crypto-utils';
2
2
  export class MongoDBSecretService {
3
3
  db;
4
4
  initialized = false;
@@ -9,6 +9,9 @@ export class MongoDBSecretService {
9
9
  auditReads;
10
10
  secrets;
11
11
  secretsAudit;
12
+ kekSaltDocs;
13
+ kekSalts = new Map();
14
+ keks = new Map();
12
15
  constructor(db, config) {
13
16
  this.db = db;
14
17
  this.key = config.key;
@@ -22,6 +25,7 @@ export class MongoDBSecretService {
22
25
  return;
23
26
  this.secrets = this.db.collection('secrets');
24
27
  this.secretsAudit = this.db.collection('secrets_audit');
28
+ this.kekSaltDocs = this.db.collection('secret_kek_salts');
25
29
  await this.secrets.createIndex({ _id: 1 });
26
30
  if (this.audit) {
27
31
  await this.secretsAudit.createIndex({ secretKey: 1 });
@@ -40,18 +44,34 @@ export class MongoDBSecretService {
40
44
  performedAt: new Date(),
41
45
  });
42
46
  }
43
- getKEK(version) {
44
- if (version === this.keyVersion)
45
- return this.key;
46
- if (this.previousKey)
47
- return this.previousKey;
48
- throw new Error(`No KEK available for key_version ${version}`);
47
+ async getKEKSalt(version) {
48
+ const cached = this.kekSalts.get(version);
49
+ if (cached)
50
+ return cached;
51
+ const doc = await this.kekSaltDocs.findOneAndUpdate({ _id: version }, { $setOnInsert: { salt: generateKEKSalt(), createdAt: new Date() } }, { upsert: true, returnDocument: 'after' });
52
+ if (!doc?.salt) {
53
+ throw new Error(`Failed to persist a KEK salt for key_version ${version}`);
54
+ }
55
+ this.kekSalts.set(version, doc.salt);
56
+ return doc.salt;
57
+ }
58
+ async getKEK(version) {
59
+ const cached = this.keks.get(version);
60
+ if (cached)
61
+ return cached;
62
+ const passphrase = version === this.keyVersion ? this.key : this.previousKey;
63
+ if (!passphrase) {
64
+ throw new Error(`No KEK available for key_version ${version}`);
65
+ }
66
+ const kek = await deriveKEK(passphrase, await this.getKEKSalt(version));
67
+ this.keks.set(version, kek);
68
+ return kek;
49
69
  }
50
70
  async getSecret(key) {
51
71
  const row = await this.secrets.findOne({ _id: key });
52
72
  if (!row)
53
73
  throw new Error('Requested secret not found');
54
- const kek = this.getKEK(row.keyVersion);
74
+ const kek = await this.getKEK(row.keyVersion);
55
75
  const result = await envelopeDecrypt(kek, row.ciphertext, row.wrappedDek);
56
76
  await this.logAudit(key, 'read');
57
77
  return result;
@@ -61,7 +81,7 @@ export class MongoDBSecretService {
61
81
  return count > 0;
62
82
  }
63
83
  async setSecret(key, value) {
64
- const { ciphertext, wrappedDEK } = await envelopeEncrypt(this.key, value);
84
+ const { ciphertext, wrappedDEK } = await envelopeEncrypt(await this.getKEK(this.keyVersion), value);
65
85
  const now = new Date();
66
86
  await this.secrets.updateOne({ _id: key }, {
67
87
  $set: {
@@ -88,11 +108,12 @@ export class MongoDBSecretService {
88
108
  const out = {};
89
109
  for (const row of rows) {
90
110
  try {
91
- const kek = this.getKEK(row.keyVersion);
111
+ const kek = await this.getKEK(row.keyVersion);
92
112
  out[row._id] = await envelopeDecrypt(kek, row.ciphertext, row.wrappedDek);
93
113
  }
94
- catch {
95
- // skip secrets that fail to decrypt
114
+ catch (cause) {
115
+ throw new Error(`Failed to decrypt secret "${row._id}" (key_version ${row.keyVersion}): ` +
116
+ `the configured KEK does not match the key it was wrapped under`, { cause });
96
117
  }
97
118
  }
98
119
  return out;
@@ -105,8 +126,10 @@ export class MongoDBSecretService {
105
126
  .find({ keyVersion: { $lt: this.keyVersion } })
106
127
  .project({ _id: 1, wrappedDek: 1 })
107
128
  .toArray();
129
+ const oldKEK = await this.getKEK(this.keyVersion - 1);
130
+ const newKEK = await this.getKEK(this.keyVersion);
108
131
  for (const row of rows) {
109
- const newWrappedDEK = await envelopeRewrap(this.previousKey, this.key, row.wrappedDek);
132
+ const newWrappedDEK = await envelopeRewrap(oldKEK, newKEK, row.wrappedDek);
110
133
  await this.secrets.updateOne({ _id: row._id }, {
111
134
  $set: {
112
135
  wrappedDek: newWrappedDEK,
@@ -49,7 +49,6 @@ export declare class MongoDBWorkflowService extends PikkuWorkflowService {
49
49
  failedNodeIds: string[];
50
50
  branchKeys: Record<string, string>;
51
51
  }>;
52
- getNodesWithoutSteps(runId: string, nodeIds: string[]): Promise<string[]>;
53
52
  getStepInstances(runId: string): Promise<Array<{
54
53
  stepName: string;
55
54
  status: StepStatus;
@@ -284,19 +284,6 @@ export class MongoDBWorkflowService extends PikkuWorkflowService {
284
284
  }
285
285
  return { completedNodeIds, failedNodeIds, branchKeys };
286
286
  }
287
- async getNodesWithoutSteps(runId, nodeIds) {
288
- if (nodeIds.length === 0)
289
- return [];
290
- const result = await this.steps
291
- .find({
292
- workflowRunId: runId,
293
- stepName: { $in: nodeIds },
294
- })
295
- .project({ stepName: 1 })
296
- .toArray();
297
- const existingStepNames = new Set(result.map((r) => r.stepName));
298
- return nodeIds.filter((id) => !existingStepNames.has(id));
299
- }
300
287
  async getStepInstances(runId) {
301
288
  const rows = await this.steps
302
289
  .find({ workflowRunId: runId })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pikku/mongodb",
3
- "version": "0.12.12",
3
+ "version": "0.12.13",
4
4
  "description": "MongoDB services for Pikku — workflow state, agent runs, secrets and channel stores",
5
5
  "author": "yasser.fadl@gmail.com",
6
6
  "license": "MIT",
@@ -18,7 +18,7 @@
18
18
  "prepublishOnly": "yarn build"
19
19
  },
20
20
  "peerDependencies": {
21
- "@pikku/core": "^0.12.71"
21
+ "@pikku/core": "^0.12.74"
22
22
  },
23
23
  "dependencies": {
24
24
  "mongodb": "^6.12.0"
@@ -1,6 +1,8 @@
1
1
  import type { SecretService } from '@pikku/core/services'
2
2
  import type { Db, Collection } from 'mongodb'
3
3
  import {
4
+ deriveKEK,
5
+ generateKEKSalt,
4
6
  envelopeEncrypt,
5
7
  envelopeDecrypt,
6
8
  envelopeRewrap,
@@ -23,6 +25,12 @@ interface SecretDoc {
23
25
  updatedAt: Date
24
26
  }
25
27
 
28
+ interface KekSaltDoc {
29
+ _id: number
30
+ salt: string
31
+ createdAt: Date
32
+ }
33
+
26
34
  interface SecretAuditDoc {
27
35
  _id: string
28
36
  secretKey: string
@@ -39,6 +47,9 @@ export class MongoDBSecretService implements SecretService {
39
47
  private auditReads: boolean
40
48
  private secrets!: Collection<SecretDoc>
41
49
  private secretsAudit!: Collection<SecretAuditDoc>
50
+ private kekSaltDocs!: Collection<KekSaltDoc>
51
+ private kekSalts = new Map<number, string>()
52
+ private keks = new Map<number, CryptoKey>()
42
53
 
43
54
  constructor(
44
55
  private db: Db,
@@ -56,6 +67,7 @@ export class MongoDBSecretService implements SecretService {
56
67
 
57
68
  this.secrets = this.db.collection<SecretDoc>('secrets')
58
69
  this.secretsAudit = this.db.collection<SecretAuditDoc>('secrets_audit')
70
+ this.kekSaltDocs = this.db.collection<KekSaltDoc>('secret_kek_salts')
59
71
 
60
72
  await this.secrets.createIndex({ _id: 1 })
61
73
 
@@ -81,17 +93,42 @@ export class MongoDBSecretService implements SecretService {
81
93
  })
82
94
  }
83
95
 
84
- private getKEK(version: number): string {
85
- if (version === this.keyVersion) return this.key
86
- if (this.previousKey) return this.previousKey
87
- throw new Error(`No KEK available for key_version ${version}`)
96
+ private async getKEKSalt(version: number): Promise<string> {
97
+ const cached = this.kekSalts.get(version)
98
+ if (cached) return cached
99
+
100
+ const doc = await this.kekSaltDocs.findOneAndUpdate(
101
+ { _id: version },
102
+ { $setOnInsert: { salt: generateKEKSalt(), createdAt: new Date() } },
103
+ { upsert: true, returnDocument: 'after' }
104
+ )
105
+ if (!doc?.salt) {
106
+ throw new Error(`Failed to persist a KEK salt for key_version ${version}`)
107
+ }
108
+
109
+ this.kekSalts.set(version, doc.salt)
110
+ return doc.salt
111
+ }
112
+
113
+ private async getKEK(version: number): Promise<CryptoKey> {
114
+ const cached = this.keks.get(version)
115
+ if (cached) return cached
116
+
117
+ const passphrase = version === this.keyVersion ? this.key : this.previousKey
118
+ if (!passphrase) {
119
+ throw new Error(`No KEK available for key_version ${version}`)
120
+ }
121
+
122
+ const kek = await deriveKEK(passphrase, await this.getKEKSalt(version))
123
+ this.keks.set(version, kek)
124
+ return kek
88
125
  }
89
126
 
90
127
  async getSecret<T = string>(key: string): Promise<T> {
91
128
  const row = await this.secrets.findOne({ _id: key })
92
129
  if (!row) throw new Error('Requested secret not found')
93
130
 
94
- const kek = this.getKEK(row.keyVersion)
131
+ const kek = await this.getKEK(row.keyVersion)
95
132
  const result = await envelopeDecrypt<T>(kek, row.ciphertext, row.wrappedDek)
96
133
  await this.logAudit(key, 'read')
97
134
  return result
@@ -103,7 +140,10 @@ export class MongoDBSecretService implements SecretService {
103
140
  }
104
141
 
105
142
  async setSecret(key: string, value: unknown): Promise<void> {
106
- const { ciphertext, wrappedDEK } = await envelopeEncrypt(this.key, value)
143
+ const { ciphertext, wrappedDEK } = await envelopeEncrypt(
144
+ await this.getKEK(this.keyVersion),
145
+ value
146
+ )
107
147
  const now = new Date()
108
148
 
109
149
  await this.secrets.updateOne(
@@ -131,23 +171,27 @@ export class MongoDBSecretService implements SecretService {
131
171
  await this.logAudit(key, 'delete')
132
172
  }
133
173
 
134
- async getSecrets<
135
- T extends Record<string, unknown> = Record<string, unknown>,
136
- >(keys: (keyof T & string)[]): Promise<T> {
174
+ async getSecrets<T extends Record<string, unknown> = Record<string, unknown>>(
175
+ keys: (keyof T & string)[]
176
+ ): Promise<T> {
137
177
  const rows = await this.secrets
138
178
  .find({ _id: { $in: keys } } as any)
139
179
  .toArray()
140
180
  const out: Record<string, unknown> = {}
141
181
  for (const row of rows) {
142
182
  try {
143
- const kek = this.getKEK(row.keyVersion)
183
+ const kek = await this.getKEK(row.keyVersion)
144
184
  out[row._id] = await envelopeDecrypt(
145
185
  kek,
146
186
  row.ciphertext,
147
187
  row.wrappedDek
148
188
  )
149
- } catch {
150
- // skip secrets that fail to decrypt
189
+ } catch (cause) {
190
+ throw new Error(
191
+ `Failed to decrypt secret "${row._id}" (key_version ${row.keyVersion}): ` +
192
+ `the configured KEK does not match the key it was wrapped under`,
193
+ { cause }
194
+ )
151
195
  }
152
196
  }
153
197
  return out as T
@@ -163,12 +207,11 @@ export class MongoDBSecretService implements SecretService {
163
207
  .project({ _id: 1, wrappedDek: 1 })
164
208
  .toArray()
165
209
 
210
+ const oldKEK = await this.getKEK(this.keyVersion - 1)
211
+ const newKEK = await this.getKEK(this.keyVersion)
212
+
166
213
  for (const row of rows) {
167
- const newWrappedDEK = await envelopeRewrap(
168
- this.previousKey,
169
- this.key,
170
- row.wrappedDek
171
- )
214
+ const newWrappedDEK = await envelopeRewrap(oldKEK, newKEK, row.wrappedDek)
172
215
  await this.secrets.updateOne(
173
216
  { _id: row._id },
174
217
  {
@@ -106,6 +106,84 @@ function registerTests(name: string, getDb: () => Db) {
106
106
  assert.equal(logs[0]!.action, 'write')
107
107
  })
108
108
  })
109
+
110
+ describe(`MongoDBSecretService decryption failures [${name}]`, () => {
111
+ const kek = 'test-key-encryption-key-32chars!'
112
+ const otherKek = 'a-totally-different-kek-32chars!'
113
+
114
+ test('getSecrets throws naming a secret it cannot decrypt', async () => {
115
+ const writer = new MongoDBSecretService(getDb(), { key: kek })
116
+ await writer.init()
117
+ await writer.setSecret('undecryptable', { token: 'abc' })
118
+
119
+ const reader = new MongoDBSecretService(getDb(), { key: otherKek })
120
+ await reader.init()
121
+
122
+ await assert.rejects(
123
+ () => reader.getSecrets(['undecryptable']),
124
+ (error: Error) => {
125
+ assert.equal(
126
+ error.message,
127
+ 'Failed to decrypt secret "undecryptable" (key_version 1): ' +
128
+ 'the configured KEK does not match the key it was wrapped under'
129
+ )
130
+ assert.ok(error.cause, 'the underlying crypto failure is preserved')
131
+ return true
132
+ }
133
+ )
134
+ })
135
+
136
+ test('getSecrets throws when no KEK is available for a stored key_version', async () => {
137
+ const writer = new MongoDBSecretService(getDb(), {
138
+ key: kek,
139
+ keyVersion: 7,
140
+ })
141
+ await writer.init()
142
+ await writer.setSecret('old-version', 'value')
143
+
144
+ const reader = new MongoDBSecretService(getDb(), {
145
+ key: kek,
146
+ keyVersion: 8,
147
+ })
148
+ await reader.init()
149
+
150
+ await assert.rejects(
151
+ () => reader.getSecrets(['old-version']),
152
+ (error: Error) => {
153
+ assert.equal(
154
+ error.message,
155
+ 'Failed to decrypt secret "old-version" (key_version 7): ' +
156
+ 'the configured KEK does not match the key it was wrapped under'
157
+ )
158
+ return true
159
+ }
160
+ )
161
+ })
162
+
163
+ test('getSecrets returns every secret when they all decrypt', async () => {
164
+ const service = new MongoDBSecretService(getDb(), { key: kek })
165
+ await service.init()
166
+ await service.setSecret('batch-a', { v: 1 })
167
+ await service.setSecret('batch-b', 'two')
168
+
169
+ const out = await service.getSecrets<{
170
+ 'batch-a': { v: number }
171
+ 'batch-b': string
172
+ }>(['batch-a', 'batch-b'])
173
+
174
+ assert.deepEqual(out, { 'batch-a': { v: 1 }, 'batch-b': 'two' })
175
+ })
176
+
177
+ test('getSecrets omits keys that were never stored', async () => {
178
+ const service = new MongoDBSecretService(getDb(), { key: kek })
179
+ await service.init()
180
+ await service.setSecret('present', 'here')
181
+
182
+ const out = await service.getSecrets(['present', 'never-stored'])
183
+
184
+ assert.deepEqual(out, { present: 'here' })
185
+ })
186
+ })
109
187
  }
110
188
 
111
189
  describe('MongoDB Services - In-Memory', () => {
@@ -487,24 +487,6 @@ export class MongoDBWorkflowService extends PikkuWorkflowService {
487
487
  return { completedNodeIds, failedNodeIds, branchKeys }
488
488
  }
489
489
 
490
- async getNodesWithoutSteps(
491
- runId: string,
492
- nodeIds: string[]
493
- ): Promise<string[]> {
494
- if (nodeIds.length === 0) return []
495
-
496
- const result = await this.steps
497
- .find({
498
- workflowRunId: runId,
499
- stepName: { $in: nodeIds },
500
- })
501
- .project({ stepName: 1 })
502
- .toArray()
503
-
504
- const existingStepNames = new Set(result.map((r) => r.stepName))
505
- return nodeIds.filter((id) => !existingStepNames.has(id))
506
- }
507
-
508
490
  async getStepInstances(
509
491
  runId: string
510
492
  ): Promise<