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

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 (171) hide show
  1. package/dist/cjs/bff-policy/crossProjectPolicy.js +309 -0
  2. package/dist/cjs/bff-policy/index.js +62 -0
  3. package/dist/cjs/bff-policy/node.js +69 -0
  4. package/dist/cjs/bff-policy/operationContracts.js +182 -0
  5. package/dist/cjs/bff-policy/resolveCrossProjectPolicy.js +65 -0
  6. package/dist/cjs/errorResponsePlugin.js +57 -0
  7. package/dist/cjs/localisedLoaderPlugin.js +62 -0
  8. package/dist/cjs/mfCache.js +6 -0
  9. package/dist/cjs/module-federation-css/plugin.js +6 -0
  10. package/dist/cjs/serverConfig.js +18 -0
  11. package/dist/cjs/serverPlugin.js +74 -0
  12. package/dist/cjs/static-serving/plugin.js +57 -0
  13. package/dist/cjs/static-serving/responders.js +158 -0
  14. package/dist/cjs/static-serving/staticModuleFederation.js +173 -0
  15. package/dist/cjs/static-serving/staticPrecompressed.js +152 -0
  16. package/dist/cjs/telemetry/canaryCompatibility.js +73 -0
  17. package/dist/cjs/telemetry/healthSetup.js +4 -0
  18. package/dist/cjs/telemetry/lifecycle.js +15 -16
  19. package/dist/cjs/telemetry/plugin.js +10 -3
  20. package/dist/cjs/telemetry/runtimeEndpoints.js +10 -3
  21. package/dist/esm/bff-policy/crossProjectPolicy.mjs +259 -0
  22. package/dist/esm/bff-policy/index.mjs +1 -0
  23. package/dist/esm/errorResponsePlugin.mjs +19 -0
  24. package/dist/esm/localisedLoaderPlugin.mjs +24 -0
  25. package/dist/esm/mfCache.mjs +6 -0
  26. package/dist/esm/module-federation-css/plugin.mjs +6 -0
  27. package/dist/esm/serverConfig.mjs +0 -0
  28. package/dist/esm/serverPlugin.mjs +34 -0
  29. package/dist/esm/telemetry/canaryCompatibility.mjs +35 -0
  30. package/dist/esm/telemetry/healthSetup.mjs +4 -0
  31. package/dist/esm/telemetry/lifecycle.mjs +15 -16
  32. package/dist/esm/telemetry/plugin.mjs +9 -2
  33. package/dist/esm/telemetry/runtimeEndpoints.mjs +10 -3
  34. package/dist/esm-node/bff-policy/crossProjectPolicy.mjs +260 -0
  35. package/dist/esm-node/bff-policy/index.mjs +2 -0
  36. package/dist/esm-node/bff-policy/node.mjs +3 -0
  37. package/dist/esm-node/bff-policy/operationContracts.mjs +129 -0
  38. package/dist/esm-node/bff-policy/resolveCrossProjectPolicy.mjs +28 -0
  39. package/dist/esm-node/errorResponsePlugin.mjs +20 -0
  40. package/dist/esm-node/localisedLoaderPlugin.mjs +25 -0
  41. package/dist/esm-node/mfCache.mjs +6 -0
  42. package/dist/esm-node/module-federation-css/plugin.mjs +6 -0
  43. package/dist/esm-node/serverConfig.mjs +1 -0
  44. package/dist/esm-node/serverPlugin.mjs +35 -0
  45. package/dist/esm-node/static-serving/plugin.mjs +18 -0
  46. package/dist/esm-node/static-serving/responders.mjs +111 -0
  47. package/dist/esm-node/static-serving/staticModuleFederation.mjs +105 -0
  48. package/dist/esm-node/static-serving/staticPrecompressed.mjs +112 -0
  49. package/dist/esm-node/telemetry/canaryCompatibility.mjs +36 -0
  50. package/dist/esm-node/telemetry/healthSetup.mjs +4 -0
  51. package/dist/esm-node/telemetry/lifecycle.mjs +15 -16
  52. package/dist/esm-node/telemetry/plugin.mjs +9 -2
  53. package/dist/esm-node/telemetry/runtimeEndpoints.mjs +10 -3
  54. package/dist/types/bff-policy/crossProjectPolicy.d.ts +110 -0
  55. package/dist/types/bff-policy/index.d.ts +1 -0
  56. package/dist/types/bff-policy/node.d.ts +2 -0
  57. package/dist/types/bff-policy/operationContracts.d.ts +85 -0
  58. package/dist/types/bff-policy/resolveCrossProjectPolicy.d.ts +39 -0
  59. package/dist/types/errorResponsePlugin.d.ts +3 -0
  60. package/dist/types/index.d.ts +1 -0
  61. package/dist/types/localisedLoaderPlugin.d.ts +2 -0
  62. package/dist/types/mfCache.d.ts +2 -3
  63. package/dist/types/module-federation-css/plugin.d.ts +2 -3
  64. package/dist/types/serverConfig.d.ts +17 -0
  65. package/dist/types/serverPlugin.d.ts +5 -0
  66. package/dist/types/static-serving/plugin.d.ts +3 -0
  67. package/dist/types/static-serving/responders.d.ts +2 -0
  68. package/dist/types/static-serving/staticModuleFederation.d.ts +13 -0
  69. package/dist/types/static-serving/staticPrecompressed.d.ts +13 -0
  70. package/dist/types/telemetry/canaryCompatibility.d.ts +19 -0
  71. package/dist/types/telemetry/healthSetup.d.ts +3 -1
  72. package/dist/types/telemetry/lifecycle.d.ts +2 -2
  73. package/dist/types/telemetry/plugin.d.ts +2 -1
  74. package/dist/types/telemetry/runtimeEndpoints.d.ts +3 -1
  75. package/dist/types-esm/backend-federation-security/index.d.mts +68 -0
  76. package/dist/types-esm/backend-federation-security/node.d.mts +3 -0
  77. package/dist/types-esm/bff-policy/crossProjectPolicy.d.mts +110 -0
  78. package/dist/types-esm/bff-policy/index.d.mts +1 -0
  79. package/dist/types-esm/bff-policy/node.d.mts +2 -0
  80. package/dist/types-esm/bff-policy/operationContracts.d.mts +85 -0
  81. package/dist/types-esm/bff-policy/resolveCrossProjectPolicy.d.mts +39 -0
  82. package/dist/types-esm/contract-gate-snapshot-normalization.d.mts +15 -0
  83. package/dist/types-esm/contract-gate-snapshot-store/file-store.d.mts +3 -0
  84. package/dist/types-esm/contract-gate-snapshot-store/http-store.d.mts +5 -0
  85. package/dist/types-esm/contract-gate-snapshot-store/index.d.mts +5 -0
  86. package/dist/types-esm/contract-gate-snapshot-store/resolve.d.mts +9 -0
  87. package/dist/types-esm/contract-gate-snapshot-store/types.d.mts +47 -0
  88. package/dist/types-esm/contractGateSnapshotObserver.d.mts +33 -0
  89. package/dist/types-esm/env.d.mts +40 -0
  90. package/dist/types-esm/errorResponsePlugin.d.mts +3 -0
  91. package/dist/types-esm/index.d.mts +8 -0
  92. package/dist/types-esm/localisedLoaderPlugin.d.mts +2 -0
  93. package/dist/types-esm/mfCache.d.mts +26 -0
  94. package/dist/types-esm/module-federation-css/collector.d.mts +25 -0
  95. package/dist/types-esm/module-federation-css/index.d.mts +8 -0
  96. package/dist/types-esm/module-federation-css/manifest.d.mts +28 -0
  97. package/dist/types-esm/module-federation-css/plugin.d.mts +22 -0
  98. package/dist/types-esm/module-federation-css/remote.d.mts +20 -0
  99. package/dist/types-esm/runtime-fallback-signal/index.d.mts +4 -0
  100. package/dist/types-esm/runtime-fallback-signal/model.d.mts +70 -0
  101. package/dist/types-esm/runtime-fallback-signal/resolver.d.mts +29 -0
  102. package/dist/types-esm/runtime-fallback-signal/store.d.mts +4 -0
  103. package/dist/types-esm/runtimeFallbackSignal.d.mts +1 -0
  104. package/dist/types-esm/runtimeLifecycle.d.mts +10 -0
  105. package/dist/types-esm/serverConfig.d.mts +17 -0
  106. package/dist/types-esm/serverPlugin.d.mts +5 -0
  107. package/dist/types-esm/static-serving/plugin.d.mts +3 -0
  108. package/dist/types-esm/static-serving/responders.d.mts +2 -0
  109. package/dist/types-esm/static-serving/staticModuleFederation.d.mts +13 -0
  110. package/dist/types-esm/static-serving/staticPrecompressed.d.mts +13 -0
  111. package/dist/types-esm/telemetry/canaryCompatibility.d.mts +19 -0
  112. package/dist/types-esm/telemetry/envelope.d.mts +55 -0
  113. package/dist/types-esm/telemetry/exporters.d.mts +12 -0
  114. package/dist/types-esm/telemetry/healthEvaluation.d.mts +20 -0
  115. package/dist/types-esm/telemetry/healthMonitor.d.mts +88 -0
  116. package/dist/types-esm/telemetry/healthSetup.d.mts +19 -0
  117. package/dist/types-esm/telemetry/lifecycle.d.mts +18 -0
  118. package/dist/types-esm/telemetry/metrics.d.mts +3 -0
  119. package/dist/types-esm/telemetry/plugin.d.mts +13 -0
  120. package/dist/types-esm/telemetry/registry.d.mts +69 -0
  121. package/dist/types-esm/telemetry/registryTypes.d.mts +50 -0
  122. package/dist/types-esm/telemetry/runtimeEndpoints.d.mts +37 -0
  123. package/dist/types-esm/telemetry.d.mts +1 -0
  124. package/dist/types-esm/telemetryCore.d.mts +6 -0
  125. package/package.json +158 -26
  126. package/rslib.config.mts +36 -2
  127. package/rstest.config.mts +11 -0
  128. package/src/bff-policy/crossProjectPolicy.ts +781 -0
  129. package/src/bff-policy/index.ts +1 -0
  130. package/src/bff-policy/node.ts +2 -0
  131. package/src/bff-policy/operationContracts.ts +298 -0
  132. package/src/bff-policy/resolveCrossProjectPolicy.ts +88 -0
  133. package/src/errorResponsePlugin.ts +21 -0
  134. package/src/index.ts +4 -0
  135. package/src/localisedLoaderPlugin.ts +34 -0
  136. package/src/mfCache.ts +4 -3
  137. package/src/module-federation-css/plugin.ts +4 -3
  138. package/src/serverConfig.ts +32 -0
  139. package/src/serverPlugin.ts +38 -0
  140. package/src/static-serving/plugin.ts +16 -0
  141. package/src/static-serving/responders.ts +208 -0
  142. package/src/static-serving/staticModuleFederation.ts +255 -0
  143. package/src/static-serving/staticPrecompressed.ts +193 -0
  144. package/src/telemetry/canaryCompatibility.ts +55 -0
  145. package/src/telemetry/healthSetup.ts +9 -1
  146. package/src/telemetry/lifecycle.ts +20 -32
  147. package/src/telemetry/plugin.ts +6 -2
  148. package/src/telemetry/runtimeEndpoints.ts +13 -2
  149. package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +487 -0
  150. package/tests/bff-policy/crossProjectPolicy.test.ts +1004 -0
  151. package/tests/bff-policy/operationContracts.test.ts +221 -0
  152. package/tests/bff-policy/optionalZodPeer.test.ts +30 -0
  153. package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +134 -0
  154. package/tests/contractGateSnapshotObserver.test.ts +0 -58
  155. package/tests/env.test.ts +45 -64
  156. package/tests/localisedLoader.test.ts +120 -0
  157. package/tests/mfCache.test.ts +1 -88
  158. package/tests/moduleFederationCss.test.ts +4 -48
  159. package/tests/nativeErrorResponse.test.ts +199 -0
  160. package/tests/nativeServerAssembly.test.ts +402 -0
  161. package/tests/registration.test.ts +1 -86
  162. package/tests/runtimeLifecycle.test.ts +69 -17
  163. package/tests/static-serving/ordering.test.ts +214 -0
  164. package/tests/static-serving/provider.test.ts +83 -0
  165. package/tests/static-serving/static.test.ts +400 -0
  166. package/tests/telemetry.test.ts +35 -101
  167. package/tests/telemetryAutopilot.test.ts +5 -146
  168. package/tests/telemetryCanaryCompatibility.test.ts +152 -0
  169. package/tests/telemetryHealthMonitor.test.ts +0 -2
  170. package/tests/telemetryLifecycle.test.ts +11 -253
  171. package/tests/telemetryRegistryQueueMatrix.test.ts +0 -1
@@ -0,0 +1,1004 @@
1
+ import {
2
+ type CrossProjectOperationContract,
3
+ evaluateCrossProjectPolicy,
4
+ resolveCrossProjectRequestObservation,
5
+ } from '../../src/bff-policy/crossProjectPolicy';
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
+
30
+ 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', () => {
214
+ const contracts = buildOperationContractMap({
215
+ handlers: [
216
+ {
217
+ name: 'getCustomer',
218
+ httpMethod: 'GET',
219
+ routePath: '/api/customer',
220
+ },
221
+ {
222
+ name: 'createInvoice',
223
+ httpMethod: 'POST',
224
+ routePath: '/api/invoice',
225
+ },
226
+ ],
227
+ requestId: 'crm.producer-a',
228
+ });
229
+ const forgedContract = contracts['POST:/api/invoice'];
230
+ expect(forgedContract).toBeDefined();
231
+ if (!forgedContract) {
232
+ throw new Error('Expected generated invoice contract');
233
+ }
234
+
235
+ const violation = evaluateCrossProjectPolicy(
236
+ {
237
+ 'x-modernjs-bff-envelope': JSON.stringify({
238
+ requestId: forgedContract.requestId,
239
+ }),
240
+ '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,
309
+ },
310
+ );
311
+
312
+ expect(violation?.reason).toBe('operation_context_mismatch');
313
+ });
314
+
315
+ test('should bind operation identity when a manual contract omits operationId', () => {
316
+ const requestId = 'crm.producer-a';
317
+ const violation = evaluateCrossProjectPolicy(
318
+ {
319
+ 'x-modernjs-bff-envelope': JSON.stringify({ requestId }),
320
+ 'x-operation-id': `${requestId}:forged-operation`,
321
+ 'x-modernjs-bff-operation-context': JSON.stringify({
322
+ requestId,
323
+ operationId: `${requestId}:forged-operation`,
324
+ method: 'GET',
325
+ routePath: '/api/customer',
326
+ schemaHash: 'schema-customer',
327
+ operationVersion: 1,
328
+ }),
329
+ },
330
+ {
331
+ enabled: true,
332
+ expectedOperationContracts: {
333
+ 'GET:/api/customer': {
334
+ schemaHash: 'schema-customer',
335
+ operationVersion: 1,
336
+ },
337
+ },
338
+ },
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
+ },
373
+ );
374
+
375
+ expect(violation?.reason).toBe('operation_context_mismatch');
376
+ });
377
+
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', () => {
565
+ const violation = evaluateCrossProjectPolicy(
566
+ {
567
+ 'x-modernjs-bff-envelope': JSON.stringify({
568
+ requestId: 'crm.producer-a',
569
+ }),
570
+ 'x-operation-id': 'crm.producer-a:GET:/api/unknown',
571
+ 'x-modernjs-bff-operation-context': JSON.stringify({
572
+ requestId: 'crm.producer-a',
573
+ operationId: 'crm.producer-a:GET:/api/unknown',
574
+ method: 'GET',
575
+ routePath: '/api/unknown',
576
+ schemaHash: 'schema-1',
577
+ operationVersion: 1,
578
+ }),
579
+ },
580
+ {
581
+ enabled: true,
582
+ allowUnknownOperations: true,
583
+ expectedOperationContracts: {
584
+ 'GET:/api/customer': {
585
+ schemaHash: 'schema-1',
586
+ operationVersion: 1,
587
+ },
588
+ },
589
+ },
590
+ );
591
+
592
+ expect(violation).toBeNull();
593
+ });
594
+ });
595
+
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
+ describe('cross-project request observation', () => {
834
+ const resolve = (
835
+ pathname: string,
836
+ expectedOperationContracts: Record<string, CrossProjectOperationContract>,
837
+ method = 'GET',
838
+ ) =>
839
+ resolveCrossProjectRequestObservation(
840
+ { method, pathname },
841
+ { expectedOperationContracts },
842
+ );
843
+
844
+ test('fails closed when two route templates have the same specificity', () => {
845
+ expect(
846
+ resolve('/foo/42', {
847
+ 'GET:/api/:entity/42': {},
848
+ 'GET:/api/foo/:id': {},
849
+ }),
850
+ ).toEqual({
851
+ method: 'GET',
852
+ routePath: '/foo/42',
853
+ });
854
+ });
855
+
856
+ test('does not strip a dynamic mount prefix', () => {
857
+ expect(
858
+ resolve('/products/42', {
859
+ 'GET:/api/:tenant/products/:id': {},
860
+ }),
861
+ ).toEqual({
862
+ method: 'GET',
863
+ routePath: '/products/42',
864
+ });
865
+ });
866
+
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({
872
+ method: 'GET',
873
+ routePath: pathname,
874
+ });
875
+ });
876
+
877
+ test.each([
878
+ '/files/a%2Fb',
879
+ '/files/a%5Cb',
880
+ '/files/a%252Fb',
881
+ '/files/%ZZ',
882
+ ])('fails closed for ambiguous or malformed percent encoding in %s', pathname => {
883
+ expect(
884
+ resolve(pathname, {
885
+ 'GET:/api/files/:id': {},
886
+ }),
887
+ ).toEqual({
888
+ method: 'GET',
889
+ routePath: pathname,
890
+ });
891
+ });
892
+
893
+ 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({
899
+ method: 'GET',
900
+ routePath: '/products/42/',
901
+ });
902
+ expect(
903
+ resolve('/products/42/', {
904
+ 'GET:/api/products/:id/': {},
905
+ }),
906
+ ).toEqual({
907
+ method: 'GET',
908
+ routePath: '/api/products/:id/',
909
+ });
910
+ });
911
+
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
+ test('binds a mounted root request to the fixed producer prefix', () => {
945
+ expect(
946
+ resolve('/', {
947
+ 'GET:/api': {},
948
+ }),
949
+ ).toEqual({
950
+ method: 'GET',
951
+ routePath: '/api',
952
+ });
953
+ });
954
+
955
+ test('denies a GET contract sent to a same-route POST operation', () => {
956
+ const contracts = buildOperationContractMap({
957
+ handlers: [
958
+ {
959
+ name: 'getProduct',
960
+ httpMethod: 'GET',
961
+ routePath: '/api/products/:id',
962
+ },
963
+ {
964
+ name: 'updateProduct',
965
+ httpMethod: 'POST',
966
+ routePath: '/api/products/:id',
967
+ },
968
+ ],
969
+ requestId: 'catalog.producer-a',
970
+ });
971
+ const getContract = contracts['GET:/api/products/:id']!;
972
+ const observedRequest = resolveCrossProjectRequestObservation(
973
+ {
974
+ method: 'POST',
975
+ pathname: '/products/42',
976
+ },
977
+ { expectedOperationContracts: contracts },
978
+ );
979
+
980
+ const violation = evaluateCrossProjectPolicy(
981
+ {
982
+ 'x-modernjs-bff-envelope': JSON.stringify({
983
+ requestId: getContract.requestId,
984
+ }),
985
+ '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,
998
+ },
999
+ observedRequest,
1000
+ );
1001
+
1002
+ expect(violation?.reason).toBe('operation_context_mismatch');
1003
+ });
1004
+ });