@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,199 @@
1
+ /**
2
+ * Tests for PluginIntegrityVerifier (ADR-145 P1, ruvnet/ruflo#2254).
3
+ *
4
+ * Covers:
5
+ * - canonicalize is deterministic (key order doesn't change the digest)
6
+ * - findAnchor matches by exact key, by exact-id scope, and by glob scope
7
+ * - findAnchor rejects expired anchors
8
+ * - verify reports `signature-missing` for unsigned manifests
9
+ * - verify reports `manifest-hash-mismatch` when the manifest is tampered
10
+ * - verify reports `unknown-signer` when the key isn't in trust anchors
11
+ * - verify reports `pass` on a valid round-trip sign → verify
12
+ * - verify reports `signature-invalid` on a flipped signature byte
13
+ */
14
+
15
+ import { describe, it, expect, beforeAll } from 'vitest';
16
+ import { createHash } from 'node:crypto';
17
+ import {
18
+ PluginIntegrityVerifier,
19
+ canonicalize,
20
+ hashManifest,
21
+ findAnchor,
22
+ type PluginManifest,
23
+ type SignedPluginManifest,
24
+ type TrustAnchor,
25
+ type TrustAnchors,
26
+ } from '../src/plugins/integrity-verifier.js';
27
+
28
+ // ─── helpers ────────────────────────────────────────────────────────────
29
+
30
+ let ed: typeof import('@noble/ed25519') | null = null;
31
+ beforeAll(async () => {
32
+ try {
33
+ ed = await import('@noble/ed25519');
34
+ if (!ed.etc.sha512Sync) {
35
+ ed.etc.sha512Sync = (...m: Uint8Array[]) => {
36
+ const h = createHash('sha512');
37
+ for (const x of m) h.update(x);
38
+ return h.digest();
39
+ };
40
+ }
41
+ } catch {
42
+ ed = null;
43
+ }
44
+ });
45
+
46
+ function hex(b: Uint8Array): string {
47
+ return Array.from(b, x => x.toString(16).padStart(2, '0')).join('');
48
+ }
49
+
50
+ async function makeSigner(): Promise<{ priv: Uint8Array; pub: string }> {
51
+ if (!ed) throw new Error('ed25519 not available');
52
+ const priv = ed.utils.randomPrivateKey();
53
+ const pub = hex(await ed.getPublicKey(priv));
54
+ return { priv, pub };
55
+ }
56
+
57
+ async function sign(
58
+ manifest: PluginManifest,
59
+ priv: Uint8Array,
60
+ pub: string,
61
+ ): Promise<SignedPluginManifest> {
62
+ if (!ed) throw new Error('ed25519 not available');
63
+ const manifestHash = hashManifest(manifest);
64
+ const sig = await ed.sign(hexBytes(manifestHash), priv);
65
+ return { manifest, manifestHash, signature: hex(sig), publicKey: pub };
66
+ }
67
+
68
+ function hexBytes(h: string): Uint8Array {
69
+ const out = new Uint8Array(h.length / 2);
70
+ for (let i = 0; i < out.length; i++) out[i] = parseInt(h.substr(i * 2, 2), 16);
71
+ return out;
72
+ }
73
+
74
+ function anchorsFor(pub: string, scope = '*'): TrustAnchors {
75
+ return {
76
+ version: 1,
77
+ anchors: [{ publicKey: pub, owner: 'test', scope }],
78
+ };
79
+ }
80
+
81
+ // ─── canonicalize ──────────────────────────────────────────────────────
82
+
83
+ describe('canonicalize — deterministic JSON for hashing', () => {
84
+ it('orders keys deterministically at every level', () => {
85
+ const a = canonicalize({ b: 1, a: 2, c: { y: 1, x: 2 } });
86
+ const b = canonicalize({ a: 2, c: { x: 2, y: 1 }, b: 1 });
87
+ expect(a).toBe(b);
88
+ });
89
+
90
+ it('arrays preserve order', () => {
91
+ expect(canonicalize({ k: [3, 1, 2] })).toBe('{"k":[3,1,2]}');
92
+ });
93
+
94
+ it('different content yields different output', () => {
95
+ expect(canonicalize({ a: 1 })).not.toBe(canonicalize({ a: 2 }));
96
+ });
97
+ });
98
+
99
+ // ─── findAnchor ────────────────────────────────────────────────────────
100
+
101
+ describe('findAnchor — trust resolution', () => {
102
+ const NOW = Date.now();
103
+
104
+ it('matches by exact key + matching exact scope', () => {
105
+ const anchors: TrustAnchor[] = [
106
+ { publicKey: 'PK', owner: 'o', scope: '@claude-flow/memory' },
107
+ ];
108
+ expect(findAnchor(anchors, '@claude-flow/memory', 'PK', NOW)).not.toBeNull();
109
+ });
110
+
111
+ it('matches by exact key + wildcard scope', () => {
112
+ const anchors: TrustAnchor[] = [{ publicKey: 'PK', owner: 'o', scope: '@claude-flow/*' }];
113
+ expect(findAnchor(anchors, '@claude-flow/anything', 'PK', NOW)).not.toBeNull();
114
+ });
115
+
116
+ it('rejects exact-key with out-of-scope plugin id', () => {
117
+ const anchors: TrustAnchor[] = [
118
+ { publicKey: 'PK', owner: 'o', scope: '@claude-flow/*' },
119
+ ];
120
+ expect(findAnchor(anchors, '@third-party/x', 'PK', NOW)).toBeNull();
121
+ });
122
+
123
+ it('rejects wrong key even if scope matches', () => {
124
+ const anchors: TrustAnchor[] = [{ publicKey: 'PK', owner: 'o', scope: '*' }];
125
+ expect(findAnchor(anchors, 'anything', 'OTHER', NOW)).toBeNull();
126
+ });
127
+
128
+ it('rejects expired anchor', () => {
129
+ const anchors: TrustAnchor[] = [
130
+ { publicKey: 'PK', owner: 'o', scope: '*', expiresAt: '2020-01-01' },
131
+ ];
132
+ expect(findAnchor(anchors, 'x', 'PK', NOW)).toBeNull();
133
+ });
134
+ });
135
+
136
+ // ─── verify ────────────────────────────────────────────────────────────
137
+
138
+ describe('PluginIntegrityVerifier.verify', () => {
139
+ const skipMsg = '@noble/ed25519 unavailable — skipping signature round-trip tests';
140
+
141
+ it('returns signature-missing when signature is empty', async () => {
142
+ const v = new PluginIntegrityVerifier({ trustAnchors: anchorsFor('PK') });
143
+ const r = await v.verify({
144
+ manifest: { id: 'p', version: '1.0.0' },
145
+ manifestHash: 'abc',
146
+ signature: '',
147
+ publicKey: 'PK',
148
+ });
149
+ expect(r.status).toBe('signature-missing');
150
+ expect(r.pluginId).toBe('p');
151
+ });
152
+
153
+ it('returns manifest-hash-mismatch when the manifest is tampered', async () => {
154
+ if (!ed) { console.warn(skipMsg); return; }
155
+ const { priv, pub } = await makeSigner();
156
+ const signed = await sign({ id: 'p', version: '1.0.0' }, priv, pub);
157
+ // Tamper: change the manifest after signing
158
+ const tampered: SignedPluginManifest = {
159
+ ...signed,
160
+ manifest: { id: 'p', version: '1.0.0', extra: 'sneaky' },
161
+ };
162
+ const v = new PluginIntegrityVerifier({ trustAnchors: anchorsFor(pub) });
163
+ const r = await v.verify(tampered);
164
+ expect(r.status).toBe('manifest-hash-mismatch');
165
+ });
166
+
167
+ it('returns unknown-signer when the publicKey is not in trust anchors', async () => {
168
+ if (!ed) { console.warn(skipMsg); return; }
169
+ const { priv, pub } = await makeSigner();
170
+ const signed = await sign({ id: 'p', version: '1.0.0' }, priv, pub);
171
+ const v = new PluginIntegrityVerifier({
172
+ trustAnchors: anchorsFor('OTHER-PUBLIC-KEY'),
173
+ });
174
+ const r = await v.verify(signed);
175
+ expect(r.status).toBe('unknown-signer');
176
+ });
177
+
178
+ it('passes a valid round-trip sign → verify', async () => {
179
+ if (!ed) { console.warn(skipMsg); return; }
180
+ const { priv, pub } = await makeSigner();
181
+ const signed = await sign({ id: 'p', version: '1.0.0' }, priv, pub);
182
+ const v = new PluginIntegrityVerifier({ trustAnchors: anchorsFor(pub) });
183
+ const r = await v.verify(signed);
184
+ expect(r.status).toBe('pass');
185
+ expect(r.signerFingerprint).toBe(pub.slice(0, 16));
186
+ });
187
+
188
+ it('returns signature-invalid when a signature byte is flipped', async () => {
189
+ if (!ed) { console.warn(skipMsg); return; }
190
+ const { priv, pub } = await makeSigner();
191
+ const signed = await sign({ id: 'p', version: '1.0.0' }, priv, pub);
192
+ // Flip the first byte of the signature
193
+ const flipped = signed.signature.slice(0, 2) === 'ff' ? '00' : 'ff';
194
+ const tampered = { ...signed, signature: flipped + signed.signature.slice(2) };
195
+ const v = new PluginIntegrityVerifier({ trustAnchors: anchorsFor(pub) });
196
+ const r = await v.verify(tampered);
197
+ expect(r.status).toBe('signature-invalid');
198
+ });
199
+ });