@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
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { buildOperationContractMap } from "../security/operationContracts.mjs";
|
|
2
2
|
import { HttpMethod } from "../types.mjs";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import { createCrossProjectDenialScenarios } from "./parity-scenarios/cross-project-denial.mjs";
|
|
4
|
+
import { createEnvelopeParityScenarios } from "./parity-scenarios/envelope.mjs";
|
|
5
|
+
import { createOperationContextDenialScenarios, createOperationContextSuccessScenarios } from "./parity-scenarios/operation-context.mjs";
|
|
6
|
+
import { createSchemaParityScenarios } from "./parity-scenarios/schema.mjs";
|
|
7
|
+
import { PARITY_PRODUCER_REQUEST_ID, PARITY_REQUEST_ID, envelopeHeader } from "./parity-scenarios/shared.mjs";
|
|
5
8
|
const PARITY_FIXTURE_FILENAME = 'bff-core/adapter-kit/parity-fixture.ts';
|
|
6
9
|
const HANDLER_WITH_SCHEMA = 'HANDLER_WITH_SCHEMA';
|
|
7
10
|
const isRecord = (value)=>Boolean(value) && 'object' == typeof value;
|
|
@@ -96,368 +99,30 @@ const createParityBffConfig = ()=>({
|
|
|
96
99
|
enabled: true,
|
|
97
100
|
allowedNamespaces: [
|
|
98
101
|
PARITY_REQUEST_ID
|
|
99
|
-
]
|
|
102
|
+
],
|
|
103
|
+
allowClientAssertedNamespace: true
|
|
100
104
|
}
|
|
101
105
|
});
|
|
102
106
|
const getParityContracts = ()=>buildOperationContractMap({
|
|
103
107
|
handlers: createParityApiHandlerInfos(),
|
|
104
108
|
requestId: PARITY_REQUEST_ID
|
|
105
109
|
});
|
|
106
|
-
const envelopeHeader = (requestId)=>JSON.stringify(void 0 === requestId ? {} : {
|
|
107
|
-
requestId
|
|
108
|
-
});
|
|
109
|
-
const detailHeader = (details)=>JSON.stringify(details);
|
|
110
|
-
const deniedScenario = (name, reason, headers)=>({
|
|
111
|
-
name,
|
|
112
|
-
policy: true,
|
|
113
|
-
request: {
|
|
114
|
-
method: 'get',
|
|
115
|
-
path: '/hello',
|
|
116
|
-
headers
|
|
117
|
-
},
|
|
118
|
-
expected: {
|
|
119
|
-
kind: 'denied',
|
|
120
|
-
status: 403,
|
|
121
|
-
reason
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
110
|
const createAdapterParityScenarios = ()=>{
|
|
125
111
|
const contracts = getParityContracts();
|
|
126
112
|
const helloContract = contracts['GET:/hello'];
|
|
127
113
|
const validEnvelope = envelopeHeader(PARITY_PRODUCER_REQUEST_ID);
|
|
128
114
|
const validOperationId = `${PARITY_PRODUCER_REQUEST_ID}:parity`;
|
|
115
|
+
const context = {
|
|
116
|
+
helloContract,
|
|
117
|
+
validEnvelope,
|
|
118
|
+
validOperationId
|
|
119
|
+
};
|
|
129
120
|
return [
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
path: '/hello'
|
|
136
|
-
},
|
|
137
|
-
expected: {
|
|
138
|
-
kind: 'payload',
|
|
139
|
-
status: 200,
|
|
140
|
-
payload: {
|
|
141
|
-
message: 'hello'
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
name: 'plain handler returns scalar payload',
|
|
147
|
-
policy: false,
|
|
148
|
-
request: {
|
|
149
|
-
method: 'post',
|
|
150
|
-
path: '/hello'
|
|
151
|
-
},
|
|
152
|
-
expected: {
|
|
153
|
-
kind: 'payload',
|
|
154
|
-
status: 200,
|
|
155
|
-
payload: 'hello'
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
{
|
|
159
|
-
name: 'plain handler returning undefined (pinned adapter drift)',
|
|
160
|
-
policy: false,
|
|
161
|
-
request: {
|
|
162
|
-
method: 'get',
|
|
163
|
-
path: '/nothing'
|
|
164
|
-
},
|
|
165
|
-
expected: {
|
|
166
|
-
kind: 'perAdapter',
|
|
167
|
-
expectations: {
|
|
168
|
-
express: {
|
|
169
|
-
status: 200,
|
|
170
|
-
payload: void 0
|
|
171
|
-
},
|
|
172
|
-
koa: {
|
|
173
|
-
status: 404,
|
|
174
|
-
payload: 'Not Found'
|
|
175
|
-
},
|
|
176
|
-
hono: {
|
|
177
|
-
status: 404,
|
|
178
|
-
payload: '404 Not Found'
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
},
|
|
183
|
-
{
|
|
184
|
-
name: 'plain handler receives data, query and cookies',
|
|
185
|
-
policy: false,
|
|
186
|
-
request: {
|
|
187
|
-
method: 'post',
|
|
188
|
-
path: '/echo?q=z',
|
|
189
|
-
headers: {
|
|
190
|
-
'content-type': 'application/json',
|
|
191
|
-
cookie: 'id=666'
|
|
192
|
-
},
|
|
193
|
-
body: {
|
|
194
|
-
a: 1
|
|
195
|
-
}
|
|
196
|
-
},
|
|
197
|
-
expected: {
|
|
198
|
-
kind: 'payload',
|
|
199
|
-
status: 200,
|
|
200
|
-
payload: {
|
|
201
|
-
data: {
|
|
202
|
-
a: 1
|
|
203
|
-
},
|
|
204
|
-
query: {
|
|
205
|
-
q: 'z'
|
|
206
|
-
},
|
|
207
|
-
cookie: 'id=666'
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
},
|
|
211
|
-
{
|
|
212
|
-
name: 'plain handler receives positional route params',
|
|
213
|
-
policy: false,
|
|
214
|
-
request: {
|
|
215
|
-
method: 'get',
|
|
216
|
-
path: '/items/123?q=x'
|
|
217
|
-
},
|
|
218
|
-
expected: {
|
|
219
|
-
kind: 'payload',
|
|
220
|
-
status: 200,
|
|
221
|
-
payload: {
|
|
222
|
-
id: '123',
|
|
223
|
-
query: {
|
|
224
|
-
q: 'x'
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
},
|
|
229
|
-
{
|
|
230
|
-
name: 'schema handler success (pinned adapter drift)',
|
|
231
|
-
policy: false,
|
|
232
|
-
request: {
|
|
233
|
-
method: 'patch',
|
|
234
|
-
path: '/schema',
|
|
235
|
-
headers: {
|
|
236
|
-
'content-type': 'application/json'
|
|
237
|
-
},
|
|
238
|
-
body: {
|
|
239
|
-
id: 777
|
|
240
|
-
}
|
|
241
|
-
},
|
|
242
|
-
expected: {
|
|
243
|
-
kind: 'perAdapter',
|
|
244
|
-
expectations: {
|
|
245
|
-
express: {
|
|
246
|
-
status: 200,
|
|
247
|
-
payload: {
|
|
248
|
-
id: 777
|
|
249
|
-
}
|
|
250
|
-
},
|
|
251
|
-
koa: {
|
|
252
|
-
status: 200,
|
|
253
|
-
payload: {
|
|
254
|
-
id: 777
|
|
255
|
-
}
|
|
256
|
-
},
|
|
257
|
-
hono: {
|
|
258
|
-
status: 200,
|
|
259
|
-
payload: {
|
|
260
|
-
type: 'HandleSuccess',
|
|
261
|
-
value: {
|
|
262
|
-
id: 777
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
},
|
|
269
|
-
{
|
|
270
|
-
name: 'schema handler input validation error (pinned adapter drift)',
|
|
271
|
-
policy: false,
|
|
272
|
-
request: {
|
|
273
|
-
method: 'patch',
|
|
274
|
-
path: '/schema',
|
|
275
|
-
headers: {
|
|
276
|
-
'content-type': 'application/json'
|
|
277
|
-
},
|
|
278
|
-
body: {
|
|
279
|
-
id: 'aaa'
|
|
280
|
-
}
|
|
281
|
-
},
|
|
282
|
-
expected: {
|
|
283
|
-
kind: 'perAdapter',
|
|
284
|
-
expectations: {
|
|
285
|
-
express: {
|
|
286
|
-
status: 400,
|
|
287
|
-
payload: 'invalid input'
|
|
288
|
-
},
|
|
289
|
-
koa: {
|
|
290
|
-
status: 400,
|
|
291
|
-
payload: 'invalid input'
|
|
292
|
-
},
|
|
293
|
-
hono: {
|
|
294
|
-
status: 200,
|
|
295
|
-
payload: {
|
|
296
|
-
type: 'InputValidationError',
|
|
297
|
-
message: 'invalid input'
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
},
|
|
303
|
-
{
|
|
304
|
-
name: 'schema handler output validation error (pinned adapter drift)',
|
|
305
|
-
policy: false,
|
|
306
|
-
request: {
|
|
307
|
-
method: 'patch',
|
|
308
|
-
path: '/schema',
|
|
309
|
-
headers: {
|
|
310
|
-
'content-type': 'application/json'
|
|
311
|
-
},
|
|
312
|
-
body: {
|
|
313
|
-
id: 'boom'
|
|
314
|
-
}
|
|
315
|
-
},
|
|
316
|
-
expected: {
|
|
317
|
-
kind: 'perAdapter',
|
|
318
|
-
expectations: {
|
|
319
|
-
express: {
|
|
320
|
-
status: 500,
|
|
321
|
-
payload: 'invalid output'
|
|
322
|
-
},
|
|
323
|
-
koa: {
|
|
324
|
-
status: 500,
|
|
325
|
-
payload: 'invalid output'
|
|
326
|
-
},
|
|
327
|
-
hono: {
|
|
328
|
-
status: 200,
|
|
329
|
-
payload: {
|
|
330
|
-
type: 'OutputValidationError',
|
|
331
|
-
message: 'invalid output'
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
},
|
|
337
|
-
{
|
|
338
|
-
name: 'policy pass with full operation context',
|
|
339
|
-
policy: true,
|
|
340
|
-
request: {
|
|
341
|
-
method: 'get',
|
|
342
|
-
path: '/hello',
|
|
343
|
-
headers: {
|
|
344
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
345
|
-
'x-operation-id': validOperationId,
|
|
346
|
-
'x-modernjs-bff-operation-context': detailHeader({
|
|
347
|
-
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
348
|
-
method: 'GET',
|
|
349
|
-
routePath: '/hello',
|
|
350
|
-
schemaHash: helloContract.schemaHash,
|
|
351
|
-
operationVersion: helloContract.operationVersion
|
|
352
|
-
})
|
|
353
|
-
}
|
|
354
|
-
},
|
|
355
|
-
expected: {
|
|
356
|
-
kind: 'payload',
|
|
357
|
-
status: 200,
|
|
358
|
-
payload: {
|
|
359
|
-
message: 'hello'
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
},
|
|
363
|
-
deniedScenario('policy denies missing envelope', 'missing_envelope', {}),
|
|
364
|
-
deniedScenario('policy denies invalid envelope', 'invalid_envelope', {
|
|
365
|
-
'x-modernjs-bff-envelope': 'not-json'
|
|
366
|
-
}),
|
|
367
|
-
deniedScenario('policy denies envelope that is valid JSON but not an object', 'invalid_envelope', {
|
|
368
|
-
'x-modernjs-bff-envelope': '123'
|
|
369
|
-
}),
|
|
370
|
-
deniedScenario('policy denies missing requestId', 'missing_request_id', {
|
|
371
|
-
'x-modernjs-bff-envelope': envelopeHeader(void 0)
|
|
372
|
-
}),
|
|
373
|
-
deniedScenario('policy denies namespace outside allowlist', 'namespace_not_allowed', {
|
|
374
|
-
'x-modernjs-bff-envelope': envelopeHeader('billing.producer-z')
|
|
375
|
-
}),
|
|
376
|
-
deniedScenario('policy denies missing operation context', 'missing_operation_context', {
|
|
377
|
-
'x-modernjs-bff-envelope': validEnvelope
|
|
378
|
-
}),
|
|
379
|
-
deniedScenario('policy denies operation context mismatch', 'operation_context_mismatch', {
|
|
380
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
381
|
-
'x-operation-id': 'someone-else:parity'
|
|
382
|
-
}),
|
|
383
|
-
deniedScenario('policy denies missing operation context details', 'missing_operation_context_details', {
|
|
384
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
385
|
-
'x-operation-id': validOperationId
|
|
386
|
-
}),
|
|
387
|
-
deniedScenario('policy denies JSON-array operation context details', 'invalid_operation_context_details', {
|
|
388
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
389
|
-
'x-operation-id': validOperationId,
|
|
390
|
-
'x-modernjs-bff-operation-context': '[]'
|
|
391
|
-
}),
|
|
392
|
-
deniedScenario('policy denies invalid operation context details', 'invalid_operation_context_details', {
|
|
393
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
394
|
-
'x-operation-id': validOperationId,
|
|
395
|
-
'x-modernjs-bff-operation-context': 'not-json'
|
|
396
|
-
}),
|
|
397
|
-
deniedScenario('policy denies detail requestId mismatch', 'operation_context_details_request_id_mismatch', {
|
|
398
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
399
|
-
'x-operation-id': validOperationId,
|
|
400
|
-
'x-modernjs-bff-operation-context': detailHeader({
|
|
401
|
-
requestId: 'crm.producer-b',
|
|
402
|
-
method: 'GET',
|
|
403
|
-
routePath: '/hello',
|
|
404
|
-
schemaHash: helloContract.schemaHash,
|
|
405
|
-
operationVersion: helloContract.operationVersion
|
|
406
|
-
})
|
|
407
|
-
}),
|
|
408
|
-
deniedScenario('policy denies missing operation schema hash', 'missing_operation_schema_hash', {
|
|
409
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
410
|
-
'x-operation-id': validOperationId,
|
|
411
|
-
'x-modernjs-bff-operation-context': detailHeader({
|
|
412
|
-
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
413
|
-
method: 'GET',
|
|
414
|
-
routePath: '/hello',
|
|
415
|
-
operationVersion: helloContract.operationVersion
|
|
416
|
-
})
|
|
417
|
-
}),
|
|
418
|
-
deniedScenario('policy denies missing operation version', 'missing_operation_version', {
|
|
419
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
420
|
-
'x-operation-id': validOperationId,
|
|
421
|
-
'x-modernjs-bff-operation-context': detailHeader({
|
|
422
|
-
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
423
|
-
method: 'GET',
|
|
424
|
-
routePath: '/hello',
|
|
425
|
-
schemaHash: helloContract.schemaHash
|
|
426
|
-
})
|
|
427
|
-
}),
|
|
428
|
-
deniedScenario('policy denies unknown operation contract', 'unknown_operation_contract', {
|
|
429
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
430
|
-
'x-operation-id': validOperationId,
|
|
431
|
-
'x-modernjs-bff-operation-context': detailHeader({
|
|
432
|
-
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
433
|
-
method: 'GET',
|
|
434
|
-
routePath: '/does-not-exist',
|
|
435
|
-
schemaHash: helloContract.schemaHash,
|
|
436
|
-
operationVersion: helloContract.operationVersion
|
|
437
|
-
})
|
|
438
|
-
}),
|
|
439
|
-
deniedScenario('policy denies operation schema hash mismatch', 'operation_schema_hash_mismatch', {
|
|
440
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
441
|
-
'x-operation-id': validOperationId,
|
|
442
|
-
'x-modernjs-bff-operation-context': detailHeader({
|
|
443
|
-
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
444
|
-
method: 'GET',
|
|
445
|
-
routePath: '/hello',
|
|
446
|
-
schemaHash: 'deadbeef',
|
|
447
|
-
operationVersion: helloContract.operationVersion
|
|
448
|
-
})
|
|
449
|
-
}),
|
|
450
|
-
deniedScenario('policy denies operation version mismatch', 'operation_version_mismatch', {
|
|
451
|
-
'x-modernjs-bff-envelope': validEnvelope,
|
|
452
|
-
'x-operation-id': validOperationId,
|
|
453
|
-
'x-modernjs-bff-operation-context': detailHeader({
|
|
454
|
-
requestId: PARITY_PRODUCER_REQUEST_ID,
|
|
455
|
-
method: 'GET',
|
|
456
|
-
routePath: '/hello',
|
|
457
|
-
schemaHash: helloContract.schemaHash,
|
|
458
|
-
operationVersion: helloContract.operationVersion + 1
|
|
459
|
-
})
|
|
460
|
-
})
|
|
121
|
+
...createEnvelopeParityScenarios(),
|
|
122
|
+
...createSchemaParityScenarios(),
|
|
123
|
+
...createOperationContextSuccessScenarios(context),
|
|
124
|
+
...createCrossProjectDenialScenarios(),
|
|
125
|
+
...createOperationContextDenialScenarios(context)
|
|
461
126
|
];
|
|
462
127
|
};
|
|
463
128
|
const toParityResult = (res)=>({
|
|
@@ -465,17 +130,10 @@ const toParityResult = (res)=>({
|
|
|
465
130
|
payload: res.type.includes('json') ? res.body : '' === res.text ? void 0 : res.text
|
|
466
131
|
});
|
|
467
132
|
const formatValue = (value)=>JSON.stringify(value);
|
|
468
|
-
const assertParityResult = (scenario, res
|
|
133
|
+
const assertParityResult = (scenario, res)=>{
|
|
469
134
|
const result = toParityResult(res);
|
|
470
135
|
const failures = [];
|
|
471
|
-
|
|
472
|
-
if ('perAdapter' === expected.kind) {
|
|
473
|
-
if (void 0 === adapter) throw new Error(`Adapter parity scenario "${scenario.name}" pins per-adapter drift; pass the adapter id to assertParityResult.`);
|
|
474
|
-
expected = {
|
|
475
|
-
kind: 'payload',
|
|
476
|
-
...expected.expectations[adapter]
|
|
477
|
-
};
|
|
478
|
-
}
|
|
136
|
+
const { expected } = scenario;
|
|
479
137
|
if (result.status !== expected.status) failures.push(`status: expected ${expected.status}, received ${result.status}`);
|
|
480
138
|
if ('payload' === expected.kind) {
|
|
481
139
|
if (formatValue(result.payload) !== formatValue(expected.payload)) failures.push(`payload: expected ${formatValue(expected.payload)}, received ${formatValue(result.payload)}`);
|
package/dist/esm/index.mjs
CHANGED
|
@@ -124,10 +124,10 @@ const Upload = (urlPath, schema)=>({
|
|
|
124
124
|
},
|
|
125
125
|
async validate (helper, next) {
|
|
126
126
|
if (!schema) return next();
|
|
127
|
-
const { inputs: { formData
|
|
127
|
+
const { inputs: { formData } } = helper;
|
|
128
128
|
helper.inputs = {
|
|
129
129
|
...helper.inputs,
|
|
130
|
-
|
|
130
|
+
formData: await validateInput(schema, formData)
|
|
131
131
|
};
|
|
132
132
|
return next();
|
|
133
133
|
}
|
|
@@ -2,6 +2,8 @@ const BFF_ENVELOPE_HEADER = 'x-modernjs-bff-envelope';
|
|
|
2
2
|
const BFF_OPERATION_CONTEXT_HEADER = 'x-operation-id';
|
|
3
3
|
const BFF_OPERATION_CONTEXT_DETAIL_HEADER = 'x-modernjs-bff-operation-context';
|
|
4
4
|
const DEFAULT_DENY_STATUS = 403;
|
|
5
|
+
const NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE = 'cross-project namespace allowlist requires verifyProducerIdentity';
|
|
6
|
+
let hasWarnedAdvisoryNamespaceAllowlist = false;
|
|
5
7
|
const normalizeHeaderName = (headerName, fallback)=>(headerName || fallback).trim().toLowerCase();
|
|
6
8
|
const normalizeStatusCode = (statusCode)=>{
|
|
7
9
|
if ('number' == typeof statusCode && Number.isFinite(statusCode) && statusCode >= 400 && statusCode <= 599) return Math.floor(statusCode);
|
|
@@ -22,22 +24,46 @@ const createViolation = (reason, message, status)=>({
|
|
|
22
24
|
message,
|
|
23
25
|
status
|
|
24
26
|
});
|
|
25
|
-
const
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
const warnAdvisoryNamespaceAllowlist = ()=>{
|
|
28
|
+
if (hasWarnedAdvisoryNamespaceAllowlist) return;
|
|
29
|
+
hasWarnedAdvisoryNamespaceAllowlist = true;
|
|
30
|
+
console.warn(`[Modern.js BFF] ${NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE}. allowClientAssertedNamespace=true will continue to evaluate allowlist against client-asserted requestId namespaces only.`);
|
|
31
|
+
};
|
|
32
|
+
const createCrossProjectPolicyEvaluationState = (headers, policy)=>({
|
|
33
|
+
headers,
|
|
34
|
+
policy,
|
|
35
|
+
status: normalizeStatusCode(policy.denyStatus),
|
|
36
|
+
requireEnvelope: policy.requireEnvelope ?? true,
|
|
37
|
+
requireOperationContext: policy.requireOperationContext ?? true,
|
|
38
|
+
requireOperationContextDetails: policy.requireOperationContextDetails ?? true,
|
|
39
|
+
requireOperationSchemaHash: policy.requireOperationSchemaHash ?? true,
|
|
40
|
+
requireOperationVersion: policy.requireOperationVersion ?? true,
|
|
41
|
+
allowUnknownOperations: policy.allowUnknownOperations ?? false,
|
|
42
|
+
allowClientAssertedNamespace: policy.allowClientAssertedNamespace ?? false,
|
|
43
|
+
verifyProducerIdentity: 'function' == typeof policy.verifyProducerIdentity ? policy.verifyProducerIdentity : void 0,
|
|
44
|
+
allowedNamespaces: (policy.allowedNamespaces || []).map((item)=>item.trim().toLowerCase()).filter(Boolean),
|
|
45
|
+
envelopeHeader: normalizeHeaderName(policy.envelopeHeader, BFF_ENVELOPE_HEADER),
|
|
46
|
+
operationContextHeader: normalizeHeaderName(policy.operationContextHeader, BFF_OPERATION_CONTEXT_HEADER),
|
|
47
|
+
operationContextDetailHeader: normalizeHeaderName(policy.operationContextDetailHeader, BFF_OPERATION_CONTEXT_DETAIL_HEADER),
|
|
48
|
+
requestId: '',
|
|
49
|
+
detailSchemaHash: '',
|
|
50
|
+
detailOperationVersion: void 0,
|
|
51
|
+
complete: false
|
|
52
|
+
});
|
|
53
|
+
const checkNamespaceAllowlistVerifier = (state)=>{
|
|
54
|
+
if (state.allowedNamespaces.length > 0 && void 0 === state.verifyProducerIdentity) {
|
|
55
|
+
if (!state.allowClientAssertedNamespace) return createViolation('producer_identity_mismatch', NAMESPACE_ALLOWLIST_REQUIRES_VERIFIER_MESSAGE, state.status);
|
|
56
|
+
warnAdvisoryNamespaceAllowlist();
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const checkEnvelopeHeader = (state)=>{
|
|
60
|
+
const envelopeRaw = readHeader(state.headers, state.envelopeHeader);
|
|
38
61
|
if (!envelopeRaw) {
|
|
39
|
-
if (!requireEnvelope)
|
|
40
|
-
|
|
62
|
+
if (!state.requireEnvelope) {
|
|
63
|
+
state.complete = true;
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
return createViolation('missing_envelope', `Missing cross-project envelope header "${state.envelopeHeader}"`, state.status);
|
|
41
67
|
}
|
|
42
68
|
let envelope;
|
|
43
69
|
try {
|
|
@@ -45,57 +71,92 @@ const evaluateCrossProjectPolicy = (headers, policy)=>{
|
|
|
45
71
|
if (!parsed || 'object' != typeof parsed) throw new Error('invalid envelope object');
|
|
46
72
|
envelope = parsed;
|
|
47
73
|
} catch (_error) {
|
|
48
|
-
return createViolation('invalid_envelope', `Invalid cross-project envelope header "${envelopeHeader}"`, status);
|
|
74
|
+
return createViolation('invalid_envelope', `Invalid cross-project envelope header "${state.envelopeHeader}"`, state.status);
|
|
49
75
|
}
|
|
50
76
|
const requestId = String(envelope.requestId || '').trim();
|
|
51
|
-
if (!requestId) return createViolation('missing_request_id', 'Cross-project envelope does not include a valid requestId', status);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
if (!requestId) return createViolation('missing_request_id', 'Cross-project envelope does not include a valid requestId', state.status);
|
|
78
|
+
state.requestId = requestId;
|
|
79
|
+
state.claimedNamespace = extractNamespace(requestId);
|
|
80
|
+
state.effectiveNamespace = state.claimedNamespace;
|
|
81
|
+
};
|
|
82
|
+
const checkProducerIdentity = (state)=>{
|
|
83
|
+
if (!state.verifyProducerIdentity) return;
|
|
84
|
+
const verifiedNamespaceRaw = state.verifyProducerIdentity(state.headers);
|
|
85
|
+
const verifiedNamespace = 'string' == typeof verifiedNamespaceRaw ? verifiedNamespaceRaw.trim().toLowerCase() : void 0;
|
|
86
|
+
if (!verifiedNamespace) return createViolation('producer_identity_mismatch', 'Producer identity could not be verified for request', state.status);
|
|
87
|
+
if (verifiedNamespace !== state.claimedNamespace) return createViolation('producer_identity_mismatch', `Envelope namespace "${state.claimedNamespace || 'unknown'}" does not match verified producer identity "${verifiedNamespace}"`, state.status);
|
|
88
|
+
state.effectiveNamespace = verifiedNamespace;
|
|
89
|
+
};
|
|
90
|
+
const checkNamespaceAllowlist = (state)=>{
|
|
91
|
+
if (state.allowedNamespaces.length > 0) {
|
|
92
|
+
if (!state.effectiveNamespace || !state.allowedNamespaces.includes(state.effectiveNamespace)) return createViolation('namespace_not_allowed', `Producer namespace "${state.effectiveNamespace || 'unknown'}" not allowed`, state.status);
|
|
60
93
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
94
|
+
};
|
|
95
|
+
const checkOperationContext = (state)=>{
|
|
96
|
+
if (!state.requireOperationContext) {
|
|
97
|
+
state.complete = true;
|
|
98
|
+
return;
|
|
64
99
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
100
|
+
const operationContext = readHeader(state.headers, state.operationContextHeader);
|
|
101
|
+
if (!operationContext) return createViolation('missing_operation_context', `Missing operation context header "${state.operationContextHeader}"`, state.status);
|
|
102
|
+
if (!operationContext.startsWith(`${state.requestId}:`)) return createViolation('operation_context_mismatch', `Operation context header "${state.operationContextHeader}" does not match requestId "${state.requestId}"`, state.status);
|
|
103
|
+
state.operationContext = operationContext;
|
|
104
|
+
};
|
|
105
|
+
const checkOperationContextDetails = (state)=>{
|
|
106
|
+
const operationContextDetailsRaw = readHeader(state.headers, state.operationContextDetailHeader);
|
|
107
|
+
if (!operationContextDetailsRaw) {
|
|
108
|
+
if (state.requireOperationContextDetails) return createViolation('missing_operation_context_details', `Missing operation context details header "${state.operationContextDetailHeader}"`, state.status);
|
|
109
|
+
state.complete = true;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
let operationContextDetails;
|
|
113
|
+
try {
|
|
114
|
+
const parsed = JSON.parse(operationContextDetailsRaw);
|
|
115
|
+
if (!parsed || 'object' != typeof parsed || Array.isArray(parsed)) throw new Error('invalid operation context details object');
|
|
116
|
+
operationContextDetails = parsed;
|
|
117
|
+
} catch (_error) {
|
|
118
|
+
return createViolation('invalid_operation_context_details', `Invalid operation context details header "${state.operationContextDetailHeader}"`, state.status);
|
|
119
|
+
}
|
|
120
|
+
const detailRequestId = String(operationContextDetails.requestId || '').trim();
|
|
121
|
+
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);
|
|
122
|
+
const detailOperationId = String(operationContextDetails.operationId || '').trim();
|
|
123
|
+
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);
|
|
124
|
+
state.operationContextDetails = operationContextDetails;
|
|
125
|
+
state.detailSchemaHash = String(operationContextDetails.schemaHash || '').trim();
|
|
126
|
+
if (state.requireOperationSchemaHash && !state.detailSchemaHash) return createViolation('missing_operation_schema_hash', `Operation context details header "${state.operationContextDetailHeader}" must include schemaHash`, state.status);
|
|
127
|
+
state.detailOperationVersion = operationContextDetails.operationVersion;
|
|
128
|
+
if (state.requireOperationVersion && 'number' != typeof state.detailOperationVersion) return createViolation('missing_operation_version', `Operation context details header "${state.operationContextDetailHeader}" must include operationVersion`, state.status);
|
|
129
|
+
};
|
|
130
|
+
const checkOperationContract = (state)=>{
|
|
131
|
+
if (!state.operationContextDetails) return;
|
|
132
|
+
const expectedContracts = state.policy.expectedOperationContracts;
|
|
133
|
+
if (expectedContracts && 'object' == typeof expectedContracts) {
|
|
134
|
+
const method = String(state.operationContextDetails.method || '').toUpperCase();
|
|
135
|
+
const routePath = String(state.operationContextDetails.routePath || '').trim();
|
|
136
|
+
const operationId = String(state.operationContextDetails.operationId || '').trim();
|
|
137
|
+
const expectedContract = expectedContracts[`${method}:${routePath}`] || expectedContracts[`operation:${operationId}`];
|
|
138
|
+
if (expectedContract) {
|
|
139
|
+
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);
|
|
140
|
+
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);
|
|
141
|
+
} else if (!state.allowUnknownOperations) return createViolation('unknown_operation_contract', `No expected operation contract found for operation "${operationId || `${method}:${routePath}`}"`, state.status);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
const CROSS_PROJECT_POLICY_CHECKS = [
|
|
145
|
+
checkNamespaceAllowlistVerifier,
|
|
146
|
+
checkEnvelopeHeader,
|
|
147
|
+
checkProducerIdentity,
|
|
148
|
+
checkNamespaceAllowlist,
|
|
149
|
+
checkOperationContext,
|
|
150
|
+
checkOperationContextDetails,
|
|
151
|
+
checkOperationContract
|
|
152
|
+
];
|
|
153
|
+
const evaluateCrossProjectPolicy = (headers, policy)=>{
|
|
154
|
+
if (!policy?.enabled) return null;
|
|
155
|
+
const state = createCrossProjectPolicyEvaluationState(headers, policy);
|
|
156
|
+
for (const check of CROSS_PROJECT_POLICY_CHECKS){
|
|
157
|
+
const violation = check(state);
|
|
158
|
+
if (violation) return violation;
|
|
159
|
+
if (state.complete) break;
|
|
99
160
|
}
|
|
100
161
|
return null;
|
|
101
162
|
};
|