@claude-flow/security 3.0.0-alpha.7 → 3.0.0-alpha.9

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 (61) hide show
  1. package/.claude-flow/data/pending-insights.jsonl +6 -0
  2. package/.claude-flow/neural/stats.json +6 -0
  3. package/README.md +292 -234
  4. package/__tests__/acceptance/security-compliance.test.ts +674 -674
  5. package/__tests__/authorization-propagator.test.ts +155 -0
  6. package/__tests__/credential-generator.test.ts +310 -310
  7. package/__tests__/fixtures/configurations.ts +425 -425
  8. package/__tests__/fixtures/index.ts +21 -21
  9. package/__tests__/helpers/create-mock.ts +469 -469
  10. package/__tests__/helpers/index.ts +32 -32
  11. package/__tests__/input-validator.test.ts +382 -382
  12. package/__tests__/integration/security-flow.test.ts +606 -606
  13. package/__tests__/password-hasher.test.ts +239 -239
  14. package/__tests__/path-validator.test.ts +302 -302
  15. package/__tests__/plugin-integrity-verifier.test.ts +199 -0
  16. package/__tests__/safe-executor.test.ts +292 -292
  17. package/__tests__/token-generator.test.ts +371 -371
  18. package/__tests__/tool-output-guardrail.test.ts +256 -0
  19. package/__tests__/unit/credential-generator.test.ts +182 -182
  20. package/__tests__/unit/password-hasher.test.ts +359 -359
  21. package/__tests__/unit/path-validator.test.ts +509 -509
  22. package/__tests__/unit/safe-executor.test.ts +667 -667
  23. package/__tests__/unit/token-generator.test.ts +310 -310
  24. package/dist/authorization/propagator.d.ts +148 -0
  25. package/dist/authorization/propagator.d.ts.map +1 -0
  26. package/dist/authorization/propagator.js +182 -0
  27. package/dist/authorization/propagator.js.map +1 -0
  28. package/dist/credential-generator.js +9 -9
  29. package/dist/index.d.ts +3 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +10 -0
  32. package/dist/index.js.map +1 -1
  33. package/dist/plugins/integrity-verifier.d.ts +125 -0
  34. package/dist/plugins/integrity-verifier.d.ts.map +1 -0
  35. package/dist/plugins/integrity-verifier.js +186 -0
  36. package/dist/plugins/integrity-verifier.js.map +1 -0
  37. package/dist/tool-output-guardrail.d.ts +126 -0
  38. package/dist/tool-output-guardrail.d.ts.map +1 -0
  39. package/dist/tool-output-guardrail.js +257 -0
  40. package/dist/tool-output-guardrail.js.map +1 -0
  41. package/package.json +27 -27
  42. package/ruvector.db +0 -0
  43. package/src/CVE-REMEDIATION.ts +251 -251
  44. package/src/application/index.ts +10 -10
  45. package/src/application/services/security-application-service.ts +193 -193
  46. package/src/authorization/propagator.ts +279 -0
  47. package/src/credential-generator.ts +368 -368
  48. package/src/domain/entities/security-context.ts +173 -173
  49. package/src/domain/index.ts +17 -17
  50. package/src/domain/services/security-domain-service.ts +296 -296
  51. package/src/index.ts +314 -271
  52. package/src/input-validator.ts +466 -466
  53. package/src/password-hasher.ts +277 -277
  54. package/src/path-validator.ts +525 -525
  55. package/src/plugins/integrity-verifier.ts +265 -0
  56. package/src/safe-executor.ts +546 -546
  57. package/src/token-generator.ts +463 -463
  58. package/src/tool-output-guardrail.ts +360 -0
  59. package/tsconfig.json +9 -9
  60. package/tsconfig.tsbuildinfo +1 -1
  61. package/vitest.config.ts +20 -20
@@ -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
+ });