@bleedingdev/modern-js-server-runtime-extensions 3.9.0-ultramodern.1 → 3.9.0-ultramodern.11
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 +8 -6
- 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/module-federation-css/remote.js +2 -1
- 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/module-federation-css/remote.mjs +2 -1
- 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/module-federation-css/remote.mjs +2 -1
- 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/module-federation-css/remote.d.ts +2 -0
- 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 +22 -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 +159 -27
- 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/module-federation-css/remote.ts +3 -1
- 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 +450 -0
- package/tests/bff-policy/crossProjectPolicy.test.ts +197 -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 +76 -0
- package/tests/contractGateSnapshotObserver.test.ts +0 -58
- package/tests/contractGateSnapshotStore.test.ts +0 -51
- 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 +98 -41
- package/tests/nativeErrorResponse.test.ts +118 -0
- package/tests/nativeServerAssembly.test.ts +385 -0
- package/tests/runtimeLifecycle.test.ts +69 -17
- package/tests/static-serving/ordering.test.ts +214 -0
- package/tests/static-serving/static.test.ts +411 -0
- package/tests/telemetry.test.ts +35 -198
- package/tests/telemetryHealthMonitor.test.ts +0 -4
- package/tests/telemetryHealthObservation.test.ts +302 -0
- package/tests/telemetryLifecycle.test.ts +7 -303
- package/tests/telemetryRegistryQueueMatrix.test.ts +14 -111
- package/tests/registration.test.ts +0 -112
- package/tests/telemetryAutopilot.test.ts +0 -1134
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CrossProjectPolicyConfig,
|
|
3
|
+
type CrossProjectPolicyViolationReason,
|
|
4
|
+
evaluateCrossProjectPolicy,
|
|
5
|
+
} from '../../src/bff-policy/crossProjectPolicy';
|
|
6
|
+
|
|
7
|
+
const ENVELOPE_HEADER = 'x-modernjs-bff-envelope';
|
|
8
|
+
const OPERATION_CONTEXT_HEADER = 'x-operation-id';
|
|
9
|
+
const OPERATION_CONTEXT_DETAIL_HEADER = 'x-modernjs-bff-operation-context';
|
|
10
|
+
const VERIFIED_PRODUCER_HEADER = 'x-verified-producer';
|
|
11
|
+
|
|
12
|
+
const OMIT = Symbol('omit header');
|
|
13
|
+
|
|
14
|
+
type RawHeader = {
|
|
15
|
+
kind: 'raw';
|
|
16
|
+
value: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type HeaderInput = RawHeader | unknown | typeof OMIT;
|
|
20
|
+
|
|
21
|
+
type PolicyMatrixScenario = {
|
|
22
|
+
name: string;
|
|
23
|
+
headers?: Record<string, unknown>;
|
|
24
|
+
envelope?: HeaderInput;
|
|
25
|
+
operationContext?: string | typeof OMIT;
|
|
26
|
+
detail?: HeaderInput;
|
|
27
|
+
config: CrossProjectPolicyConfig;
|
|
28
|
+
nodeEnv?: string;
|
|
29
|
+
expected:
|
|
30
|
+
| {
|
|
31
|
+
kind: 'allow';
|
|
32
|
+
}
|
|
33
|
+
| {
|
|
34
|
+
kind: 'deny';
|
|
35
|
+
reason: CrossProjectPolicyViolationReason;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const raw = (value: string): RawHeader => ({
|
|
40
|
+
kind: 'raw',
|
|
41
|
+
value,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const isRawHeader = (value: HeaderInput): value is RawHeader =>
|
|
45
|
+
Boolean(
|
|
46
|
+
value &&
|
|
47
|
+
typeof value === 'object' &&
|
|
48
|
+
'kind' in value &&
|
|
49
|
+
value.kind === 'raw',
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const encodeHeader = (value: HeaderInput): string =>
|
|
53
|
+
isRawHeader(value) ? value.value : JSON.stringify(value);
|
|
54
|
+
|
|
55
|
+
const validEnvelope = {
|
|
56
|
+
requestId: 'crm.producer-a',
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const validOperationContext = 'crm.producer-a:GET:/api/customer';
|
|
60
|
+
|
|
61
|
+
const validDetail = {
|
|
62
|
+
requestId: 'crm.producer-a',
|
|
63
|
+
operationId: validOperationContext,
|
|
64
|
+
method: 'GET',
|
|
65
|
+
routePath: '/api/customer',
|
|
66
|
+
schemaHash: 'schema-1',
|
|
67
|
+
operationVersion: 1,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const strictContractConfig = {
|
|
71
|
+
enabled: true,
|
|
72
|
+
expectedOperationContracts: {
|
|
73
|
+
'GET:/api/customer': {
|
|
74
|
+
schemaHash: 'schema-1',
|
|
75
|
+
operationVersion: 1,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
} satisfies CrossProjectPolicyConfig;
|
|
79
|
+
|
|
80
|
+
const verifyProducerHeader = (headers: Record<string, unknown>) =>
|
|
81
|
+
typeof headers[VERIFIED_PRODUCER_HEADER] === 'string'
|
|
82
|
+
? headers[VERIFIED_PRODUCER_HEADER]
|
|
83
|
+
: undefined;
|
|
84
|
+
|
|
85
|
+
const buildHeaders = ({
|
|
86
|
+
headers,
|
|
87
|
+
envelope,
|
|
88
|
+
operationContext,
|
|
89
|
+
detail,
|
|
90
|
+
}: Pick<
|
|
91
|
+
PolicyMatrixScenario,
|
|
92
|
+
'headers' | 'envelope' | 'operationContext' | 'detail'
|
|
93
|
+
>): Record<string, unknown> => {
|
|
94
|
+
const result: Record<string, unknown> = {
|
|
95
|
+
...headers,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const envelopeInput = envelope ?? validEnvelope;
|
|
99
|
+
if (envelopeInput !== OMIT) {
|
|
100
|
+
result[ENVELOPE_HEADER] = encodeHeader(envelopeInput);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const operationContextInput = operationContext ?? validOperationContext;
|
|
104
|
+
if (operationContextInput !== OMIT) {
|
|
105
|
+
result[OPERATION_CONTEXT_HEADER] = operationContextInput;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const detailInput = detail ?? validDetail;
|
|
109
|
+
if (detailInput !== OMIT) {
|
|
110
|
+
result[OPERATION_CONTEXT_DETAIL_HEADER] = encodeHeader(detailInput);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return result;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const withNodeEnv = <T>(nodeEnv: string | undefined, callback: () => T): T => {
|
|
117
|
+
if (!nodeEnv) {
|
|
118
|
+
return callback();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const previousNodeEnv = process.env.NODE_ENV;
|
|
122
|
+
process.env.NODE_ENV = nodeEnv;
|
|
123
|
+
try {
|
|
124
|
+
return callback();
|
|
125
|
+
} finally {
|
|
126
|
+
if (typeof previousNodeEnv === 'undefined') {
|
|
127
|
+
delete process.env.NODE_ENV;
|
|
128
|
+
} else {
|
|
129
|
+
process.env.NODE_ENV = previousNodeEnv;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const policyMatrix = [
|
|
135
|
+
{
|
|
136
|
+
name: 'allow: full valid context with verified namespace and contract',
|
|
137
|
+
headers: {
|
|
138
|
+
[VERIFIED_PRODUCER_HEADER]: 'crm',
|
|
139
|
+
},
|
|
140
|
+
config: {
|
|
141
|
+
...strictContractConfig,
|
|
142
|
+
allowedNamespaces: ['crm'],
|
|
143
|
+
verifyProducerIdentity: verifyProducerHeader,
|
|
144
|
+
},
|
|
145
|
+
expected: {
|
|
146
|
+
kind: 'allow',
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
name: 'allow: valid context with optional detail operationId omitted',
|
|
151
|
+
detail: {
|
|
152
|
+
requestId: 'crm.producer-a',
|
|
153
|
+
method: 'GET',
|
|
154
|
+
routePath: '/api/customer',
|
|
155
|
+
schemaHash: 'schema-1',
|
|
156
|
+
operationVersion: 1,
|
|
157
|
+
},
|
|
158
|
+
config: strictContractConfig,
|
|
159
|
+
expected: {
|
|
160
|
+
kind: 'allow',
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
name: 'deny: envelope header missing',
|
|
165
|
+
envelope: OMIT,
|
|
166
|
+
config: strictContractConfig,
|
|
167
|
+
expected: {
|
|
168
|
+
kind: 'deny',
|
|
169
|
+
reason: 'missing_envelope',
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
name: 'deny: envelope header is not JSON',
|
|
174
|
+
envelope: raw('not-json'),
|
|
175
|
+
config: strictContractConfig,
|
|
176
|
+
expected: {
|
|
177
|
+
kind: 'deny',
|
|
178
|
+
reason: 'invalid_envelope',
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: 'deny: envelope header JSON is not an object',
|
|
183
|
+
envelope: 123,
|
|
184
|
+
config: strictContractConfig,
|
|
185
|
+
expected: {
|
|
186
|
+
kind: 'deny',
|
|
187
|
+
reason: 'invalid_envelope',
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
name: 'deny: envelope requestId missing',
|
|
192
|
+
envelope: {},
|
|
193
|
+
config: strictContractConfig,
|
|
194
|
+
expected: {
|
|
195
|
+
kind: 'deny',
|
|
196
|
+
reason: 'missing_request_id',
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: 'deny: namespace outside verified allowlist',
|
|
201
|
+
headers: {
|
|
202
|
+
[VERIFIED_PRODUCER_HEADER]: 'billing',
|
|
203
|
+
},
|
|
204
|
+
envelope: {
|
|
205
|
+
requestId: 'billing.producer-a',
|
|
206
|
+
},
|
|
207
|
+
operationContext: 'billing.producer-a:GET:/api/customer',
|
|
208
|
+
detail: {
|
|
209
|
+
requestId: 'billing.producer-a',
|
|
210
|
+
operationId: 'billing.producer-a:GET:/api/customer',
|
|
211
|
+
method: 'GET',
|
|
212
|
+
routePath: '/api/customer',
|
|
213
|
+
schemaHash: 'schema-1',
|
|
214
|
+
operationVersion: 1,
|
|
215
|
+
},
|
|
216
|
+
config: {
|
|
217
|
+
...strictContractConfig,
|
|
218
|
+
allowedNamespaces: ['crm'],
|
|
219
|
+
verifyProducerIdentity: verifyProducerHeader,
|
|
220
|
+
},
|
|
221
|
+
expected: {
|
|
222
|
+
kind: 'deny',
|
|
223
|
+
reason: 'namespace_not_allowed',
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
name: 'deny: producer identity cannot be verified',
|
|
228
|
+
config: {
|
|
229
|
+
...strictContractConfig,
|
|
230
|
+
verifyProducerIdentity: () => undefined,
|
|
231
|
+
},
|
|
232
|
+
expected: {
|
|
233
|
+
kind: 'deny',
|
|
234
|
+
reason: 'producer_identity_mismatch',
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
name: 'deny: producer identity does not match envelope namespace',
|
|
239
|
+
headers: {
|
|
240
|
+
[VERIFIED_PRODUCER_HEADER]: 'billing',
|
|
241
|
+
},
|
|
242
|
+
config: {
|
|
243
|
+
...strictContractConfig,
|
|
244
|
+
verifyProducerIdentity: verifyProducerHeader,
|
|
245
|
+
},
|
|
246
|
+
expected: {
|
|
247
|
+
kind: 'deny',
|
|
248
|
+
reason: 'producer_identity_mismatch',
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: 'deny: allowlist without producer verifier fails closed',
|
|
253
|
+
config: {
|
|
254
|
+
enabled: true,
|
|
255
|
+
allowedNamespaces: ['crm'],
|
|
256
|
+
requireOperationContext: false,
|
|
257
|
+
},
|
|
258
|
+
expected: {
|
|
259
|
+
kind: 'deny',
|
|
260
|
+
reason: 'producer_identity_mismatch',
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
name: 'deny: operation context header missing',
|
|
265
|
+
operationContext: OMIT,
|
|
266
|
+
config: strictContractConfig,
|
|
267
|
+
expected: {
|
|
268
|
+
kind: 'deny',
|
|
269
|
+
reason: 'missing_operation_context',
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
name: 'deny: operation context requestId does not match envelope',
|
|
274
|
+
operationContext: 'billing.producer-a:GET:/api/customer',
|
|
275
|
+
config: strictContractConfig,
|
|
276
|
+
expected: {
|
|
277
|
+
kind: 'deny',
|
|
278
|
+
reason: 'operation_context_mismatch',
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
name: 'deny: operation context details header missing',
|
|
283
|
+
detail: OMIT,
|
|
284
|
+
config: strictContractConfig,
|
|
285
|
+
expected: {
|
|
286
|
+
kind: 'deny',
|
|
287
|
+
reason: 'missing_operation_context_details',
|
|
288
|
+
},
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
name: 'deny: operation context details header is not JSON',
|
|
292
|
+
detail: raw('not-json'),
|
|
293
|
+
config: strictContractConfig,
|
|
294
|
+
expected: {
|
|
295
|
+
kind: 'deny',
|
|
296
|
+
reason: 'invalid_operation_context_details',
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
name: 'deny: operation context details JSON is not an object',
|
|
301
|
+
detail: ['not-an-object'],
|
|
302
|
+
config: strictContractConfig,
|
|
303
|
+
expected: {
|
|
304
|
+
kind: 'deny',
|
|
305
|
+
reason: 'invalid_operation_context_details',
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
name: 'deny: operation context details requestId mismatch',
|
|
310
|
+
detail: {
|
|
311
|
+
...validDetail,
|
|
312
|
+
requestId: 'billing.producer-a',
|
|
313
|
+
},
|
|
314
|
+
config: strictContractConfig,
|
|
315
|
+
expected: {
|
|
316
|
+
kind: 'deny',
|
|
317
|
+
reason: 'operation_context_details_request_id_mismatch',
|
|
318
|
+
},
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
name: 'deny: operation context detail operationId conflicts with header',
|
|
322
|
+
detail: {
|
|
323
|
+
requestId: 'crm.producer-a',
|
|
324
|
+
operationId: 'crm.producer-a:GET:/api/invoice',
|
|
325
|
+
method: 'GET',
|
|
326
|
+
routePath: '/api/invoice',
|
|
327
|
+
schemaHash: 'schema-invoice',
|
|
328
|
+
operationVersion: 1,
|
|
329
|
+
},
|
|
330
|
+
config: {
|
|
331
|
+
enabled: true,
|
|
332
|
+
expectedOperationContracts: {
|
|
333
|
+
'GET:/api/invoice': {
|
|
334
|
+
schemaHash: 'schema-invoice',
|
|
335
|
+
operationVersion: 1,
|
|
336
|
+
},
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
expected: {
|
|
340
|
+
kind: 'deny',
|
|
341
|
+
reason: 'operation_context_mismatch',
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
name: 'deny: operation schema hash missing',
|
|
346
|
+
detail: {
|
|
347
|
+
requestId: 'crm.producer-a',
|
|
348
|
+
operationId: validOperationContext,
|
|
349
|
+
method: 'GET',
|
|
350
|
+
routePath: '/api/customer',
|
|
351
|
+
operationVersion: 1,
|
|
352
|
+
},
|
|
353
|
+
config: strictContractConfig,
|
|
354
|
+
expected: {
|
|
355
|
+
kind: 'deny',
|
|
356
|
+
reason: 'missing_operation_schema_hash',
|
|
357
|
+
},
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
name: 'deny: operation schema hash mismatches contract',
|
|
361
|
+
detail: {
|
|
362
|
+
...validDetail,
|
|
363
|
+
schemaHash: 'schema-actual',
|
|
364
|
+
},
|
|
365
|
+
config: {
|
|
366
|
+
enabled: true,
|
|
367
|
+
expectedOperationContracts: {
|
|
368
|
+
'GET:/api/customer': {
|
|
369
|
+
schemaHash: 'schema-expected',
|
|
370
|
+
operationVersion: 1,
|
|
371
|
+
},
|
|
372
|
+
},
|
|
373
|
+
},
|
|
374
|
+
expected: {
|
|
375
|
+
kind: 'deny',
|
|
376
|
+
reason: 'operation_schema_hash_mismatch',
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
{
|
|
380
|
+
name: 'deny: operation version missing',
|
|
381
|
+
detail: {
|
|
382
|
+
requestId: 'crm.producer-a',
|
|
383
|
+
operationId: validOperationContext,
|
|
384
|
+
method: 'GET',
|
|
385
|
+
routePath: '/api/customer',
|
|
386
|
+
schemaHash: 'schema-1',
|
|
387
|
+
},
|
|
388
|
+
config: strictContractConfig,
|
|
389
|
+
expected: {
|
|
390
|
+
kind: 'deny',
|
|
391
|
+
reason: 'missing_operation_version',
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
name: 'deny: operation version mismatches contract',
|
|
396
|
+
detail: {
|
|
397
|
+
...validDetail,
|
|
398
|
+
operationVersion: 2,
|
|
399
|
+
},
|
|
400
|
+
config: strictContractConfig,
|
|
401
|
+
expected: {
|
|
402
|
+
kind: 'deny',
|
|
403
|
+
reason: 'operation_version_mismatch',
|
|
404
|
+
},
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
name: 'deny: unknown operation contract',
|
|
408
|
+
operationContext: 'crm.producer-a:GET:/api/unknown',
|
|
409
|
+
detail: {
|
|
410
|
+
requestId: 'crm.producer-a',
|
|
411
|
+
operationId: 'crm.producer-a:GET:/api/unknown',
|
|
412
|
+
method: 'GET',
|
|
413
|
+
routePath: '/api/unknown',
|
|
414
|
+
schemaHash: 'schema-1',
|
|
415
|
+
operationVersion: 1,
|
|
416
|
+
},
|
|
417
|
+
config: strictContractConfig,
|
|
418
|
+
expected: {
|
|
419
|
+
kind: 'deny',
|
|
420
|
+
reason: 'unknown_operation_contract',
|
|
421
|
+
},
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
name: 'deny: empty operation contract map fails closed',
|
|
425
|
+
config: {
|
|
426
|
+
enabled: true,
|
|
427
|
+
expectedOperationContracts: {},
|
|
428
|
+
},
|
|
429
|
+
expected: {
|
|
430
|
+
kind: 'deny',
|
|
431
|
+
reason: 'unknown_operation_contract',
|
|
432
|
+
},
|
|
433
|
+
},
|
|
434
|
+
] satisfies PolicyMatrixScenario[];
|
|
435
|
+
|
|
436
|
+
describe('cross-project policy matrix', () => {
|
|
437
|
+
for (const scenario of policyMatrix) {
|
|
438
|
+
test(scenario.name, () => {
|
|
439
|
+
const violation = withNodeEnv(scenario.nodeEnv, () =>
|
|
440
|
+
evaluateCrossProjectPolicy(buildHeaders(scenario), scenario.config),
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
if (scenario.expected.kind === 'allow') {
|
|
444
|
+
expect(violation).toBeNull();
|
|
445
|
+
} else {
|
|
446
|
+
expect(violation?.reason).toBe(scenario.expected.reason);
|
|
447
|
+
}
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
});
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CrossProjectOperationContract,
|
|
3
|
+
evaluateCrossProjectPolicy,
|
|
4
|
+
resolveCrossProjectRequestObservation,
|
|
5
|
+
} from '../../src/bff-policy/crossProjectPolicy';
|
|
6
|
+
import { buildOperationContractMap } from '../../src/bff-policy/operationContracts';
|
|
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.
|
|
11
|
+
describe('cross-project policy', () => {
|
|
12
|
+
test('denies operation metadata that does not match the observed request', () => {
|
|
13
|
+
const contracts = buildOperationContractMap({
|
|
14
|
+
handlers: [
|
|
15
|
+
{ name: 'getCustomer', httpMethod: 'GET', routePath: '/api/customer' },
|
|
16
|
+
{
|
|
17
|
+
name: 'createInvoice',
|
|
18
|
+
httpMethod: 'POST',
|
|
19
|
+
routePath: '/api/invoice',
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
requestId: 'crm.producer-a',
|
|
23
|
+
});
|
|
24
|
+
const forgedContract = contracts['POST:/api/invoice']!;
|
|
25
|
+
|
|
26
|
+
const violation = evaluateCrossProjectPolicy(
|
|
27
|
+
{
|
|
28
|
+
'x-modernjs-bff-envelope': JSON.stringify({
|
|
29
|
+
requestId: forgedContract.requestId,
|
|
30
|
+
}),
|
|
31
|
+
'x-operation-id': forgedContract.operationId,
|
|
32
|
+
'x-modernjs-bff-operation-context': JSON.stringify(forgedContract),
|
|
33
|
+
},
|
|
34
|
+
{ enabled: true, expectedOperationContracts: contracts },
|
|
35
|
+
{ method: 'GET', routePath: '/api/customer' },
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
expect(violation?.reason).toBe('operation_context_mismatch');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('binds operation identity when a manual contract omits operationId', () => {
|
|
42
|
+
const requestId = 'crm.producer-a';
|
|
43
|
+
const violation = evaluateCrossProjectPolicy(
|
|
44
|
+
{
|
|
45
|
+
'x-modernjs-bff-envelope': JSON.stringify({ requestId }),
|
|
46
|
+
'x-operation-id': `${requestId}:forged-operation`,
|
|
47
|
+
'x-modernjs-bff-operation-context': JSON.stringify({
|
|
48
|
+
requestId,
|
|
49
|
+
operationId: `${requestId}:forged-operation`,
|
|
50
|
+
method: 'GET',
|
|
51
|
+
routePath: '/api/customer',
|
|
52
|
+
schemaHash: 'schema-customer',
|
|
53
|
+
operationVersion: 1,
|
|
54
|
+
}),
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
enabled: true,
|
|
58
|
+
expectedOperationContracts: {
|
|
59
|
+
'GET:/api/customer': {
|
|
60
|
+
schemaHash: 'schema-customer',
|
|
61
|
+
operationVersion: 1,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
{ method: 'GET', routePath: '/api/customer' },
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect(violation?.reason).toBe('operation_context_mismatch');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('allows unknown operation contracts when allowUnknownOperations is true', () => {
|
|
72
|
+
const violation = evaluateCrossProjectPolicy(
|
|
73
|
+
{
|
|
74
|
+
'x-modernjs-bff-envelope': JSON.stringify({
|
|
75
|
+
requestId: 'crm.producer-a',
|
|
76
|
+
}),
|
|
77
|
+
'x-operation-id': 'crm.producer-a:GET:/api/unknown',
|
|
78
|
+
'x-modernjs-bff-operation-context': JSON.stringify({
|
|
79
|
+
requestId: 'crm.producer-a',
|
|
80
|
+
operationId: 'crm.producer-a:GET:/api/unknown',
|
|
81
|
+
method: 'GET',
|
|
82
|
+
routePath: '/api/unknown',
|
|
83
|
+
schemaHash: 'schema-1',
|
|
84
|
+
operationVersion: 1,
|
|
85
|
+
}),
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
enabled: true,
|
|
89
|
+
allowUnknownOperations: true,
|
|
90
|
+
expectedOperationContracts: {
|
|
91
|
+
'GET:/api/customer': { schemaHash: 'schema-1', operationVersion: 1 },
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
expect(violation).toBeNull();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('cross-project request observation', () => {
|
|
101
|
+
const resolve = (
|
|
102
|
+
pathname: string,
|
|
103
|
+
expectedOperationContracts: Record<string, CrossProjectOperationContract>,
|
|
104
|
+
method = 'GET',
|
|
105
|
+
) =>
|
|
106
|
+
resolveCrossProjectRequestObservation(
|
|
107
|
+
{ method, pathname },
|
|
108
|
+
{ expectedOperationContracts },
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
test('fails closed when two route templates have the same specificity', () => {
|
|
112
|
+
expect(
|
|
113
|
+
resolve('/foo/42', {
|
|
114
|
+
'GET:/api/:entity/42': {},
|
|
115
|
+
'GET:/api/foo/:id': {},
|
|
116
|
+
}),
|
|
117
|
+
).toEqual({ method: 'GET', routePath: '/foo/42' });
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('does not strip a dynamic mount prefix', () => {
|
|
121
|
+
expect(
|
|
122
|
+
resolve('/products/42', { 'GET:/api/:tenant/products/:id': {} }),
|
|
123
|
+
).toEqual({ method: 'GET', routePath: '/products/42' });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('fails closed for a nonterminal wildcard template', () => {
|
|
127
|
+
expect(resolve('/anything/admin', { 'GET:/api/*/admin': {} })).toEqual({
|
|
128
|
+
method: 'GET',
|
|
129
|
+
routePath: '/anything/admin',
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test.each([
|
|
134
|
+
'/files/a%2Fb',
|
|
135
|
+
'/files/%ZZ',
|
|
136
|
+
])('fails closed for ambiguous or malformed percent encoding in %s', pathname => {
|
|
137
|
+
expect(resolve(pathname, { 'GET:/api/files/:id': {} })).toEqual({
|
|
138
|
+
method: 'GET',
|
|
139
|
+
routePath: pathname,
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('preserves trailing-slash distinctions while accepting an exact trailing template', () => {
|
|
144
|
+
expect(resolve('/products/42/', { 'GET:/api/products/:id': {} })).toEqual({
|
|
145
|
+
method: 'GET',
|
|
146
|
+
routePath: '/products/42/',
|
|
147
|
+
});
|
|
148
|
+
expect(resolve('/products/42/', { 'GET:/api/products/:id/': {} })).toEqual({
|
|
149
|
+
method: 'GET',
|
|
150
|
+
routePath: '/api/products/:id/',
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('binds a mounted root request to the fixed producer prefix', () => {
|
|
155
|
+
expect(resolve('/', { 'GET:/api': {} })).toEqual({
|
|
156
|
+
method: 'GET',
|
|
157
|
+
routePath: '/api',
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('denies a GET contract sent to a same-route POST operation', () => {
|
|
162
|
+
const contracts = buildOperationContractMap({
|
|
163
|
+
handlers: [
|
|
164
|
+
{
|
|
165
|
+
name: 'getProduct',
|
|
166
|
+
httpMethod: 'GET',
|
|
167
|
+
routePath: '/api/products/:id',
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
name: 'updateProduct',
|
|
171
|
+
httpMethod: 'POST',
|
|
172
|
+
routePath: '/api/products/:id',
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
requestId: 'catalog.producer-a',
|
|
176
|
+
});
|
|
177
|
+
const getContract = contracts['GET:/api/products/:id']!;
|
|
178
|
+
const observedRequest = resolveCrossProjectRequestObservation(
|
|
179
|
+
{ method: 'POST', pathname: '/products/42' },
|
|
180
|
+
{ expectedOperationContracts: contracts },
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const violation = evaluateCrossProjectPolicy(
|
|
184
|
+
{
|
|
185
|
+
'x-modernjs-bff-envelope': JSON.stringify({
|
|
186
|
+
requestId: getContract.requestId,
|
|
187
|
+
}),
|
|
188
|
+
'x-operation-id': getContract.operationId,
|
|
189
|
+
'x-modernjs-bff-operation-context': JSON.stringify(getContract),
|
|
190
|
+
},
|
|
191
|
+
{ enabled: true, expectedOperationContracts: contracts },
|
|
192
|
+
observedRequest,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
expect(violation?.reason).toBe('operation_context_mismatch');
|
|
196
|
+
});
|
|
197
|
+
});
|