@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,754 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-project BFF policy evaluator.
|
|
3
|
+
*
|
|
4
|
+
* THREAT MODEL — read before relying on this module:
|
|
5
|
+
*
|
|
6
|
+
* Every header this evaluator inspects (envelope, operation id, operation
|
|
7
|
+
* context details) is constructed by the CLIENT from public, open-source
|
|
8
|
+
* formats. Without an out-of-band identity binding the checks below are a
|
|
9
|
+
* version-skew and misconfiguration gate, not an authentication or
|
|
10
|
+
* authorization boundary: any caller can echo an allowed `requestId` and a
|
|
11
|
+
* matching operation context.
|
|
12
|
+
*
|
|
13
|
+
* To use `allowedNamespaces`, supply
|
|
14
|
+
* {@link CrossProjectPolicyConfig.verifyProducerIdentity}: a server-side
|
|
15
|
+
* hook that derives the producer namespace from a VERIFIED channel (mTLS
|
|
16
|
+
* peer identity, gateway-authenticated JWT claims, service-mesh headers
|
|
17
|
+
* stripped at the edge, ...). Requests fail closed when a namespace allowlist
|
|
18
|
+
* is configured without this hook. When the hook is
|
|
19
|
+
* present, the client-asserted namespace must match the verified namespace
|
|
20
|
+
* and the allowlist is checked against the verified value.
|
|
21
|
+
*
|
|
22
|
+
* Client-side counterparts in `@modern-js/create-request` (identity binding,
|
|
23
|
+
* operation contract validation) are developer-experience aids that fail
|
|
24
|
+
* fast in well-behaved clients; they protect nothing against a malicious
|
|
25
|
+
* caller.
|
|
26
|
+
*/
|
|
27
|
+
export const BFF_ENVELOPE_HEADER = 'x-modernjs-bff-envelope';
|
|
28
|
+
export const BFF_OPERATION_CONTEXT_HEADER = 'x-operation-id';
|
|
29
|
+
export const BFF_OPERATION_CONTEXT_DETAIL_HEADER =
|
|
30
|
+
'x-modernjs-bff-operation-context';
|
|
31
|
+
|
|
32
|
+
export type CrossProjectOperationContract = {
|
|
33
|
+
schemaHash?: string;
|
|
34
|
+
operationVersion?: number;
|
|
35
|
+
operationId?: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Request facts observed by the server adapter rather than asserted by the
|
|
40
|
+
* client. `routePath` is the matched route template when the router exposes
|
|
41
|
+
* one (for example `/api/customers/:id`), not the client-provided detail.
|
|
42
|
+
*/
|
|
43
|
+
export type CrossProjectRequestObservation = {
|
|
44
|
+
method: string;
|
|
45
|
+
routePath: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export type CrossProjectRequestTarget = {
|
|
49
|
+
method: string;
|
|
50
|
+
pathname: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type CrossProjectPolicyViolationReason =
|
|
54
|
+
| 'missing_envelope'
|
|
55
|
+
| 'invalid_envelope'
|
|
56
|
+
| 'missing_request_id'
|
|
57
|
+
| 'namespace_not_allowed'
|
|
58
|
+
| 'missing_operation_context'
|
|
59
|
+
| 'operation_context_mismatch'
|
|
60
|
+
| 'missing_operation_context_details'
|
|
61
|
+
| 'invalid_operation_context_details'
|
|
62
|
+
| 'operation_context_details_request_id_mismatch'
|
|
63
|
+
| 'missing_operation_schema_hash'
|
|
64
|
+
| 'missing_operation_version'
|
|
65
|
+
| 'unknown_operation_contract'
|
|
66
|
+
| 'operation_schema_hash_mismatch'
|
|
67
|
+
| 'operation_version_mismatch'
|
|
68
|
+
| 'producer_identity_mismatch';
|
|
69
|
+
|
|
70
|
+
export type CrossProjectPolicyViolation = {
|
|
71
|
+
code: 'BFF_CROSS_PROJECT_POLICY_DENIED';
|
|
72
|
+
reason: CrossProjectPolicyViolationReason;
|
|
73
|
+
message: string;
|
|
74
|
+
status: number;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const DEFAULT_DENY_STATUS = 403;
|
|
78
|
+
const NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE =
|
|
79
|
+
'cross-project namespace allowlist requires verifyProducerIdentity';
|
|
80
|
+
|
|
81
|
+
export interface CrossProjectPolicyConfig {
|
|
82
|
+
enabled?: boolean;
|
|
83
|
+
requireEnvelope?: boolean;
|
|
84
|
+
requireOperationContext?: boolean;
|
|
85
|
+
requireOperationContextDetails?: boolean;
|
|
86
|
+
requireOperationSchemaHash?: boolean;
|
|
87
|
+
requireOperationVersion?: boolean;
|
|
88
|
+
allowedNamespaces?: string[];
|
|
89
|
+
envelopeHeader?: string;
|
|
90
|
+
operationContextHeader?: string;
|
|
91
|
+
operationContextDetailHeader?: string;
|
|
92
|
+
expectedOperationContracts?: Record<string, CrossProjectOperationContract>;
|
|
93
|
+
allowUnknownOperations?: boolean;
|
|
94
|
+
denyStatus?: number;
|
|
95
|
+
/**
|
|
96
|
+
* Server-side hook binding producer namespace to VERIFIED identity
|
|
97
|
+
* channel (mTLS peer, gateway-authenticated JWT, mesh identity headers).
|
|
98
|
+
*
|
|
99
|
+
* When provided, the namespace asserted by the client envelope must match
|
|
100
|
+
* the namespace returned by this hook, and `allowedNamespaces` is checked
|
|
101
|
+
* against the verified value instead of the client-asserted one. Returning
|
|
102
|
+
* `undefined` (identity could not be verified) denies the request.
|
|
103
|
+
*
|
|
104
|
+
* By default, `allowedNamespaces` requires this hook and fails closed
|
|
105
|
+
* without it.
|
|
106
|
+
*/
|
|
107
|
+
verifyProducerIdentity?: (
|
|
108
|
+
headers: Record<string, unknown>,
|
|
109
|
+
) => string | undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Serializable, fully-defaulted policy accepted by edge dispatchers. */
|
|
113
|
+
export type NormalizedCrossProjectPolicy = CrossProjectPolicyConfig & {
|
|
114
|
+
enabled: boolean;
|
|
115
|
+
requireEnvelope: boolean;
|
|
116
|
+
requireOperationContext: boolean;
|
|
117
|
+
requireOperationContextDetails: boolean;
|
|
118
|
+
requireOperationSchemaHash: boolean;
|
|
119
|
+
requireOperationVersion: boolean;
|
|
120
|
+
allowUnknownOperations: boolean;
|
|
121
|
+
expectedOperationContracts: Record<string, CrossProjectOperationContract>;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const splitRoutePath = (routePath: string): string[] =>
|
|
125
|
+
routePath.split('/').filter(Boolean);
|
|
126
|
+
|
|
127
|
+
const isDynamicRouteSegment = (segment: string): boolean =>
|
|
128
|
+
segment.startsWith(':') ||
|
|
129
|
+
segment === '*' ||
|
|
130
|
+
segment.startsWith('*') ||
|
|
131
|
+
segment.startsWith('{*');
|
|
132
|
+
|
|
133
|
+
const hasTrailingSlash = (routePath: string): boolean =>
|
|
134
|
+
routePath.length > 1 && routePath.endsWith('/');
|
|
135
|
+
|
|
136
|
+
const hasUnsafePathEncoding = (requestPath: string): boolean => {
|
|
137
|
+
let decoded = requestPath;
|
|
138
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
139
|
+
if (/%(?:2f|5c)/i.test(decoded)) {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
const next = decodeURIComponent(decoded);
|
|
144
|
+
if (next === decoded) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
decoded = next;
|
|
148
|
+
} catch {
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return decoded.includes('%');
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
const routeSegmentsMatch = (
|
|
156
|
+
templateSegments: string[],
|
|
157
|
+
requestSegments: string[],
|
|
158
|
+
): boolean => {
|
|
159
|
+
let templateIndex = 0;
|
|
160
|
+
let requestIndex = 0;
|
|
161
|
+
while (templateIndex < templateSegments.length) {
|
|
162
|
+
const segment = templateSegments[templateIndex]!;
|
|
163
|
+
const wildcard =
|
|
164
|
+
segment === '*' || segment.startsWith('*') || segment.startsWith('{*');
|
|
165
|
+
const optional = segment.startsWith(':') && segment.endsWith('?');
|
|
166
|
+
if (templateIndex < templateSegments.length - 1 && (wildcard || optional)) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
if (wildcard) {
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
const requestSegment = requestSegments[requestIndex];
|
|
173
|
+
if (requestSegment === undefined) {
|
|
174
|
+
return optional && templateIndex === templateSegments.length - 1;
|
|
175
|
+
}
|
|
176
|
+
if (!segment.startsWith(':') && segment !== requestSegment) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
templateIndex += 1;
|
|
180
|
+
requestIndex += 1;
|
|
181
|
+
}
|
|
182
|
+
return requestIndex === requestSegments.length;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const routeTemplateMatchScore = (
|
|
186
|
+
routePath: string,
|
|
187
|
+
requestPath: string,
|
|
188
|
+
): number | undefined => {
|
|
189
|
+
if (hasTrailingSlash(routePath) !== hasTrailingSlash(requestPath)) {
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
192
|
+
const templateSegments = splitRoutePath(routePath);
|
|
193
|
+
const requestSegments = splitRoutePath(requestPath);
|
|
194
|
+
for (
|
|
195
|
+
let prefixLength = 0;
|
|
196
|
+
prefixLength <= templateSegments.length;
|
|
197
|
+
prefixLength += 1
|
|
198
|
+
) {
|
|
199
|
+
const removedPrefix = templateSegments.slice(0, prefixLength);
|
|
200
|
+
if (removedPrefix.some(isDynamicRouteSegment)) {
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
const mountedTemplate = templateSegments.slice(prefixLength);
|
|
204
|
+
if (!routeSegmentsMatch(mountedTemplate, requestSegments)) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
const literalSegments = mountedTemplate.filter(
|
|
208
|
+
segment => !isDynamicRouteSegment(segment),
|
|
209
|
+
).length;
|
|
210
|
+
const dynamicSegments = mountedTemplate.length - literalSegments;
|
|
211
|
+
return (
|
|
212
|
+
(prefixLength === 0 ? 1_000_000 : 0) +
|
|
213
|
+
literalSegments * 10_000 -
|
|
214
|
+
dynamicSegments * 100 -
|
|
215
|
+
prefixLength
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
return undefined;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Resolves a concrete adapter request to the server-owned route template in
|
|
223
|
+
* the operation contract map. Effect receives requests after its BFF mount
|
|
224
|
+
* prefix has been stripped, so leading literal route segments may be mount
|
|
225
|
+
* prefixes. Ambiguous matches fail closed by retaining the concrete path.
|
|
226
|
+
*/
|
|
227
|
+
export const resolveCrossProjectRequestObservation = (
|
|
228
|
+
request: CrossProjectRequestTarget,
|
|
229
|
+
policy: Pick<CrossProjectPolicyConfig, 'expectedOperationContracts'>,
|
|
230
|
+
): CrossProjectRequestObservation | undefined => {
|
|
231
|
+
const requestMethod = String(request.method || '')
|
|
232
|
+
.trim()
|
|
233
|
+
.toUpperCase();
|
|
234
|
+
const routePaths = [
|
|
235
|
+
...new Set(
|
|
236
|
+
Object.keys(policy.expectedOperationContracts ?? {}).flatMap(key => {
|
|
237
|
+
if (key.startsWith('operation:')) {
|
|
238
|
+
return [];
|
|
239
|
+
}
|
|
240
|
+
const separator = key.indexOf(':');
|
|
241
|
+
if (
|
|
242
|
+
separator === -1 ||
|
|
243
|
+
key.slice(0, separator).trim().toUpperCase() !== requestMethod
|
|
244
|
+
) {
|
|
245
|
+
return [];
|
|
246
|
+
}
|
|
247
|
+
return [key.slice(separator + 1)];
|
|
248
|
+
}),
|
|
249
|
+
),
|
|
250
|
+
];
|
|
251
|
+
if (routePaths.length === 0) {
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (hasUnsafePathEncoding(request.pathname)) {
|
|
256
|
+
return {
|
|
257
|
+
method: request.method,
|
|
258
|
+
routePath: request.pathname,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const matches = routePaths.flatMap(routePath => {
|
|
263
|
+
const score = routeTemplateMatchScore(routePath, request.pathname);
|
|
264
|
+
return score === undefined ? [] : [{ routePath, score }];
|
|
265
|
+
});
|
|
266
|
+
if (matches.length === 0) {
|
|
267
|
+
return {
|
|
268
|
+
method: request.method,
|
|
269
|
+
routePath: request.pathname,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
matches.sort((left, right) => right.score - left.score);
|
|
273
|
+
return {
|
|
274
|
+
method: request.method,
|
|
275
|
+
routePath:
|
|
276
|
+
matches[1]?.score === matches[0]?.score
|
|
277
|
+
? request.pathname
|
|
278
|
+
: matches[0]!.routePath,
|
|
279
|
+
};
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
const normalizeHeaderName = (
|
|
283
|
+
headerName: string | undefined,
|
|
284
|
+
fallback: string,
|
|
285
|
+
) => (headerName || fallback).trim().toLowerCase();
|
|
286
|
+
|
|
287
|
+
const normalizeStatusCode = (statusCode: number | undefined) => {
|
|
288
|
+
if (
|
|
289
|
+
typeof statusCode === 'number' &&
|
|
290
|
+
Number.isFinite(statusCode) &&
|
|
291
|
+
statusCode >= 400 &&
|
|
292
|
+
statusCode <= 599
|
|
293
|
+
) {
|
|
294
|
+
return Math.floor(statusCode);
|
|
295
|
+
}
|
|
296
|
+
return DEFAULT_DENY_STATUS;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const readHeader = (
|
|
300
|
+
headers: Record<string, unknown>,
|
|
301
|
+
headerName: string,
|
|
302
|
+
): string | undefined => {
|
|
303
|
+
const lowerHeaderName = headerName.toLowerCase();
|
|
304
|
+
for (const [name, value] of Object.entries(headers || {})) {
|
|
305
|
+
if (name.toLowerCase() !== lowerHeaderName) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (Array.isArray(value)) {
|
|
309
|
+
return value.length > 0 ? String(value[0]) : undefined;
|
|
310
|
+
}
|
|
311
|
+
if (typeof value === 'undefined' || value === null) {
|
|
312
|
+
return undefined;
|
|
313
|
+
}
|
|
314
|
+
return String(value);
|
|
315
|
+
}
|
|
316
|
+
return undefined;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
const extractNamespace = (requestId: string) =>
|
|
320
|
+
requestId.split(/[/:.]/)[0]?.trim().toLowerCase();
|
|
321
|
+
|
|
322
|
+
const createViolation = (
|
|
323
|
+
reason: CrossProjectPolicyViolation['reason'],
|
|
324
|
+
message: string,
|
|
325
|
+
status: number,
|
|
326
|
+
): CrossProjectPolicyViolation => ({
|
|
327
|
+
code: 'BFF_CROSS_PROJECT_POLICY_DENIED',
|
|
328
|
+
reason,
|
|
329
|
+
message,
|
|
330
|
+
status,
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
type ProducerIdentityVerifier = NonNullable<
|
|
334
|
+
CrossProjectPolicyConfig['verifyProducerIdentity']
|
|
335
|
+
>;
|
|
336
|
+
|
|
337
|
+
type CrossProjectPolicyEvaluationState = {
|
|
338
|
+
headers: Record<string, unknown>;
|
|
339
|
+
policy: CrossProjectPolicyConfig;
|
|
340
|
+
status: number;
|
|
341
|
+
requireEnvelope: boolean;
|
|
342
|
+
requireOperationContext: boolean;
|
|
343
|
+
requireOperationContextDetails: boolean;
|
|
344
|
+
requireOperationSchemaHash: boolean;
|
|
345
|
+
requireOperationVersion: boolean;
|
|
346
|
+
allowUnknownOperations: boolean;
|
|
347
|
+
verifyProducerIdentity?: ProducerIdentityVerifier;
|
|
348
|
+
allowedNamespaces: string[];
|
|
349
|
+
envelopeHeader: string;
|
|
350
|
+
operationContextHeader: string;
|
|
351
|
+
operationContextDetailHeader: string;
|
|
352
|
+
operationContext?: string;
|
|
353
|
+
requestId: string;
|
|
354
|
+
claimedNamespace?: string;
|
|
355
|
+
effectiveNamespace?: string;
|
|
356
|
+
operationContextDetails?: Record<string, unknown>;
|
|
357
|
+
detailSchemaHash: string;
|
|
358
|
+
detailOperationVersion: unknown;
|
|
359
|
+
observedRequest?: CrossProjectRequestObservation;
|
|
360
|
+
complete: boolean;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
type CrossProjectPolicyCheck = (
|
|
364
|
+
state: CrossProjectPolicyEvaluationState,
|
|
365
|
+
) => CrossProjectPolicyViolation | undefined;
|
|
366
|
+
|
|
367
|
+
const createCrossProjectPolicyEvaluationState = (
|
|
368
|
+
headers: Record<string, unknown>,
|
|
369
|
+
policy: CrossProjectPolicyConfig,
|
|
370
|
+
observedRequest?: CrossProjectRequestObservation,
|
|
371
|
+
): CrossProjectPolicyEvaluationState => ({
|
|
372
|
+
headers,
|
|
373
|
+
policy,
|
|
374
|
+
status: normalizeStatusCode(policy.denyStatus),
|
|
375
|
+
requireEnvelope: policy.requireEnvelope ?? true,
|
|
376
|
+
requireOperationContext: policy.requireOperationContext ?? true,
|
|
377
|
+
requireOperationContextDetails: policy.requireOperationContextDetails ?? true,
|
|
378
|
+
requireOperationSchemaHash: policy.requireOperationSchemaHash ?? true,
|
|
379
|
+
requireOperationVersion: policy.requireOperationVersion ?? true,
|
|
380
|
+
allowUnknownOperations: policy.allowUnknownOperations ?? false,
|
|
381
|
+
verifyProducerIdentity:
|
|
382
|
+
typeof policy.verifyProducerIdentity === 'function'
|
|
383
|
+
? policy.verifyProducerIdentity
|
|
384
|
+
: undefined,
|
|
385
|
+
allowedNamespaces: (policy.allowedNamespaces || [])
|
|
386
|
+
.map(item => item.trim().toLowerCase())
|
|
387
|
+
.filter(Boolean),
|
|
388
|
+
envelopeHeader: normalizeHeaderName(
|
|
389
|
+
policy.envelopeHeader,
|
|
390
|
+
BFF_ENVELOPE_HEADER,
|
|
391
|
+
),
|
|
392
|
+
operationContextHeader: normalizeHeaderName(
|
|
393
|
+
policy.operationContextHeader,
|
|
394
|
+
BFF_OPERATION_CONTEXT_HEADER,
|
|
395
|
+
),
|
|
396
|
+
operationContextDetailHeader: normalizeHeaderName(
|
|
397
|
+
policy.operationContextDetailHeader,
|
|
398
|
+
BFF_OPERATION_CONTEXT_DETAIL_HEADER,
|
|
399
|
+
),
|
|
400
|
+
requestId: '',
|
|
401
|
+
detailSchemaHash: '',
|
|
402
|
+
detailOperationVersion: undefined,
|
|
403
|
+
observedRequest: observedRequest
|
|
404
|
+
? {
|
|
405
|
+
method: String(observedRequest.method || '')
|
|
406
|
+
.trim()
|
|
407
|
+
.toUpperCase(),
|
|
408
|
+
routePath: String(observedRequest.routePath || '').trim(),
|
|
409
|
+
}
|
|
410
|
+
: undefined,
|
|
411
|
+
complete: false,
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
const checkNamespaceAllowlistVerifier: CrossProjectPolicyCheck = state => {
|
|
415
|
+
if (
|
|
416
|
+
state.allowedNamespaces.length > 0 &&
|
|
417
|
+
state.verifyProducerIdentity === undefined
|
|
418
|
+
) {
|
|
419
|
+
return createViolation(
|
|
420
|
+
'producer_identity_mismatch',
|
|
421
|
+
NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE,
|
|
422
|
+
state.status,
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
return undefined;
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
const checkEnvelopeHeader: CrossProjectPolicyCheck = state => {
|
|
429
|
+
const envelopeRaw = readHeader(state.headers, state.envelopeHeader);
|
|
430
|
+
if (!envelopeRaw) {
|
|
431
|
+
if (!state.requireEnvelope) {
|
|
432
|
+
state.complete = true;
|
|
433
|
+
return undefined;
|
|
434
|
+
}
|
|
435
|
+
return createViolation(
|
|
436
|
+
'missing_envelope',
|
|
437
|
+
`Missing cross-project envelope header "${state.envelopeHeader}"`,
|
|
438
|
+
state.status,
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
let envelope: Record<string, unknown>;
|
|
443
|
+
try {
|
|
444
|
+
const parsed = JSON.parse(envelopeRaw);
|
|
445
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
446
|
+
throw new Error('invalid envelope object');
|
|
447
|
+
}
|
|
448
|
+
envelope = parsed as Record<string, unknown>;
|
|
449
|
+
} catch (_error) {
|
|
450
|
+
return createViolation(
|
|
451
|
+
'invalid_envelope',
|
|
452
|
+
`Invalid cross-project envelope header "${state.envelopeHeader}"`,
|
|
453
|
+
state.status,
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const requestId = String(envelope.requestId || '').trim();
|
|
458
|
+
if (!requestId) {
|
|
459
|
+
return createViolation(
|
|
460
|
+
'missing_request_id',
|
|
461
|
+
'Cross-project envelope does not include a valid requestId',
|
|
462
|
+
state.status,
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
state.requestId = requestId;
|
|
467
|
+
state.claimedNamespace = extractNamespace(requestId);
|
|
468
|
+
state.effectiveNamespace = state.claimedNamespace;
|
|
469
|
+
return undefined;
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
const checkProducerIdentity: CrossProjectPolicyCheck = state => {
|
|
473
|
+
if (!state.verifyProducerIdentity) {
|
|
474
|
+
return undefined;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const verifiedNamespaceRaw = state.verifyProducerIdentity(state.headers);
|
|
478
|
+
const verifiedNamespace =
|
|
479
|
+
typeof verifiedNamespaceRaw === 'string'
|
|
480
|
+
? verifiedNamespaceRaw.trim().toLowerCase()
|
|
481
|
+
: undefined;
|
|
482
|
+
if (!verifiedNamespace) {
|
|
483
|
+
return createViolation(
|
|
484
|
+
'producer_identity_mismatch',
|
|
485
|
+
'Producer identity could not be verified for request',
|
|
486
|
+
state.status,
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
if (verifiedNamespace !== state.claimedNamespace) {
|
|
490
|
+
return createViolation(
|
|
491
|
+
'producer_identity_mismatch',
|
|
492
|
+
`Envelope namespace "${state.claimedNamespace || 'unknown'}" does not match verified producer identity "${verifiedNamespace}"`,
|
|
493
|
+
state.status,
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// From here on, authorization decisions use verified namespace, not
|
|
498
|
+
// the client-asserted envelope value.
|
|
499
|
+
state.effectiveNamespace = verifiedNamespace;
|
|
500
|
+
return undefined;
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
const checkNamespaceAllowlist: CrossProjectPolicyCheck = state => {
|
|
504
|
+
if (state.allowedNamespaces.length > 0) {
|
|
505
|
+
if (
|
|
506
|
+
!state.effectiveNamespace ||
|
|
507
|
+
!state.allowedNamespaces.includes(state.effectiveNamespace)
|
|
508
|
+
) {
|
|
509
|
+
return createViolation(
|
|
510
|
+
'namespace_not_allowed',
|
|
511
|
+
`Producer namespace "${state.effectiveNamespace || 'unknown'}" not allowed`,
|
|
512
|
+
state.status,
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return undefined;
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const checkOperationContext: CrossProjectPolicyCheck = state => {
|
|
520
|
+
if (!state.requireOperationContext) {
|
|
521
|
+
state.complete = true;
|
|
522
|
+
return undefined;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const operationContext = readHeader(
|
|
526
|
+
state.headers,
|
|
527
|
+
state.operationContextHeader,
|
|
528
|
+
);
|
|
529
|
+
if (!operationContext) {
|
|
530
|
+
return createViolation(
|
|
531
|
+
'missing_operation_context',
|
|
532
|
+
`Missing operation context header "${state.operationContextHeader}"`,
|
|
533
|
+
state.status,
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
if (!operationContext.startsWith(`${state.requestId}:`)) {
|
|
537
|
+
return createViolation(
|
|
538
|
+
'operation_context_mismatch',
|
|
539
|
+
`Operation context header "${state.operationContextHeader}" does not match requestId "${state.requestId}"`,
|
|
540
|
+
state.status,
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
state.operationContext = operationContext;
|
|
544
|
+
return undefined;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
const checkOperationContextDetails: CrossProjectPolicyCheck = state => {
|
|
548
|
+
const operationContextDetailsRaw = readHeader(
|
|
549
|
+
state.headers,
|
|
550
|
+
state.operationContextDetailHeader,
|
|
551
|
+
);
|
|
552
|
+
if (!operationContextDetailsRaw) {
|
|
553
|
+
if (state.requireOperationContextDetails) {
|
|
554
|
+
return createViolation(
|
|
555
|
+
'missing_operation_context_details',
|
|
556
|
+
`Missing operation context details header "${state.operationContextDetailHeader}"`,
|
|
557
|
+
state.status,
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
state.complete = true;
|
|
561
|
+
return undefined;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
let operationContextDetails: Record<string, unknown>;
|
|
565
|
+
try {
|
|
566
|
+
const parsed = JSON.parse(operationContextDetailsRaw);
|
|
567
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
568
|
+
throw new Error('invalid operation context details object');
|
|
569
|
+
}
|
|
570
|
+
operationContextDetails = parsed as Record<string, unknown>;
|
|
571
|
+
} catch (_error) {
|
|
572
|
+
return createViolation(
|
|
573
|
+
'invalid_operation_context_details',
|
|
574
|
+
`Invalid operation context details header "${state.operationContextDetailHeader}"`,
|
|
575
|
+
state.status,
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const detailRequestId = String(
|
|
580
|
+
operationContextDetails.requestId || '',
|
|
581
|
+
).trim();
|
|
582
|
+
if (detailRequestId && detailRequestId !== state.requestId) {
|
|
583
|
+
return createViolation(
|
|
584
|
+
'operation_context_details_request_id_mismatch',
|
|
585
|
+
`Operation context details requestId "${detailRequestId}" does not match envelope requestId "${state.requestId}"`,
|
|
586
|
+
state.status,
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
const detailOperationId = String(
|
|
590
|
+
operationContextDetails.operationId || '',
|
|
591
|
+
).trim();
|
|
592
|
+
if (
|
|
593
|
+
state.operationContext &&
|
|
594
|
+
detailOperationId &&
|
|
595
|
+
detailOperationId !== state.operationContext
|
|
596
|
+
) {
|
|
597
|
+
return createViolation(
|
|
598
|
+
'operation_context_mismatch',
|
|
599
|
+
`Operation context details operationId "${detailOperationId}" does not match operation context "${state.operationContext}"`,
|
|
600
|
+
state.status,
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
state.operationContextDetails = operationContextDetails;
|
|
605
|
+
state.detailSchemaHash = String(
|
|
606
|
+
operationContextDetails.schemaHash || '',
|
|
607
|
+
).trim();
|
|
608
|
+
if (state.requireOperationSchemaHash && !state.detailSchemaHash) {
|
|
609
|
+
return createViolation(
|
|
610
|
+
'missing_operation_schema_hash',
|
|
611
|
+
`Operation context details header "${state.operationContextDetailHeader}" must include schemaHash`,
|
|
612
|
+
state.status,
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
state.detailOperationVersion = operationContextDetails.operationVersion;
|
|
617
|
+
if (
|
|
618
|
+
state.requireOperationVersion &&
|
|
619
|
+
typeof state.detailOperationVersion !== 'number'
|
|
620
|
+
) {
|
|
621
|
+
return createViolation(
|
|
622
|
+
'missing_operation_version',
|
|
623
|
+
`Operation context details header "${state.operationContextDetailHeader}" must include operationVersion`,
|
|
624
|
+
state.status,
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
return undefined;
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
const checkOperationContract: CrossProjectPolicyCheck = state => {
|
|
632
|
+
if (!state.operationContextDetails) {
|
|
633
|
+
return undefined;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
const expectedContracts = state.policy.expectedOperationContracts;
|
|
637
|
+
if (expectedContracts && typeof expectedContracts === 'object') {
|
|
638
|
+
const method = String(
|
|
639
|
+
state.operationContextDetails.method || '',
|
|
640
|
+
).toUpperCase();
|
|
641
|
+
const routePath = String(
|
|
642
|
+
state.operationContextDetails.routePath || '',
|
|
643
|
+
).trim();
|
|
644
|
+
const operationId = String(
|
|
645
|
+
state.operationContextDetails.operationId || '',
|
|
646
|
+
).trim();
|
|
647
|
+
const observedMethod = state.observedRequest?.method;
|
|
648
|
+
const observedRoutePath = state.observedRequest?.routePath;
|
|
649
|
+
if (
|
|
650
|
+
observedMethod &&
|
|
651
|
+
observedRoutePath &&
|
|
652
|
+
(method !== observedMethod || routePath !== observedRoutePath)
|
|
653
|
+
) {
|
|
654
|
+
return createViolation(
|
|
655
|
+
'operation_context_mismatch',
|
|
656
|
+
`Client operation "${operationId || `${method}:${routePath}`}" does not match observed request "${observedMethod}:${observedRoutePath}"`,
|
|
657
|
+
state.status,
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const contractMethod = observedMethod || method;
|
|
662
|
+
const contractRoutePath = observedRoutePath || routePath;
|
|
663
|
+
const expectedContract =
|
|
664
|
+
expectedContracts[`${contractMethod}:${contractRoutePath}`] ||
|
|
665
|
+
(!state.observedRequest
|
|
666
|
+
? expectedContracts[`operation:${operationId}`]
|
|
667
|
+
: undefined);
|
|
668
|
+
|
|
669
|
+
if (!expectedContract) {
|
|
670
|
+
if (!state.allowUnknownOperations) {
|
|
671
|
+
return createViolation(
|
|
672
|
+
'unknown_operation_contract',
|
|
673
|
+
`No expected operation contract found for operation "${operationId || `${method}:${routePath}`}"`,
|
|
674
|
+
state.status,
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
} else {
|
|
678
|
+
const assertedOperationId = operationId || state.operationContext || '';
|
|
679
|
+
const canonicalRouteOperationId = `${state.requestId}:${contractMethod}:${contractRoutePath}`;
|
|
680
|
+
if (
|
|
681
|
+
state.observedRequest &&
|
|
682
|
+
assertedOperationId !== expectedContract.operationId &&
|
|
683
|
+
assertedOperationId !== canonicalRouteOperationId
|
|
684
|
+
) {
|
|
685
|
+
return createViolation(
|
|
686
|
+
'operation_context_mismatch',
|
|
687
|
+
`Client operation "${assertedOperationId}" does not identify observed contract "${expectedContract.operationId || canonicalRouteOperationId}"`,
|
|
688
|
+
state.status,
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
if (
|
|
692
|
+
expectedContract.schemaHash &&
|
|
693
|
+
state.detailSchemaHash &&
|
|
694
|
+
expectedContract.schemaHash !== state.detailSchemaHash
|
|
695
|
+
) {
|
|
696
|
+
return createViolation(
|
|
697
|
+
'operation_schema_hash_mismatch',
|
|
698
|
+
`Operation schema hash mismatch for "${operationId || `${method}:${routePath}`}"`,
|
|
699
|
+
state.status,
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
if (
|
|
703
|
+
typeof expectedContract.operationVersion === 'number' &&
|
|
704
|
+
typeof state.detailOperationVersion === 'number' &&
|
|
705
|
+
expectedContract.operationVersion !== state.detailOperationVersion
|
|
706
|
+
) {
|
|
707
|
+
return createViolation(
|
|
708
|
+
'operation_version_mismatch',
|
|
709
|
+
`Operation version mismatch for "${operationId || `${method}:${routePath}`}"`,
|
|
710
|
+
state.status,
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
return undefined;
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
const CROSS_PROJECT_POLICY_CHECKS: CrossProjectPolicyCheck[] = [
|
|
720
|
+
checkNamespaceAllowlistVerifier,
|
|
721
|
+
checkEnvelopeHeader,
|
|
722
|
+
checkProducerIdentity,
|
|
723
|
+
checkNamespaceAllowlist,
|
|
724
|
+
checkOperationContext,
|
|
725
|
+
checkOperationContextDetails,
|
|
726
|
+
checkOperationContract,
|
|
727
|
+
];
|
|
728
|
+
|
|
729
|
+
export const evaluateCrossProjectPolicy = (
|
|
730
|
+
headers: Record<string, unknown>,
|
|
731
|
+
policy?: CrossProjectPolicyConfig,
|
|
732
|
+
observedRequest?: CrossProjectRequestObservation,
|
|
733
|
+
): CrossProjectPolicyViolation | null => {
|
|
734
|
+
if (!policy?.enabled) {
|
|
735
|
+
return null;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
const state = createCrossProjectPolicyEvaluationState(
|
|
739
|
+
headers,
|
|
740
|
+
policy,
|
|
741
|
+
observedRequest,
|
|
742
|
+
);
|
|
743
|
+
for (const check of CROSS_PROJECT_POLICY_CHECKS) {
|
|
744
|
+
const violation = check(state);
|
|
745
|
+
if (violation) {
|
|
746
|
+
return violation;
|
|
747
|
+
}
|
|
748
|
+
if (state.complete) {
|
|
749
|
+
return null;
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
return null;
|
|
754
|
+
};
|