@bleedingdev/modern-js-bff-core 3.5.0-ultramodern.4 → 3.5.0-ultramodern.41
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/dist/cjs/adapter-kit/index.js +6 -2
- package/dist/cjs/adapter-kit/parity-scenarios/cross-project-denial.js +55 -0
- package/dist/cjs/adapter-kit/parity-scenarios/envelope.js +128 -0
- package/dist/cjs/adapter-kit/parity-scenarios/operation-context.js +158 -0
- package/dist/cjs/adapter-kit/parity-scenarios/schema.js +109 -0
- package/dist/cjs/adapter-kit/parity-scenarios/shared.js +71 -0
- package/dist/cjs/adapter-kit/parity.js +26 -368
- package/dist/cjs/index.js +29 -48
- package/dist/cjs/operators/http.js +2 -2
- package/dist/cjs/security/crossProjectPolicy.js +123 -62
- package/dist/esm/adapter-kit/index.mjs +6 -2
- package/dist/esm/adapter-kit/parity-scenarios/cross-project-denial.mjs +17 -0
- package/dist/esm/adapter-kit/parity-scenarios/envelope.mjs +90 -0
- package/dist/esm/adapter-kit/parity-scenarios/operation-context.mjs +117 -0
- package/dist/esm/adapter-kit/parity-scenarios/schema.mjs +71 -0
- package/dist/esm/adapter-kit/parity-scenarios/shared.mjs +21 -0
- package/dist/esm/adapter-kit/parity.mjs +19 -361
- package/dist/esm/index.mjs +0 -1
- package/dist/esm/operators/http.mjs +2 -2
- package/dist/esm/security/crossProjectPolicy.mjs +123 -62
- package/dist/esm/security/operationContracts.mjs +2 -2
- package/dist/esm-node/adapter-kit/index.mjs +6 -2
- package/dist/esm-node/adapter-kit/parity-scenarios/cross-project-denial.mjs +18 -0
- package/dist/esm-node/adapter-kit/parity-scenarios/envelope.mjs +91 -0
- package/dist/esm-node/adapter-kit/parity-scenarios/operation-context.mjs +118 -0
- package/dist/esm-node/adapter-kit/parity-scenarios/schema.mjs +72 -0
- package/dist/esm-node/adapter-kit/parity-scenarios/shared.mjs +22 -0
- package/dist/esm-node/adapter-kit/parity.mjs +19 -361
- package/dist/esm-node/index.mjs +0 -1
- package/dist/esm-node/operators/http.mjs +2 -2
- package/dist/esm-node/security/crossProjectPolicy.mjs +123 -62
- package/dist/types/adapter-kit/index.d.ts +2 -2
- package/dist/types/adapter-kit/parity-scenarios/cross-project-denial.d.ts +2 -0
- package/dist/types/adapter-kit/parity-scenarios/envelope.d.ts +2 -0
- package/dist/types/adapter-kit/parity-scenarios/operation-context.d.ts +3 -0
- package/dist/types/adapter-kit/parity-scenarios/schema.d.ts +2 -0
- package/dist/types/adapter-kit/parity-scenarios/shared.d.ts +32 -0
- package/dist/types/adapter-kit/parity.d.ts +12 -71
- package/dist/types/index.d.ts +0 -1
- package/dist/types/operators/http.d.ts +1 -1
- package/dist/types/security/crossProjectPolicy.d.ts +17 -8
- package/package.json +9 -9
- package/dist/cjs/contracts/eventContracts.js +0 -72
- package/dist/esm/contracts/eventContracts.mjs +0 -28
- package/dist/esm-node/contracts/eventContracts.mjs +0 -29
- package/dist/types/contracts/eventContracts.d.ts +0 -22
|
@@ -31,6 +31,8 @@ const BFF_ENVELOPE_HEADER = 'x-modernjs-bff-envelope';
|
|
|
31
31
|
const BFF_OPERATION_CONTEXT_HEADER = 'x-operation-id';
|
|
32
32
|
const BFF_OPERATION_CONTEXT_DETAIL_HEADER = 'x-modernjs-bff-operation-context';
|
|
33
33
|
const DEFAULT_DENY_STATUS = 403;
|
|
34
|
+
const NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE = 'cross-project namespace allowlist requires verifyProducerIdentity';
|
|
35
|
+
let hasWarnedAdvisoryNamespaceAllowlist = false;
|
|
34
36
|
const normalizeHeaderName = (headerName, fallback)=>(headerName || fallback).trim().toLowerCase();
|
|
35
37
|
const normalizeStatusCode = (statusCode)=>{
|
|
36
38
|
if ('number' == typeof statusCode && Number.isFinite(statusCode) && statusCode >= 400 && statusCode <= 599) return Math.floor(statusCode);
|
|
@@ -51,22 +53,46 @@ const createViolation = (reason, message, status)=>({
|
|
|
51
53
|
message,
|
|
52
54
|
status
|
|
53
55
|
});
|
|
54
|
-
const
|
|
55
|
-
if (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
const warnAdvisoryNamespaceAllowlist = ()=>{
|
|
57
|
+
if (hasWarnedAdvisoryNamespaceAllowlist) return;
|
|
58
|
+
hasWarnedAdvisoryNamespaceAllowlist = true;
|
|
59
|
+
console.warn(`[Modern.js BFF] ${NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE}. allowClientAssertedNamespace=true will continue to evaluate allowlist against client-asserted requestId namespaces only.`);
|
|
60
|
+
};
|
|
61
|
+
const createCrossProjectPolicyEvaluationState = (headers, policy)=>({
|
|
62
|
+
headers,
|
|
63
|
+
policy,
|
|
64
|
+
status: normalizeStatusCode(policy.denyStatus),
|
|
65
|
+
requireEnvelope: policy.requireEnvelope ?? true,
|
|
66
|
+
requireOperationContext: policy.requireOperationContext ?? true,
|
|
67
|
+
requireOperationContextDetails: policy.requireOperationContextDetails ?? true,
|
|
68
|
+
requireOperationSchemaHash: policy.requireOperationSchemaHash ?? true,
|
|
69
|
+
requireOperationVersion: policy.requireOperationVersion ?? true,
|
|
70
|
+
allowUnknownOperations: policy.allowUnknownOperations ?? false,
|
|
71
|
+
allowClientAssertedNamespace: policy.allowClientAssertedNamespace ?? false,
|
|
72
|
+
verifyProducerIdentity: 'function' == typeof policy.verifyProducerIdentity ? policy.verifyProducerIdentity : void 0,
|
|
73
|
+
allowedNamespaces: (policy.allowedNamespaces || []).map((item)=>item.trim().toLowerCase()).filter(Boolean),
|
|
74
|
+
envelopeHeader: normalizeHeaderName(policy.envelopeHeader, BFF_ENVELOPE_HEADER),
|
|
75
|
+
operationContextHeader: normalizeHeaderName(policy.operationContextHeader, BFF_OPERATION_CONTEXT_HEADER),
|
|
76
|
+
operationContextDetailHeader: normalizeHeaderName(policy.operationContextDetailHeader, BFF_OPERATION_CONTEXT_DETAIL_HEADER),
|
|
77
|
+
requestId: '',
|
|
78
|
+
detailSchemaHash: '',
|
|
79
|
+
detailOperationVersion: void 0,
|
|
80
|
+
complete: false
|
|
81
|
+
});
|
|
82
|
+
const checkNamespaceAllowlistVerifier = (state)=>{
|
|
83
|
+
if (state.allowedNamespaces.length > 0 && void 0 === state.verifyProducerIdentity) {
|
|
84
|
+
if (!state.allowClientAssertedNamespace) return createViolation('producer_identity_mismatch', NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE, state.status);
|
|
85
|
+
warnAdvisoryNamespaceAllowlist();
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const checkEnvelopeHeader = (state)=>{
|
|
89
|
+
const envelopeRaw = readHeader(state.headers, state.envelopeHeader);
|
|
67
90
|
if (!envelopeRaw) {
|
|
68
|
-
if (!requireEnvelope)
|
|
69
|
-
|
|
91
|
+
if (!state.requireEnvelope) {
|
|
92
|
+
state.complete = true;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
return createViolation('missing_envelope', `Missing cross-project envelope header "${state.envelopeHeader}"`, state.status);
|
|
70
96
|
}
|
|
71
97
|
let envelope;
|
|
72
98
|
try {
|
|
@@ -74,57 +100,92 @@ const evaluateCrossProjectPolicy = (headers, policy)=>{
|
|
|
74
100
|
if (!parsed || 'object' != typeof parsed) throw new Error('invalid envelope object');
|
|
75
101
|
envelope = parsed;
|
|
76
102
|
} catch (_error) {
|
|
77
|
-
return createViolation('invalid_envelope', `Invalid cross-project envelope header "${envelopeHeader}"`, status);
|
|
103
|
+
return createViolation('invalid_envelope', `Invalid cross-project envelope header "${state.envelopeHeader}"`, state.status);
|
|
78
104
|
}
|
|
79
105
|
const requestId = String(envelope.requestId || '').trim();
|
|
80
|
-
if (!requestId) return createViolation('missing_request_id', 'Cross-project envelope does not include a valid requestId', status);
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
106
|
+
if (!requestId) return createViolation('missing_request_id', 'Cross-project envelope does not include a valid requestId', state.status);
|
|
107
|
+
state.requestId = requestId;
|
|
108
|
+
state.claimedNamespace = extractNamespace(requestId);
|
|
109
|
+
state.effectiveNamespace = state.claimedNamespace;
|
|
110
|
+
};
|
|
111
|
+
const checkProducerIdentity = (state)=>{
|
|
112
|
+
if (!state.verifyProducerIdentity) return;
|
|
113
|
+
const verifiedNamespaceRaw = state.verifyProducerIdentity(state.headers);
|
|
114
|
+
const verifiedNamespace = 'string' == typeof verifiedNamespaceRaw ? verifiedNamespaceRaw.trim().toLowerCase() : void 0;
|
|
115
|
+
if (!verifiedNamespace) return createViolation('producer_identity_mismatch', 'Producer identity could not be verified for request', state.status);
|
|
116
|
+
if (verifiedNamespace !== state.claimedNamespace) return createViolation('producer_identity_mismatch', `Envelope namespace "${state.claimedNamespace || 'unknown'}" does not match verified producer identity "${verifiedNamespace}"`, state.status);
|
|
117
|
+
state.effectiveNamespace = verifiedNamespace;
|
|
118
|
+
};
|
|
119
|
+
const checkNamespaceAllowlist = (state)=>{
|
|
120
|
+
if (state.allowedNamespaces.length > 0) {
|
|
121
|
+
if (!state.effectiveNamespace || !state.allowedNamespaces.includes(state.effectiveNamespace)) return createViolation('namespace_not_allowed', `Producer namespace "${state.effectiveNamespace || 'unknown'}" not allowed`, state.status);
|
|
89
122
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
123
|
+
};
|
|
124
|
+
const checkOperationContext = (state)=>{
|
|
125
|
+
if (!state.requireOperationContext) {
|
|
126
|
+
state.complete = true;
|
|
127
|
+
return;
|
|
93
128
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
129
|
+
const operationContext = readHeader(state.headers, state.operationContextHeader);
|
|
130
|
+
if (!operationContext) return createViolation('missing_operation_context', `Missing operation context header "${state.operationContextHeader}"`, state.status);
|
|
131
|
+
if (!operationContext.startsWith(`${state.requestId}:`)) return createViolation('operation_context_mismatch', `Operation context header "${state.operationContextHeader}" does not match requestId "${state.requestId}"`, state.status);
|
|
132
|
+
state.operationContext = operationContext;
|
|
133
|
+
};
|
|
134
|
+
const checkOperationContextDetails = (state)=>{
|
|
135
|
+
const operationContextDetailsRaw = readHeader(state.headers, state.operationContextDetailHeader);
|
|
136
|
+
if (!operationContextDetailsRaw) {
|
|
137
|
+
if (state.requireOperationContextDetails) return createViolation('missing_operation_context_details', `Missing operation context details header "${state.operationContextDetailHeader}"`, state.status);
|
|
138
|
+
state.complete = true;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
let operationContextDetails;
|
|
142
|
+
try {
|
|
143
|
+
const parsed = JSON.parse(operationContextDetailsRaw);
|
|
144
|
+
if (!parsed || 'object' != typeof parsed || Array.isArray(parsed)) throw new Error('invalid operation context details object');
|
|
145
|
+
operationContextDetails = parsed;
|
|
146
|
+
} catch (_error) {
|
|
147
|
+
return createViolation('invalid_operation_context_details', `Invalid operation context details header "${state.operationContextDetailHeader}"`, state.status);
|
|
148
|
+
}
|
|
149
|
+
const detailRequestId = String(operationContextDetails.requestId || '').trim();
|
|
150
|
+
if (detailRequestId && detailRequestId !== state.requestId) return createViolation('operation_context_details_request_id_mismatch', `Operation context details requestId "${detailRequestId}" does not match envelope requestId "${state.requestId}"`, state.status);
|
|
151
|
+
const detailOperationId = String(operationContextDetails.operationId || '').trim();
|
|
152
|
+
if (state.operationContext && detailOperationId && detailOperationId !== state.operationContext) return createViolation('operation_context_mismatch', `Operation context details operationId "${detailOperationId}" does not match operation context "${state.operationContext}"`, state.status);
|
|
153
|
+
state.operationContextDetails = operationContextDetails;
|
|
154
|
+
state.detailSchemaHash = String(operationContextDetails.schemaHash || '').trim();
|
|
155
|
+
if (state.requireOperationSchemaHash && !state.detailSchemaHash) return createViolation('missing_operation_schema_hash', `Operation context details header "${state.operationContextDetailHeader}" must include schemaHash`, state.status);
|
|
156
|
+
state.detailOperationVersion = operationContextDetails.operationVersion;
|
|
157
|
+
if (state.requireOperationVersion && 'number' != typeof state.detailOperationVersion) return createViolation('missing_operation_version', `Operation context details header "${state.operationContextDetailHeader}" must include operationVersion`, state.status);
|
|
158
|
+
};
|
|
159
|
+
const checkOperationContract = (state)=>{
|
|
160
|
+
if (!state.operationContextDetails) return;
|
|
161
|
+
const expectedContracts = state.policy.expectedOperationContracts;
|
|
162
|
+
if (expectedContracts && 'object' == typeof expectedContracts) {
|
|
163
|
+
const method = String(state.operationContextDetails.method || '').toUpperCase();
|
|
164
|
+
const routePath = String(state.operationContextDetails.routePath || '').trim();
|
|
165
|
+
const operationId = String(state.operationContextDetails.operationId || '').trim();
|
|
166
|
+
const expectedContract = expectedContracts[`${method}:${routePath}`] || expectedContracts[`operation:${operationId}`];
|
|
167
|
+
if (expectedContract) {
|
|
168
|
+
if (expectedContract.schemaHash && state.detailSchemaHash && expectedContract.schemaHash !== state.detailSchemaHash) return createViolation('operation_schema_hash_mismatch', `Operation schema hash mismatch for "${operationId || `${method}:${routePath}`}"`, state.status);
|
|
169
|
+
if ('number' == typeof expectedContract.operationVersion && 'number' == typeof state.detailOperationVersion && expectedContract.operationVersion !== state.detailOperationVersion) return createViolation('operation_version_mismatch', `Operation version mismatch for "${operationId || `${method}:${routePath}`}"`, state.status);
|
|
170
|
+
} else if (!state.allowUnknownOperations) return createViolation('unknown_operation_contract', `No expected operation contract found for operation "${operationId || `${method}:${routePath}`}"`, state.status);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const CROSS_PROJECT_POLICY_CHECKS = [
|
|
174
|
+
checkNamespaceAllowlistVerifier,
|
|
175
|
+
checkEnvelopeHeader,
|
|
176
|
+
checkProducerIdentity,
|
|
177
|
+
checkNamespaceAllowlist,
|
|
178
|
+
checkOperationContext,
|
|
179
|
+
checkOperationContextDetails,
|
|
180
|
+
checkOperationContract
|
|
181
|
+
];
|
|
182
|
+
const evaluateCrossProjectPolicy = (headers, policy)=>{
|
|
183
|
+
if (!policy?.enabled) return null;
|
|
184
|
+
const state = createCrossProjectPolicyEvaluationState(headers, policy);
|
|
185
|
+
for (const check of CROSS_PROJECT_POLICY_CHECKS){
|
|
186
|
+
const violation = check(state);
|
|
187
|
+
if (violation) return violation;
|
|
188
|
+
if (state.complete) break;
|
|
128
189
|
}
|
|
129
190
|
return null;
|
|
130
191
|
};
|
|
@@ -56,10 +56,14 @@ const getResponseMetaList = (handler)=>{
|
|
|
56
56
|
const responseMeta = Reflect.getMetadata(HttpMetadata.Response, handler);
|
|
57
57
|
return Array.isArray(responseMeta) ? responseMeta : [];
|
|
58
58
|
};
|
|
59
|
-
const buildPositionalHandlerArgs = (input)=>
|
|
60
|
-
|
|
59
|
+
const buildPositionalHandlerArgs = (input, routePath)=>{
|
|
60
|
+
const paramNames = routePath?.match(/:(\w+)/g)?.map((param)=>param.slice(1)) ?? [];
|
|
61
|
+
const positionalParams = paramNames.length > 0 ? paramNames.map((param)=>input.params[param]) : Object.values(input.params);
|
|
62
|
+
return [
|
|
63
|
+
...positionalParams,
|
|
61
64
|
input
|
|
62
65
|
];
|
|
66
|
+
};
|
|
63
67
|
const checkCrossProjectPolicy = (headers, policy)=>{
|
|
64
68
|
const violation = evaluateCrossProjectPolicy(headers, policy);
|
|
65
69
|
if (!violation) return null;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { deniedScenario, envelopeHeader } from "./shared.mjs";
|
|
2
|
+
const createCrossProjectDenialScenarios = ()=>[
|
|
3
|
+
deniedScenario('policy denies missing envelope', 'missing_envelope', {}),
|
|
4
|
+
deniedScenario('policy denies invalid envelope', 'invalid_envelope', {
|
|
5
|
+
'x-modernjs-bff-envelope': 'not-json'
|
|
6
|
+
}),
|
|
7
|
+
deniedScenario('policy denies envelope that is valid JSON but not an object', 'invalid_envelope', {
|
|
8
|
+
'x-modernjs-bff-envelope': '123'
|
|
9
|
+
}),
|
|
10
|
+
deniedScenario('policy denies missing requestId', 'missing_request_id', {
|
|
11
|
+
'x-modernjs-bff-envelope': envelopeHeader(void 0)
|
|
12
|
+
}),
|
|
13
|
+
deniedScenario('policy denies namespace outside allowlist', 'namespace_not_allowed', {
|
|
14
|
+
'x-modernjs-bff-envelope': envelopeHeader('billing.producer-z')
|
|
15
|
+
})
|
|
16
|
+
];
|
|
17
|
+
export { createCrossProjectDenialScenarios };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const createEnvelopeParityScenarios = ()=>[
|
|
2
|
+
{
|
|
3
|
+
name: 'plain handler returns object payload',
|
|
4
|
+
policy: false,
|
|
5
|
+
request: {
|
|
6
|
+
method: 'get',
|
|
7
|
+
path: '/hello'
|
|
8
|
+
},
|
|
9
|
+
expected: {
|
|
10
|
+
kind: 'payload',
|
|
11
|
+
status: 200,
|
|
12
|
+
payload: {
|
|
13
|
+
message: 'hello'
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
name: 'plain handler returns scalar payload',
|
|
19
|
+
policy: false,
|
|
20
|
+
request: {
|
|
21
|
+
method: 'post',
|
|
22
|
+
path: '/hello'
|
|
23
|
+
},
|
|
24
|
+
expected: {
|
|
25
|
+
kind: 'payload',
|
|
26
|
+
status: 200,
|
|
27
|
+
payload: 'hello'
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: 'plain handler returning undefined',
|
|
32
|
+
policy: false,
|
|
33
|
+
request: {
|
|
34
|
+
method: 'get',
|
|
35
|
+
path: '/nothing'
|
|
36
|
+
},
|
|
37
|
+
expected: {
|
|
38
|
+
kind: 'payload',
|
|
39
|
+
status: 404,
|
|
40
|
+
payload: '404 Not Found'
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: 'plain handler receives data, query and cookies',
|
|
45
|
+
policy: false,
|
|
46
|
+
request: {
|
|
47
|
+
method: 'post',
|
|
48
|
+
path: '/echo?q=z',
|
|
49
|
+
headers: {
|
|
50
|
+
'content-type': 'application/json',
|
|
51
|
+
cookie: 'id=666'
|
|
52
|
+
},
|
|
53
|
+
body: {
|
|
54
|
+
a: 1
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
expected: {
|
|
58
|
+
kind: 'payload',
|
|
59
|
+
status: 200,
|
|
60
|
+
payload: {
|
|
61
|
+
data: {
|
|
62
|
+
a: 1
|
|
63
|
+
},
|
|
64
|
+
query: {
|
|
65
|
+
q: 'z'
|
|
66
|
+
},
|
|
67
|
+
cookie: 'id=666'
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: 'plain handler receives positional route params',
|
|
73
|
+
policy: false,
|
|
74
|
+
request: {
|
|
75
|
+
method: 'get',
|
|
76
|
+
path: '/items/123?q=x'
|
|
77
|
+
},
|
|
78
|
+
expected: {
|
|
79
|
+
kind: 'payload',
|
|
80
|
+
status: 200,
|
|
81
|
+
payload: {
|
|
82
|
+
id: '123',
|
|
83
|
+
query: {
|
|
84
|
+
q: 'x'
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
];
|
|
90
|
+
export { createEnvelopeParityScenarios };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { PARITY_PRODUCER_REQUEST_ID, deniedScenario, detailHeader } from "./shared.mjs";
|
|
2
|
+
const createOperationContextSuccessScenarios = ({ helloContract, validEnvelope, validOperationId })=>[
|
|
3
|
+
{
|
|
4
|
+
name: 'policy pass with full operation context',
|
|
5
|
+
policy: true,
|
|
6
|
+
request: {
|
|
7
|
+
method: 'get',
|
|
8
|
+
path: '/hello',
|
|
9
|
+
headers: {
|
|
10
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
11
|
+
'x-operation-id': validOperationId,
|
|
12
|
+
'x-modernjs-bff-operation-context': detailHeader({
|
|
13
|
+
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
14
|
+
method: 'GET',
|
|
15
|
+
routePath: '/hello',
|
|
16
|
+
schemaHash: helloContract.schemaHash,
|
|
17
|
+
operationVersion: helloContract.operationVersion
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
expected: {
|
|
22
|
+
kind: 'payload',
|
|
23
|
+
status: 200,
|
|
24
|
+
payload: {
|
|
25
|
+
message: 'hello'
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
];
|
|
30
|
+
const createOperationContextDenialScenarios = ({ helloContract, validEnvelope, validOperationId })=>[
|
|
31
|
+
deniedScenario('policy denies missing operation context', 'missing_operation_context', {
|
|
32
|
+
'x-modernjs-bff-envelope': validEnvelope
|
|
33
|
+
}),
|
|
34
|
+
deniedScenario('policy denies operation context mismatch', 'operation_context_mismatch', {
|
|
35
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
36
|
+
'x-operation-id': 'someone-else:parity'
|
|
37
|
+
}),
|
|
38
|
+
deniedScenario('policy denies missing operation context details', 'missing_operation_context_details', {
|
|
39
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
40
|
+
'x-operation-id': validOperationId
|
|
41
|
+
}),
|
|
42
|
+
deniedScenario('policy denies JSON-array operation context details', 'invalid_operation_context_details', {
|
|
43
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
44
|
+
'x-operation-id': validOperationId,
|
|
45
|
+
'x-modernjs-bff-operation-context': '[]'
|
|
46
|
+
}),
|
|
47
|
+
deniedScenario('policy denies invalid operation context details', 'invalid_operation_context_details', {
|
|
48
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
49
|
+
'x-operation-id': validOperationId,
|
|
50
|
+
'x-modernjs-bff-operation-context': 'not-json'
|
|
51
|
+
}),
|
|
52
|
+
deniedScenario('policy denies detail requestId mismatch', 'operation_context_details_request_id_mismatch', {
|
|
53
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
54
|
+
'x-operation-id': validOperationId,
|
|
55
|
+
'x-modernjs-bff-operation-context': detailHeader({
|
|
56
|
+
requestId: 'crm.producer-b',
|
|
57
|
+
method: 'GET',
|
|
58
|
+
routePath: '/hello',
|
|
59
|
+
schemaHash: helloContract.schemaHash,
|
|
60
|
+
operationVersion: helloContract.operationVersion
|
|
61
|
+
})
|
|
62
|
+
}),
|
|
63
|
+
deniedScenario('policy denies missing operation schema hash', 'missing_operation_schema_hash', {
|
|
64
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
65
|
+
'x-operation-id': validOperationId,
|
|
66
|
+
'x-modernjs-bff-operation-context': detailHeader({
|
|
67
|
+
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
68
|
+
method: 'GET',
|
|
69
|
+
routePath: '/hello',
|
|
70
|
+
operationVersion: helloContract.operationVersion
|
|
71
|
+
})
|
|
72
|
+
}),
|
|
73
|
+
deniedScenario('policy denies missing operation version', 'missing_operation_version', {
|
|
74
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
75
|
+
'x-operation-id': validOperationId,
|
|
76
|
+
'x-modernjs-bff-operation-context': detailHeader({
|
|
77
|
+
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
78
|
+
method: 'GET',
|
|
79
|
+
routePath: '/hello',
|
|
80
|
+
schemaHash: helloContract.schemaHash
|
|
81
|
+
})
|
|
82
|
+
}),
|
|
83
|
+
deniedScenario('policy denies unknown operation contract', 'unknown_operation_contract', {
|
|
84
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
85
|
+
'x-operation-id': validOperationId,
|
|
86
|
+
'x-modernjs-bff-operation-context': detailHeader({
|
|
87
|
+
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
88
|
+
method: 'GET',
|
|
89
|
+
routePath: '/does-not-exist',
|
|
90
|
+
schemaHash: helloContract.schemaHash,
|
|
91
|
+
operationVersion: helloContract.operationVersion
|
|
92
|
+
})
|
|
93
|
+
}),
|
|
94
|
+
deniedScenario('policy denies operation schema hash mismatch', 'operation_schema_hash_mismatch', {
|
|
95
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
96
|
+
'x-operation-id': validOperationId,
|
|
97
|
+
'x-modernjs-bff-operation-context': detailHeader({
|
|
98
|
+
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
99
|
+
method: 'GET',
|
|
100
|
+
routePath: '/hello',
|
|
101
|
+
schemaHash: 'deadbeef',
|
|
102
|
+
operationVersion: helloContract.operationVersion
|
|
103
|
+
})
|
|
104
|
+
}),
|
|
105
|
+
deniedScenario('policy denies operation version mismatch', 'operation_version_mismatch', {
|
|
106
|
+
'x-modernjs-bff-envelope': validEnvelope,
|
|
107
|
+
'x-operation-id': validOperationId,
|
|
108
|
+
'x-modernjs-bff-operation-context': detailHeader({
|
|
109
|
+
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
110
|
+
method: 'GET',
|
|
111
|
+
routePath: '/hello',
|
|
112
|
+
schemaHash: helloContract.schemaHash,
|
|
113
|
+
operationVersion: helloContract.operationVersion + 1
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
];
|
|
117
|
+
export { createOperationContextDenialScenarios, createOperationContextSuccessScenarios };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const createSchemaParityScenarios = ()=>[
|
|
2
|
+
{
|
|
3
|
+
name: 'schema handler success',
|
|
4
|
+
policy: false,
|
|
5
|
+
request: {
|
|
6
|
+
method: 'patch',
|
|
7
|
+
path: '/schema',
|
|
8
|
+
headers: {
|
|
9
|
+
'content-type': 'application/json'
|
|
10
|
+
},
|
|
11
|
+
body: {
|
|
12
|
+
id: 777
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
expected: {
|
|
16
|
+
kind: 'payload',
|
|
17
|
+
status: 200,
|
|
18
|
+
payload: {
|
|
19
|
+
type: 'HandleSuccess',
|
|
20
|
+
value: {
|
|
21
|
+
id: 777
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: 'schema handler input validation error',
|
|
28
|
+
policy: false,
|
|
29
|
+
request: {
|
|
30
|
+
method: 'patch',
|
|
31
|
+
path: '/schema',
|
|
32
|
+
headers: {
|
|
33
|
+
'content-type': 'application/json'
|
|
34
|
+
},
|
|
35
|
+
body: {
|
|
36
|
+
id: 'aaa'
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
expected: {
|
|
40
|
+
kind: 'payload',
|
|
41
|
+
status: 200,
|
|
42
|
+
payload: {
|
|
43
|
+
type: 'InputValidationError',
|
|
44
|
+
message: 'invalid input'
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: 'schema handler output validation error',
|
|
50
|
+
policy: false,
|
|
51
|
+
request: {
|
|
52
|
+
method: 'patch',
|
|
53
|
+
path: '/schema',
|
|
54
|
+
headers: {
|
|
55
|
+
'content-type': 'application/json'
|
|
56
|
+
},
|
|
57
|
+
body: {
|
|
58
|
+
id: 'boom'
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
expected: {
|
|
62
|
+
kind: 'payload',
|
|
63
|
+
status: 200,
|
|
64
|
+
payload: {
|
|
65
|
+
type: 'OutputValidationError',
|
|
66
|
+
message: 'invalid output'
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
];
|
|
71
|
+
export { createSchemaParityScenarios };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const PARITY_REQUEST_ID = 'crm';
|
|
2
|
+
const PARITY_PRODUCER_REQUEST_ID = 'crm.producer-a';
|
|
3
|
+
const envelopeHeader = (requestId)=>JSON.stringify(void 0 === requestId ? {} : {
|
|
4
|
+
requestId
|
|
5
|
+
});
|
|
6
|
+
const detailHeader = (details)=>JSON.stringify(details);
|
|
7
|
+
const deniedScenario = (name, reason, headers)=>({
|
|
8
|
+
name,
|
|
9
|
+
policy: true,
|
|
10
|
+
request: {
|
|
11
|
+
method: 'get',
|
|
12
|
+
path: '/hello',
|
|
13
|
+
headers
|
|
14
|
+
},
|
|
15
|
+
expected: {
|
|
16
|
+
kind: 'denied',
|
|
17
|
+
status: 403,
|
|
18
|
+
reason
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
export { PARITY_PRODUCER_REQUEST_ID, PARITY_REQUEST_ID, deniedScenario, detailHeader, envelopeHeader };
|