-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdispatcher-plugin.ts
More file actions
1922 lines (1839 loc) · 107 KB
/
Copy pathdispatcher-plugin.ts
File metadata and controls
1922 lines (1839 loc) · 107 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { Plugin, PluginContext, IHttpServer, ANONYMOUS_DENY_BODY, ANONYMOUS_DENY_STATUS } from '@objectstack/core';
import { looksLikeInternalErrorLeak, INTERNAL_ERROR_MESSAGE, resolveThrownHttpError, serverFaultProvenance, demotedDeclaredCode } from '@objectstack/types';
import { DispatcherErrorCode } from '@objectstack/spec/api';
import type { IAuthService, IMetadataService } from '@objectstack/spec/contracts';
import type { CounterStore } from '@objectstack/plugin-auth/rate-limit-storage';
import { HttpDispatcher, HttpDispatcherResult, type HttpProtocolContext } from './http-dispatcher.js';
import { isServiceServeable } from './service-serveable.js';
import { validationFailureDetails, VALIDATION_FAILED_STATUS } from './validation-failure.js';
import { buildApiError } from './error-envelope.js';
import { appEndpointMountPrefix, isAppEndpointPath, runAppEndpointStep } from './api-endpoint-step.js';
import { callData } from './action-execution.js';
import { createEndpointRateLimiterRegistry } from './endpoint-policy.js';
import {
buildSecurityHeaders,
createInboundRateLimitMiddleware,
type InboundRateLimitBudget,
type SecurityHeadersOptions,
} from './security/index.js';
import { resolveSessionData, resolveSessionPrincipalId } from './security/resolve-session-principal.js';
import { buildActorUser } from './security/actor-user.js';
import {
NoopMetricsRegistry,
NoopErrorReporter,
instrumentRouteHandler,
armHttpRequestCounter,
armHttpRequestDurationHistogram,
type MetricsRegistry,
type ErrorReporter,
} from './observability/index.js';
export interface DispatcherPluginConfig {
/**
* API path prefix for all endpoints.
* @default '/api/v1'
*/
prefix?: string;
/**
* Project-scoping configuration. Must match the REST API
* `enableProjectScoping` / `projectResolution` fields so AI / automation
* routes stay in lockstep with /data and /meta.
*
* When `enableProjectScoping` is true and `projectResolution` is:
* - `required` — only `/environments/:environmentId/...` variants are registered.
* - `optional` / `auto` — both unscoped and scoped variants are registered
* (the scoped handler forwards `req.params.environmentId` into context).
*/
scoping?: {
enableProjectScoping?: boolean;
projectResolution?: 'required' | 'optional' | 'auto';
};
/**
* Enforce per-project membership (`sys_environment_member`) on scoped
* data-plane routes. Returns 403 for non-members unless they are
* staff (platform org) or the project is the well-known system
* project.
*
* Defaults to `true` when `scoping.enableProjectScoping` is enabled;
* explicitly set to `false` for tests and single-tenant deployments
* where membership has not been seeded.
*/
enforceProjectMembership?: boolean;
/**
* Security response headers. When provided, every response routed
* through this plugin gets the headers merged in (route-specific
* headers still win on conflict).
*
* Pass `false` to disable. Pass `true` (or omit) to enable with
* conservative API-server defaults (CSP=deny-all, XCTO=nosniff,
* X-Frame-Options=DENY, etc.). Pass an object to customize — see
* {@link SecurityHeadersOptions}.
*
* @default true
*/
securityHeaders?: boolean | SecurityHeadersOptions;
/**
* Observability wiring. All fields optional; defaults are noop
* (zero overhead, no behavior change).
*
* - `metrics`: registry receiving `http_requests_total` and
* `http_request_duration_ms`. Both are emitted by the TRANSPORT
* through the `IHttpServer.afterResponse` seam when it offers one, so
* they cover every inbound request on the server rather than only the
* routes this plugin mounts; on a transport without the seam this
* plugin emits them for its own routes instead. Plug in `prom-client` /
* `@opentelemetry/api-metrics` / your own adapter.
* (`http_request_errors_total` was retired by #9834 — read the 5xx rate
* from `http_requests_total{status=~"5.."}`.)
*
* - `errorReporter`: invoked on 5xx responses with the thrown
* error and `{ requestId, method, route }`. Plug in Sentry /
* Datadog / Rollbar.
*
* - `generateRequestId`: customize the format of minted request
* ids (default: `req_<uuid>` via `crypto.randomUUID`). The
* incoming `X-Request-Id` header is honored when present and
* well-formed, regardless of this setting.
*
* - `requestIdHeader`: response header name to echo the id back
* on. Defaults to `X-Request-Id`.
*/
observability?: {
metrics?: MetricsRegistry;
errorReporter?: ErrorReporter;
generateRequestId?: () => string;
requestIdHeader?: string;
};
/**
* Inbound rate limiting, forwarded from the stack's authored
* `server:` block by `objectstack serve` (#4910).
*
* This field is the one `security/rate-limit.ts` used to name in the
* present tense while it did not exist (#4937) — it exists now, and it is
* the only way the limiter is armed: omit it, or leave
* `budget.enabled` false, and no middleware is registered at all
* (zero per-request cost, not a disabled check).
*
* When armed, the plugin installs the limiter as GLOBAL middleware on the
* `http.server` service. Global is load-bearing:
* `server.security.rateLimit` is a SERVER-level budget, and a limiter
* covering only this plugin's own routes while `/data` ran unmetered would
* be the same declared-≠-enforced half-truth the key was introduced to end.
*
* It goes in `start()` rather than `init()` so that "this kernel has no
* `http.server`" is a settled fact when it is reported, not a Phase-1 guess
* a later-initializing transport could contradict (#4771). That is only safe
* because the transport mounts the middleware SEAM at the end of its own
* `init()` — a `use()` at any later point still gates every route, so the
* gate does not have to win a race with route registration to be complete.
*
* Endpoint-level `ApiEndpointSchema.rateLimit` is NOT read here. It remains
* KNOWN-UNWIRED and is now tracked by #5040, the endpoint-executor build.
* (`ApiEndpointRegistrationSchema.rateLimit`, the second spelling this note
* used to name, no longer exists — that whole registry family was retired
* in #4939.) #4936 settled the fate this note called undecided: the
* `ApiEndpoint` vocabulary is KEPT, a non-empty `apis:` is rejected at
* publish/validate until the executor exists, and every endpoint-level key
* — this one included — gets wired there, reusing the server-level seam
* below as its pattern.
*/
rateLimit?: {
/** The authored `server.security.rateLimit` budget. */
budget?: InboundRateLimitBudget;
/** The authored `server.trustProxy`. */
trustProxy?: boolean;
};
}
/**
* `ctx.getService(name)` without the throw.
*
* The kernel's accessor raises for an unregistered name, and every consumer
* here treats absence as a legitimate composition ("no auth in this stack", "no
* cache service yet"). Spelled once so no branch quietly turns a missing
* OPTIONAL service into a boot failure.
*/
function safeGetService<T>(ctx: PluginContext, name: string): T | undefined {
try {
return ctx.getService<T>(name) ?? undefined;
} catch {
return undefined;
}
}
/**
* Route definition emitted by service plugins (e.g. AIServicePlugin) via hooks.
* Minimal interface — matches the shape produced by `buildAIRoutes()`.
*/
interface RouteDefinition {
method: 'GET' | 'POST' | 'PATCH' | 'DELETE';
path: string;
description: string;
/** Whether this route requires authentication (default: true). */
auth?: boolean;
/** Required permissions for accessing this route. */
permissions?: string[];
handler: (req: any) => Promise<any>;
}
/**
* Register a single RouteDefinition on the HTTP server.
* Returns true if the route was successfully registered.
*/
function mountRouteOnServer(
route: RouteDefinition,
server: IHttpServer,
routePath: string,
securityHeaders?: Record<string, string>,
resolveUser?: (headers: Record<string, any>) => Promise<any | undefined>,
): boolean {
const handler = async (req: any, res: any) => {
try {
// Resolve the authenticated user from request headers (cookie /
// bearer) so route handlers can attribute the request to an
// actor — wires up `req.user` for AI routes, action endpoints,
// anything that needs identity-aware execution.
let user: any;
if (resolveUser) {
try {
user = await resolveUser(req.headers ?? {});
} catch {
/* fall through anonymous — enforced just below */
}
}
// Enforce the route's declared `auth` contract. This used to be
// assumed to run "separately"/upstream, but nothing did: an
// anonymous caller reached `auth: true` handlers (e.g.
// `GET /ai/status`) and got adapter/model config back. [#3963] The
// gate is unconditional now — the deployment-wide `requireAuth`
// opt-out is retired, so only a route declaring `auth: false` opens
// itself, and it does so by declaration.
if (route.auth !== false && !user) {
res.status(ANONYMOUS_DENY_STATUS);
if (securityHeaders) {
for (const [k, v] of Object.entries(securityHeaders)) res.header(k, v);
}
// [#9823] The shared flat deny body from @objectstack/core —
// this used to be an inline `{ error, message }` copy, which is
// exactly why #9487's additive `code` key never reached it.
// Writing the constant keeps this seam from drifting again;
// the wrapper question (flat vs nested, ADR-0112 D5) is not
// settled here.
res.json(ANONYMOUS_DENY_BODY);
return;
}
const result = await route.handler({
body: req.body,
params: req.params,
query: req.query,
headers: req.headers,
user,
});
if (result.stream && result.events) {
// SSE streaming response
res.status(result.status);
if (securityHeaders) {
for (const [k, v] of Object.entries(securityHeaders)) {
res.header(k, v);
}
}
// Apply headers from the route result if available
if (result.headers) {
for (const [k, v] of Object.entries(result.headers)) {
res.header(k, String(v));
}
} else {
res.header('Content-Type', 'text/event-stream');
res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
}
// Write the stream — events are pre-encoded SSE strings
if (typeof res.write === 'function' && typeof res.end === 'function') {
for await (const event of result.events) {
res.write(typeof event === 'string' ? event : `data: ${JSON.stringify(event)}\n\n`);
}
res.end();
} else {
// [#9936] Buffered fallback — the IHttpResponse contract's
// own prescription (#3607, ADR-0076 OQ#10; the JSDoc on
// `write` in packages/spec/src/contracts/http-server.ts):
// a transport that omits the OPTIONAL `write`/`end`
// streaming surface receives the SAME SSE bytes, buffered
// and delivered through `send()` under the streaming
// headers already set above. A caller that asked for a
// stream parses this body with the same `data:`-line
// reader, frame for frame — the encoding ternary below is
// deliberately identical to the streamed branch's.
//
// This used to answer a bare `res.json({ events })`: a
// JSON dialect of the same frames that no SSE reader could
// decode (both shipped readers split raw bytes on
// newlines, and a JSON body contains none), off
// BaseResponseSchema besides. No shipped transport lacks
// `write`/`end`, so this branch is live only for an
// external `Runtime({ server })` transport — exactly the
// composition the contract note anticipates.
let buffered = '';
for await (const event of result.events) {
buffered += typeof event === 'string' ? event : `data: ${JSON.stringify(event)}\n\n`;
}
res.send(buffered);
}
} else {
res.status(result.status);
if (securityHeaders) {
for (const [k, v] of Object.entries(securityHeaders)) {
res.header(k, v);
}
}
if (result.body !== undefined) {
res.json(result.body);
} else {
res.end();
}
}
} catch (err: any) {
errorResponseBase(err, res, securityHeaders);
}
};
const m = route.method.toLowerCase();
if (m === 'get' && typeof server.get === 'function') {
server.get(routePath, handler);
return true;
} else if (m === 'post' && typeof server.post === 'function') {
server.post(routePath, handler);
return true;
} else if (m === 'delete' && typeof server.delete === 'function') {
server.delete(routePath, handler);
return true;
} else if (m === 'patch' && typeof server.patch === 'function') {
server.patch(routePath, handler);
return true;
}
return false;
}
/**
* Send an HttpDispatcherResult through IHttpResponse.
* Differentiates between handled, unhandled (404), and special results.
*
* @param securityHeaders headers to merge into every response (under
* the route-specific headers so the dispatcher can override on a
* per-route basis when truly needed).
*/
function sendResultBase(
result: HttpDispatcherResult,
res: any,
securityHeaders?: Record<string, string>,
): void {
const applySecurityHeaders = () => {
if (!securityHeaders) return;
for (const [k, v] of Object.entries(securityHeaders)) {
// Don't clobber route-set headers — `res.header` semantics
// vary by adapter, so we set unconditionally and rely on the
// call ordering (security headers first, route headers
// overwrite below).
res.header(k, v);
}
};
if (result.handled) {
if (result.response) {
res.status(result.response.status);
applySecurityHeaders();
if (result.response.headers) {
for (const [k, v] of Object.entries(result.response.headers)) {
res.header(k, v);
}
}
res.json(result.response.body);
return;
}
if (result.result) {
// Special results from the dispatcher's `result.result` channel.
// Currently the only shape we handle here is the SSE/streaming
// descriptor returned by AI routes:
// { status, stream: true, events: AsyncIterable<string>,
// headers?: Record<string, string>, contentType?: string }
// Anything else falls through to JSON so older callers keep
// working.
const r = result.result as any;
const isStream = r && typeof r === 'object' && (r.type === 'stream' || r.stream === true) && r.events;
if (isStream && typeof res.write === 'function' && typeof res.end === 'function') {
res.status(typeof r.status === 'number' ? r.status : 200);
applySecurityHeaders();
if (r.headers && typeof r.headers === 'object') {
for (const [k, v] of Object.entries(r.headers)) {
res.header(k, String(v));
}
} else {
res.header('Content-Type', r.contentType || 'text/event-stream');
res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
}
// Flip the adapter's `isStreaming` flag synchronously so the
// outer handler can return before the AsyncIterable is fully
// drained. Without this empty write, the Hono adapter would
// see no streaming activity by the time the route handler
// resolves and would close the body, truncating the SSE.
res.write('');
// Drain the events in the background; the adapter's
// ReadableStream stays open until res.end() fires.
(async () => {
try {
for await (const event of r.events as AsyncIterable<unknown>) {
if (event == null) continue;
res.write(typeof event === 'string' ? event : `data: ${JSON.stringify(event)}\n\n`);
}
} catch (streamErr) {
try {
res.write(`event: error\ndata: ${JSON.stringify({ message: streamErr instanceof Error ? streamErr.message : String(streamErr) })}\n\n`);
} catch { /* connection already gone */ }
} finally {
try { res.end(); } catch { /* idem */ }
}
})();
return;
}
if (isStream) {
// [#9961] Buffered fallback for a transport whose `res` cannot
// stream — the same #3607 / ADR-0076 OQ#10 contract
// prescription the route-wrapper branch applies (see
// `mountRouteOnServer`): drain the descriptor's AsyncIterable
// and deliver the identical SSE bytes through `send()` under
// the streaming headers. Frame encoding, the `null` skip and
// the trailing `event: error` frame all mirror the streamed
// branch above so the buffered body is byte-identical to what
// a streaming transport would have received.
//
// This used to fall through to `res.json(result.result)`,
// which serialized the descriptor itself: `JSON.stringify`
// collapses the `events` AsyncIterable to `{}`, so the caller
// got HTTP 200 with the payload gone and the iterable was
// never drained — silent total event loss.
res.status(typeof r.status === 'number' ? r.status : 200);
applySecurityHeaders();
if (r.headers && typeof r.headers === 'object') {
for (const [k, v] of Object.entries(r.headers)) {
res.header(k, String(v));
}
} else {
res.header('Content-Type', r.contentType || 'text/event-stream');
res.header('Cache-Control', 'no-cache');
res.header('Connection', 'keep-alive');
}
// Drained in the same detached shape as the streaming path —
// this function is synchronous by signature, and the response
// is delivered when the iterable settles.
(async () => {
let buffered = '';
try {
for await (const event of r.events as AsyncIterable<unknown>) {
if (event == null) continue;
buffered += typeof event === 'string' ? event : `data: ${JSON.stringify(event)}\n\n`;
}
} catch (streamErr) {
buffered += `event: error\ndata: ${JSON.stringify({ message: streamErr instanceof Error ? streamErr.message : String(streamErr) })}\n\n`;
} finally {
try { res.send(buffered); } catch { /* connection already gone */ }
}
})();
return;
}
res.status(200);
applySecurityHeaders();
res.json(result.result);
return;
}
}
// Semantic 404: no route matched — include diagnostic info
res.status(404);
applySecurityHeaders();
res.json({
success: false,
error: buildApiError({
code: DispatcherErrorCode.enum.ROUTE_NOT_FOUND,
message: 'Not Found',
httpStatus: 404,
extra: {
hint: 'No handler matched this request. Check the API discovery endpoint for available routes.',
},
}),
});
}
/**
* The single error exit for EVERY dispatcher-plugin route — `/analytics`,
* `/packages`, `/i18n`, `/automation`, `/auth`, `/notifications`,
* `/mcp`, … Each handler catches and calls here rather than re-throwing.
*
* [#3867] Two things were wrong with it, both invisible until a driver error
* actually reached this path:
*
* 1. **It only honoured `statusCode`.** Domain errors across this codebase
* carry their HTTP status as `status` (the protocol layer's
* `OBJECT_NOT_FOUND`/`RECORD_NOT_FOUND`/`CLONE_DISABLED`, plugin-sharing's
* `FORBIDDEN`, …); `HttpDispatcher.errorFromThrown` already reads `status`
* first, `statusCode` second. Here a deliberate 404 was rendered as a
* **500** — the wrong code, and it dragged the message through the
* sanitiser below for no reason. Now aligned with `errorFromThrown`.
*
* 2. **It returned `err.message` verbatim.** `@objectstack/rest` has guarded
* its data routes against driver dumps since forever (`mapDataError`), but
* this boundary had no equivalent, so `POST /analytics/query` on an
* unresolvable cube answered with a real SQL statement in the body. The
* shared predicate now applies here too — but ONLY on a 5xx: a 4xx message
* is a deliberate business/validation answer and must reach the caller
* intact.
*
* Sanitising costs no diagnostics: the untouched error is still handed to
* `errorReporter` through the `__obsRecordedError` side-channel below.
*
* [#3918] A third defect, of the same family as (1): a record-level
* `ValidationError` carries neither `status` nor `statusCode`, so it landed on
* the 500 fallback — and because the body was only `{message, code}`, its
* `fields[]` was dropped AND the 5xx sanitiser above replaced the human message
* with the generic INTERNAL_ERROR_MESSAGE. A user typing a bad email got back a
* 500 "internal error" with nothing to attach to the offending input.
* `@objectstack/rest` has mapped this shape to a 400 with `fields[]` since
* forever (`mapDataError`); this exit now does the same, so a form served by
* the dispatcher can highlight the field the way a form served by /data can.
* `details` is only emitted for that shape — everything else keeps the exact
* two-key body it had.
*
* [#5811] A fourth, and the reason (2)'s "shared predicate" is now two of them.
* `looksLikeInternalErrorLeak` is a heuristic over SQL/driver PHRASING, so it
* closes this exit only against faults that *sound* like a driver. It never saw
* `service-analytics`' fail-closed read-scope refusals — measured, all eleven
* shapes return FALSE — and those messages name the field names and comparands of
* the RLS POLICY the tenant is being filtered by:
*
* ```
* ⚠️ PAST TENSE — the leak this entry CLOSED, not what this exit answers today.
* For the current body see the corrected shape at the end of this block.
* POST /analytics/query (tenant caller, object with a broken sharing rule)
* → 500 {"success":false,"error":{"message":"[read-scope-sql] unsafe field
* identifier \"secret_policy_field\" — refusing to build read scope
* (fail-closed).","code":"READ_SCOPE_COMPILE_FAILED"}}
* ```
*
* The sibling face — `/analytics/dataset/query` in `@objectstack/rest` — closed
* this in #5367/#5808 by keying on the DECLARATION rather than the prose, but the
* rule was written in-line there because one consumer does not justify a shared
* surface. This exit is the second consumer, so it was promoted:
* `declaresServerFault`, next to the heuristic it complements, read by both
* boundaries. ⛔ It is NOT "withhold every 5xx" — #5667 kept UNDECLARED 5xx
* legible on purpose, and a bare `Error` still goes through the heuristic alone.
*
* [#12281] A fifth, and the reason that predicate is now {@link
* serverFaultProvenance}: `declaresServerFault` required a non-empty string
* `code` beside the 5xx and read the `status` spelling only, so this exit
* withheld a NARROWER band than `/data` — a declared 5xx with no code, and a
* declared 5xx spelled `statusCode`, both shipped their prose here and were
* withheld one door over. Ruled 2026-08-27 on #12509 (option D): this exit
* adopts the structural withhold for EVERY declared 5xx message, and reads the
* shared judgement rather than growing a second copy of it. Measured before the
* change: the population that changes hands today is EMPTY (the two repo-wide
* no-code 5xx producer families cannot reach this door), which is precisely why
* now was the cheapest moment — the alignment costs no legibility that exists
* and buys the invariant forward.
*
* The code still travels, and `READ_SCOPE_COMPILE_FAILED` reaches the client
* untouched — so what a machine reads is unchanged and only the prose is withheld,
* into `errorReporter` and the log.
*
* ⚠️ It reaches the client at `error.code` — NOT `error.details.code`, which is
* where this note pointed until #6270 corrected it (#6123 corrected the same
* sentence at three sibling sites). The `details` assembly below (#3842) only
* STAGES the code in a local object; `buildApiError` then runs
* `splitSemanticCode` (`./error-envelope.ts`), which PROMOTES it into the declared
* `ApiErrorSchema` field and returns the now-empty `details` as `undefined` — so
* the key is omitted from the body and `error.details.code` is never present to
* read. Do not mistake the local variable name for the wire contract. The measured
* 500 body is exactly:
*
* ```json
* {"success":false,"error":{"code":"READ_SCOPE_COMPILE_FAILED",
* "message":"Internal server error","httpStatus":500}}
* ```
*
* Pinned end-to-end in `analytics-query-read-scope-withhold.test.ts`, which
* asserts the code at `error.code` against a real `AnalyticsService` on a real
* mounted route.
*/
function errorResponseBase(err: any, res: any, securityHeaders?: Record<string, string>): void {
const validation = validationFailureDetails(err);
const httpStatus =
(typeof err?.status === 'number' ? err.status : undefined) ??
(typeof err?.statusCode === 'number' ? err.statusCode : undefined) ??
(validation ? VALIDATION_FAILED_STATUS : 500);
res.status(httpStatus);
if (securityHeaders) {
for (const [k, v] of Object.entries(securityHeaders)) {
res.header(k, v);
}
}
// Side-channel: remember the original error so the observability
// wrapper can hand it to errorReporter on 5xx. Handlers catch the
// error and call us here instead of re-throwing, so this is the
// only place we still have it.
if (httpStatus >= 500) {
try {
(res as any).__obsRecordedError = err;
} catch {
// res is a frozen / proxy object — skip
}
}
const raw = err?.message;
// [#3842] A thrown error's own `.code` finally has somewhere to go — see the
// `declaredCode` note below for WHICH spelling travels. Resolved HERE, above
// the message ternary, because [#12281] that ternary now reads the same
// resolver answer: one read of the throw, one set of facts, so the prose rule
// and the code rule can never be looking at different errors.
const thrown = resolveThrownHttpError(err, 500);
// [#5811/#12281] Two independent reasons to withhold, both 5xx-only. The
// declaration comes first because it needs no guess about the text.
//
// [#12281] The declaration limb is `serverFaultProvenance(thrown) ===
// 'declared'` — the ONE definition of "the producer named this 5xx itself"
// (`@objectstack/types`), ruled 2026-08-27 (option D) and already read by
// `demotedDeclaredCode` for the code channel. ⛔ Not re-derived here: "one
// rule, every door inherits" is the point of #12509, and a per-door variant
// is the divergence this family has now been repaired for twice.
//
// It replaces `declaresServerFault`, which withheld on `status >= 500` AND a
// non-empty string `code`, and closes the TWO axes that read apart from
// `/data`'s `declaredHttpStatus`:
// 1. a declared 5xx carrying NO `code` fell through to the heuristic alone
// — the half #5811's own argument found insufficient;
// 2. `declaresServerFault` read `status` ONLY, so a fully ADR-0112-compliant
// producer that merely spells `statusCode` shipped its prose here while
// `/data` withheld it. `declaredStatus` reads `status ?? statusCode`.
//
// ⛔ Still NOT "withhold every 5xx". The gate is the DECLARED status, never
// the resolved `httpStatus` above (which falls back to 500 for a throw that
// declared nothing): `serverFaultProvenance` answers `'undeclared'` exactly
// when `declaredStatus` is absent, so #5667's tiering survives intact — a
// bare `Error` from our own code is the operator's own bug report and still
// goes through the heuristic alone. A naive `httpStatus >= 500` test would
// silently delete that, which is the one way this change could do harm.
//
// The author-facing text channel is `userMessage` (#9934), never the raw
// message — a producer whose 5xx prose is addressed to a human declares it
// there and it survives the withhold on its own channel.
const message =
serverFaultProvenance(thrown) === 'declared' || (httpStatus >= 500 && looksLikeInternalErrorLeak(raw))
? INTERNAL_ERROR_MESSAGE
: raw || 'Internal Server Error';
// [#3842] A thrown error's own `.code` finally has somewhere to go — the
// declared field, so the same SDK method reports the same code whichever
// exit answered. [#9106] WHICH spelling goes there is the shared resolver's
// answer, not this exit's: `error.code` is a closed vocabulary at every
// door (maintainer ruling 2026-08-16), so the resolver's narrowed `code` is
// passed explicitly — for a registered code that IS the producer's own
// spelling, for a validation shape it is `VALIDATION_FAILED`, exactly the
// answers this exit's details-promotion used to produce — and an
// unregistered spelling rides the wire's `declaredCode` sibling instead
// (presence means demotion; the tenant-authored limb #9106 measured). The
// resolver's status chain is byte-identical to `httpStatus` above (status →
// statusCode → validation 400 → 500), so the two reads cannot disagree. A
// non-string `.code` (a driver errno) stays in `details`, as context.
const declaredCode = demotedDeclaredCode(thrown);
const details =
(err?.code && typeof err.code !== 'string') || validation
? { ...(err?.code && typeof err.code !== 'string' ? { code: err.code } : {}), ...(validation ?? {}) }
: undefined;
res.json({
success: false,
error: buildApiError({
message,
httpStatus,
code: thrown.code,
details,
...(declaredCode !== undefined ? { extra: { declaredCode } } : {}),
}),
});
}
/**
* Dispatcher Plugin
*
* Bridges legacy HttpDispatcher handlers to the IHttpServer route-registration model.
* Registers routes for domains NOT covered by @objectstack/rest:
* - /.well-known/objectstack (discovery)
* - /auth (authentication)
* - /analytics (BI queries)
* - /packages (package management)
* - /i18n (internationalization — locales, translations, field labels)
* - /automation (CRUD + triggers + runs)
*
* NOT /storage — `@objectstack/service-storage` owns that surface and mounts
* it on this same http-server (#4087).
*
* Usage:
* ```ts
* import { createDispatcherPlugin } from '@objectstack/runtime';
* runtime.use(createDispatcherPlugin({ prefix: '/api/v1' }));
* ```
*/
export function createDispatcherPlugin(config: DispatcherPluginConfig = {}): Plugin {
return {
name: 'com.objectstack.runtime.dispatcher',
version: '1.0.0',
init: async (_ctx: PluginContext) => {
// Consumer-only plugin — no services registered.
},
start: async (ctx: PluginContext) => {
let server: IHttpServer | undefined;
try {
server = ctx.getService<IHttpServer>('http.server');
} catch {
// No HTTP server available — skip silently
server = undefined;
}
// ── Inbound rate limit (#4910) ──────────────────────────────
// Installed in `start()`, deliberately. Phase 1 is over, so "no
// `http.server`" is a FACT rather than a mid-boot guess a later
// plugin could contradict — the #4771 class of defect, which is
// exactly what makes the warning below safe to emit. The gate still
// precedes every route because the transport mounts the middleware
// SEAM at the end of its own `init()`; `use()` appends to a chain
// that seam reads per request, so registration order stops mattering
// (see `HonoHttpServer.installMiddlewareSeam`).
const rateLimitMiddleware = createInboundRateLimitMiddleware({
...(config.rateLimit?.budget ? { budget: config.rateLimit.budget } : {}),
trustProxy: config.rateLimit?.trustProxy === true,
resolvePrincipalId: (headers) =>
resolveSessionPrincipalId(
safeGetService<IAuthService>(ctx, 'auth'),
headers as Record<string, unknown>,
),
resolveCache: async () => safeGetService<CounterStore>(ctx, 'cache'),
logger: ctx.logger,
});
// `null` = no budget declared, or declared disabled. Nothing is
// registered at all, so an unmetered deployment pays zero
// per-request cost — not a disabled check, no check.
if (rateLimitMiddleware) {
if (server) {
server.use(rateLimitMiddleware);
const budget = config.rateLimit?.budget;
ctx.logger.info('Inbound rate limit armed', {
maxRequests: budget?.maxRequests ?? 100,
windowMs: budget?.windowMs ?? 60_000,
// NOT `key:` — the logger redacts that field name, and
// `key: ***REDACTED***` tells an operator nothing about
// what the limit is actually keyed on.
keyedBy: 'principal, falling back to caller IP',
trustProxy: config.rateLimit?.trustProxy === true,
});
} else {
// Absence must be loud (route-ownership rule 3): a stack
// that ASKED to be rate limited and is not must never find
// out from a load test.
ctx.logger.warn(
'[dispatcher] `server.security.rateLimit` is enabled but this kernel has no `http.server` '
+ 'service, so no request can be metered. Mount a transport plugin (e.g. '
+ '@objectstack/plugin-hono-server), or remove the rate-limit declaration.',
);
}
}
if (!server) return;
const kernel = ctx.getKernel();
// Default: enable membership enforcement iff environment-scoping is on.
// Tests / single-tenant deploys can opt out via the explicit flag.
const enforceMembership =
config.enforceProjectMembership ?? (config.scoping?.enableProjectScoping ?? false);
// [#3963] Anonymous callers are denied unconditionally on every
// surface that reaches object data — the deployment-wide opt-out is
// retired, so there is nothing to resolve or warn about here. The
// dispatcher gates the same data as REST through sibling surfaces
// (`/ai`, the `/meta` catch-all, service routes), and by-surface
// consistency is exactly what #2567 established.
const dispatcher = new HttpDispatcher(kernel, undefined, {
enforceProjectMembership: enforceMembership,
});
const prefix = config.prefix || '/api/v1';
// ── Security: resolve once at startup; applied on every response.
// Defaults to ON because every production API server should be
// sending these headers. Opt out with `securityHeaders: false`
// (only sensible for tests or when an upstream reverse proxy is
// already setting them).
const securityHeaders: Record<string, string> | undefined =
config.securityHeaders === false
? undefined
: buildSecurityHeaders(
typeof config.securityHeaders === 'object'
? config.securityHeaders
: {},
);
// Locally-shadowed wrappers — every `sendResult(...)` /
// `errorResponse(...)` call below picks these up via lexical
// scope, so the 50+ route handlers don't need to thread the
// security headers through manually.
const sendResult = (result: HttpDispatcherResult, res: any) =>
sendResultBase(result, res, securityHeaders);
const errorResponse = (err: any, res: any) =>
errorResponseBase(err, res, securityHeaders);
// ── Observability ──────────────────────────────────────────
// Noop defaults; production hosts inject real adapters.
const metrics: MetricsRegistry =
config.observability?.metrics ?? new NoopMetricsRegistry();
const errorReporter: ErrorReporter =
config.observability?.errorReporter ?? new NoopErrorReporter();
const generateRequestId = config.observability?.generateRequestId;
const requestIdHeader =
config.observability?.requestIdHeader ?? 'X-Request-Id';
/**
* Wrap the IHttpServer so every route registration is
* automatically instrumented. We only override the three
* verb methods the dispatcher uses; everything else passes
* through unchanged.
*/
const rawServer = server;
// ── `http_requests_total` on the transport seam (#9835) ────
// A transport implementing `IHttpServer.afterResponse` OWNS the
// request counter for every inbound request on it (the 2026-08-18
// ruling on #9650 put the counter at the transport; the contract
// JSDoc records the ownership rule). Two consequences here, both
// feature-detected runtime-real per the contract:
//
// 1. When the host wired a registry to THIS plugin, offer it to
// the transport seam. `armHttpRequestCounter` latches
// per-server, first caller wins — a transport plugin that
// already armed its own registry in Phase 1 keeps ownership
// and this call is a no-op, so one registry handed to both
// layers (the ordinary wiring) can never double-count. A host
// that wired ONLY the dispatcher — the wiring the docs
// demonstrate — now gets every inbound surface counted, not
// just the dispatcher's own routes.
// 2. The per-route wrapper below stops emitting its copy of the
// counter (`emitHttpRequestsTotal: false`): the seam already
// counts these routes, and the duplicate would land on the
// same series under the same labels — the measured #9833
// distortion, counting ONLY the dispatcher's routes twice.
//
// On a transport WITHOUT the seam both revert to the legacy
// behavior: the wrapper counts the dispatcher's own routes, and
// (documented expectation, #9650 ruling) every other surface on
// that transport reports no HTTP metrics — zero there is "not
// instrumented", never "no traffic".
const transportCountsRequests =
typeof (rawServer as IHttpServer).afterResponse === 'function';
if (transportCountsRequests && config.observability?.metrics) {
armHttpRequestCounter(rawServer as IHttpServer, config.observability.metrics);
// #9834: the duration histogram takes the same route, on its
// own first-wins latch. Without it the docs' two derived
// signals disagreed — 5xx rate saw every inbound surface
// while p95 latency saw the dispatcher's own routes only.
armHttpRequestDurationHistogram(
rawServer as IHttpServer,
config.observability.metrics,
);
}
server = new Proxy(rawServer, {
get(target, prop, receiver) {
if (prop === 'get' || prop === 'post' || prop === 'delete') {
const method = String(prop).toUpperCase();
const original = (target as any)[prop];
if (typeof original !== 'function') return original;
return (route: string, handler: any) => {
return original.call(
target,
route,
instrumentRouteHandler(method, route, handler, {
metrics,
errorReporter,
generateRequestId,
requestIdHeader,
emitHttpRequestsTotal: !transportCountsRequests,
emitHttpRequestDurationMs: !transportCountsRequests,
}),
);
};
}
return Reflect.get(target, prop, receiver);
},
}) as IHttpServer;
// ── Discovery (.well-known) ─────────────────────────────────
server.get('/.well-known/objectstack', async (_req: any, res: any) => {
if (securityHeaders) {
for (const [k, v] of Object.entries(securityHeaders)) {
res.header(k, v);
}
}
// Discovery reflects MUTABLE runtime config (which routes/services
// are live — e.g. `mcp` unless OS_MCP_SERVER_ENABLED=false). It
// must never be cached by an edge/CDN, or a config change (enable
// MCP) leaves clients reading a stale payload that still says the
// route is absent — the Integrations UI then shows "MCP not
// enabled" against a live server (cloud#152). The body is computed
// fresh per request; the only staleness is the HTTP cache layer.
res.header('Cache-Control', 'no-store');
// Enveloped (`{ success: true, data }`) under the #9436 maintainer
// ruling (2026-08-18, option A), inherited by #9813 — machine-read
// discovery bodies are the envelope's core constituency and the
// migration is one additive key. Deliberately NOT #9389's pre-auth
// exemption: that is a closed list of SPA-read surfaces, and this
// body is read by SDKs (`connect()`'s fallback probe), codegen and
// AI clients. Every measured reader tolerates the added key.
res.json({ success: true, data: await dispatcher.getDiscoveryInfo(prefix) });
});
// ── Discovery (versioned API path) ──────────────────────────
// Single owner (ADR-0076 D11 / OQ#9): when the REST plugin is
// mounted on the same kernel it registers `${prefix}/discovery`
// itself (rest-server registerDiscoveryEndpoints), and which
// payload a client saw used to depend on plugin start order
// (first-registration-wins on Hono). Cede the route to REST
// deterministically; this bridge registers it only as the
// fallback owner in REST-less compositions. `/.well-known/
// objectstack` above stays dispatcher-owned unconditionally —
// no other plugin registers it.
const restRegistered =
typeof (kernel as { hasPlugin?: (n: string) => boolean }).hasPlugin === 'function' &&
(kernel as { hasPlugin: (n: string) => boolean }).hasPlugin('com.objectstack.rest.api');
if (!restRegistered) {
server.get(`${prefix}/discovery`, async (_req: any, res: any) => {
if (securityHeaders) {
for (const [k, v] of Object.entries(securityHeaders)) {
res.header(k, v);
}
}
// See the .well-known handler above: discovery must not be cached
// (mutable runtime config; cloud#152 stale `routes.mcp`), and the
// body is enveloped under the same #9436 ruling (via #9813).
res.header('Cache-Control', 'no-store');
res.json({ success: true, data: await dispatcher.getDiscoveryInfo(prefix) });
});
} else {
ctx.logger.info(`[Dispatcher] ${prefix}/discovery ceded to com.objectstack.rest.api (single owner)`);
}
// ── Health ──────────────────────────────────────────────────
server.get(`${prefix}/health`, async (_req: any, res: any) => {
try {
const result = await dispatcher.dispatch('GET', '/health', undefined, {}, { request: _req });
sendResult(result, res);
} catch (err: any) {
errorResponse(err, res);
}
});
// ── Readiness ───────────────────────────────────────────────
// Like /health, the dispatcher owns the /ready branch but it is
// only reachable over HTTP once mounted EXPLICITLY here (there is
// no catch-all). 200 while the kernel is `running`, 503 while it is
// booting or shutting down — the contract the EE multi-node
// rolling-restart drain gate polls (cloud ADR-0018) so a load
// balancer stops routing to a replica before it closes.
server.get(`${prefix}/ready`, async (_req: any, res: any) => {
try {
const result = await dispatcher.dispatch('GET', '/ready', undefined, {}, { request: _req });
sendResult(result, res);
} catch (err: any) {
errorResponse(err, res);
}
});
// ── Auth: DELIBERATELY NOT MOUNTED ──────────────────────────
// The /auth/* wildcard is mounted by AuthProxyPlugin (cloud) or
// AuthPlugin (single-tenant) directly on the raw Hono app —
// those handlers can return native Web `Response` objects which
// is what better-auth produces. The dispatcher cannot represent
// a streaming Response cleanly through `IHttpServer.send`, so
// this plugin registers NO auth route at all.
//
// [#5085] It used to register exactly one — a "legacy explicit
// `POST ${prefix}/auth/login` retained for self-hosted clients"
// — and that single mount was the only producer in this repo that
// handed better-auth a NON-Fetch request. `IHttpServer` gives a
// handler the adapter's internal `IHttpRequest`, whose `headers`
// is a PLAIN OBJECT (`HonoHttpServer.runHandler` builds it from
// `c.req.header()`); `handleAuthRequest` forwards
// `context.request` whole to `IAuthService.handleRequest(request:
// Request)`, and better-auth's fetch-style handler opens with
// `request.headers.get(…)`. Measured on a real showcase boot:
// `POST /api/v1/auth/login` → HTTP 500 with the raw
// `request.headers.get is not a function` in the response body,
// while `POST /api/v1/auth/sign-in/email` — the same forwarding
// layer, reached through the raw-app wildcard with `c.req.raw` —
// answered 200.
//
// The route could not work for any caller: `/login` is not a
// better-auth endpoint (it is absent from `plugin-auth`'s
// `auth-route-ledger.ts`, and `content/docs/api/
// plugin-endpoints.mdx` says in as many words "There is no
// `/auth/login` route"), and `handleAuthRequest` does not route on
// the sub-path at all (#4113) — so the ONLY thing this mount ever
// added over the wildcard was a 500 where the wildcard yields
// better-auth's own clean 404. Converting the internal request
// into a Fetch `Request` here would be the consumer-side
// accommodation Prime Directive #12 rejects, and would buy nothing
// but a more expensive 404. So it is deleted, and every unknown
// auth sub-path now falls to the namespace owner exactly like
// every other one.