@bleedingdev/modern-js-bff-core 3.5.0-ultramodern.3 → 3.5.0-ultramodern.31

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.
Files changed (33) hide show
  1. package/dist/cjs/adapter-kit/index.js +6 -2
  2. package/dist/cjs/adapter-kit/parity-scenarios/cross-project-denial.js +55 -0
  3. package/dist/cjs/adapter-kit/parity-scenarios/envelope.js +128 -0
  4. package/dist/cjs/adapter-kit/parity-scenarios/operation-context.js +158 -0
  5. package/dist/cjs/adapter-kit/parity-scenarios/schema.js +109 -0
  6. package/dist/cjs/adapter-kit/parity-scenarios/shared.js +71 -0
  7. package/dist/cjs/adapter-kit/parity.js +24 -367
  8. package/dist/cjs/security/crossProjectPolicy.js +123 -62
  9. package/dist/esm/adapter-kit/index.mjs +6 -2
  10. package/dist/esm/adapter-kit/parity-scenarios/cross-project-denial.mjs +17 -0
  11. package/dist/esm/adapter-kit/parity-scenarios/envelope.mjs +90 -0
  12. package/dist/esm/adapter-kit/parity-scenarios/operation-context.mjs +117 -0
  13. package/dist/esm/adapter-kit/parity-scenarios/schema.mjs +71 -0
  14. package/dist/esm/adapter-kit/parity-scenarios/shared.mjs +21 -0
  15. package/dist/esm/adapter-kit/parity.mjs +17 -360
  16. package/dist/esm/security/crossProjectPolicy.mjs +123 -62
  17. package/dist/esm-node/adapter-kit/index.mjs +6 -2
  18. package/dist/esm-node/adapter-kit/parity-scenarios/cross-project-denial.mjs +18 -0
  19. package/dist/esm-node/adapter-kit/parity-scenarios/envelope.mjs +91 -0
  20. package/dist/esm-node/adapter-kit/parity-scenarios/operation-context.mjs +118 -0
  21. package/dist/esm-node/adapter-kit/parity-scenarios/schema.mjs +72 -0
  22. package/dist/esm-node/adapter-kit/parity-scenarios/shared.mjs +22 -0
  23. package/dist/esm-node/adapter-kit/parity.mjs +17 -360
  24. package/dist/esm-node/security/crossProjectPolicy.mjs +123 -62
  25. package/dist/types/adapter-kit/index.d.ts +2 -2
  26. package/dist/types/adapter-kit/parity-scenarios/cross-project-denial.d.ts +2 -0
  27. package/dist/types/adapter-kit/parity-scenarios/envelope.d.ts +2 -0
  28. package/dist/types/adapter-kit/parity-scenarios/operation-context.d.ts +3 -0
  29. package/dist/types/adapter-kit/parity-scenarios/schema.d.ts +2 -0
  30. package/dist/types/adapter-kit/parity-scenarios/shared.d.ts +32 -0
  31. package/dist/types/adapter-kit/parity.d.ts +10 -70
  32. package/dist/types/security/crossProjectPolicy.d.ts +11 -8
  33. package/package.json +4 -4
@@ -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
- ...Object.values(input.params),
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 };