// init-security.test.js - Security validation tests for init operations
import { describe, it, expect, beforeEach, afterEach, jest } from '@jest/globals';
import { promises as fs } from 'fs';
import { initCommand } from '../../src/cli/simple-commands/init/index.js';
const TEST_DIR = '/tmp/claude-flow-security-test';
describe('Init Security Tests', () => {
beforeEach(async () => {
try {
await fs.rm(TEST_DIR, { recursive: true, force: true });
} catch (err) {
// Directory might not exist
}
await fs.mkdir(TEST_DIR, { recursive: true });
process.chdir(TEST_DIR);
});
afterEach(async () => {
try {
await fs.rm(TEST_DIR, { recursive: true, force: true });
} catch (err) {
// Ignore cleanup errors
}
});
describe('Path Traversal Prevention', () => {
it('should not create files outside target directory', async () => {
// Mock a malicious template that tries path traversal
const maliciousTemplate = jest.fn(() => '# Malicious content');
jest.doMock('../../src/cli/simple-commands/init/templates/claude-md.js', () => ({
createOptimizedSparcClaudeMd: maliciousTemplate,
createFullClaudeMd: maliciousTemplate,
}));
// Mock writeFile to track file paths
const originalWriteFile = fs.writeFile;
const writePaths = [];
fs.writeFile = jest.fn(async (path, content, options) => {
writePaths.push(path);
// Only allow writes within TEST_DIR
if (!path.startsWith(TEST_DIR) && !path.startsWith('.')) {
throw new Error('Path traversal attempted');
}
return originalWriteFile(path, content, options);
});
await initCommand([], {});
// Restore original function
fs.writeFile = originalWriteFile;
// Verify no path traversal occurred
const suspiciousPaths = writePaths.filter(path =>
path.includes('..') || path.startsWith('/')
);
expect(suspiciousPaths).toHaveLength(0);
});
it('should sanitize directory names', async () => {
// Test with potentially dangerous directory names
const dangerousNames = [
'../../../etc/passwd',
'..\\..\\windows\\system32',
'/etc/shadow',
'con', // Windows reserved name
'aux', // Windows reserved name
'nul', // Windows reserved name
];
for (const name of dangerousNames) {
try {
// This should either sanitize the name or fail safely
await fs.mkdir(name, { recursive: true });
// If it succeeded, verify it didn't create outside our test dir
const realPath = await fs.realpath(name);
expect(realPath).toContain(TEST_DIR);
} catch (err) {
// Failing is acceptable for dangerous names
expect(err).toBeDefined();
}
}
});
});
describe('File Permissions and Ownership', () => {
it('should create files with appropriate permissions', async () => {
await initCommand([], {});
// Check permissions on created files
const files = [
'CLAUDE.md',
'.claude/settings.json',
'memory/claude-flow@alpha-data.json',
];
for (const file of files) {
const stats = await fs.stat(file);
const mode = stats.mode & parseInt('777', 8);
// Should be readable and writable by owner, readable by group/others
expect(mode & parseInt('600', 8)).toBe(parseInt('600', 8)); // Owner read/write
expect(mode & parseInt('007', 8)).toBe(0); // No execute for others on data files
}
});
it('should create executable files with correct permissions', async () => {
await initCommand([], {});
// Check executable files (if any are created)
try {
const helperFiles = await fs.readdir('.claude/helpers');
for (const helper of helperFiles) {
if (helper.endsWith('.sh')) {
const stats = await fs.stat(`.claude/helpers/${helper}`);
const mode = stats.mode & parseInt('777', 8);
// Should be executable by owner
expect(mode & parseInt('100', 8)).toBe(parseInt('100', 8));
}
}
} catch (err) {
// Helpers directory might not exist
}
});
it('should not create world-writable files', async () => {
await initCommand([], {});
// Recursively check all created files
async function checkPermissions(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = `${dir}/${entry.name}`;
const stats = await fs.stat(fullPath);
const mode = stats.mode & parseInt('777', 8);
// Should not be world-writable
expect(mode & parseInt('002', 8)).toBe(0);
if (entry.isDirectory()) {
await checkPermissions(fullPath);
}
}
}
await checkPermissions('.');
});
});
describe('Content Sanitization', () => {
it('should not include sensitive information in generated files', async () => {
await initCommand([], {});
const sensitivePatterns = [
/password\s*[:=]\s*['"][^'"]+['"]/i,
/api_key\s*[:=]\s*['"][^'"]+['"]/i,
/secret\s*[:=]\s*['"][^'"]+['"]/i,
/token\s*[:=]\s*['"][^'"]+['"]/i,
/private_key\s*[:=]/i,
];
const filesToCheck = [
'CLAUDE.md',
'.claude/settings.json',
'.mcp.json',
];
for (const file of filesToCheck) {
const content = await fs.readFile(file, 'utf8');
for (const pattern of sensitivePatterns) {
expect(content).not.toMatch(pattern);
}
}
});
it('should escape special characters in generated content', async () => {
await initCommand([], {});
const configFiles = [
'.claude/settings.json',
'.mcp.json',
'memory/claude-flow@alpha-data.json',
];
for (const file of configFiles) {
const content = await fs.readFile(file, 'utf8');
// Should be valid JSON (no unescaped characters)
expect(() => JSON.parse(content)).not.toThrow();
}
});
it('should not include code injection vectors', async () => {
await initCommand([], {});
const scriptPatterns = [
/';
process.env.USER_NAME = '$(whoami)';
await initCommand([], {});
const files = [
'CLAUDE.md',
'.claude/settings.json',
];
for (const file of files) {
const content = await fs.readFile(file, 'utf8');
// Should not contain unescaped environment variable values
expect(content).not.toContain('');
expect(content).not.toContain('$(whoami)');
}
// Clean up
delete process.env.PROJECT_NAME;
delete process.env.USER_NAME;
});
});
describe('Temporary File Security', () => {
it('should create temporary files with secure permissions', async () => {
await initCommand([], {});
// Check for any temporary files created
const tmpFiles = await fs.readdir('/tmp').catch(() => []);
const claudeTmpFiles = tmpFiles.filter(f => f.includes('claude-flow'));
for (const tmpFile of claudeTmpFiles) {
const stats = await fs.stat(`/tmp/${tmpFile}`).catch(() => null);
if (stats) {
const mode = stats.mode & parseInt('777', 8);
// Temporary files should not be world-readable
expect(mode & parseInt('044', 8)).toBe(0);
}
}
});
it('should clean up temporary files on completion', async () => {
const beforeTmpFiles = await fs.readdir('/tmp').catch(() => []);
await initCommand([], {});
const afterTmpFiles = await fs.readdir('/tmp').catch(() => []);
const newTmpFiles = afterTmpFiles.filter(f =>
!beforeTmpFiles.includes(f) && f.includes('claude-flow')
);
// Should not leave temporary files behind
expect(newTmpFiles).toHaveLength(0);
});
});
});