@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.5 → 3.9.0-ultramodern.7

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.
Files changed (68) hide show
  1. package/README.md +5 -5
  2. package/dist/cjs/bff-policy/crossProjectPolicy.js +1 -11
  3. package/dist/cjs/contract-gate-snapshot-store/http-store.js +1 -8
  4. package/dist/cjs/telemetry/exporters.js +4 -4
  5. package/dist/cjs/telemetry/healthSetup.js +14 -18
  6. package/dist/cjs/telemetry/lifecycle.js +5 -5
  7. package/dist/cjs/telemetry/plugin.js +3 -4
  8. package/dist/cjs/telemetry/runtimeEndpoints.js +1 -7
  9. package/dist/cjs/telemetryCore.js +5 -5
  10. package/dist/esm/bff-policy/crossProjectPolicy.mjs +1 -11
  11. package/dist/esm/contract-gate-snapshot-store/http-store.mjs +1 -8
  12. package/dist/esm/telemetry/exporters.mjs +2 -2
  13. package/dist/esm/telemetry/healthSetup.mjs +14 -18
  14. package/dist/esm/telemetry/lifecycle.mjs +6 -6
  15. package/dist/esm/telemetry/plugin.mjs +3 -4
  16. package/dist/esm/telemetry/runtimeEndpoints.mjs +1 -7
  17. package/dist/esm/telemetryCore.mjs +1 -1
  18. package/dist/esm-node/bff-policy/crossProjectPolicy.mjs +1 -11
  19. package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +1 -8
  20. package/dist/esm-node/telemetry/exporters.mjs +2 -2
  21. package/dist/esm-node/telemetry/healthSetup.mjs +14 -18
  22. package/dist/esm-node/telemetry/lifecycle.mjs +6 -6
  23. package/dist/esm-node/telemetry/plugin.mjs +3 -4
  24. package/dist/esm-node/telemetry/runtimeEndpoints.mjs +1 -7
  25. package/dist/esm-node/telemetryCore.mjs +1 -1
  26. package/dist/types/bff-policy/crossProjectPolicy.d.ts +2 -11
  27. package/dist/types/telemetry/exporters.d.ts +1 -1
  28. package/dist/types/telemetry/healthSetup.d.ts +2 -4
  29. package/dist/types/telemetry/lifecycle.d.ts +2 -2
  30. package/dist/types/telemetry/runtimeEndpoints.d.ts +1 -3
  31. package/dist/types/telemetryCore.d.ts +1 -1
  32. package/dist/types-esm/bff-policy/crossProjectPolicy.d.mts +2 -11
  33. package/dist/types-esm/telemetry/exporters.d.mts +1 -1
  34. package/dist/types-esm/telemetry/healthSetup.d.mts +2 -4
  35. package/dist/types-esm/telemetry/lifecycle.d.mts +2 -2
  36. package/dist/types-esm/telemetry/runtimeEndpoints.d.mts +1 -3
  37. package/dist/types-esm/telemetryCore.d.mts +1 -1
  38. package/package.json +10 -10
  39. package/src/bff-policy/crossProjectPolicy.ts +7 -34
  40. package/src/contract-gate-snapshot-store/http-store.ts +1 -11
  41. package/src/telemetry/exporters.ts +1 -1
  42. package/src/telemetry/healthSetup.ts +16 -27
  43. package/src/telemetry/lifecycle.ts +7 -7
  44. package/src/telemetry/plugin.ts +2 -4
  45. package/src/telemetry/runtimeEndpoints.ts +0 -10
  46. package/src/telemetryCore.ts +1 -1
  47. package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +0 -37
  48. package/tests/bff-policy/crossProjectPolicy.test.ts +26 -833
  49. package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +0 -58
  50. package/tests/contractGateSnapshotStore.test.ts +0 -51
  51. package/tests/nativeErrorResponse.test.ts +0 -81
  52. package/tests/nativeServerAssembly.test.ts +34 -51
  53. package/tests/static-serving/static.test.ts +31 -20
  54. package/tests/telemetry.test.ts +0 -97
  55. package/tests/telemetryHealthMonitor.test.ts +0 -2
  56. package/tests/telemetryHealthObservation.test.ts +302 -0
  57. package/tests/telemetryLifecycle.test.ts +0 -54
  58. package/tests/telemetryRegistryQueueMatrix.test.ts +14 -110
  59. package/dist/cjs/telemetry/canaryCompatibility.js +0 -73
  60. package/dist/esm/telemetry/canaryCompatibility.mjs +0 -35
  61. package/dist/esm-node/telemetry/canaryCompatibility.mjs +0 -36
  62. package/dist/types/telemetry/canaryCompatibility.d.ts +0 -19
  63. package/dist/types-esm/telemetry/canaryCompatibility.d.mts +0 -19
  64. package/src/telemetry/canaryCompatibility.ts +0 -55
  65. package/tests/registration.test.ts +0 -27
  66. package/tests/static-serving/provider.test.ts +0 -83
  67. package/tests/telemetryAutopilot.test.ts +0 -993
  68. package/tests/telemetryCanaryCompatibility.test.ts +0 -152
@@ -4,220 +4,15 @@ import {
4
4
  resolveCrossProjectRequestObservation,
5
5
  } from '../../src/bff-policy/crossProjectPolicy';
6
6
  import { buildOperationContractMap } from '../../src/bff-policy/operationContracts';
7
- import { resolveCrossProjectPolicy } from '../../src/bff-policy/resolveCrossProjectPolicy';
8
-
9
- const NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE =
10
- 'cross-project namespace allowlist requires verifyProducerIdentity';
11
-
12
- const withNodeEnv = <T>(nodeEnv: string | undefined, callback: () => T): T => {
13
- const previousNodeEnv = process.env.NODE_ENV;
14
- if (typeof nodeEnv === 'undefined') {
15
- delete process.env.NODE_ENV;
16
- } else {
17
- process.env.NODE_ENV = nodeEnv;
18
- }
19
- try {
20
- return callback();
21
- } finally {
22
- if (typeof previousNodeEnv === 'undefined') {
23
- delete process.env.NODE_ENV;
24
- } else {
25
- process.env.NODE_ENV = previousNodeEnv;
26
- }
27
- }
28
- };
29
7
 
8
+ // Header/reason permutations live in crossProjectPolicy.matrix.test.ts.
9
+ // This file only covers behaviour that matrix cannot express: binding a
10
+ // contract to the *observed* request.
30
11
  describe('cross-project policy', () => {
31
- test('should skip policy checks when disabled', () => {
32
- expect(evaluateCrossProjectPolicy({}, { enabled: false })).toBeNull();
33
- });
34
-
35
- test('should deny missing envelope when policy is enabled', () => {
36
- const violation = evaluateCrossProjectPolicy(
37
- {},
38
- {
39
- enabled: true,
40
- },
41
- );
42
-
43
- expect(violation?.reason).toBe('missing_envelope');
44
- expect(violation?.status).toBe(403);
45
- });
46
-
47
- test('should deny invalid envelope payload', () => {
48
- const violation = evaluateCrossProjectPolicy(
49
- {
50
- 'x-modernjs-bff-envelope': 'not-json',
51
- },
52
- {
53
- enabled: true,
54
- },
55
- );
56
-
57
- expect(violation?.reason).toBe('invalid_envelope');
58
- });
59
-
60
- test('should deny namespace outside allowlist', () => {
61
- const violation = evaluateCrossProjectPolicy(
62
- {
63
- 'x-modernjs-bff-envelope': JSON.stringify({
64
- requestId: 'billing.producer-a',
65
- }),
66
- 'x-operation-id': 'billing.producer-a:GET:/api/invoice',
67
- 'x-modernjs-bff-operation-context': JSON.stringify({
68
- requestId: 'billing.producer-a',
69
- operationId: 'billing.producer-a:GET:/api/invoice',
70
- method: 'GET',
71
- routePath: '/api/invoice',
72
- schemaHash: 'schema-1',
73
- operationVersion: 1,
74
- }),
75
- },
76
- {
77
- enabled: true,
78
- allowedNamespaces: ['crm'],
79
- verifyProducerIdentity: () => 'billing',
80
- },
81
- );
82
-
83
- expect(violation?.reason).toBe('namespace_not_allowed');
84
- });
85
-
86
- test('should deny missing operation context header', () => {
87
- const violation = evaluateCrossProjectPolicy(
88
- {
89
- 'x-modernjs-bff-envelope': JSON.stringify({
90
- requestId: 'crm.producer-a',
91
- }),
92
- },
93
- {
94
- enabled: true,
95
- },
96
- );
97
-
98
- expect(violation?.reason).toBe('missing_operation_context');
99
- });
100
-
101
- test('should deny missing operation context details header by default', () => {
102
- const violation = evaluateCrossProjectPolicy(
103
- {
104
- 'x-modernjs-bff-envelope': JSON.stringify({
105
- requestId: 'crm.producer-a',
106
- }),
107
- 'x-operation-id': 'crm.producer-a:GET:/api/customer',
108
- },
109
- {
110
- enabled: true,
111
- },
112
- );
113
-
114
- expect(violation?.reason).toBe('missing_operation_context_details');
115
- });
116
-
117
- test('should allow valid envelope and operation context', () => {
118
- const violation = evaluateCrossProjectPolicy(
119
- {
120
- 'x-modernjs-bff-envelope': JSON.stringify({
121
- requestId: 'crm.producer-a',
122
- }),
123
- 'x-operation-id': 'crm.producer-a:GET:/api/customer',
124
- 'x-modernjs-bff-operation-context': JSON.stringify({
125
- requestId: 'crm.producer-a',
126
- operationId: 'crm.producer-a:GET:/api/customer',
127
- method: 'GET',
128
- routePath: '/api/customer',
129
- schemaHash: 'schema-1',
130
- operationVersion: 1,
131
- }),
132
- },
133
- {
134
- enabled: true,
135
- allowedNamespaces: ['crm', 'billing'],
136
- verifyProducerIdentity: () => 'crm',
137
- },
138
- );
139
-
140
- expect(violation).toBeNull();
141
- });
142
-
143
- test('enforces resolver defaults at the policy boundary', () => {
144
- const policy = resolveCrossProjectPolicy({
145
- handlers: [
146
- {
147
- name: 'getCustomer',
148
- httpMethod: 'GET',
149
- routePath: '/api/customer',
150
- },
151
- ],
152
- isCrossProjectServer: true,
153
- requestId: 'crm.producer-a',
154
- });
155
-
156
- expect(policy).toBeDefined();
157
- expect(evaluateCrossProjectPolicy({}, policy)).toMatchObject({
158
- reason: 'missing_envelope',
159
- status: 403,
160
- });
161
- });
162
-
163
- test('enforces generated contracts over stale user entries at dispatch', () => {
164
- const policy = resolveCrossProjectPolicy({
165
- crossProjectPolicy: {
166
- enabled: true,
167
- expectedOperationContracts: {
168
- 'GET:/api/customer': {
169
- schemaHash: 'stale-user-contract',
170
- operationVersion: 99,
171
- },
172
- },
173
- },
174
- handlers: [
175
- {
176
- name: 'getCustomer',
177
- httpMethod: 'GET',
178
- routePath: '/api/customer',
179
- },
180
- ],
181
- requestId: 'crm.producer-a',
182
- });
183
- const contract = policy?.expectedOperationContracts['GET:/api/customer'];
184
- expect(contract).toBeDefined();
185
- if (!policy || !contract) {
186
- throw new Error('Expected generated dispatch contract');
187
- }
188
-
189
- const violation = evaluateCrossProjectPolicy(
190
- {
191
- 'x-modernjs-bff-envelope': JSON.stringify({
192
- requestId: contract.requestId,
193
- }),
194
- 'x-operation-id': contract.operationId,
195
- 'x-modernjs-bff-operation-context': JSON.stringify({
196
- requestId: contract.requestId,
197
- operationId: contract.operationId,
198
- method: contract.method,
199
- routePath: contract.routePath,
200
- schemaHash: contract.schemaHash,
201
- operationVersion: contract.operationVersion,
202
- }),
203
- },
204
- policy,
205
- { method: 'GET', routePath: '/api/customer' },
206
- );
207
-
208
- expect(contract.schemaHash).not.toBe('stale-user-contract');
209
- expect(contract.operationVersion).toBe(1);
210
- expect(violation).toBeNull();
211
- });
212
-
213
- test('should deny client-declared operation metadata that does not match the observed request', () => {
12
+ test('denies operation metadata that does not match the observed request', () => {
214
13
  const contracts = buildOperationContractMap({
215
14
  handlers: [
216
- {
217
- name: 'getCustomer',
218
- httpMethod: 'GET',
219
- routePath: '/api/customer',
220
- },
15
+ { name: 'getCustomer', httpMethod: 'GET', routePath: '/api/customer' },
221
16
  {
222
17
  name: 'createInvoice',
223
18
  httpMethod: 'POST',
@@ -226,11 +21,7 @@ describe('cross-project policy', () => {
226
21
  ],
227
22
  requestId: 'crm.producer-a',
228
23
  });
229
- const forgedContract = contracts['POST:/api/invoice'];
230
- expect(forgedContract).toBeDefined();
231
- if (!forgedContract) {
232
- throw new Error('Expected generated invoice contract');
233
- }
24
+ const forgedContract = contracts['POST:/api/invoice']!;
234
25
 
235
26
  const violation = evaluateCrossProjectPolicy(
236
27
  {
@@ -238,81 +29,16 @@ describe('cross-project policy', () => {
238
29
  requestId: forgedContract.requestId,
239
30
  }),
240
31
  'x-operation-id': forgedContract.operationId,
241
- 'x-modernjs-bff-operation-context': JSON.stringify({
242
- requestId: forgedContract.requestId,
243
- operationId: forgedContract.operationId,
244
- method: forgedContract.method,
245
- routePath: forgedContract.routePath,
246
- schemaHash: forgedContract.schemaHash,
247
- operationVersion: forgedContract.operationVersion,
248
- }),
249
- },
250
- {
251
- enabled: true,
252
- expectedOperationContracts: contracts,
253
- },
254
- {
255
- method: 'GET',
256
- routePath: '/api/customer',
257
- },
258
- );
259
-
260
- expect(violation?.reason).toBe('operation_context_mismatch');
261
- });
262
-
263
- test('should deny a client-declared operation id that does not identify the observed contract', () => {
264
- const contracts = buildOperationContractMap({
265
- handlers: [
266
- {
267
- name: 'getCustomer',
268
- httpMethod: 'GET',
269
- routePath: '/api/customer',
270
- },
271
- {
272
- name: 'getInvoice',
273
- httpMethod: 'GET',
274
- routePath: '/api/invoice',
275
- },
276
- ],
277
- requestId: 'crm.producer-a',
278
- });
279
- const actualContract = contracts['GET:/api/customer'];
280
- const forgedContract = contracts['GET:/api/invoice'];
281
- expect(actualContract).toBeDefined();
282
- expect(forgedContract).toBeDefined();
283
- if (!actualContract || !forgedContract) {
284
- throw new Error('Expected generated customer and invoice contracts');
285
- }
286
-
287
- const violation = evaluateCrossProjectPolicy(
288
- {
289
- 'x-modernjs-bff-envelope': JSON.stringify({
290
- requestId: actualContract.requestId,
291
- }),
292
- 'x-operation-id': forgedContract.operationId,
293
- 'x-modernjs-bff-operation-context': JSON.stringify({
294
- requestId: actualContract.requestId,
295
- operationId: forgedContract.operationId,
296
- method: actualContract.method,
297
- routePath: actualContract.routePath,
298
- schemaHash: actualContract.schemaHash,
299
- operationVersion: actualContract.operationVersion,
300
- }),
301
- },
302
- {
303
- enabled: true,
304
- expectedOperationContracts: contracts,
305
- },
306
- {
307
- method: actualContract.method,
308
- routePath: actualContract.routePath,
32
+ 'x-modernjs-bff-operation-context': JSON.stringify(forgedContract),
309
33
  },
34
+ { enabled: true, expectedOperationContracts: contracts },
35
+ { method: 'GET', routePath: '/api/customer' },
310
36
  );
311
37
 
312
38
  expect(violation?.reason).toBe('operation_context_mismatch');
313
39
  });
314
40
 
315
- test('should bind operation identity when a manual contract omits operationId', () => {
41
+ test('binds operation identity when a manual contract omits operationId', () => {
316
42
  const requestId = 'crm.producer-a';
317
43
  const violation = evaluateCrossProjectPolicy(
318
44
  {
@@ -336,232 +62,13 @@ describe('cross-project policy', () => {
336
62
  },
337
63
  },
338
64
  },
339
- {
340
- method: 'GET',
341
- routePath: '/api/customer',
342
- },
343
- );
344
-
345
- expect(violation?.reason).toBe('operation_context_mismatch');
346
- });
347
-
348
- test('should deny operation context header/detail operationId mismatch', () => {
349
- const violation = evaluateCrossProjectPolicy(
350
- {
351
- 'x-modernjs-bff-envelope': JSON.stringify({
352
- requestId: 'crm.producer-a',
353
- }),
354
- 'x-operation-id': 'crm.producer-a:GET:/api/customer',
355
- 'x-modernjs-bff-operation-context': JSON.stringify({
356
- requestId: 'crm.producer-a',
357
- operationId: 'crm.producer-a:GET:/api/invoice',
358
- method: 'GET',
359
- routePath: '/api/invoice',
360
- schemaHash: 'schema-invoice',
361
- operationVersion: 1,
362
- }),
363
- },
364
- {
365
- enabled: true,
366
- expectedOperationContracts: {
367
- 'GET:/api/invoice': {
368
- schemaHash: 'schema-invoice',
369
- operationVersion: 1,
370
- },
371
- },
372
- },
65
+ { method: 'GET', routePath: '/api/customer' },
373
66
  );
374
67
 
375
68
  expect(violation?.reason).toBe('operation_context_mismatch');
376
69
  });
377
70
 
378
- test('should not flag an operation context mismatch when the detail omits operationId', () => {
379
- const violation = evaluateCrossProjectPolicy(
380
- {
381
- 'x-modernjs-bff-envelope': JSON.stringify({
382
- requestId: 'crm.producer-a',
383
- }),
384
- 'x-operation-id': 'crm.producer-a:GET:/api/customer',
385
- 'x-modernjs-bff-operation-context': JSON.stringify({
386
- requestId: 'crm.producer-a',
387
- method: 'GET',
388
- routePath: '/api/customer',
389
- schemaHash: 'schema-1',
390
- operationVersion: 1,
391
- }),
392
- },
393
- {
394
- enabled: true,
395
- allowedNamespaces: ['crm', 'billing'],
396
- verifyProducerIdentity: () => 'crm',
397
- },
398
- );
399
-
400
- expect(violation).toBeNull();
401
- });
402
-
403
- test('should accept generated operation contract metadata for effect-first producer clients', () => {
404
- const contracts = buildOperationContractMap({
405
- handlers: [
406
- {
407
- name: 'getProfile',
408
- httpMethod: 'GET',
409
- routePath: '/api/profile',
410
- },
411
- ],
412
- requestId: 'crm.producer-a',
413
- });
414
- const contract = contracts['GET:/api/profile'];
415
- expect(contract).toBeDefined();
416
- if (!contract) {
417
- throw new Error('Expected generated operation contract');
418
- }
419
-
420
- const violation = evaluateCrossProjectPolicy(
421
- {
422
- 'x-modernjs-bff-envelope': JSON.stringify({
423
- requestId: contract.requestId,
424
- }),
425
- 'x-operation-id': contract.operationId,
426
- 'x-modernjs-bff-operation-context': JSON.stringify({
427
- requestId: contract.requestId,
428
- operationId: contract.operationId,
429
- method: contract.method,
430
- routePath: contract.routePath,
431
- schemaHash: contract.schemaHash,
432
- operationVersion: contract.operationVersion,
433
- }),
434
- },
435
- {
436
- enabled: true,
437
- expectedOperationContracts: {
438
- [`${contract.method}:${contract.routePath}`]: {
439
- schemaHash: contract.schemaHash,
440
- operationVersion: contract.operationVersion,
441
- },
442
- },
443
- },
444
- );
445
-
446
- expect(violation).toBeNull();
447
- });
448
-
449
- test('should deny schema hash mismatches against expected operation contracts', () => {
450
- const violation = evaluateCrossProjectPolicy(
451
- {
452
- 'x-modernjs-bff-envelope': JSON.stringify({
453
- requestId: 'crm.producer-a',
454
- }),
455
- 'x-operation-id': 'crm.producer-a:GET:/api/customer',
456
- 'x-modernjs-bff-operation-context': JSON.stringify({
457
- requestId: 'crm.producer-a',
458
- operationId: 'crm.producer-a:GET:/api/customer',
459
- method: 'GET',
460
- routePath: '/api/customer',
461
- schemaHash: 'schema-actual',
462
- operationVersion: 1,
463
- }),
464
- },
465
- {
466
- enabled: true,
467
- expectedOperationContracts: {
468
- 'GET:/api/customer': {
469
- schemaHash: 'schema-expected',
470
- operationVersion: 1,
471
- },
472
- },
473
- },
474
- );
475
-
476
- expect(violation?.reason).toBe('operation_schema_hash_mismatch');
477
- });
478
-
479
- test('should deny operation version mismatches against expected operation contracts', () => {
480
- const violation = evaluateCrossProjectPolicy(
481
- {
482
- 'x-modernjs-bff-envelope': JSON.stringify({
483
- requestId: 'crm.producer-a',
484
- }),
485
- 'x-operation-id': 'crm.producer-a:GET:/api/customer',
486
- 'x-modernjs-bff-operation-context': JSON.stringify({
487
- requestId: 'crm.producer-a',
488
- operationId: 'crm.producer-a:GET:/api/customer',
489
- method: 'GET',
490
- routePath: '/api/customer',
491
- schemaHash: 'schema-expected',
492
- operationVersion: 2,
493
- }),
494
- },
495
- {
496
- enabled: true,
497
- expectedOperationContracts: {
498
- 'GET:/api/customer': {
499
- schemaHash: 'schema-expected',
500
- operationVersion: 1,
501
- },
502
- },
503
- },
504
- );
505
-
506
- expect(violation?.reason).toBe('operation_version_mismatch');
507
- });
508
-
509
- test('should deny unknown operation contracts when allowUnknownOperations is false', () => {
510
- const violation = evaluateCrossProjectPolicy(
511
- {
512
- 'x-modernjs-bff-envelope': JSON.stringify({
513
- requestId: 'crm.producer-a',
514
- }),
515
- 'x-operation-id': 'crm.producer-a:GET:/api/unknown',
516
- 'x-modernjs-bff-operation-context': JSON.stringify({
517
- requestId: 'crm.producer-a',
518
- operationId: 'crm.producer-a:GET:/api/unknown',
519
- method: 'GET',
520
- routePath: '/api/unknown',
521
- schemaHash: 'schema-1',
522
- operationVersion: 1,
523
- }),
524
- },
525
- {
526
- enabled: true,
527
- expectedOperationContracts: {
528
- 'GET:/api/customer': {
529
- schemaHash: 'schema-1',
530
- operationVersion: 1,
531
- },
532
- },
533
- },
534
- );
535
-
536
- expect(violation?.reason).toBe('unknown_operation_contract');
537
- });
538
-
539
- test('should deny unknown operation contracts when the expected contract map is empty', () => {
540
- const violation = evaluateCrossProjectPolicy(
541
- {
542
- 'x-modernjs-bff-envelope': JSON.stringify({
543
- requestId: 'crm.producer-a',
544
- }),
545
- 'x-operation-id': 'crm.producer-a:GET:/api/customer',
546
- 'x-modernjs-bff-operation-context': JSON.stringify({
547
- requestId: 'crm.producer-a',
548
- operationId: 'crm.producer-a:GET:/api/customer',
549
- method: 'GET',
550
- routePath: '/api/customer',
551
- schemaHash: 'schema-1',
552
- operationVersion: 1,
553
- }),
554
- },
555
- {
556
- enabled: true,
557
- expectedOperationContracts: {},
558
- },
559
- );
560
-
561
- expect(violation?.reason).toBe('unknown_operation_contract');
562
- });
563
-
564
- test('should allow unknown operation contracts when allowUnknownOperations is true', () => {
71
+ test('allows unknown operation contracts when allowUnknownOperations is true', () => {
565
72
  const violation = evaluateCrossProjectPolicy(
566
73
  {
567
74
  'x-modernjs-bff-envelope': JSON.stringify({
@@ -581,10 +88,7 @@ describe('cross-project policy', () => {
581
88
  enabled: true,
582
89
  allowUnknownOperations: true,
583
90
  expectedOperationContracts: {
584
- 'GET:/api/customer': {
585
- schemaHash: 'schema-1',
586
- operationVersion: 1,
587
- },
91
+ 'GET:/api/customer': { schemaHash: 'schema-1', operationVersion: 1 },
588
92
  },
589
93
  },
590
94
  );
@@ -593,243 +97,6 @@ describe('cross-project policy', () => {
593
97
  });
594
98
  });
595
99
 
596
- describe('cross-project policy input validation', () => {
597
- const validEnvelopeHeaders = {
598
- 'x-modernjs-bff-envelope': JSON.stringify({
599
- requestId: 'crm.producer-a',
600
- }),
601
- 'x-operation-id': 'crm.producer-a:GET:/api/customer',
602
- };
603
- const strictContractPolicy = {
604
- enabled: true,
605
- expectedOperationContracts: {
606
- 'GET:/api/customer': {
607
- schemaHash: 'schema-1',
608
- operationVersion: 1,
609
- },
610
- },
611
- };
612
- const detail = {
613
- requestId: 'crm.producer-a',
614
- operationId: 'crm.producer-a:GET:/api/customer',
615
- method: 'GET',
616
- routePath: '/api/customer',
617
- schemaHash: 'schema-1',
618
- operationVersion: 1,
619
- };
620
-
621
- const cases = [
622
- {
623
- name: 'rejects a non-object envelope',
624
- headers: { 'x-modernjs-bff-envelope': '123' },
625
- policy: { enabled: true },
626
- reason: 'invalid_envelope',
627
- },
628
- {
629
- name: 'rejects an envelope without requestId',
630
- headers: { 'x-modernjs-bff-envelope': JSON.stringify({}) },
631
- policy: { enabled: true },
632
- reason: 'missing_request_id',
633
- },
634
- {
635
- name: 'rejects non-JSON operation details',
636
- headers: {
637
- ...validEnvelopeHeaders,
638
- 'x-modernjs-bff-operation-context': 'not-json',
639
- },
640
- policy: { enabled: true },
641
- reason: 'invalid_operation_context_details',
642
- },
643
- {
644
- name: 'rejects non-object operation details',
645
- headers: {
646
- ...validEnvelopeHeaders,
647
- 'x-modernjs-bff-operation-context': JSON.stringify([]),
648
- },
649
- policy: { enabled: true },
650
- reason: 'invalid_operation_context_details',
651
- },
652
- {
653
- name: 'rejects operation details with another requestId',
654
- headers: {
655
- ...validEnvelopeHeaders,
656
- 'x-modernjs-bff-operation-context': JSON.stringify({
657
- ...detail,
658
- requestId: 'billing.producer-a',
659
- }),
660
- },
661
- policy: strictContractPolicy,
662
- reason: 'operation_context_details_request_id_mismatch',
663
- },
664
- {
665
- name: 'rejects missing operation schema hash',
666
- headers: {
667
- ...validEnvelopeHeaders,
668
- 'x-modernjs-bff-operation-context': JSON.stringify({
669
- ...detail,
670
- schemaHash: undefined,
671
- }),
672
- },
673
- policy: strictContractPolicy,
674
- reason: 'missing_operation_schema_hash',
675
- },
676
- {
677
- name: 'rejects missing operation version',
678
- headers: {
679
- ...validEnvelopeHeaders,
680
- 'x-modernjs-bff-operation-context': JSON.stringify({
681
- ...detail,
682
- operationVersion: undefined,
683
- }),
684
- },
685
- policy: strictContractPolicy,
686
- reason: 'missing_operation_version',
687
- },
688
- ] as const;
689
-
690
- for (const policyCase of cases) {
691
- test(policyCase.name, () => {
692
- expect(
693
- evaluateCrossProjectPolicy(policyCase.headers, policyCase.policy)
694
- ?.reason,
695
- ).toBe(policyCase.reason);
696
- });
697
- }
698
- });
699
-
700
- describe('cross-project policy producer identity binding', () => {
701
- const spoofableHeaders = {
702
- 'x-modernjs-bff-envelope': JSON.stringify({
703
- requestId: 'crm.producer-a',
704
- }),
705
- };
706
-
707
- test('denies when the client-asserted namespace does not match verified identity', () => {
708
- // Spoofed-header case: the caller echoes an allowlisted requestId, but
709
- // the verified channel (e.g. mTLS peer identity) says "billing".
710
- const violation = evaluateCrossProjectPolicy(spoofableHeaders, {
711
- enabled: true,
712
- allowedNamespaces: ['crm'],
713
- verifyProducerIdentity: () => 'billing',
714
- });
715
-
716
- expect(violation?.reason).toBe('producer_identity_mismatch');
717
- expect(violation?.status).toBe(403);
718
- });
719
-
720
- test('denies when producer identity cannot be verified', () => {
721
- const violation = evaluateCrossProjectPolicy(spoofableHeaders, {
722
- enabled: true,
723
- verifyProducerIdentity: () => undefined,
724
- });
725
-
726
- expect(violation?.reason).toBe('producer_identity_mismatch');
727
- });
728
-
729
- test('checks the allowlist against the verified namespace when identities match', () => {
730
- const verifier = (headers: Record<string, unknown>) =>
731
- typeof headers['x-verified-producer'] === 'string'
732
- ? (headers['x-verified-producer'] as string)
733
- : undefined;
734
-
735
- const allowed = evaluateCrossProjectPolicy(
736
- {
737
- ...spoofableHeaders,
738
- 'x-verified-producer': 'crm',
739
- },
740
- {
741
- enabled: true,
742
- allowedNamespaces: ['crm'],
743
- requireOperationContext: false,
744
- verifyProducerIdentity: verifier,
745
- },
746
- );
747
- expect(allowed).toBeNull();
748
-
749
- const denied = evaluateCrossProjectPolicy(
750
- {
751
- 'x-modernjs-bff-envelope': JSON.stringify({
752
- requestId: 'billing.producer-z',
753
- }),
754
- 'x-verified-producer': 'billing',
755
- },
756
- {
757
- enabled: true,
758
- allowedNamespaces: ['crm'],
759
- requireOperationContext: false,
760
- verifyProducerIdentity: verifier,
761
- },
762
- );
763
- expect(denied?.reason).toBe('namespace_not_allowed');
764
- });
765
-
766
- test('namespace allowlist without verifier fails closed outside production modes', () => {
767
- for (const nodeEnv of [undefined, 'test']) {
768
- const violation = withNodeEnv(nodeEnv, () =>
769
- evaluateCrossProjectPolicy(spoofableHeaders, {
770
- enabled: true,
771
- allowedNamespaces: ['crm'],
772
- requireOperationContext: false,
773
- }),
774
- );
775
-
776
- expect(violation?.reason).toBe('producer_identity_mismatch');
777
- expect(violation?.message).toBe(
778
- NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE,
779
- );
780
- }
781
- });
782
-
783
- test('explicit client-asserted namespace opt-out warns and remains advisory', () => {
784
- const warnSpy = rstest
785
- .spyOn(console, 'warn')
786
- .mockImplementation(() => undefined);
787
-
788
- try {
789
- const firstResult = withNodeEnv('development', () =>
790
- evaluateCrossProjectPolicy(spoofableHeaders, {
791
- enabled: true,
792
- allowedNamespaces: ['crm'],
793
- allowClientAssertedNamespace: true,
794
- requireOperationContext: false,
795
- }),
796
- );
797
- const secondResult = withNodeEnv('development', () =>
798
- evaluateCrossProjectPolicy(spoofableHeaders, {
799
- enabled: true,
800
- allowedNamespaces: ['crm'],
801
- allowClientAssertedNamespace: true,
802
- requireOperationContext: false,
803
- }),
804
- );
805
-
806
- expect(firstResult).toBeNull();
807
- expect(secondResult).toBeNull();
808
- expect(warnSpy).toHaveBeenCalledTimes(1);
809
- expect(warnSpy.mock.calls[0][0]).toContain(
810
- NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE,
811
- );
812
- } finally {
813
- warnSpy.mockRestore();
814
- }
815
- });
816
-
817
- test('in production, namespace allowlist without verifier fails closed', () => {
818
- const violation = withNodeEnv('production', () =>
819
- evaluateCrossProjectPolicy(spoofableHeaders, {
820
- enabled: true,
821
- allowedNamespaces: ['crm'],
822
- requireOperationContext: false,
823
- }),
824
- );
825
-
826
- expect(violation?.reason).toBe('producer_identity_mismatch');
827
- expect(violation?.message).toBe(
828
- NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE,
829
- );
830
- });
831
- });
832
-
833
100
  describe('cross-project request observation', () => {
834
101
  const resolve = (
835
102
  pathname: string,
@@ -847,106 +114,45 @@ describe('cross-project request observation', () => {
847
114
  'GET:/api/:entity/42': {},
848
115
  'GET:/api/foo/:id': {},
849
116
  }),
850
- ).toEqual({
851
- method: 'GET',
852
- routePath: '/foo/42',
853
- });
117
+ ).toEqual({ method: 'GET', routePath: '/foo/42' });
854
118
  });
855
119
 
856
120
  test('does not strip a dynamic mount prefix', () => {
857
121
  expect(
858
- resolve('/products/42', {
859
- 'GET:/api/:tenant/products/:id': {},
860
- }),
861
- ).toEqual({
862
- method: 'GET',
863
- routePath: '/products/42',
864
- });
122
+ resolve('/products/42', { 'GET:/api/:tenant/products/:id': {} }),
123
+ ).toEqual({ method: 'GET', routePath: '/products/42' });
865
124
  });
866
125
 
867
- test.each([
868
- ['/anything/admin', '/api/*/admin'],
869
- ['/scope/admin', '/api/:scope?/admin'],
870
- ])('fails closed for nonterminal optional or wildcard template %s', (pathname, routePath) => {
871
- expect(resolve(pathname, { [`GET:${routePath}`]: {} })).toEqual({
126
+ test('fails closed for a nonterminal wildcard template', () => {
127
+ expect(resolve('/anything/admin', { 'GET:/api/*/admin': {} })).toEqual({
872
128
  method: 'GET',
873
- routePath: pathname,
129
+ routePath: '/anything/admin',
874
130
  });
875
131
  });
876
132
 
877
133
  test.each([
878
134
  '/files/a%2Fb',
879
- '/files/a%5Cb',
880
- '/files/a%252Fb',
881
135
  '/files/%ZZ',
882
136
  ])('fails closed for ambiguous or malformed percent encoding in %s', pathname => {
883
- expect(
884
- resolve(pathname, {
885
- 'GET:/api/files/:id': {},
886
- }),
887
- ).toEqual({
137
+ expect(resolve(pathname, { 'GET:/api/files/:id': {} })).toEqual({
888
138
  method: 'GET',
889
139
  routePath: pathname,
890
140
  });
891
141
  });
892
142
 
893
143
  test('preserves trailing-slash distinctions while accepting an exact trailing template', () => {
894
- expect(
895
- resolve('/products/42/', {
896
- 'GET:/api/products/:id': {},
897
- }),
898
- ).toEqual({
144
+ expect(resolve('/products/42/', { 'GET:/api/products/:id': {} })).toEqual({
899
145
  method: 'GET',
900
146
  routePath: '/products/42/',
901
147
  });
902
- expect(
903
- resolve('/products/42/', {
904
- 'GET:/api/products/:id/': {},
905
- }),
906
- ).toEqual({
148
+ expect(resolve('/products/42/', { 'GET:/api/products/:id/': {} })).toEqual({
907
149
  method: 'GET',
908
150
  routePath: '/api/products/:id/',
909
151
  });
910
152
  });
911
153
 
912
- test('preserves the observed method when GET and POST share one route', () => {
913
- expect(
914
- resolve(
915
- '/products/42',
916
- {
917
- 'GET:/api/products/:id': {},
918
- 'POST:/api/products/:id': {},
919
- },
920
- 'POST',
921
- ),
922
- ).toEqual({
923
- method: 'POST',
924
- routePath: '/api/products/:id',
925
- });
926
- });
927
-
928
- test('ranks route specificity only within the observed method', () => {
929
- expect(
930
- resolve(
931
- '/api/fixed',
932
- {
933
- 'GET:/api/:id': {},
934
- 'POST:/api/fixed': {},
935
- },
936
- 'GET',
937
- ),
938
- ).toEqual({
939
- method: 'GET',
940
- routePath: '/api/:id',
941
- });
942
- });
943
-
944
154
  test('binds a mounted root request to the fixed producer prefix', () => {
945
- expect(
946
- resolve('/', {
947
- 'GET:/api': {},
948
- }),
949
- ).toEqual({
155
+ expect(resolve('/', { 'GET:/api': {} })).toEqual({
950
156
  method: 'GET',
951
157
  routePath: '/api',
952
158
  });
@@ -970,10 +176,7 @@ describe('cross-project request observation', () => {
970
176
  });
971
177
  const getContract = contracts['GET:/api/products/:id']!;
972
178
  const observedRequest = resolveCrossProjectRequestObservation(
973
- {
974
- method: 'POST',
975
- pathname: '/products/42',
976
- },
179
+ { method: 'POST', pathname: '/products/42' },
977
180
  { expectedOperationContracts: contracts },
978
181
  );
979
182
 
@@ -983,19 +186,9 @@ describe('cross-project request observation', () => {
983
186
  requestId: getContract.requestId,
984
187
  }),
985
188
  'x-operation-id': getContract.operationId,
986
- 'x-modernjs-bff-operation-context': JSON.stringify({
987
- requestId: getContract.requestId,
988
- operationId: getContract.operationId,
989
- method: getContract.method,
990
- routePath: getContract.routePath,
991
- schemaHash: getContract.schemaHash,
992
- operationVersion: getContract.operationVersion,
993
- }),
994
- },
995
- {
996
- enabled: true,
997
- expectedOperationContracts: contracts,
189
+ 'x-modernjs-bff-operation-context': JSON.stringify(getContract),
998
190
  },
191
+ { enabled: true, expectedOperationContracts: contracts },
999
192
  observedRequest,
1000
193
  );
1001
194