@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.4 → 3.9.0-ultramodern.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/cjs/bff-policy/crossProjectPolicy.js +299 -0
- package/dist/cjs/bff-policy/index.js +62 -0
- package/dist/cjs/bff-policy/node.js +69 -0
- package/dist/cjs/bff-policy/operationContracts.js +182 -0
- package/dist/cjs/bff-policy/resolveCrossProjectPolicy.js +65 -0
- package/dist/cjs/contract-gate-snapshot-store/http-store.js +1 -8
- package/dist/cjs/errorResponsePlugin.js +57 -0
- package/dist/cjs/localisedLoaderPlugin.js +62 -0
- package/dist/cjs/mfCache.js +6 -0
- package/dist/cjs/module-federation-css/plugin.js +6 -0
- package/dist/cjs/serverConfig.js +18 -0
- package/dist/cjs/serverPlugin.js +74 -0
- package/dist/cjs/static-serving/plugin.js +57 -0
- package/dist/cjs/static-serving/responders.js +158 -0
- package/dist/cjs/static-serving/staticModuleFederation.js +173 -0
- package/dist/cjs/static-serving/staticPrecompressed.js +152 -0
- package/dist/cjs/telemetry/exporters.js +4 -4
- package/dist/cjs/telemetry/healthSetup.js +14 -14
- package/dist/cjs/telemetry/lifecycle.js +20 -21
- package/dist/cjs/telemetry/plugin.js +10 -4
- package/dist/cjs/telemetry/runtimeEndpoints.js +3 -2
- package/dist/cjs/telemetryCore.js +5 -5
- package/dist/esm/bff-policy/crossProjectPolicy.mjs +249 -0
- package/dist/esm/bff-policy/index.mjs +1 -0
- package/dist/esm/contract-gate-snapshot-store/http-store.mjs +1 -8
- package/dist/esm/errorResponsePlugin.mjs +19 -0
- package/dist/esm/localisedLoaderPlugin.mjs +24 -0
- package/dist/esm/mfCache.mjs +6 -0
- package/dist/esm/module-federation-css/plugin.mjs +6 -0
- package/dist/esm/serverConfig.mjs +0 -0
- package/dist/esm/serverPlugin.mjs +34 -0
- package/dist/esm/telemetry/exporters.mjs +2 -2
- package/dist/esm/telemetry/healthSetup.mjs +14 -14
- package/dist/esm/telemetry/lifecycle.mjs +21 -22
- package/dist/esm/telemetry/plugin.mjs +9 -3
- package/dist/esm/telemetry/runtimeEndpoints.mjs +3 -2
- package/dist/esm/telemetryCore.mjs +1 -1
- package/dist/esm-node/bff-policy/crossProjectPolicy.mjs +250 -0
- package/dist/esm-node/bff-policy/index.mjs +2 -0
- package/dist/esm-node/bff-policy/node.mjs +3 -0
- package/dist/esm-node/bff-policy/operationContracts.mjs +129 -0
- package/dist/esm-node/bff-policy/resolveCrossProjectPolicy.mjs +28 -0
- package/dist/esm-node/contract-gate-snapshot-store/http-store.mjs +1 -8
- package/dist/esm-node/errorResponsePlugin.mjs +20 -0
- package/dist/esm-node/localisedLoaderPlugin.mjs +25 -0
- package/dist/esm-node/mfCache.mjs +6 -0
- package/dist/esm-node/module-federation-css/plugin.mjs +6 -0
- package/dist/esm-node/serverConfig.mjs +1 -0
- package/dist/esm-node/serverPlugin.mjs +35 -0
- package/dist/esm-node/static-serving/plugin.mjs +18 -0
- package/dist/esm-node/static-serving/responders.mjs +111 -0
- package/dist/esm-node/static-serving/staticModuleFederation.mjs +105 -0
- package/dist/esm-node/static-serving/staticPrecompressed.mjs +112 -0
- package/dist/esm-node/telemetry/exporters.mjs +2 -2
- package/dist/esm-node/telemetry/healthSetup.mjs +14 -14
- package/dist/esm-node/telemetry/lifecycle.mjs +21 -22
- package/dist/esm-node/telemetry/plugin.mjs +9 -3
- package/dist/esm-node/telemetry/runtimeEndpoints.mjs +3 -2
- package/dist/esm-node/telemetryCore.mjs +1 -1
- package/dist/types/bff-policy/crossProjectPolicy.d.ts +101 -0
- package/dist/types/bff-policy/index.d.ts +1 -0
- package/dist/types/bff-policy/node.d.ts +2 -0
- package/dist/types/bff-policy/operationContracts.d.ts +85 -0
- package/dist/types/bff-policy/resolveCrossProjectPolicy.d.ts +39 -0
- package/dist/types/errorResponsePlugin.d.ts +3 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/localisedLoaderPlugin.d.ts +2 -0
- package/dist/types/mfCache.d.ts +2 -3
- package/dist/types/module-federation-css/plugin.d.ts +2 -3
- package/dist/types/serverConfig.d.ts +17 -0
- package/dist/types/serverPlugin.d.ts +5 -0
- package/dist/types/static-serving/plugin.d.ts +3 -0
- package/dist/types/static-serving/responders.d.ts +2 -0
- package/dist/types/static-serving/staticModuleFederation.d.ts +13 -0
- package/dist/types/static-serving/staticPrecompressed.d.ts +13 -0
- package/dist/types/telemetry/exporters.d.ts +1 -1
- package/dist/types/telemetry/healthSetup.d.ts +3 -3
- package/dist/types/telemetry/lifecycle.d.ts +4 -4
- package/dist/types/telemetry/plugin.d.ts +2 -1
- package/dist/types/telemetryCore.d.ts +1 -1
- package/dist/types-esm/backend-federation-security/index.d.mts +68 -0
- package/dist/types-esm/backend-federation-security/node.d.mts +3 -0
- package/dist/types-esm/bff-policy/crossProjectPolicy.d.mts +101 -0
- package/dist/types-esm/bff-policy/index.d.mts +1 -0
- package/dist/types-esm/bff-policy/node.d.mts +2 -0
- package/dist/types-esm/bff-policy/operationContracts.d.mts +85 -0
- package/dist/types-esm/bff-policy/resolveCrossProjectPolicy.d.mts +39 -0
- package/dist/types-esm/contract-gate-snapshot-normalization.d.mts +15 -0
- package/dist/types-esm/contract-gate-snapshot-store/file-store.d.mts +3 -0
- package/dist/types-esm/contract-gate-snapshot-store/http-store.d.mts +5 -0
- package/dist/types-esm/contract-gate-snapshot-store/index.d.mts +5 -0
- package/dist/types-esm/contract-gate-snapshot-store/resolve.d.mts +9 -0
- package/dist/types-esm/contract-gate-snapshot-store/types.d.mts +47 -0
- package/dist/types-esm/contractGateSnapshotObserver.d.mts +33 -0
- package/dist/types-esm/env.d.mts +40 -0
- package/dist/types-esm/errorResponsePlugin.d.mts +3 -0
- package/dist/types-esm/index.d.mts +8 -0
- package/dist/types-esm/localisedLoaderPlugin.d.mts +2 -0
- package/dist/types-esm/mfCache.d.mts +26 -0
- package/dist/types-esm/module-federation-css/collector.d.mts +25 -0
- package/dist/types-esm/module-federation-css/index.d.mts +8 -0
- package/dist/types-esm/module-federation-css/manifest.d.mts +28 -0
- package/dist/types-esm/module-federation-css/plugin.d.mts +22 -0
- package/dist/types-esm/module-federation-css/remote.d.mts +20 -0
- package/dist/types-esm/runtime-fallback-signal/index.d.mts +4 -0
- package/dist/types-esm/runtime-fallback-signal/model.d.mts +70 -0
- package/dist/types-esm/runtime-fallback-signal/resolver.d.mts +29 -0
- package/dist/types-esm/runtime-fallback-signal/store.d.mts +4 -0
- package/dist/types-esm/runtimeFallbackSignal.d.mts +1 -0
- package/dist/types-esm/runtimeLifecycle.d.mts +10 -0
- package/dist/types-esm/serverConfig.d.mts +17 -0
- package/dist/types-esm/serverPlugin.d.mts +5 -0
- package/dist/types-esm/static-serving/plugin.d.mts +3 -0
- package/dist/types-esm/static-serving/responders.d.mts +2 -0
- package/dist/types-esm/static-serving/staticModuleFederation.d.mts +13 -0
- package/dist/types-esm/static-serving/staticPrecompressed.d.mts +13 -0
- package/dist/types-esm/telemetry/envelope.d.mts +55 -0
- package/dist/types-esm/telemetry/exporters.d.mts +12 -0
- package/dist/types-esm/telemetry/healthEvaluation.d.mts +20 -0
- package/dist/types-esm/telemetry/healthMonitor.d.mts +88 -0
- package/dist/types-esm/telemetry/healthSetup.d.mts +17 -0
- package/dist/types-esm/telemetry/lifecycle.d.mts +18 -0
- package/dist/types-esm/telemetry/metrics.d.mts +3 -0
- package/dist/types-esm/telemetry/plugin.d.mts +13 -0
- package/dist/types-esm/telemetry/registry.d.mts +69 -0
- package/dist/types-esm/telemetry/registryTypes.d.mts +50 -0
- package/dist/types-esm/telemetry/runtimeEndpoints.d.mts +35 -0
- package/dist/types-esm/telemetry.d.mts +1 -0
- package/dist/types-esm/telemetryCore.d.mts +6 -0
- package/package.json +158 -26
- package/rslib.config.mts +36 -2
- package/rstest.config.mts +11 -0
- package/src/bff-policy/crossProjectPolicy.ts +754 -0
- package/src/bff-policy/index.ts +1 -0
- package/src/bff-policy/node.ts +2 -0
- package/src/bff-policy/operationContracts.ts +298 -0
- package/src/bff-policy/resolveCrossProjectPolicy.ts +88 -0
- package/src/contract-gate-snapshot-store/http-store.ts +1 -11
- package/src/errorResponsePlugin.ts +21 -0
- package/src/index.ts +4 -0
- package/src/localisedLoaderPlugin.ts +34 -0
- package/src/mfCache.ts +4 -3
- package/src/module-federation-css/plugin.ts +4 -3
- package/src/serverConfig.ts +32 -0
- package/src/serverPlugin.ts +38 -0
- package/src/static-serving/plugin.ts +16 -0
- package/src/static-serving/responders.ts +208 -0
- package/src/static-serving/staticModuleFederation.ts +255 -0
- package/src/static-serving/staticPrecompressed.ts +193 -0
- package/src/telemetry/exporters.ts +1 -1
- package/src/telemetry/healthSetup.ts +17 -20
- package/src/telemetry/lifecycle.ts +27 -39
- package/src/telemetry/plugin.ts +6 -4
- package/src/telemetry/runtimeEndpoints.ts +3 -2
- package/src/telemetryCore.ts +1 -1
- package/tests/bff-policy/crossProjectPolicy.matrix.test.ts +487 -0
- package/tests/bff-policy/crossProjectPolicy.test.ts +970 -0
- package/tests/bff-policy/operationContracts.test.ts +221 -0
- package/tests/bff-policy/optionalZodPeer.test.ts +30 -0
- package/tests/bff-policy/resolveCrossProjectPolicy.test.ts +134 -0
- package/tests/contractGateSnapshotObserver.test.ts +0 -58
- package/tests/env.test.ts +45 -64
- package/tests/localisedLoader.test.ts +120 -0
- package/tests/mfCache.test.ts +1 -88
- package/tests/moduleFederationCss.test.ts +4 -48
- package/tests/nativeErrorResponse.test.ts +199 -0
- package/tests/nativeServerAssembly.test.ts +402 -0
- package/tests/registration.test.ts +2 -87
- package/tests/runtimeLifecycle.test.ts +69 -17
- package/tests/static-serving/ordering.test.ts +214 -0
- package/tests/static-serving/provider.test.ts +83 -0
- package/tests/static-serving/static.test.ts +400 -0
- package/tests/telemetry.test.ts +35 -101
- package/tests/telemetryHealthMonitor.test.ts +0 -2
- package/tests/{telemetryAutopilot.test.ts → telemetryHealthObservation.test.ts} +43 -182
- package/tests/telemetryLifecycle.test.ts +11 -253
- package/tests/telemetryRegistryQueueMatrix.test.ts +0 -1
|
@@ -0,0 +1,970 @@
|
|
|
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('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
|
+
describe('cross-project request observation', () => {
|
|
800
|
+
const resolve = (
|
|
801
|
+
pathname: string,
|
|
802
|
+
expectedOperationContracts: Record<string, CrossProjectOperationContract>,
|
|
803
|
+
method = 'GET',
|
|
804
|
+
) =>
|
|
805
|
+
resolveCrossProjectRequestObservation(
|
|
806
|
+
{ method, pathname },
|
|
807
|
+
{ expectedOperationContracts },
|
|
808
|
+
);
|
|
809
|
+
|
|
810
|
+
test('fails closed when two route templates have the same specificity', () => {
|
|
811
|
+
expect(
|
|
812
|
+
resolve('/foo/42', {
|
|
813
|
+
'GET:/api/:entity/42': {},
|
|
814
|
+
'GET:/api/foo/:id': {},
|
|
815
|
+
}),
|
|
816
|
+
).toEqual({
|
|
817
|
+
method: 'GET',
|
|
818
|
+
routePath: '/foo/42',
|
|
819
|
+
});
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
test('does not strip a dynamic mount prefix', () => {
|
|
823
|
+
expect(
|
|
824
|
+
resolve('/products/42', {
|
|
825
|
+
'GET:/api/:tenant/products/:id': {},
|
|
826
|
+
}),
|
|
827
|
+
).toEqual({
|
|
828
|
+
method: 'GET',
|
|
829
|
+
routePath: '/products/42',
|
|
830
|
+
});
|
|
831
|
+
});
|
|
832
|
+
|
|
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({
|
|
838
|
+
method: 'GET',
|
|
839
|
+
routePath: pathname,
|
|
840
|
+
});
|
|
841
|
+
});
|
|
842
|
+
|
|
843
|
+
test.each([
|
|
844
|
+
'/files/a%2Fb',
|
|
845
|
+
'/files/a%5Cb',
|
|
846
|
+
'/files/a%252Fb',
|
|
847
|
+
'/files/%ZZ',
|
|
848
|
+
])('fails closed for ambiguous or malformed percent encoding in %s', pathname => {
|
|
849
|
+
expect(
|
|
850
|
+
resolve(pathname, {
|
|
851
|
+
'GET:/api/files/:id': {},
|
|
852
|
+
}),
|
|
853
|
+
).toEqual({
|
|
854
|
+
method: 'GET',
|
|
855
|
+
routePath: pathname,
|
|
856
|
+
});
|
|
857
|
+
});
|
|
858
|
+
|
|
859
|
+
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({
|
|
865
|
+
method: 'GET',
|
|
866
|
+
routePath: '/products/42/',
|
|
867
|
+
});
|
|
868
|
+
expect(
|
|
869
|
+
resolve('/products/42/', {
|
|
870
|
+
'GET:/api/products/:id/': {},
|
|
871
|
+
}),
|
|
872
|
+
).toEqual({
|
|
873
|
+
method: 'GET',
|
|
874
|
+
routePath: '/api/products/:id/',
|
|
875
|
+
});
|
|
876
|
+
});
|
|
877
|
+
|
|
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
|
+
test('binds a mounted root request to the fixed producer prefix', () => {
|
|
911
|
+
expect(
|
|
912
|
+
resolve('/', {
|
|
913
|
+
'GET:/api': {},
|
|
914
|
+
}),
|
|
915
|
+
).toEqual({
|
|
916
|
+
method: 'GET',
|
|
917
|
+
routePath: '/api',
|
|
918
|
+
});
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
test('denies a GET contract sent to a same-route POST operation', () => {
|
|
922
|
+
const contracts = buildOperationContractMap({
|
|
923
|
+
handlers: [
|
|
924
|
+
{
|
|
925
|
+
name: 'getProduct',
|
|
926
|
+
httpMethod: 'GET',
|
|
927
|
+
routePath: '/api/products/:id',
|
|
928
|
+
},
|
|
929
|
+
{
|
|
930
|
+
name: 'updateProduct',
|
|
931
|
+
httpMethod: 'POST',
|
|
932
|
+
routePath: '/api/products/:id',
|
|
933
|
+
},
|
|
934
|
+
],
|
|
935
|
+
requestId: 'catalog.producer-a',
|
|
936
|
+
});
|
|
937
|
+
const getContract = contracts['GET:/api/products/:id']!;
|
|
938
|
+
const observedRequest = resolveCrossProjectRequestObservation(
|
|
939
|
+
{
|
|
940
|
+
method: 'POST',
|
|
941
|
+
pathname: '/products/42',
|
|
942
|
+
},
|
|
943
|
+
{ expectedOperationContracts: contracts },
|
|
944
|
+
);
|
|
945
|
+
|
|
946
|
+
const violation = evaluateCrossProjectPolicy(
|
|
947
|
+
{
|
|
948
|
+
'x-modernjs-bff-envelope': JSON.stringify({
|
|
949
|
+
requestId: getContract.requestId,
|
|
950
|
+
}),
|
|
951
|
+
'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,
|
|
964
|
+
},
|
|
965
|
+
observedRequest,
|
|
966
|
+
);
|
|
967
|
+
|
|
968
|
+
expect(violation?.reason).toBe('operation_context_mismatch');
|
|
969
|
+
});
|
|
970
|
+
});
|