@claude-flow/security 3.0.0-alpha.9 → 3.0.0
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.
- package/dist/CVE-REMEDIATION.d.ts +17 -6
- package/dist/CVE-REMEDIATION.d.ts.map +1 -1
- package/dist/CVE-REMEDIATION.js +328 -12
- package/dist/CVE-REMEDIATION.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -1
- package/dist/keychain-adapter.d.ts +48 -0
- package/dist/keychain-adapter.d.ts.map +1 -0
- package/dist/keychain-adapter.js +0 -0
- package/dist/keychain-adapter.js.map +1 -0
- package/dist/mcp-caller-identity.d.ts +101 -0
- package/dist/mcp-caller-identity.d.ts.map +1 -0
- package/dist/mcp-caller-identity.js +0 -0
- package/dist/mcp-caller-identity.js.map +1 -0
- package/dist/mcp-composition-inspector.d.ts +133 -0
- package/dist/mcp-composition-inspector.d.ts.map +1 -0
- package/dist/mcp-composition-inspector.js +254 -0
- package/dist/mcp-composition-inspector.js.map +1 -0
- package/dist/oauth/browser.d.ts +31 -0
- package/dist/oauth/browser.d.ts.map +1 -0
- package/dist/oauth/browser.js +55 -0
- package/dist/oauth/browser.js.map +1 -0
- package/dist/oauth/callback-server.d.ts +37 -0
- package/dist/oauth/callback-server.d.ts.map +1 -0
- package/dist/oauth/callback-server.js +86 -0
- package/dist/oauth/callback-server.js.map +1 -0
- package/dist/oauth/client.d.ts +57 -0
- package/dist/oauth/client.d.ts.map +1 -0
- package/dist/oauth/client.js +120 -0
- package/dist/oauth/client.js.map +1 -0
- package/dist/oauth/pkce.d.ts +25 -0
- package/dist/oauth/pkce.d.ts.map +1 -0
- package/dist/oauth/pkce.js +36 -0
- package/dist/oauth/pkce.js.map +1 -0
- package/dist/path-validator.d.ts +23 -0
- package/dist/path-validator.d.ts.map +1 -1
- package/dist/path-validator.js +128 -5
- package/dist/path-validator.js.map +1 -1
- package/dist/policy/canonical.d.ts +5 -0
- package/dist/policy/canonical.d.ts.map +1 -0
- package/dist/policy/canonical.js +30 -0
- package/dist/policy/canonical.js.map +1 -0
- package/dist/policy/engine.d.ts +34 -0
- package/dist/policy/engine.d.ts.map +1 -0
- package/dist/policy/engine.js +285 -0
- package/dist/policy/engine.js.map +1 -0
- package/dist/policy/envelope.d.ts +9 -0
- package/dist/policy/envelope.d.ts.map +1 -0
- package/dist/policy/envelope.js +90 -0
- package/dist/policy/envelope.js.map +1 -0
- package/dist/policy/evaluator.d.ts +4 -0
- package/dist/policy/evaluator.d.ts.map +1 -0
- package/dist/policy/evaluator.js +119 -0
- package/dist/policy/evaluator.js.map +1 -0
- package/dist/policy/index.d.ts +7 -0
- package/dist/policy/index.d.ts.map +1 -0
- package/dist/policy/index.js +7 -0
- package/dist/policy/index.js.map +1 -0
- package/dist/policy/product-plane.d.ts +238 -0
- package/dist/policy/product-plane.d.ts.map +1 -0
- package/dist/policy/product-plane.js +1022 -0
- package/dist/policy/product-plane.js.map +1 -0
- package/dist/policy/types.d.ts +169 -0
- package/dist/policy/types.d.ts.map +1 -0
- package/dist/policy/types.js +2 -0
- package/dist/policy/types.js.map +1 -0
- package/dist/safe-git.d.ts +22 -0
- package/dist/safe-git.d.ts.map +1 -0
- package/dist/safe-git.js +63 -0
- package/dist/safe-git.js.map +1 -0
- package/package.json +17 -5
- package/.claude-flow/data/pending-insights.jsonl +0 -6
- package/.claude-flow/neural/stats.json +0 -6
- package/__tests__/acceptance/security-compliance.test.ts +0 -674
- package/__tests__/authorization-propagator.test.ts +0 -155
- package/__tests__/credential-generator.test.ts +0 -310
- package/__tests__/fixtures/configurations.ts +0 -425
- package/__tests__/fixtures/index.ts +0 -21
- package/__tests__/helpers/create-mock.ts +0 -469
- package/__tests__/helpers/index.ts +0 -32
- package/__tests__/input-validator.test.ts +0 -382
- package/__tests__/integration/security-flow.test.ts +0 -606
- package/__tests__/password-hasher.test.ts +0 -239
- package/__tests__/path-validator.test.ts +0 -302
- package/__tests__/plugin-integrity-verifier.test.ts +0 -199
- package/__tests__/safe-executor.test.ts +0 -292
- package/__tests__/token-generator.test.ts +0 -371
- package/__tests__/tool-output-guardrail.test.ts +0 -256
- package/__tests__/unit/credential-generator.test.ts +0 -182
- package/__tests__/unit/password-hasher.test.ts +0 -359
- package/__tests__/unit/path-validator.test.ts +0 -509
- package/__tests__/unit/safe-executor.test.ts +0 -667
- package/__tests__/unit/token-generator.test.ts +0 -310
- package/ruvector.db +0 -0
- package/src/CVE-REMEDIATION.ts +0 -251
- package/src/application/index.ts +0 -10
- package/src/application/services/security-application-service.ts +0 -193
- package/src/authorization/propagator.ts +0 -279
- package/src/credential-generator.ts +0 -368
- package/src/domain/entities/security-context.ts +0 -173
- package/src/domain/index.ts +0 -17
- package/src/domain/services/security-domain-service.ts +0 -296
- package/src/index.ts +0 -314
- package/src/input-validator.ts +0 -466
- package/src/password-hasher.ts +0 -277
- package/src/path-validator.ts +0 -525
- package/src/plugins/integrity-verifier.ts +0 -265
- package/src/safe-executor.ts +0 -546
- package/src/token-generator.ts +0 -463
- package/src/tool-output-guardrail.ts +0 -360
- package/tmp.json +0 -0
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
- package/vitest.config.ts +0 -20
|
@@ -1,199 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Safe Executor Tests - HIGH-1 Remediation Validation
|
|
3
|
-
*
|
|
4
|
-
* Tests verify:
|
|
5
|
-
* - Commands execute without shell
|
|
6
|
-
* - Command allowlist enforcement
|
|
7
|
-
* - Argument sanitization
|
|
8
|
-
* - Dangerous pattern detection
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { describe, it, expect, beforeEach } from 'vitest';
|
|
12
|
-
import {
|
|
13
|
-
SafeExecutor,
|
|
14
|
-
SafeExecutorError,
|
|
15
|
-
createDevelopmentExecutor,
|
|
16
|
-
createReadOnlyExecutor,
|
|
17
|
-
} from '../src/safe-executor.js';
|
|
18
|
-
|
|
19
|
-
describe('SafeExecutor', () => {
|
|
20
|
-
let executor: SafeExecutor;
|
|
21
|
-
|
|
22
|
-
beforeEach(() => {
|
|
23
|
-
executor = new SafeExecutor({
|
|
24
|
-
allowedCommands: ['echo', 'ls', 'git', 'npm', 'node'],
|
|
25
|
-
timeout: 5000,
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
describe('Configuration', () => {
|
|
30
|
-
it('should require at least one allowed command', () => {
|
|
31
|
-
expect(() => new SafeExecutor({
|
|
32
|
-
allowedCommands: [],
|
|
33
|
-
})).toThrow(SafeExecutorError);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should reject dangerous commands in allowlist', () => {
|
|
37
|
-
expect(() => new SafeExecutor({
|
|
38
|
-
allowedCommands: ['rm'],
|
|
39
|
-
})).toThrow(SafeExecutorError);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should reject multiple dangerous commands', () => {
|
|
43
|
-
expect(() => new SafeExecutor({
|
|
44
|
-
allowedCommands: ['chmod', 'chown', 'rm'],
|
|
45
|
-
})).toThrow(SafeExecutorError);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('should allow safe commands', () => {
|
|
49
|
-
expect(() => new SafeExecutor({
|
|
50
|
-
allowedCommands: ['git', 'npm', 'node'],
|
|
51
|
-
})).not.toThrow();
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('Command Validation', () => {
|
|
56
|
-
it('should allow commands in allowlist', async () => {
|
|
57
|
-
const result = await executor.execute('echo', ['hello']);
|
|
58
|
-
expect(result.exitCode).toBe(0);
|
|
59
|
-
expect(result.stdout.trim()).toBe('hello');
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('should block commands not in allowlist', async () => {
|
|
63
|
-
await expect(executor.execute('cat', ['/etc/passwd'])).rejects.toThrow(SafeExecutorError);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should block sudo commands by default', async () => {
|
|
67
|
-
const sudoExecutor = new SafeExecutor({
|
|
68
|
-
allowedCommands: ['sudo', 'ls'],
|
|
69
|
-
allowSudo: false,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
await expect(sudoExecutor.execute('sudo', ['ls'])).rejects.toThrow(SafeExecutorError);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('should allow sudo when configured', async () => {
|
|
76
|
-
const sudoExecutor = new SafeExecutor({
|
|
77
|
-
allowedCommands: ['sudo', 'ls'],
|
|
78
|
-
allowSudo: true,
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// Will fail due to password requirement, but shouldn't throw allowlist error
|
|
82
|
-
const error = await sudoExecutor.execute('sudo', ['-n', 'ls']).catch(e => e);
|
|
83
|
-
if (error instanceof SafeExecutorError) {
|
|
84
|
-
expect(error.code).not.toBe('SUDO_NOT_ALLOWED');
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
describe('Argument Validation', () => {
|
|
90
|
-
it('should block null bytes in arguments', async () => {
|
|
91
|
-
await expect(executor.execute('echo', ['hello\x00world'])).rejects.toThrow(SafeExecutorError);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('should block semicolon (command chaining)', async () => {
|
|
95
|
-
await expect(executor.execute('echo', ['hello; rm -rf /'])).rejects.toThrow(SafeExecutorError);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('should block && (command chaining)', async () => {
|
|
99
|
-
await expect(executor.execute('echo', ['hello && rm -rf /'])).rejects.toThrow(SafeExecutorError);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('should block || (command chaining)', async () => {
|
|
103
|
-
await expect(executor.execute('echo', ['hello || rm -rf /'])).rejects.toThrow(SafeExecutorError);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('should block pipe character', async () => {
|
|
107
|
-
await expect(executor.execute('echo', ['hello | cat'])).rejects.toThrow(SafeExecutorError);
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
it('should block backticks (command substitution)', async () => {
|
|
111
|
-
await expect(executor.execute('echo', ['`whoami`'])).rejects.toThrow(SafeExecutorError);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it('should block $() (command substitution)', async () => {
|
|
115
|
-
await expect(executor.execute('echo', ['$(whoami)'])).rejects.toThrow(SafeExecutorError);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('should block redirect operators', async () => {
|
|
119
|
-
await expect(executor.execute('echo', ['hello > /etc/passwd'])).rejects.toThrow(SafeExecutorError);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should block newlines', async () => {
|
|
123
|
-
await expect(executor.execute('echo', ['hello\nrm -rf /'])).rejects.toThrow(SafeExecutorError);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('should allow safe arguments', async () => {
|
|
127
|
-
const result = await executor.execute('echo', ['hello', 'world']);
|
|
128
|
-
expect(result.exitCode).toBe(0);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it('should allow arguments with dashes', async () => {
|
|
132
|
-
const result = await executor.execute('echo', ['-n', 'hello']);
|
|
133
|
-
expect(result.exitCode).toBe(0);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('should allow arguments with equals', async () => {
|
|
137
|
-
const result = await executor.execute('echo', ['KEY=value']);
|
|
138
|
-
expect(result.exitCode).toBe(0);
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
describe('Argument Sanitization', () => {
|
|
143
|
-
it('should sanitize null bytes', () => {
|
|
144
|
-
const sanitized = executor.sanitizeArgument('hello\x00world');
|
|
145
|
-
expect(sanitized).not.toContain('\x00');
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
it('should sanitize shell metacharacters', () => {
|
|
149
|
-
const sanitized = executor.sanitizeArgument('hello; rm -rf /');
|
|
150
|
-
expect(sanitized).not.toContain(';');
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
describe('Command Execution', () => {
|
|
155
|
-
it('should return stdout', async () => {
|
|
156
|
-
const result = await executor.execute('echo', ['hello']);
|
|
157
|
-
expect(result.stdout.trim()).toBe('hello');
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it('should return stderr', async () => {
|
|
161
|
-
// Use node to generate stderr
|
|
162
|
-
const result = await executor.execute('node', ['-e', 'console.error("error")']);
|
|
163
|
-
expect(result.stderr.trim()).toBe('error');
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it('should return exit code', async () => {
|
|
167
|
-
const result = await executor.execute('node', ['-e', 'process.exit(42)']);
|
|
168
|
-
expect(result.exitCode).toBe(42);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it('should track execution duration', async () => {
|
|
172
|
-
const result = await executor.execute('echo', ['hello']);
|
|
173
|
-
expect(result.duration).toBeGreaterThanOrEqual(0);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it('should include command in result', async () => {
|
|
177
|
-
const result = await executor.execute('echo', ['hello']);
|
|
178
|
-
expect(result.command).toBe('echo');
|
|
179
|
-
expect(result.args).toEqual(['hello']);
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
describe('Timeout Handling', () => {
|
|
184
|
-
it('should timeout long-running commands', async () => {
|
|
185
|
-
const shortTimeoutExecutor = new SafeExecutor({
|
|
186
|
-
allowedCommands: ['node'],
|
|
187
|
-
timeout: 100,
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
await expect(
|
|
191
|
-
shortTimeoutExecutor.execute('node', ['-e', 'setTimeout(() => {}, 10000)'])
|
|
192
|
-
).rejects.toThrow(SafeExecutorError);
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
describe('Streaming Execution', () => {
|
|
197
|
-
it('should return process handle', () => {
|
|
198
|
-
const streaming = executor.executeStreaming('echo', ['hello']);
|
|
199
|
-
expect(streaming.process).toBeDefined();
|
|
200
|
-
expect(streaming.promise).toBeInstanceOf(Promise);
|
|
201
|
-
|
|
202
|
-
// Clean up
|
|
203
|
-
streaming.process.kill();
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it('should stream stdout', async () => {
|
|
207
|
-
const streaming = executor.executeStreaming('echo', ['hello']);
|
|
208
|
-
const result = await streaming.promise;
|
|
209
|
-
expect(result.stdout.trim()).toBe('hello');
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
describe('Dynamic Allowlist', () => {
|
|
214
|
-
it('should check if command is allowed', () => {
|
|
215
|
-
expect(executor.isCommandAllowed('echo')).toBe(true);
|
|
216
|
-
expect(executor.isCommandAllowed('cat')).toBe(false);
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
it('should add commands at runtime', () => {
|
|
220
|
-
expect(executor.isCommandAllowed('pwd')).toBe(false);
|
|
221
|
-
executor.allowCommand('pwd');
|
|
222
|
-
expect(executor.isCommandAllowed('pwd')).toBe(true);
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
it('should reject dangerous commands when adding', () => {
|
|
226
|
-
expect(() => executor.allowCommand('rm')).toThrow(SafeExecutorError);
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it('should return allowed commands', () => {
|
|
230
|
-
const commands = executor.getAllowedCommands();
|
|
231
|
-
expect(commands).toContain('echo');
|
|
232
|
-
expect(commands).toContain('git');
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
describe('Factory Functions', () => {
|
|
237
|
-
it('should create development executor', () => {
|
|
238
|
-
const devExecutor = createDevelopmentExecutor();
|
|
239
|
-
expect(devExecutor.isCommandAllowed('git')).toBe(true);
|
|
240
|
-
expect(devExecutor.isCommandAllowed('npm')).toBe(true);
|
|
241
|
-
expect(devExecutor.isCommandAllowed('node')).toBe(true);
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
it('should create read-only executor', () => {
|
|
245
|
-
const readOnlyExecutor = createReadOnlyExecutor();
|
|
246
|
-
expect(readOnlyExecutor.isCommandAllowed('git')).toBe(true);
|
|
247
|
-
expect(readOnlyExecutor.isCommandAllowed('cat')).toBe(true);
|
|
248
|
-
expect(readOnlyExecutor.isCommandAllowed('ls')).toBe(true);
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
describe('HIGH-1 Security Verification', () => {
|
|
253
|
-
it('should NOT use shell for execution', async () => {
|
|
254
|
-
// If shell were enabled, this would be interpreted as command chaining
|
|
255
|
-
// With shell disabled, it's just a string argument
|
|
256
|
-
const result = await executor.execute('echo', ['hello']);
|
|
257
|
-
expect(result.exitCode).toBe(0);
|
|
258
|
-
|
|
259
|
-
// This should fail validation before reaching execution
|
|
260
|
-
await expect(executor.execute('echo', ['hello; cat /etc/passwd'])).rejects.toThrow();
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
it('should prevent command injection via arguments', async () => {
|
|
264
|
-
// Classic injection attempts
|
|
265
|
-
const injections = [
|
|
266
|
-
'hello; rm -rf /',
|
|
267
|
-
'hello && rm -rf /',
|
|
268
|
-
'hello || rm -rf /',
|
|
269
|
-
'hello | rm -rf /',
|
|
270
|
-
'`rm -rf /`',
|
|
271
|
-
'$(rm -rf /)',
|
|
272
|
-
'hello\nrm -rf /',
|
|
273
|
-
'hello\x00rm -rf /',
|
|
274
|
-
];
|
|
275
|
-
|
|
276
|
-
for (const injection of injections) {
|
|
277
|
-
await expect(executor.execute('echo', [injection])).rejects.toThrow(SafeExecutorError);
|
|
278
|
-
}
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it('should prevent environment variable injection', async () => {
|
|
282
|
-
await expect(executor.execute('echo', ['${PATH}'])).rejects.toThrow(SafeExecutorError);
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it('should not allow arbitrary commands', async () => {
|
|
286
|
-
// Only commands in allowlist should execute
|
|
287
|
-
await expect(executor.execute('wget', ['http://evil.com'])).rejects.toThrow(SafeExecutorError);
|
|
288
|
-
await expect(executor.execute('curl', ['http://evil.com'])).rejects.toThrow(SafeExecutorError);
|
|
289
|
-
await expect(executor.execute('bash', ['-c', 'rm -rf /'])).rejects.toThrow(SafeExecutorError);
|
|
290
|
-
});
|
|
291
|
-
});
|
|
292
|
-
});
|