@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.6 → 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.
@@ -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,209 +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('in production, namespace allowlist without verifier fails closed', () => {
784
- const violation = withNodeEnv('production', () =>
785
- evaluateCrossProjectPolicy(spoofableHeaders, {
786
- enabled: true,
787
- allowedNamespaces: ['crm'],
788
- requireOperationContext: false,
789
- }),
790
- );
791
-
792
- expect(violation?.reason).toBe('producer_identity_mismatch');
793
- expect(violation?.message).toBe(
794
- NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE,
795
- );
796
- });
797
- });
798
-
799
100
  describe('cross-project request observation', () => {
800
101
  const resolve = (
801
102
  pathname: string,
@@ -813,106 +114,45 @@ describe('cross-project request observation', () => {
813
114
  'GET:/api/:entity/42': {},
814
115
  'GET:/api/foo/:id': {},
815
116
  }),
816
- ).toEqual({
817
- method: 'GET',
818
- routePath: '/foo/42',
819
- });
117
+ ).toEqual({ method: 'GET', routePath: '/foo/42' });
820
118
  });
821
119
 
822
120
  test('does not strip a dynamic mount prefix', () => {
823
121
  expect(
824
- resolve('/products/42', {
825
- 'GET:/api/:tenant/products/:id': {},
826
- }),
827
- ).toEqual({
828
- method: 'GET',
829
- routePath: '/products/42',
830
- });
122
+ resolve('/products/42', { 'GET:/api/:tenant/products/:id': {} }),
123
+ ).toEqual({ method: 'GET', routePath: '/products/42' });
831
124
  });
832
125
 
833
- test.each([
834
- ['/anything/admin', '/api/*/admin'],
835
- ['/scope/admin', '/api/:scope?/admin'],
836
- ])('fails closed for nonterminal optional or wildcard template %s', (pathname, routePath) => {
837
- expect(resolve(pathname, { [`GET:${routePath}`]: {} })).toEqual({
126
+ test('fails closed for a nonterminal wildcard template', () => {
127
+ expect(resolve('/anything/admin', { 'GET:/api/*/admin': {} })).toEqual({
838
128
  method: 'GET',
839
- routePath: pathname,
129
+ routePath: '/anything/admin',
840
130
  });
841
131
  });
842
132
 
843
133
  test.each([
844
134
  '/files/a%2Fb',
845
- '/files/a%5Cb',
846
- '/files/a%252Fb',
847
135
  '/files/%ZZ',
848
136
  ])('fails closed for ambiguous or malformed percent encoding in %s', pathname => {
849
- expect(
850
- resolve(pathname, {
851
- 'GET:/api/files/:id': {},
852
- }),
853
- ).toEqual({
137
+ expect(resolve(pathname, { 'GET:/api/files/:id': {} })).toEqual({
854
138
  method: 'GET',
855
139
  routePath: pathname,
856
140
  });
857
141
  });
858
142
 
859
143
  test('preserves trailing-slash distinctions while accepting an exact trailing template', () => {
860
- expect(
861
- resolve('/products/42/', {
862
- 'GET:/api/products/:id': {},
863
- }),
864
- ).toEqual({
144
+ expect(resolve('/products/42/', { 'GET:/api/products/:id': {} })).toEqual({
865
145
  method: 'GET',
866
146
  routePath: '/products/42/',
867
147
  });
868
- expect(
869
- resolve('/products/42/', {
870
- 'GET:/api/products/:id/': {},
871
- }),
872
- ).toEqual({
148
+ expect(resolve('/products/42/', { 'GET:/api/products/:id/': {} })).toEqual({
873
149
  method: 'GET',
874
150
  routePath: '/api/products/:id/',
875
151
  });
876
152
  });
877
153
 
878
- test('preserves the observed method when GET and POST share one route', () => {
879
- expect(
880
- resolve(
881
- '/products/42',
882
- {
883
- 'GET:/api/products/:id': {},
884
- 'POST:/api/products/:id': {},
885
- },
886
- 'POST',
887
- ),
888
- ).toEqual({
889
- method: 'POST',
890
- routePath: '/api/products/:id',
891
- });
892
- });
893
-
894
- test('ranks route specificity only within the observed method', () => {
895
- expect(
896
- resolve(
897
- '/api/fixed',
898
- {
899
- 'GET:/api/:id': {},
900
- 'POST:/api/fixed': {},
901
- },
902
- 'GET',
903
- ),
904
- ).toEqual({
905
- method: 'GET',
906
- routePath: '/api/:id',
907
- });
908
- });
909
-
910
154
  test('binds a mounted root request to the fixed producer prefix', () => {
911
- expect(
912
- resolve('/', {
913
- 'GET:/api': {},
914
- }),
915
- ).toEqual({
155
+ expect(resolve('/', { 'GET:/api': {} })).toEqual({
916
156
  method: 'GET',
917
157
  routePath: '/api',
918
158
  });
@@ -936,10 +176,7 @@ describe('cross-project request observation', () => {
936
176
  });
937
177
  const getContract = contracts['GET:/api/products/:id']!;
938
178
  const observedRequest = resolveCrossProjectRequestObservation(
939
- {
940
- method: 'POST',
941
- pathname: '/products/42',
942
- },
179
+ { method: 'POST', pathname: '/products/42' },
943
180
  { expectedOperationContracts: contracts },
944
181
  );
945
182
 
@@ -949,19 +186,9 @@ describe('cross-project request observation', () => {
949
186
  requestId: getContract.requestId,
950
187
  }),
951
188
  'x-operation-id': getContract.operationId,
952
- 'x-modernjs-bff-operation-context': JSON.stringify({
953
- requestId: getContract.requestId,
954
- operationId: getContract.operationId,
955
- method: getContract.method,
956
- routePath: getContract.routePath,
957
- schemaHash: getContract.schemaHash,
958
- operationVersion: getContract.operationVersion,
959
- }),
960
- },
961
- {
962
- enabled: true,
963
- expectedOperationContracts: contracts,
189
+ 'x-modernjs-bff-operation-context': JSON.stringify(getContract),
964
190
  },
191
+ { enabled: true, expectedOperationContracts: contracts },
965
192
  observedRequest,
966
193
  );
967
194