mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-23 21:59:14 +09:00
* fix: critical security & performance fixes across 6 packages Addresses 11 vulnerabilities from security audit + 5 additional issues found during devil's advocate review, plus test suite fixes across memory, security, and swarm packages. ## Security Fixes (Critical) - CRIT-01: SQL injection in embeddings search — replaced string interpolation with parameterized queries (db.prepare/bind) - CRIT-02: Command injection in 4 files — replaced execSync template literals with execFileSync + input validation regex: - ruvector/import.ts (docker container name) - init.ts (embedding model name, 2 locations) - agent-browser-adapter.ts (CLI args) - CRIT-03: Browser eval hardening — 12 dangerous patterns blocked, configurable 20KB length limit via CLAUDE_FLOW_MAX_EVAL_SCRIPT_LENGTH ## Security Fixes (High) - HIGH-01: ESLint rules restricting child_process imports (both import and require) with overrides for safe-executor/daemon/mcp-server - HIGH-02: Hive-mind truthiness fix (=== true vs !== false) - HIGH-03: Prototype pollution prevention — safeJsonParse with JSON reviver stripping __proto__/constructor/prototype in 3 memory backends - HIGH-04: Plugin sandbox warning on unsandboxed plugin load - HIGH-05: HNSW hash collision fix — dual-hash (djb2+sdbm) producing 53-bit values with bidirectional map and linear probing ## Security Fixes (DA Review) - DA-CRIT-1: Local json-security.ts to avoid cross-package imports - DA-CRIT-2: PostgreSQL schema name validation (validateSchemaName) added to all 6 ruvector commands via pg-utils.ts - DA-CRIT-3: PID injection prevention in mcp-server — /proc read + execFileSync replacing execSync with PID interpolation - DA-HIGH-3: Timestamp validation (validateTimestamp) in ruvector import - DA-HIGH-4: 4 additional browser eval bypass patterns (globalThis, .constructor, Reflect, eval()) - DA-HIGH-5: ESLint no-restricted-modules for require() coverage ## Performance Fixes - PERF-01: BoundedSet<T> for gossip seenMessages — Map-based LRU eviction capped at 100K entries, prevents unbounded memory growth - PERF-02: Bulk operations use Promise.allSettled with batch size 50, logging partial failures instead of aborting - PERF-03: CLI lazy loading — reduced sync imports from 37 to 10 core commands, async getCommandsByCategory() for help display ## Test Suite Fixes - Fixed 11 broken import paths in security tests (../../security/ → ../src/ and ../../helpers/ → ../helpers/) - Added vitest.config.ts for security package (was inheriting root config with missing setup.ts reference) - Added allowedDirectories to SecurityConfig test fixture - Fixed sanitizePath test expectation (leading slash removal) - Fixed hardcoded /workspaces/claude-flow path in memory tests - Fixed semanticRouter controller test (auto-enables with agentdb) - Fixed database-provider count test (namespace-scoped assertion) - Fixed swarm latency tests (toBeGreaterThanOrEqual for sub-ms mocks) - Added TypeScript as dev dependency for build verification ## Verification - 22 automated security verification tests in security-verification.test.ts covering all 10 user-facing scenarios - All 5 packages type-check clean (0 errors) - memory: 9/9 suites, 353/353 tests pass - swarm: 4/4 suites, 172/172 tests pass - security: 13/13 suites, 444/444 tests pass - CLI help displays all 35 commands correctly with lazy loading ## New Files - v3/@claude-flow/cli/src/commands/ruvector/pg-utils.ts - v3/@claude-flow/cli/.eslintrc.json - v3/@claude-flow/cli/__tests__/security-verification.test.ts - v3/@claude-flow/memory/src/json-security.ts - v3/@claude-flow/security/vitest.config.ts Co-Authored-By: claude-flow <ruv@ruv.net> * fix: address Sherlock review findings for PR #1558 Finding 2 (MUST FIX): Add formatEmbedding array-element validation - Validate each embedding value is typeof number && Number.isFinite() - Prevents SQL injection via crafted JSON arrays with string payloads - Throws descriptive error with index for debugging Finding 1: Reclassify CRIT-03 eval blocklist as defense-in-depth - Add comment clarifying pattern blocklist is best-effort, not a sandbox - Primary defense is the browser sandbox itself - Update test description to reflect defense-in-depth classification Finding 3: Add behavioral tests alongside source-guards - Add formatEmbedding behavioral test with hostile inputs (NaN, Infinity, strings) - Add CRIT-02 behavioral test verifying execFileSync array argument pattern - Categorize existing tests as source-guard vs behavioral in comments Finding 4: Flag HIGH-02 as behavior change - Add comment explaining === true vs !== false semantic difference - Documents that undefined/null no longer treated as "skip permissions" Finding 6: Export BoundedSet, fix test semantics - Export BoundedSet from gossip.ts for reuse - Fix test to use FIFO semantics matching production (not LRU) - Add FIFO-specific test: re-adding doesn't refresh position Finding 7: Fix BoundedSet comment math - Remove speculative throughput time estimate - Use factual memory calculation: 100K × ~40B IDs ≈ 4MB Finding 5: No change (false positive — Math.abs in manhattanDistance is correct) Co-Authored-By: claude-flow <ruv@ruv.net> --------- Co-authored-by: claude-flow <ruv@ruv.net>
383 lines
12 KiB
TypeScript
383 lines
12 KiB
TypeScript
/**
|
|
* Input Validator Tests
|
|
*
|
|
* Tests verify:
|
|
* - Zod schema validation
|
|
* - Input sanitization
|
|
* - Authentication schemas
|
|
* - Command and path schemas
|
|
*/
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
InputValidator,
|
|
sanitizeString,
|
|
sanitizeHtml,
|
|
sanitizePath,
|
|
SafeStringSchema,
|
|
IdentifierSchema,
|
|
EmailSchema,
|
|
PasswordSchema,
|
|
UUIDSchema,
|
|
HttpsUrlSchema,
|
|
PortSchema,
|
|
UserRoleSchema,
|
|
PermissionSchema,
|
|
LoginRequestSchema,
|
|
CreateUserSchema,
|
|
TaskInputSchema,
|
|
CommandArgumentSchema,
|
|
PathSchema,
|
|
PATTERNS,
|
|
LIMITS,
|
|
} from '../src/input-validator.js';
|
|
|
|
describe('InputValidator', () => {
|
|
describe('SafeStringSchema', () => {
|
|
it('should accept safe strings', () => {
|
|
expect(() => SafeStringSchema.parse('hello world')).not.toThrow();
|
|
});
|
|
|
|
it('should reject empty strings', () => {
|
|
expect(() => SafeStringSchema.parse('')).toThrow();
|
|
});
|
|
|
|
it('should reject strings with shell metacharacters', () => {
|
|
const dangerous = [';', '&&', '||', '|', '`', '$()', '${}', '>', '<'];
|
|
for (const char of dangerous) {
|
|
expect(() => SafeStringSchema.parse(`hello${char}world`)).toThrow();
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('IdentifierSchema', () => {
|
|
it('should accept valid identifiers', () => {
|
|
expect(() => IdentifierSchema.parse('validId')).not.toThrow();
|
|
expect(() => IdentifierSchema.parse('valid-id')).not.toThrow();
|
|
expect(() => IdentifierSchema.parse('valid_id')).not.toThrow();
|
|
expect(() => IdentifierSchema.parse('validId123')).not.toThrow();
|
|
});
|
|
|
|
it('should reject identifiers starting with number', () => {
|
|
expect(() => IdentifierSchema.parse('123invalid')).toThrow();
|
|
});
|
|
|
|
it('should reject identifiers with special characters', () => {
|
|
expect(() => IdentifierSchema.parse('invalid@id')).toThrow();
|
|
expect(() => IdentifierSchema.parse('invalid id')).toThrow();
|
|
});
|
|
|
|
it('should reject empty identifiers', () => {
|
|
expect(() => IdentifierSchema.parse('')).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('EmailSchema', () => {
|
|
it('should accept valid emails', () => {
|
|
expect(() => EmailSchema.parse('user@example.com')).not.toThrow();
|
|
expect(() => EmailSchema.parse('user.name@example.co.uk')).not.toThrow();
|
|
});
|
|
|
|
it('should reject invalid emails', () => {
|
|
expect(() => EmailSchema.parse('notanemail')).toThrow();
|
|
expect(() => EmailSchema.parse('@nodomain.com')).toThrow();
|
|
expect(() => EmailSchema.parse('no@')).toThrow();
|
|
});
|
|
|
|
it('should lowercase emails', () => {
|
|
const result = EmailSchema.parse('USER@EXAMPLE.COM');
|
|
expect(result).toBe('user@example.com');
|
|
});
|
|
|
|
it('should reject too long emails', () => {
|
|
const longEmail = 'a'.repeat(300) + '@example.com';
|
|
expect(() => EmailSchema.parse(longEmail)).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('PasswordSchema', () => {
|
|
it('should accept valid passwords', () => {
|
|
expect(() => PasswordSchema.parse('SecurePass123')).not.toThrow();
|
|
});
|
|
|
|
it('should reject short passwords', () => {
|
|
expect(() => PasswordSchema.parse('Short1')).toThrow();
|
|
});
|
|
|
|
it('should reject passwords without uppercase', () => {
|
|
expect(() => PasswordSchema.parse('lowercase123')).toThrow();
|
|
});
|
|
|
|
it('should reject passwords without lowercase', () => {
|
|
expect(() => PasswordSchema.parse('UPPERCASE123')).toThrow();
|
|
});
|
|
|
|
it('should reject passwords without digits', () => {
|
|
expect(() => PasswordSchema.parse('NoDigitsHere')).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('UUIDSchema', () => {
|
|
it('should accept valid UUIDs', () => {
|
|
expect(() => UUIDSchema.parse('550e8400-e29b-41d4-a716-446655440000')).not.toThrow();
|
|
});
|
|
|
|
it('should reject invalid UUIDs', () => {
|
|
expect(() => UUIDSchema.parse('not-a-uuid')).toThrow();
|
|
expect(() => UUIDSchema.parse('550e8400-e29b-41d4-a716')).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('HttpsUrlSchema', () => {
|
|
it('should accept HTTPS URLs', () => {
|
|
expect(() => HttpsUrlSchema.parse('https://example.com')).not.toThrow();
|
|
expect(() => HttpsUrlSchema.parse('https://example.com/path')).not.toThrow();
|
|
});
|
|
|
|
it('should reject HTTP URLs', () => {
|
|
expect(() => HttpsUrlSchema.parse('http://example.com')).toThrow();
|
|
});
|
|
|
|
it('should reject invalid URLs', () => {
|
|
expect(() => HttpsUrlSchema.parse('not-a-url')).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('PortSchema', () => {
|
|
it('should accept valid ports', () => {
|
|
expect(() => PortSchema.parse(80)).not.toThrow();
|
|
expect(() => PortSchema.parse(443)).not.toThrow();
|
|
expect(() => PortSchema.parse(3000)).not.toThrow();
|
|
expect(() => PortSchema.parse(65535)).not.toThrow();
|
|
});
|
|
|
|
it('should reject invalid ports', () => {
|
|
expect(() => PortSchema.parse(0)).toThrow();
|
|
expect(() => PortSchema.parse(-1)).toThrow();
|
|
expect(() => PortSchema.parse(65536)).toThrow();
|
|
expect(() => PortSchema.parse(3.14)).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('UserRoleSchema', () => {
|
|
it('should accept valid roles', () => {
|
|
expect(() => UserRoleSchema.parse('admin')).not.toThrow();
|
|
expect(() => UserRoleSchema.parse('operator')).not.toThrow();
|
|
expect(() => UserRoleSchema.parse('developer')).not.toThrow();
|
|
expect(() => UserRoleSchema.parse('viewer')).not.toThrow();
|
|
expect(() => UserRoleSchema.parse('service')).not.toThrow();
|
|
});
|
|
|
|
it('should reject invalid roles', () => {
|
|
expect(() => UserRoleSchema.parse('superuser')).toThrow();
|
|
expect(() => UserRoleSchema.parse('root')).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('PermissionSchema', () => {
|
|
it('should accept valid permissions', () => {
|
|
expect(() => PermissionSchema.parse('swarm.create')).not.toThrow();
|
|
expect(() => PermissionSchema.parse('agent.spawn')).not.toThrow();
|
|
expect(() => PermissionSchema.parse('system.admin')).not.toThrow();
|
|
});
|
|
|
|
it('should reject invalid permissions', () => {
|
|
expect(() => PermissionSchema.parse('invalid.permission')).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('LoginRequestSchema', () => {
|
|
it('should accept valid login request', () => {
|
|
expect(() => LoginRequestSchema.parse({
|
|
email: 'user@example.com',
|
|
password: 'password123',
|
|
})).not.toThrow();
|
|
});
|
|
|
|
it('should accept login with MFA code', () => {
|
|
expect(() => LoginRequestSchema.parse({
|
|
email: 'user@example.com',
|
|
password: 'password123',
|
|
mfaCode: '123456',
|
|
})).not.toThrow();
|
|
});
|
|
|
|
it('should reject invalid MFA code length', () => {
|
|
expect(() => LoginRequestSchema.parse({
|
|
email: 'user@example.com',
|
|
password: 'password123',
|
|
mfaCode: '12345', // 5 digits instead of 6
|
|
})).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('CreateUserSchema', () => {
|
|
it('should accept valid user creation', () => {
|
|
expect(() => CreateUserSchema.parse({
|
|
email: 'user@example.com',
|
|
password: 'SecurePass123',
|
|
role: 'developer',
|
|
})).not.toThrow();
|
|
});
|
|
|
|
it('should require strong password', () => {
|
|
expect(() => CreateUserSchema.parse({
|
|
email: 'user@example.com',
|
|
password: 'weak',
|
|
role: 'developer',
|
|
})).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('TaskInputSchema', () => {
|
|
it('should accept valid task input', () => {
|
|
expect(() => TaskInputSchema.parse({
|
|
taskId: '550e8400-e29b-41d4-a716-446655440000',
|
|
content: 'Implement new feature',
|
|
agentType: 'coder',
|
|
})).not.toThrow();
|
|
});
|
|
|
|
it('should reject task with shell characters in content', () => {
|
|
expect(() => TaskInputSchema.parse({
|
|
taskId: '550e8400-e29b-41d4-a716-446655440000',
|
|
content: 'Implement feature; rm -rf /',
|
|
agentType: 'coder',
|
|
})).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('CommandArgumentSchema', () => {
|
|
it('should accept safe arguments', () => {
|
|
expect(() => CommandArgumentSchema.parse('--flag')).not.toThrow();
|
|
expect(() => CommandArgumentSchema.parse('value')).not.toThrow();
|
|
expect(() => CommandArgumentSchema.parse('path/to/file')).not.toThrow();
|
|
});
|
|
|
|
it('should reject arguments with null bytes', () => {
|
|
expect(() => CommandArgumentSchema.parse('arg\x00injected')).toThrow();
|
|
});
|
|
|
|
it('should reject arguments with shell metacharacters', () => {
|
|
expect(() => CommandArgumentSchema.parse('arg;injected')).toThrow();
|
|
expect(() => CommandArgumentSchema.parse('arg&&injected')).toThrow();
|
|
expect(() => CommandArgumentSchema.parse('arg|injected')).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('PathSchema', () => {
|
|
it('should accept valid paths', () => {
|
|
expect(() => PathSchema.parse('/path/to/file.ts')).not.toThrow();
|
|
expect(() => PathSchema.parse('./relative/path')).not.toThrow();
|
|
});
|
|
|
|
it('should reject paths with traversal', () => {
|
|
expect(() => PathSchema.parse('/path/../etc/passwd')).toThrow();
|
|
});
|
|
|
|
it('should reject paths with null bytes', () => {
|
|
expect(() => PathSchema.parse('/path/file\x00.jpg')).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('Sanitization Functions', () => {
|
|
describe('sanitizeString', () => {
|
|
it('should remove null bytes', () => {
|
|
expect(sanitizeString('hello\x00world')).toBe('helloworld');
|
|
});
|
|
|
|
it('should remove HTML brackets', () => {
|
|
expect(sanitizeString('<script>alert(1)</script>')).toBe('scriptalert(1)/script');
|
|
});
|
|
|
|
it('should remove javascript: protocol', () => {
|
|
expect(sanitizeString('javascript:alert(1)')).toBe('alert(1)');
|
|
});
|
|
|
|
it('should trim whitespace', () => {
|
|
expect(sanitizeString(' hello ')).toBe('hello');
|
|
});
|
|
});
|
|
|
|
describe('sanitizeHtml', () => {
|
|
it('should escape HTML entities', () => {
|
|
expect(sanitizeHtml('<script>')).toBe('<script>');
|
|
expect(sanitizeHtml('"quoted"')).toBe('"quoted"');
|
|
expect(sanitizeHtml("'apostrophe'")).toBe(''apostrophe'');
|
|
expect(sanitizeHtml('a & b')).toBe('a & b');
|
|
});
|
|
});
|
|
|
|
describe('sanitizePath', () => {
|
|
it('should remove null bytes', () => {
|
|
expect(sanitizePath('/path\x00/file')).toBe('path/file');
|
|
});
|
|
|
|
it('should remove traversal patterns', () => {
|
|
// '../etc/passwd' → remove '..' → '/etc/passwd' → remove leading '/' → 'etc/passwd'
|
|
expect(sanitizePath('../etc/passwd')).toBe('etc/passwd');
|
|
});
|
|
|
|
it('should normalize slashes', () => {
|
|
expect(sanitizePath('/path//to///file')).toBe('path/to/file');
|
|
});
|
|
|
|
it('should remove leading slash', () => {
|
|
expect(sanitizePath('/absolute/path')).toBe('absolute/path');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('InputValidator Class', () => {
|
|
it('should validate email', () => {
|
|
expect(InputValidator.validateEmail('user@example.com')).toBe('user@example.com');
|
|
});
|
|
|
|
it('should validate password', () => {
|
|
expect(() => InputValidator.validatePassword('SecurePass123')).not.toThrow();
|
|
});
|
|
|
|
it('should validate identifier', () => {
|
|
expect(InputValidator.validateIdentifier('myId')).toBe('myId');
|
|
});
|
|
|
|
it('should validate path', () => {
|
|
expect(InputValidator.validatePath('/valid/path')).toBe('/valid/path');
|
|
});
|
|
|
|
it('should validate command argument', () => {
|
|
expect(InputValidator.validateCommandArg('--flag')).toBe('--flag');
|
|
});
|
|
|
|
it('should validate login request', () => {
|
|
const result = InputValidator.validateLoginRequest({
|
|
email: 'USER@example.com',
|
|
password: 'password',
|
|
});
|
|
expect(result.email).toBe('user@example.com');
|
|
});
|
|
|
|
it('should safely parse with result', () => {
|
|
const success = InputValidator.safeParse(EmailSchema, 'user@example.com');
|
|
expect(success.success).toBe(true);
|
|
|
|
const failure = InputValidator.safeParse(EmailSchema, 'invalid');
|
|
expect(failure.success).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('Constants', () => {
|
|
it('should export PATTERNS', () => {
|
|
expect(PATTERNS.SAFE_IDENTIFIER).toBeDefined();
|
|
expect(PATTERNS.SAFE_FILENAME).toBeDefined();
|
|
expect(PATTERNS.NO_SHELL_CHARS).toBeDefined();
|
|
});
|
|
|
|
it('should export LIMITS', () => {
|
|
expect(LIMITS.MIN_PASSWORD_LENGTH).toBe(8);
|
|
expect(LIMITS.MAX_PASSWORD_LENGTH).toBe(128);
|
|
expect(LIMITS.MAX_PATH_LENGTH).toBe(4096);
|
|
});
|
|
});
|
|
});
|