@claude-flow/security 3.0.0-alpha.1 → 3.0.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/README.md +292 -234
  2. package/__tests__/acceptance/security-compliance.test.ts +674 -674
  3. package/__tests__/authorization-propagator.test.ts +155 -0
  4. package/__tests__/credential-generator.test.ts +310 -310
  5. package/__tests__/fixtures/configurations.ts +425 -419
  6. package/__tests__/fixtures/index.ts +21 -21
  7. package/__tests__/helpers/create-mock.ts +469 -469
  8. package/__tests__/helpers/index.ts +32 -32
  9. package/__tests__/input-validator.test.ts +382 -381
  10. package/__tests__/integration/security-flow.test.ts +606 -606
  11. package/__tests__/keychain-adapter.test.ts +60 -0
  12. package/__tests__/oauth-browser.test.ts +13 -0
  13. package/__tests__/oauth-export-surface.test.ts +21 -0
  14. package/__tests__/oauth-pkce.test.ts +37 -0
  15. package/__tests__/password-hasher.test.ts +239 -239
  16. package/__tests__/path-validator.test.ts +302 -302
  17. package/__tests__/plugin-integrity-verifier.test.ts +199 -0
  18. package/__tests__/safe-executor.test.ts +292 -292
  19. package/__tests__/token-generator.test.ts +371 -371
  20. package/__tests__/tool-output-guardrail.test.ts +256 -0
  21. package/__tests__/unit/credential-generator.test.ts +182 -182
  22. package/__tests__/unit/password-hasher.test.ts +359 -359
  23. package/__tests__/unit/path-validator.test.ts +509 -509
  24. package/__tests__/unit/safe-executor.test.ts +667 -667
  25. package/__tests__/unit/token-generator.test.ts +310 -310
  26. package/dist/CVE-REMEDIATION.d.ts +17 -6
  27. package/dist/CVE-REMEDIATION.d.ts.map +1 -1
  28. package/dist/CVE-REMEDIATION.js +328 -12
  29. package/dist/CVE-REMEDIATION.js.map +1 -1
  30. package/dist/authorization/propagator.d.ts +148 -0
  31. package/dist/authorization/propagator.d.ts.map +1 -0
  32. package/dist/authorization/propagator.js +182 -0
  33. package/dist/authorization/propagator.js.map +1 -0
  34. package/dist/credential-generator.js +9 -9
  35. package/dist/domain/services/security-domain-service.js +1 -1
  36. package/dist/domain/services/security-domain-service.js.map +1 -1
  37. package/dist/index.d.ts +9 -1
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +21 -1
  40. package/dist/index.js.map +1 -1
  41. package/dist/keychain-adapter.d.ts +48 -0
  42. package/dist/keychain-adapter.d.ts.map +1 -0
  43. package/dist/keychain-adapter.js +0 -0
  44. package/dist/keychain-adapter.js.map +1 -0
  45. package/dist/oauth/browser.d.ts +31 -0
  46. package/dist/oauth/browser.d.ts.map +1 -0
  47. package/dist/oauth/browser.js +55 -0
  48. package/dist/oauth/browser.js.map +1 -0
  49. package/dist/oauth/callback-server.d.ts +37 -0
  50. package/dist/oauth/callback-server.d.ts.map +1 -0
  51. package/dist/oauth/callback-server.js +86 -0
  52. package/dist/oauth/callback-server.js.map +1 -0
  53. package/dist/oauth/client.d.ts +57 -0
  54. package/dist/oauth/client.d.ts.map +1 -0
  55. package/dist/oauth/client.js +120 -0
  56. package/dist/oauth/client.js.map +1 -0
  57. package/dist/oauth/pkce.d.ts +25 -0
  58. package/dist/oauth/pkce.d.ts.map +1 -0
  59. package/dist/oauth/pkce.js +36 -0
  60. package/dist/oauth/pkce.js.map +1 -0
  61. package/dist/password-hasher.d.ts.map +1 -1
  62. package/dist/password-hasher.js +8 -1
  63. package/dist/password-hasher.js.map +1 -1
  64. package/dist/plugins/integrity-verifier.d.ts +125 -0
  65. package/dist/plugins/integrity-verifier.d.ts.map +1 -0
  66. package/dist/plugins/integrity-verifier.js +186 -0
  67. package/dist/plugins/integrity-verifier.js.map +1 -0
  68. package/dist/safe-executor.d.ts +7 -0
  69. package/dist/safe-executor.d.ts.map +1 -1
  70. package/dist/safe-executor.js +21 -1
  71. package/dist/safe-executor.js.map +1 -1
  72. package/dist/tool-output-guardrail.d.ts +126 -0
  73. package/dist/tool-output-guardrail.d.ts.map +1 -0
  74. package/dist/tool-output-guardrail.js +257 -0
  75. package/dist/tool-output-guardrail.js.map +1 -0
  76. package/node_modules-incomplete-release-test/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
  77. package/package.json +33 -28
  78. package/scripts/verify-oauth-exports.mjs +29 -0
  79. package/src/CVE-REMEDIATION.ts +591 -251
  80. package/src/application/index.ts +10 -10
  81. package/src/application/services/security-application-service.ts +193 -193
  82. package/src/authorization/propagator.ts +279 -0
  83. package/src/credential-generator.ts +368 -368
  84. package/src/domain/entities/security-context.ts +173 -173
  85. package/src/domain/index.ts +17 -17
  86. package/src/domain/services/security-domain-service.ts +296 -296
  87. package/src/index.ts +340 -271
  88. package/src/input-validator.ts +466 -466
  89. package/src/keychain-adapter.ts +0 -0
  90. package/src/oauth/browser.ts +58 -0
  91. package/src/oauth/callback-server.ts +97 -0
  92. package/src/oauth/client.ts +153 -0
  93. package/src/oauth/pkce.ts +45 -0
  94. package/src/password-hasher.ts +277 -270
  95. package/src/path-validator.ts +525 -525
  96. package/src/plugins/integrity-verifier.ts +265 -0
  97. package/src/safe-executor.ts +546 -525
  98. package/src/token-generator.ts +463 -463
  99. package/src/tool-output-guardrail.ts +360 -0
  100. package/tmp.json +0 -0
  101. package/tsconfig.json +9 -9
  102. package/tsconfig.tsbuildinfo +1 -1
  103. package/vitest.config.ts +20 -0
  104. package/.agentic-flow/intelligence.json +0 -16
  105. package/__tests__/coverage/base.css +0 -224
  106. package/__tests__/coverage/block-navigation.js +0 -87
  107. package/__tests__/coverage/coverage-final.json +0 -11
  108. package/__tests__/coverage/favicon.png +0 -0
  109. package/__tests__/coverage/index.html +0 -161
  110. package/__tests__/coverage/lcov-report/base.css +0 -224
  111. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  112. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  113. package/__tests__/coverage/lcov-report/index.html +0 -161
  114. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  115. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  116. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  117. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  118. package/__tests__/coverage/lcov-report/src/CVE-REMEDIATION.ts.html +0 -838
  119. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  120. package/__tests__/coverage/lcov-report/src/application/services/security-application-service.ts.html +0 -664
  121. package/__tests__/coverage/lcov-report/src/credential-generator.ts.html +0 -1189
  122. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  123. package/__tests__/coverage/lcov-report/src/domain/entities/security-context.ts.html +0 -604
  124. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  125. package/__tests__/coverage/lcov-report/src/domain/services/security-domain-service.ts.html +0 -973
  126. package/__tests__/coverage/lcov-report/src/index.html +0 -206
  127. package/__tests__/coverage/lcov-report/src/input-validator.ts.html +0 -1483
  128. package/__tests__/coverage/lcov-report/src/password-hasher.ts.html +0 -895
  129. package/__tests__/coverage/lcov-report/src/path-validator.ts.html +0 -1660
  130. package/__tests__/coverage/lcov-report/src/safe-executor.ts.html +0 -1660
  131. package/__tests__/coverage/lcov-report/src/token-generator.ts.html +0 -1474
  132. package/__tests__/coverage/lcov.info +0 -3650
  133. package/__tests__/coverage/prettify.css +0 -1
  134. package/__tests__/coverage/prettify.js +0 -2
  135. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  136. package/__tests__/coverage/sorter.js +0 -210
  137. package/__tests__/coverage/src/CVE-REMEDIATION.ts.html +0 -838
  138. package/__tests__/coverage/src/application/services/index.html +0 -116
  139. package/__tests__/coverage/src/application/services/security-application-service.ts.html +0 -664
  140. package/__tests__/coverage/src/credential-generator.ts.html +0 -1189
  141. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  142. package/__tests__/coverage/src/domain/entities/security-context.ts.html +0 -604
  143. package/__tests__/coverage/src/domain/services/index.html +0 -116
  144. package/__tests__/coverage/src/domain/services/security-domain-service.ts.html +0 -973
  145. package/__tests__/coverage/src/index.html +0 -206
  146. package/__tests__/coverage/src/input-validator.ts.html +0 -1483
  147. package/__tests__/coverage/src/password-hasher.ts.html +0 -895
  148. package/__tests__/coverage/src/path-validator.ts.html +0 -1660
  149. package/__tests__/coverage/src/safe-executor.ts.html +0 -1660
  150. package/__tests__/coverage/src/token-generator.ts.html +0 -1474
@@ -0,0 +1,155 @@
1
+ /**
2
+ * Tests for AgentAuthorizationPropagator (ADR-144 P1, ruvnet/ruflo#2248).
3
+ *
4
+ * Covers the load-bearing invariants of the action-layer security model:
5
+ * - wrapOutbound monotonically reduces scope (cannot grant more than holder)
6
+ * - delegationDepth decrements by ≥ 1 every hop; depth-0 cannot delegate
7
+ * - expired scopes are rejected at wrap and at check
8
+ * - checkToolCall returns stable, telemetry-friendly reasons
9
+ * - verifyServerAuth rejects empty / nullish credentials
10
+ * - provenance buffer ring-bound at the configured max
11
+ * - legacy permissive scope (the migration shim) allows everything
12
+ */
13
+
14
+ import { describe, it, expect } from 'vitest';
15
+ import {
16
+ AgentAuthorizationPropagator,
17
+ AuthorizationPropagationError,
18
+ makeLegacyPermissiveScope,
19
+ type AuthScope,
20
+ } from '../src/authorization/propagator.js';
21
+
22
+ const FAR_FUTURE = Date.now() + 60 * 60 * 1000; // 1 hour from now
23
+
24
+ function scope(overrides: Partial<AuthScope> = {}): AuthScope {
25
+ return {
26
+ principalId: 'principal-A',
27
+ grantedTools: ['memory_store', 'memory_search'],
28
+ grantedServers: ['server-1'],
29
+ delegationDepth: 3,
30
+ expiresAt: FAR_FUTURE,
31
+ ...overrides,
32
+ };
33
+ }
34
+
35
+ describe('wrapOutbound — scope reduction invariants', () => {
36
+ const prop = new AgentAuthorizationPropagator();
37
+
38
+ it('decrements delegationDepth by exactly 1', () => {
39
+ const env = prop.wrapOutbound({ msg: 'hi' }, scope({ delegationDepth: 3 }));
40
+ expect(env.scope.delegationDepth).toBe(2);
41
+ });
42
+
43
+ it('refuses to delegate when depth has been exhausted', () => {
44
+ expect(() =>
45
+ prop.wrapOutbound({ msg: 'hi' }, scope({ delegationDepth: 0 })),
46
+ ).toThrow(AuthorizationPropagationError);
47
+ });
48
+
49
+ it('refuses to grant a tool not held by the parent scope', () => {
50
+ expect(() =>
51
+ prop.wrapOutbound({}, scope(), { tools: ['memory_store', 'task_create'] }),
52
+ ).toThrow(/cannot grant tools 'task_create'/);
53
+ });
54
+
55
+ it('allows a strict subset of the parent tool set', () => {
56
+ const env = prop.wrapOutbound({}, scope(), { tools: ['memory_store'] });
57
+ expect(env.scope.grantedTools).toEqual(['memory_store']);
58
+ });
59
+
60
+ it('preserves principalId across the hop', () => {
61
+ const env = prop.wrapOutbound({}, scope({ principalId: 'principal-X' }));
62
+ expect(env.scope.principalId).toBe('principal-X');
63
+ });
64
+
65
+ it('rejects an expired holder scope', () => {
66
+ expect(() =>
67
+ prop.wrapOutbound({}, scope({ expiresAt: 1 })),
68
+ ).toThrow(/scope expired/);
69
+ });
70
+
71
+ it('passes the payload through untouched', () => {
72
+ const payload = { kind: 'design', body: { topic: 'auth' } };
73
+ const env = prop.wrapOutbound(payload, scope());
74
+ expect(env.payload).toBe(payload);
75
+ });
76
+
77
+ it('a wildcard parent scope can grant any specific tool', () => {
78
+ const env = prop.wrapOutbound({}, makeLegacyPermissiveScope(), {
79
+ tools: ['memory_store', 'task_create'],
80
+ });
81
+ expect(env.scope.grantedTools).toEqual(['memory_store', 'task_create']);
82
+ });
83
+ });
84
+
85
+ describe('checkToolCall — typed decisions', () => {
86
+ const prop = new AgentAuthorizationPropagator();
87
+
88
+ it('allows a tool that is in scope', () => {
89
+ expect(prop.checkToolCall('memory_store', scope())).toEqual({ allowed: true });
90
+ });
91
+
92
+ it('denies a tool not in scope with stable reason', () => {
93
+ expect(prop.checkToolCall('shell_exec', scope())).toEqual({
94
+ allowed: false,
95
+ reason: 'tool-not-in-scope',
96
+ });
97
+ });
98
+
99
+ it('denies when the named server is not in scope', () => {
100
+ expect(
101
+ prop.checkToolCall('memory_store', scope(), { serverId: 'untrusted' }),
102
+ ).toEqual({ allowed: false, reason: 'server-not-in-scope' });
103
+ });
104
+
105
+ it('denies an expired scope deterministically (fixed-now)', () => {
106
+ const expired = scope({ expiresAt: 1000 });
107
+ expect(prop.checkToolCall('memory_store', expired, { now: 2000 })).toEqual({
108
+ allowed: false,
109
+ reason: 'scope-expired',
110
+ });
111
+ });
112
+ });
113
+
114
+ describe('verifyServerAuth — fail-closed on missing credentials', () => {
115
+ const prop = new AgentAuthorizationPropagator();
116
+
117
+ it('rejects missing credential', () => {
118
+ expect(prop.verifyServerAuth('s1', null)).toBe(false);
119
+ expect(prop.verifyServerAuth('s1', undefined)).toBe(false);
120
+ });
121
+
122
+ it('rejects empty-string credential', () => {
123
+ expect(prop.verifyServerAuth('s1', '')).toBe(false);
124
+ expect(prop.verifyServerAuth('s1', ' ')).toBe(false);
125
+ });
126
+
127
+ it('rejects missing serverId', () => {
128
+ expect(prop.verifyServerAuth('', 'token')).toBe(false);
129
+ });
130
+
131
+ it('accepts a non-empty credential (P1 permissive default)', () => {
132
+ expect(prop.verifyServerAuth('s1', 'oauth-token-xyz')).toBe(true);
133
+ });
134
+ });
135
+
136
+ describe('provenance buffer', () => {
137
+ it('retains recorded actions in insertion order', () => {
138
+ const p = new AgentAuthorizationPropagator();
139
+ p.recordAction('agent-A', 'memory_store', scope(), 'allowed');
140
+ p.recordAction('agent-B', 'shell_exec', scope(), 'denied', 'tool-not-in-scope');
141
+ const log = p.getProvenance();
142
+ expect(log).toHaveLength(2);
143
+ expect(log[0]?.outcome).toBe('allowed');
144
+ expect(log[1]?.reason).toBe('tool-not-in-scope');
145
+ });
146
+
147
+ it('ring-bounds at the configured max', () => {
148
+ const p = new AgentAuthorizationPropagator({ provenanceBufferMax: 3 });
149
+ for (let i = 0; i < 5; i++) p.recordAction('a', 't', scope(), 'allowed', `i=${i}`);
150
+ const log = p.getProvenance();
151
+ expect(log).toHaveLength(3);
152
+ expect(log[0]?.reason).toBe('i=2');
153
+ expect(log[2]?.reason).toBe('i=4');
154
+ });
155
+ });