@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,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
+ });