@ultimat3/core 30.8.0 → 31.0.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/CLAUDE.md +54 -58
- package/README.md +70 -27
- package/package.json +3 -2
- package/src/adapter-slot.ts +42 -0
- package/src/adapter.ts +24 -0
- package/src/audit.ts +31 -14
- package/src/aws-sigv4.ts +9 -3
- package/src/cache-vocabulary.ts +1 -1
- package/src/client-problem.ts +10 -3
- package/src/clock.ts +68 -14
- package/src/config-defaults.ts +4 -4
- package/src/config-keys.ts +1 -1
- package/src/config-removed.ts +11 -1
- package/src/config.ts +16 -20
- package/src/context.ts +2 -2
- package/src/cursor.ts +3 -11
- package/src/der.ts +65 -0
- package/src/error-render.ts +27 -1
- package/src/error-reporter-chain.ts +1 -1
- package/src/error-reporter-sentry-hooks.ts +85 -0
- package/src/error-reporter-sentry.ts +99 -7
- package/src/error-reporter.ts +39 -11
- package/src/errors.ts +3 -1
- package/src/exports/error-contract.ts +4 -14
- package/src/exports/observability.ts +57 -17
- package/src/exports/secrets.ts +4 -18
- package/src/failure-capture.ts +22 -13
- package/src/gate-shapes.ts +88 -0
- package/src/host-rules.ts +13 -1
- package/src/html-escape.ts +7 -1
- package/src/ids.ts +59 -10
- package/src/image/png-pixels.ts +14 -8
- package/src/index.ts +123 -136
- package/src/instrument-events.ts +272 -0
- package/src/instrument-semconv.ts +65 -0
- package/src/instrument-stacks.ts +38 -0
- package/src/instrument.ts +194 -0
- package/src/keyed-fingerprint.ts +7 -11
- package/src/lifecycle-signals.ts +5 -3
- package/src/lifecycle.ts +27 -0
- package/src/logger-record.ts +35 -0
- package/src/logger.ts +24 -16
- package/src/mcp-exposure.ts +17 -0
- package/src/metrics-semconv.ts +124 -0
- package/src/metrics.ts +19 -16
- package/src/otlp-batch.ts +78 -0
- package/src/otlp-log-exporter.ts +180 -0
- package/src/otlp-metric-exporter.ts +2 -1
- package/src/otlp-post.ts +22 -2
- package/src/otlp-span-exporter.ts +10 -59
- package/src/otlp.ts +27 -9
- package/src/outbound-instrument.ts +82 -0
- package/src/outbound.ts +5 -4
- package/src/page-meta.ts +6 -0
- package/src/page.ts +23 -5
- package/src/registrar.ts +269 -0
- package/src/route-vocabulary.ts +11 -2
- package/src/safe-url.ts +10 -8
- package/src/seal-errors.ts +14 -4
- package/src/seal-keys.ts +11 -7
- package/src/secret-source.ts +125 -0
- package/src/secrets-errors.ts +32 -12
- package/src/secrets-store.ts +30 -26
- package/src/sleep-clock.ts +70 -0
- package/src/telemetry.ts +90 -31
- package/src/type-pins.ts +19 -16
- package/src/log-tee.ts +0 -73
package/src/registrar.ts
CHANGED
|
@@ -84,8 +84,11 @@ export const PRIMITIVE_FACTORIES = Object.freeze<readonly PrimitiveFactory[]>(
|
|
|
84
84
|
{ factory: 'consumer', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
85
85
|
{ factory: 'exportRows', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
86
86
|
{ factory: 'importRows', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
87
|
+
{ factory: 'ledger', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
88
|
+
{ factory: 'partitions', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
87
89
|
{ factory: 'purge', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
88
90
|
{ factory: 'refreshView', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
91
|
+
{ factory: 'reindex', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
89
92
|
{ factory: 'rollup', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
90
93
|
{ factory: 'subjectErase', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
91
94
|
{ factory: 'subjectExport', pkg: '@ultimat3/jobs', kind: 'job' },
|
|
@@ -99,11 +102,277 @@ export const PRIMITIVE_FACTORIES = Object.freeze<readonly PrimitiveFactory[]>(
|
|
|
99
102
|
{ factory: 'pushUnsubscribe', pkg: '@ultimat3/pwa', kind: 'action' },
|
|
100
103
|
// A route: the value IS a websocket mount the web role serves (`path`, `fetch`, `websocket`).
|
|
101
104
|
{ factory: 'deviceSocket', pkg: '@ultimat3/realtime', kind: 'route' },
|
|
105
|
+
{ factory: 'pdf', pkg: '@ultimat3/scraping', kind: 'action' },
|
|
102
106
|
{ factory: 'scrape', pkg: '@ultimat3/scraping', kind: 'job' },
|
|
103
107
|
] satisfies readonly PrimitiveFactory[]
|
|
104
108
|
).map((entry) => Object.freeze(entry)),
|
|
105
109
|
);
|
|
106
110
|
|
|
111
|
+
/**
|
|
112
|
+
* One swappable backend: the slot an app fills in `apps/<app>/runtime.ts`, the package that owns
|
|
113
|
+
* the interface, the default the boot builds from the environment when that file says nothing, and
|
|
114
|
+
* the `@ultimat3/testing` suite a hand-written adapter proves itself with.
|
|
115
|
+
*/
|
|
116
|
+
export interface AdapterPort {
|
|
117
|
+
/** The `RuntimeOverrides` field and the key of `@ultimat3/testing`'s per-test override seam. */
|
|
118
|
+
readonly port: string;
|
|
119
|
+
/** The package whose `./adapter` subpath exports `interface` and the errors a driver throws. */
|
|
120
|
+
readonly pkg: `@ultimat3/${string}`;
|
|
121
|
+
/** The type an adapter author implements, exported under this exact name by `pkg`. */
|
|
122
|
+
readonly interface: string;
|
|
123
|
+
/** What the boot builds when `runtime.ts` is silent — the only other selector there is. */
|
|
124
|
+
readonly envDefault: string;
|
|
125
|
+
/** `<interface>Conformance` in `@ultimat3/testing`. Named before it exists: part B enforces it. */
|
|
126
|
+
readonly conformance: `${string}Conformance`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const PG = 'over DATABASE_URL';
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Every backend an app may swap, one row each (plan 101 slice 21). Extension is adapters, wrapped
|
|
133
|
+
* primitives and event subscriptions — there is no plugin API (axiom 8) — so a backend with no row
|
|
134
|
+
* here is one an app cannot replace without a fork. Six ways to swap a backend (setters, constructor
|
|
135
|
+
* arguments, env switches, a config enum) collapse into one: a field of `runtime.ts`, keyed by
|
|
136
|
+
* `port`. `@ultimat3/cli`'s boot loops over this table; `X_ADAPTER_PORT_INCOMPLETE` (part B) refuses
|
|
137
|
+
* a row missing its slot, its `./adapter` export or its suite, and an exported setter beside it.
|
|
138
|
+
*
|
|
139
|
+
* Sorted by port name. Rows and list frozen, like `PRIMITIVE_FACTORIES`.
|
|
140
|
+
*/
|
|
141
|
+
const ADAPTER_PORT_ROWS = [
|
|
142
|
+
{
|
|
143
|
+
port: 'aiGateway',
|
|
144
|
+
pkg: '@ultimat3/ai',
|
|
145
|
+
interface: 'Gateway',
|
|
146
|
+
envDefault: 'none: the first model call is refused (X_AI_GATEWAY_MISSING)',
|
|
147
|
+
conformance: 'gatewayConformance',
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
port: 'auditSink',
|
|
151
|
+
pkg: '@ultimat3/core',
|
|
152
|
+
interface: 'AuditSink',
|
|
153
|
+
envDefault: 'none: an audit: true primitive is refused (X_AUDIT_SINK_MISSING)',
|
|
154
|
+
conformance: 'auditSinkConformance',
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
port: 'authLimiter',
|
|
158
|
+
pkg: '@ultimat3/auth',
|
|
159
|
+
interface: 'AuthLimiterFactory',
|
|
160
|
+
envDefault: `postgresAuthLimiter ${PG}`,
|
|
161
|
+
conformance: 'authLimiterFactoryConformance',
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
port: 'authServerStore',
|
|
165
|
+
pkg: '@ultimat3/auth',
|
|
166
|
+
interface: 'AuthServerStore',
|
|
167
|
+
envDefault: `postgresAuthServerStore ${PG}`,
|
|
168
|
+
conformance: 'authServerStoreConformance',
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
port: 'cacheTiers',
|
|
172
|
+
pkg: '@ultimat3/cache',
|
|
173
|
+
interface: 'CacheTier',
|
|
174
|
+
envDefault: "the rungs cache.tiers names; 'redis' reads REDIS_URL",
|
|
175
|
+
conformance: 'cacheTierConformance',
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
port: 'clock',
|
|
179
|
+
pkg: '@ultimat3/core',
|
|
180
|
+
interface: 'SleepClock',
|
|
181
|
+
envDefault: 'systemClock',
|
|
182
|
+
conformance: 'sleepClockConformance',
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
port: 'embedder',
|
|
186
|
+
pkg: '@ultimat3/ai',
|
|
187
|
+
interface: 'Embedder',
|
|
188
|
+
envDefault: 'hashEmbedder: deterministic, offline and lexical, never a network call',
|
|
189
|
+
conformance: 'embedderConformance',
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
port: 'errorReporter',
|
|
193
|
+
pkg: '@ultimat3/core',
|
|
194
|
+
interface: 'ErrorReporter',
|
|
195
|
+
envDefault: 'SENTRY_DSN set: sentryErrorReporter; unset: the log line only',
|
|
196
|
+
conformance: 'errorReporterConformance',
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
port: 'eventBus',
|
|
200
|
+
pkg: '@ultimat3/jobs',
|
|
201
|
+
interface: 'EventBus',
|
|
202
|
+
envDefault: `postgresEventBus ${PG}`,
|
|
203
|
+
conformance: 'eventBusConformance',
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
port: 'failureStore',
|
|
207
|
+
pkg: '@ultimat3/core',
|
|
208
|
+
interface: 'FailureStore',
|
|
209
|
+
envDefault: `postgresFailureStore ${PG}, while failureCapture is on`,
|
|
210
|
+
conformance: 'failureStoreConformance',
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
port: 'idempotencyStore',
|
|
214
|
+
pkg: '@ultimat3/action',
|
|
215
|
+
interface: 'IdempotencyStore',
|
|
216
|
+
envDefault: `postgresIdempotencyStore ${PG}`,
|
|
217
|
+
conformance: 'idempotencyStoreConformance',
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
port: 'images',
|
|
221
|
+
pkg: '@ultimat3/seo',
|
|
222
|
+
interface: 'ImageTransformDriver',
|
|
223
|
+
envDefault: 'builtinImageDriver',
|
|
224
|
+
conformance: 'imageTransformDriverConformance',
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
port: 'isrStore',
|
|
228
|
+
pkg: '@ultimat3/render',
|
|
229
|
+
interface: 'IsrStore',
|
|
230
|
+
envDefault: 'a memory store per process',
|
|
231
|
+
conformance: 'isrStoreConformance',
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
port: 'jobs',
|
|
235
|
+
pkg: '@ultimat3/jobs',
|
|
236
|
+
interface: 'JobDriver',
|
|
237
|
+
envDefault: `postgresJobDriver ${PG}`,
|
|
238
|
+
conformance: 'jobDriverConformance',
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
port: 'mail',
|
|
242
|
+
pkg: '@ultimat3/mail',
|
|
243
|
+
interface: 'MailDriver',
|
|
244
|
+
envDefault:
|
|
245
|
+
'SMTP_URL: smtp; RESEND_API_KEY: resend; SES_REGION: ses; none: memory in development/test, refused at the first send elsewhere',
|
|
246
|
+
conformance: 'mailDriverConformance',
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
port: 'mcpConfirmationStore',
|
|
250
|
+
pkg: '@ultimat3/mcp',
|
|
251
|
+
interface: 'McpConfirmationStore',
|
|
252
|
+
envDefault: `postgresConfirmationStore ${PG}`,
|
|
253
|
+
conformance: 'mcpConfirmationStoreConformance',
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
port: 'meterStore',
|
|
257
|
+
pkg: '@ultimat3/action',
|
|
258
|
+
interface: 'MeterStore',
|
|
259
|
+
envDefault: `postgresMeterStore ${PG}`,
|
|
260
|
+
conformance: 'meterStoreConformance',
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
port: 'notifyStores',
|
|
264
|
+
pkg: '@ultimat3/notify',
|
|
265
|
+
interface: 'NotifyStores',
|
|
266
|
+
envDefault:
|
|
267
|
+
'Postgres delivery ledger when the app has a database; memory in development/test; refused elsewhere (X_NOTIFY_STORE_MISSING); allow-all preferences, no inbox, no digest',
|
|
268
|
+
conformance: 'notifyStoresConformance',
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
port: 'outboxStore',
|
|
272
|
+
pkg: '@ultimat3/jobs',
|
|
273
|
+
interface: 'OutboxStore',
|
|
274
|
+
envDefault: `postgresOutboxStore ${PG}`,
|
|
275
|
+
conformance: 'outboxStoreConformance',
|
|
276
|
+
},
|
|
277
|
+
{
|
|
278
|
+
port: 'purge',
|
|
279
|
+
pkg: '@ultimat3/cache',
|
|
280
|
+
interface: 'PurgeDriver',
|
|
281
|
+
envDefault:
|
|
282
|
+
'CLOUDFLARE_API_TOKEN + CLOUDFLARE_ZONE_ID: cloudflare; FASTLY_API_TOKEN + FASTLY_SERVICE_ID: fastly; neither: none',
|
|
283
|
+
conformance: 'purgeDriverConformance',
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
port: 'pushSubscriptionStore',
|
|
287
|
+
pkg: '@ultimat3/pwa',
|
|
288
|
+
interface: 'PushSubscriptionStore',
|
|
289
|
+
envDefault: `postgresPushSubscriptionStore ${PG}, while VAPID keys are set`,
|
|
290
|
+
conformance: 'pushSubscriptionStoreConformance',
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
port: 'rateLimitStore',
|
|
294
|
+
pkg: '@ultimat3/http',
|
|
295
|
+
interface: 'RateLimitStore',
|
|
296
|
+
envDefault: `postgresRateLimitStore ${PG}`,
|
|
297
|
+
conformance: 'rateLimitStoreConformance',
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
port: 'scrapeDriver',
|
|
301
|
+
pkg: '@ultimat3/scraping',
|
|
302
|
+
interface: 'ScrapeDriver',
|
|
303
|
+
envDefault: 'none: a scrape() declaring no driver is refused at its first run',
|
|
304
|
+
conformance: 'scrapeDriverConformance',
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
port: 'searchIndex',
|
|
308
|
+
pkg: '@ultimat3/entity',
|
|
309
|
+
interface: 'SearchIndex',
|
|
310
|
+
envDefault: "postgresSearchIndex: .searchable()'s generated tsvector, over the app's pool",
|
|
311
|
+
conformance: 'searchIndexConformance',
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
port: 'secretSource',
|
|
315
|
+
pkg: '@ultimat3/core',
|
|
316
|
+
interface: 'SecretSource',
|
|
317
|
+
envDefault: 'ULTIMATE_SECRETS_KEY, then the .secrets.key file',
|
|
318
|
+
conformance: 'secretSourceConformance',
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
port: 'semanticCache',
|
|
322
|
+
pkg: '@ultimat3/ai',
|
|
323
|
+
interface: 'SemanticCacheFactory',
|
|
324
|
+
envDefault: 'a memorySemanticCache per scope, per process',
|
|
325
|
+
conformance: 'semanticCacheFactoryConformance',
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
port: 'smsTransport',
|
|
329
|
+
pkg: '@ultimat3/notify',
|
|
330
|
+
interface: 'SmsTransport',
|
|
331
|
+
envDefault:
|
|
332
|
+
'memory in development/test; refused at the first send elsewhere (X_NOTIFY_SMS_TRANSPORT_MISSING)',
|
|
333
|
+
conformance: 'smsTransportConformance',
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
port: 'storage',
|
|
337
|
+
pkg: '@ultimat3/storage',
|
|
338
|
+
interface: 'StorageDriver',
|
|
339
|
+
envDefault: 'S3_ENDPOINT set: s3; unset: the embedded disk',
|
|
340
|
+
conformance: 'storageDriverConformance',
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
port: 'totpReplay',
|
|
344
|
+
pkg: '@ultimat3/auth',
|
|
345
|
+
interface: 'TotpReplayGuard',
|
|
346
|
+
envDefault: `postgresTotpReplayGuard ${PG}`,
|
|
347
|
+
conformance: 'totpReplayGuardConformance',
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
port: 'transport',
|
|
351
|
+
pkg: '@ultimat3/realtime',
|
|
352
|
+
interface: 'Transport',
|
|
353
|
+
envDefault: 'NATS_URL set: nats; unset: memory (one node)',
|
|
354
|
+
conformance: 'transportConformance',
|
|
355
|
+
},
|
|
356
|
+
{
|
|
357
|
+
port: 'uploadScanner',
|
|
358
|
+
pkg: '@ultimat3/storage',
|
|
359
|
+
interface: 'UploadScanner',
|
|
360
|
+
envDefault: 'none: promoteAttachment scans nothing',
|
|
361
|
+
conformance: 'uploadScannerConformance',
|
|
362
|
+
},
|
|
363
|
+
] as const satisfies readonly AdapterPort[];
|
|
364
|
+
|
|
365
|
+
/** One port's name — `Record<AdapterPortName, …>` is how a consumer is made to cover every row. */
|
|
366
|
+
export type AdapterPortName = (typeof ADAPTER_PORT_ROWS)[number]['port'];
|
|
367
|
+
|
|
368
|
+
export const ADAPTER_PORTS = Object.freeze<readonly AdapterPort[]>(
|
|
369
|
+
ADAPTER_PORT_ROWS.map((row) => Object.freeze({ ...row })),
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
export const ADAPTER_PORT_NAMES: readonly AdapterPortName[] = Object.freeze(
|
|
373
|
+
ADAPTER_PORT_ROWS.map((row) => row.port),
|
|
374
|
+
);
|
|
375
|
+
|
|
107
376
|
/**
|
|
108
377
|
* What a registrar hands back: the primitives it actually took, each carrying the name
|
|
109
378
|
* registration stamped on it. Returning the registered set — rather than nothing — is what lets
|
package/src/route-vocabulary.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
// Single responsibility: the
|
|
2
|
-
// how it survives offline, when it hydrates. Tier 0 so every package that names one
|
|
1
|
+
// Single responsibility: the closed vocabularies a route is declared in — where it lives, how it
|
|
2
|
+
// renders, how it survives offline, when it hydrates. Tier 0 so every package that names one
|
|
3
|
+
// imports it.
|
|
3
4
|
// Deliberately not `config.ts`: `app.config.ts` CONSUMES `OfflineStrategy`, it does not own it.
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -21,3 +22,11 @@ export type OfflineStrategy = (typeof OFFLINE_STRATEGIES)[number];
|
|
|
21
22
|
|
|
22
23
|
export const HYDRATE_STRATEGIES = ['idle', 'visible', 'interaction', 'never'] as const;
|
|
23
24
|
export type HydrateStrategy = (typeof HYDRATE_STRATEGIES)[number];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The directories an app's code lives in: `site/` and `app/` hold routes, `api/` the projections
|
|
28
|
+
* `defineApi()` collects, `shared/` what the others import. `@ultimat3/render` owns the boundary
|
|
29
|
+
* between them (`SURFACE_SPECS`); `@ultimat3/seo` reads a route's. One declaration, here.
|
|
30
|
+
*/
|
|
31
|
+
export const SURFACES = ['site', 'app', 'api', 'shared'] as const;
|
|
32
|
+
export type Surface = (typeof SURFACES)[number];
|
package/src/safe-url.ts
CHANGED
|
@@ -20,6 +20,13 @@ const SCHEME = /^[a-zA-Z][a-zA-Z0-9+.-]*:/;
|
|
|
20
20
|
/** The prefix the `src` exemption is written against — see `safeUrl`'s last branch. */
|
|
21
21
|
const DATA_IMAGE = 'data:image/';
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Every code unit up to U+0020, and DEL. Spelled as the complement of `!`..U+FFFF so the pattern
|
|
25
|
+
* holds no control character (Biome refuses one, rightly): a surrogate is above U+0020 either way,
|
|
26
|
+
* so by code unit this is the same set the browser strips by code point.
|
|
27
|
+
*/
|
|
28
|
+
const CONTROL_OR_SPACE = /[^!-\uffff]|\x7f/g;
|
|
29
|
+
|
|
23
30
|
/**
|
|
24
31
|
* The value to emit, or `null` when the attribute must not be emitted at all. An anchor with no
|
|
25
32
|
* `href` is inert and still renders its text, which is strictly better than a live one nobody
|
|
@@ -31,14 +38,9 @@ export function safeUrl(value: string, attribute: string): string | null {
|
|
|
31
38
|
// A browser deletes TAB, CR and LF anywhere in a URL and trims leading C0 controls and spaces
|
|
32
39
|
// before parsing it, so `java\tscript:alert(1)` is `javascript:` by the time it is followed —
|
|
33
40
|
// the scheme has to be read off the stripped form, never off the raw one.
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
const stripped =
|
|
37
|
-
.filter((char) => {
|
|
38
|
-
const code = char.codePointAt(0) ?? 0;
|
|
39
|
-
return code > 0x20 && code !== 0x7f;
|
|
40
|
-
})
|
|
41
|
-
.join('');
|
|
41
|
+
// One native pass (`CONTROL_OR_SPACE`): an href per table row made a per-character copy the
|
|
42
|
+
// SSR walker's second-largest cost (plan 101 slice 14).
|
|
43
|
+
const stripped = value.replace(CONTROL_OR_SPACE, '');
|
|
42
44
|
const matched = SCHEME.exec(stripped);
|
|
43
45
|
// No scheme at all: relative, absolute-path, protocol-relative, query or fragment. Nothing here
|
|
44
46
|
// can execute, and refusing them would refuse most of the links an app writes.
|
package/src/seal-errors.ts
CHANGED
|
@@ -13,12 +13,22 @@ import { UltimateError } from './errors';
|
|
|
13
13
|
* the key was missing is the failure a sealed column exists to prevent, and it would look healthy.
|
|
14
14
|
*/
|
|
15
15
|
export class SealKeyMissingError extends UltimateError {
|
|
16
|
-
|
|
16
|
+
/** `source`: the `name` of an app-installed `SecretSource` that answered no key. */
|
|
17
|
+
constructor(input: { envVar: string; keyPath: string; source?: string }) {
|
|
17
18
|
super({
|
|
18
19
|
code: 'X_SEAL_KEY_MISSING',
|
|
19
|
-
cause:
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
cause:
|
|
21
|
+
input.source === undefined
|
|
22
|
+
? `${input.envVar} is unset and ${input.keyPath} does not exist, so there is no master key to seal or open a value with`
|
|
23
|
+
: `the secret source ${renderCauseValue(input.source)} installed from runtime.ts answered no master key, so there is no key to seal or open a value with`,
|
|
24
|
+
fix:
|
|
25
|
+
input.source === undefined
|
|
26
|
+
? 'x secrets init # or, where the key already exists: export ULTIMATE_SECRETS_KEY="$(cat .secrets.key)"'
|
|
27
|
+
: 'export const runtime = { secretSource } # apps/<app>/runtime.ts — that source must answer a master key (check the KMS key or Vault path it reads)',
|
|
28
|
+
meta: {
|
|
29
|
+
keyPath: input.keyPath,
|
|
30
|
+
...(input.source === undefined ? {} : { source: input.source }),
|
|
31
|
+
},
|
|
22
32
|
});
|
|
23
33
|
}
|
|
24
34
|
}
|
package/src/seal-keys.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// Single responsibility: the key ring `seal()` and `open()` work under. The CURRENT key is the one
|
|
2
2
|
// `x secrets` already manages — `ULTIMATE_SECRETS_KEY` first, `.secrets.key` second, through
|
|
3
|
-
// `
|
|
3
|
+
// the installed `SecretSource` (`secret-source.ts`), no second variable. RETIRED keys are one more env var, which `x secrets rotate`
|
|
4
4
|
// writes into the committed file and `installSecrets()` carries into the process like any secret.
|
|
5
5
|
|
|
6
6
|
import { SealKeyMissingError } from './seal-errors';
|
|
7
|
+
import { masterKeyPath, resolveMasterKey, SECRETS_KEY_ENV, secretSource } from './secret-source';
|
|
7
8
|
import { importKey, masterKeyId, parseMasterKey } from './secrets';
|
|
8
|
-
import { findMasterKey, masterKeyPath, SECRETS_KEY_ENV } from './secrets-store';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Master keys that no longer seal but still open: 64 hex characters each, separated by commas or
|
|
@@ -91,14 +91,18 @@ async function buildRing(
|
|
|
91
91
|
let memo: { readonly source: string; readonly ring: Promise<SealKeyRing> } | undefined;
|
|
92
92
|
|
|
93
93
|
/** The ring in force now, or `X_SEAL_KEY_MISSING`. A malformed key is `X_SECRETS_KEY_INVALID`. */
|
|
94
|
-
export function resolveSealKeys(source: SealKeySource = {}): Promise<SealKeyRing> {
|
|
94
|
+
export async function resolveSealKeys(source: SealKeySource = {}): Promise<SealKeyRing> {
|
|
95
95
|
const root = source.root ?? process.cwd();
|
|
96
96
|
const env = source.env ?? (process.env as EnvRecord);
|
|
97
|
-
|
|
97
|
+
// The installed `SecretSource`, the one `installSecrets()` asks: a KMS-held key seals too.
|
|
98
|
+
const found = await resolveMasterKey(root, env);
|
|
98
99
|
if (found === undefined) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
const name = secretSource().name;
|
|
101
|
+
throw new SealKeyMissingError({
|
|
102
|
+
envVar: SECRETS_KEY_ENV,
|
|
103
|
+
keyPath: masterKeyPath(root),
|
|
104
|
+
...(name === 'default' ? {} : { source: name }),
|
|
105
|
+
});
|
|
102
106
|
}
|
|
103
107
|
const retired = env[SECRETS_RETIRED_KEYS_ENV] ?? '';
|
|
104
108
|
const key = `${found.hex}\n${retired}`;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Single responsibility: WHERE the master key comes from — the `secretSource` adapter port. Two
|
|
2
|
+
// sources are built in (the env var, the key file) and the default asks them in that order; a KMS or
|
|
3
|
+
// Vault lookup is an app-written `SecretSource` installed from `apps/<app>/runtime.ts`.
|
|
4
|
+
|
|
5
|
+
// why: Bun has no synchronous file read or exists check; the built-in file source is read at boot.
|
|
6
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
7
|
+
// Bun exposes no path-join primitive.
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
import { adapterSlot } from './adapter-slot';
|
|
10
|
+
|
|
11
|
+
/** Never committed — `x secrets init` writes the ignore rule before it writes this. */
|
|
12
|
+
export const SECRETS_KEY_FILE = '.secrets.key';
|
|
13
|
+
/** Read first, so a container gets its key from the platform's secret store and never from a file. */
|
|
14
|
+
export const SECRETS_KEY_ENV = 'ULTIMATE_SECRETS_KEY';
|
|
15
|
+
|
|
16
|
+
export const masterKeyPath = (root: string): string => join(root, SECRETS_KEY_FILE);
|
|
17
|
+
|
|
18
|
+
export interface MasterKeyRef {
|
|
19
|
+
readonly hex: string;
|
|
20
|
+
/** The `name` of the source that found it: `'env'`, `'file'`, or an app's own (`'kms'`). */
|
|
21
|
+
readonly source: string;
|
|
22
|
+
/** Where it was read — a variable name, a path, a key ARN — for an error's `cause`. Never the key. */
|
|
23
|
+
readonly at: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type EnvRecord = Readonly<Record<string, string | undefined>>;
|
|
27
|
+
|
|
28
|
+
export interface SecretSourceContext {
|
|
29
|
+
/** The app root holding `secrets.enc.json` (and, for the file source, `.secrets.key`). */
|
|
30
|
+
readonly root: string;
|
|
31
|
+
readonly env: EnvRecord;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The port. Answers the master key or `undefined` when it has none — never throws for "absent":
|
|
36
|
+
* the caller owns the refusal (`X_SECRETS_KEY_MISSING`), so every source fails the same way.
|
|
37
|
+
* Async allowed: a KMS decrypt or a Vault read is a network call.
|
|
38
|
+
*/
|
|
39
|
+
export interface SecretSource {
|
|
40
|
+
/** Stable label, carried as `MasterKeyRef.source` and in `installSecrets()`'s report. */
|
|
41
|
+
readonly name: string;
|
|
42
|
+
masterKey(
|
|
43
|
+
context: SecretSourceContext,
|
|
44
|
+
): MasterKeyRef | undefined | Promise<MasterKeyRef | undefined>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function fromEnv({ env }: SecretSourceContext): MasterKeyRef | undefined {
|
|
48
|
+
const hex = env[SECRETS_KEY_ENV]?.trim();
|
|
49
|
+
return hex === undefined || hex.length === 0
|
|
50
|
+
? undefined
|
|
51
|
+
: { hex, source: 'env', at: SECRETS_KEY_ENV };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function fromFile({ root }: SecretSourceContext): MasterKeyRef | undefined {
|
|
55
|
+
const path = masterKeyPath(root);
|
|
56
|
+
if (!existsSync(path)) return undefined;
|
|
57
|
+
return { hex: readFileSync(path, 'utf-8').trim(), source: 'file', at: path };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The env var — what one image running everywhere is handed by its platform. */
|
|
61
|
+
export const envSecretSource: SecretSource = Object.freeze({ name: 'env', masterKey: fromEnv });
|
|
62
|
+
|
|
63
|
+
/** The gitignored key file a checkout has. */
|
|
64
|
+
export const fileSecretSource: SecretSource = Object.freeze({ name: 'file', masterKey: fromFile });
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Env var first, key file second — what makes one image run everywhere: a container is handed
|
|
68
|
+
* `ULTIMATE_SECRETS_KEY` and ships no key file, a checkout has the file and exports nothing.
|
|
69
|
+
* Synchronous, because both built-ins are: `findMasterKey` and `x doctor` read it without a boot.
|
|
70
|
+
*/
|
|
71
|
+
export const builtInMasterKey = (context: SecretSourceContext): MasterKeyRef | undefined =>
|
|
72
|
+
fromEnv(context) ?? fromFile(context);
|
|
73
|
+
|
|
74
|
+
export const defaultSecretSource: SecretSource = Object.freeze({
|
|
75
|
+
name: 'default',
|
|
76
|
+
masterKey: builtInMasterKey,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const installed = adapterSlot<SecretSource>(() => defaultSecretSource);
|
|
80
|
+
|
|
81
|
+
/** The source in force. `installSecrets()` and the seal ring both ask it, never a built-in directly. */
|
|
82
|
+
export const secretSource = (): SecretSource => installed.current();
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The installer — on `@ultimat3/core/adapter`, never the barrel: the boot calls it with
|
|
86
|
+
* `runtime.ts`'s `secretSource` BEFORE `app.config.ts` evaluates `installSecrets()`, and
|
|
87
|
+
* `@ultimat3/testing` for one test. Returns the release, which removes this install alone (`adapter-slot.ts`).
|
|
88
|
+
*/
|
|
89
|
+
export function installSecretSource(next: SecretSource): () => void {
|
|
90
|
+
resolved.clear();
|
|
91
|
+
const release = installed.install(next);
|
|
92
|
+
return () => {
|
|
93
|
+
resolved.clear();
|
|
94
|
+
release();
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* An app source's answer, per root, for as long as that source is installed. A KMS decrypt or a
|
|
100
|
+
* Vault read is a network round trip, and the seal ring asks on every sealed read and write — so
|
|
101
|
+
* fifty seals were fifty calls. Only a FOUND key is kept: `undefined` and a rejection are asked
|
|
102
|
+
* again next time. The built-in default is never cached: it is two local reads, and `x secrets
|
|
103
|
+
* rotate` renaming a new key file must be seen on the very next call.
|
|
104
|
+
*/
|
|
105
|
+
const resolved = new Map<string, Promise<MasterKeyRef | undefined>>();
|
|
106
|
+
|
|
107
|
+
/** The installed source's answer for this root and environment. */
|
|
108
|
+
export async function resolveMasterKey(
|
|
109
|
+
root: string,
|
|
110
|
+
env: EnvRecord,
|
|
111
|
+
): Promise<MasterKeyRef | undefined> {
|
|
112
|
+
const source = installed.current();
|
|
113
|
+
if (source === defaultSecretSource) return source.masterKey({ root, env });
|
|
114
|
+
const cached = resolved.get(root);
|
|
115
|
+
if (cached !== undefined) return cached;
|
|
116
|
+
const pending = Promise.resolve(source.masterKey({ root, env }));
|
|
117
|
+
resolved.set(root, pending);
|
|
118
|
+
const forget = (): void => {
|
|
119
|
+
if (resolved.get(root) === pending) resolved.delete(root);
|
|
120
|
+
};
|
|
121
|
+
pending.then((key) => {
|
|
122
|
+
if (key === undefined) forget();
|
|
123
|
+
}, forget);
|
|
124
|
+
return pending;
|
|
125
|
+
}
|
package/src/secrets-errors.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// command that undoes it. No error here ever carries a key, a ciphertext or a decrypted value.
|
|
5
5
|
|
|
6
6
|
import { registerErrorCodes } from './error-codes';
|
|
7
|
-
import { renderFixShellArg } from './error-render';
|
|
7
|
+
import { renderCauseValue, renderFixShellArg } from './error-render';
|
|
8
8
|
import { UltimateError } from './errors';
|
|
9
9
|
|
|
10
10
|
/** Codes `@ultimat3/core` owns for the encrypted-secrets file. */
|
|
@@ -60,36 +60,56 @@ const ENV_VAR_NAME = /^[A-Z_][A-Z0-9_]*$/;
|
|
|
60
60
|
* that authenticates against nothing and reports itself healthy.
|
|
61
61
|
*/
|
|
62
62
|
export class SecretsKeyMissingError extends UltimateError {
|
|
63
|
-
|
|
63
|
+
/** `source`: the `name` of an app-installed `SecretSource` that answered no key. */
|
|
64
|
+
constructor(input: { envVar: string; keyPath: string; source?: string }) {
|
|
64
65
|
super({
|
|
65
66
|
code: 'X_SECRETS_KEY_MISSING',
|
|
66
|
-
cause:
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
cause:
|
|
68
|
+
input.source === undefined
|
|
69
|
+
? `${input.envVar} is unset and ${input.keyPath} does not exist, so nothing can open the encrypted secrets`
|
|
70
|
+
: `the secret source ${renderCauseValue(input.source)} installed from runtime.ts answered no master key, so nothing can open the encrypted secrets`,
|
|
71
|
+
fix:
|
|
72
|
+
input.source !== undefined
|
|
73
|
+
? 'export const runtime = { secretSource } # apps/<app>/runtime.ts — that source must answer a master key (check the KMS key or Vault path it reads); delete it to fall back to ULTIMATE_SECRETS_KEY, then .secrets.key'
|
|
74
|
+
: ENV_VAR_NAME.test(input.envVar)
|
|
75
|
+
? `export ${input.envVar}="$(cat ${renderFixShellArg(input.keyPath, '<the key file the cause names>')})" # in a repo that has no key yet: x secrets init`
|
|
76
|
+
: "set the variable this error's cause names to the contents of the key file beside it, in whatever way this shell spells an assignment — in a repo that has no key yet: x secrets init",
|
|
77
|
+
meta: {
|
|
78
|
+
keyPath: input.keyPath,
|
|
79
|
+
...(input.source === undefined ? {} : { source: input.source }),
|
|
80
|
+
},
|
|
71
81
|
});
|
|
72
82
|
}
|
|
73
83
|
}
|
|
74
84
|
|
|
85
|
+
/** Where a key was read, from its `at`: the env var, a key file (live or staged), or an app's source. */
|
|
86
|
+
const keySourceOf = (at: string): 'env' | 'file' | 'source' =>
|
|
87
|
+
at === 'ULTIMATE_SECRETS_KEY' ? 'env' : /\.secrets\.key(\.next)?$/.test(at) ? 'file' : 'source';
|
|
88
|
+
|
|
75
89
|
/** Key material that is not 64 lowercase hex characters. A truncated paste is the usual cause. */
|
|
76
90
|
export class SecretsKeyInvalidError extends UltimateError {
|
|
77
91
|
constructor(input: { at: string; found: number; expected: number }) {
|
|
92
|
+
const from = keySourceOf(input.at);
|
|
78
93
|
super({
|
|
79
94
|
code: 'X_SECRETS_KEY_INVALID',
|
|
80
95
|
// The lost-key sentence is CAUSE, not fix: a `fix:` is one command, and no command restores
|
|
81
96
|
// a key file — so the file branch says what cannot be done here and the fix measures it.
|
|
82
97
|
cause:
|
|
83
|
-
|
|
98
|
+
from === 'env'
|
|
84
99
|
? `the master key in ${input.at} is ${input.found} character(s); an AES-256 key is ${input.expected} lowercase hex characters`
|
|
85
|
-
:
|
|
100
|
+
: from === 'file'
|
|
101
|
+
? `the master key in ${input.at} is ${input.found} character(s); an AES-256 key is ${input.expected} lowercase hex characters — the key FILE is what is wrong, so re-exporting it changes nothing: restore it from wherever the team keeps the key, because a lost key cannot be recovered or regenerated`
|
|
102
|
+
: `the master key the installed secret source read at ${renderCauseValue(input.at)} is ${input.found} character(s); an AES-256 key is ${input.expected} lowercase hex characters`,
|
|
86
103
|
// Branches on WHERE the bad key was read. From the variable, re-reading the file repairs
|
|
87
104
|
// it. From the FILE, that same line reads the truncated file into the variable and is
|
|
88
|
-
// refused again, so the command is the measurement that says when the restore worked.
|
|
105
|
+
// refused again, so the command is the measurement that says when the restore worked. From
|
|
106
|
+
// an app's source (a KMS ARN, a Vault path) there is no file to measure: the source is fixed.
|
|
89
107
|
fix:
|
|
90
|
-
|
|
108
|
+
from === 'env'
|
|
91
109
|
? `export ULTIMATE_SECRETS_KEY="$(cat .secrets.key)" # the key file holds the ${input.expected} characters on one line`
|
|
92
|
-
:
|
|
110
|
+
: from === 'file'
|
|
111
|
+
? `wc -c ${renderFixShellArg(input.at, '<the key file the cause names>')} # ${input.expected + 1} is a whole key and its newline; any other count is the truncated or padded file to restore`
|
|
112
|
+
: `export const runtime = { secretSource } # apps/<app>/runtime.ts — that source must answer the key as ${input.expected} lowercase hex characters (hex-encode a KMS data key)`,
|
|
93
113
|
meta: { at: input.at },
|
|
94
114
|
});
|
|
95
115
|
}
|