We've implemented critical security enhancements to the MAGI patch management system to protect against common vulnerabilities while maintaining the autonomous functionality.
Before:
execSync(`git commit -m "${commitMessage}"`, { stdio: 'pipe' });After:
// Sanitize commit message
const sanitizedMessage = patch.commit_message
.replace(/[^a-zA-Z0-9\s\-_.,!?:;()\[\]{}'"#@/]/g, '')
.trim()
.substring(0, 1000);
// Write to temp file to avoid shell injection
const commitFile = path.join('/tmp', `commit-msg-${patchId}-${Date.now()}.txt`);
fs.writeFileSync(commitFile, commitMessage, { mode: 0o600 });
execSync(`git commit -F "${commitFile}"`, { stdio: 'pipe' });Before:
const tmpFile = path.join('/tmp', `patch-${patchId}.patch`);After:
const randomId = crypto.randomBytes(16).toString('hex');
const tmpFile = path.join('/tmp', `patch-${patchId}-${randomId}.patch`);
fs.writeFileSync(tmpFile, patch.patch_content, { mode: 0o600 });Added validation to all endpoints:
const validIdPattern = /^[a-zA-Z0-9_-]+$/;
if (!validIdPattern.test(processId) || !validIdPattern.test(projectId)) {
return res.status(400).json({
success: false,
error: 'Invalid ID format',
});
}Added to assessPatchRisk():
- Binary file detection
- Security pattern detection (chmod 777, NOPASSWD, etc.)
- Critical path detection (.env, docker-compose.yml, CI/CD files)
- Increased risk scores for dangerous patterns
Created new modules:
patch_security.ts- Core security utilitiespatch_monitor.ts- Real-time anomaly detectionpatch_rollback.ts- Automatic rollback system
- ID format validation (alphanumeric + dash/underscore)
- Path normalization
- Size limits
- Pattern detection
- Cryptographically random filenames
- Restricted permissions (0600)
- Automatic cleanup in finally blocks
- Safe commit message handling
- Never auto-merge patches with security issues
- Enhanced risk scoring
- Configurable policies per project type
class PatchMonitor {
// Detects:
// - High patch volume (>50/hour)
// - High failure rates (>30%)
// - Suspicious patterns
// - Rapid status changes
}- Creates rollback points before patches
- Monitors health after application
- Automatic revert on failures
- Configurable thresholds
- Complete trail of all operations
- Structured logging
- IP address tracking
- Success/failure recording
-- Add security fields to patches table
ALTER TABLE patches
ADD COLUMN patch_signature VARCHAR(64),
ADD COLUMN security_issues JSONB;
-- Audit log table
CREATE TABLE patch_audit_log (
id BIGSERIAL PRIMARY KEY,
timestamp TIMESTAMPTZ,
action VARCHAR(50),
patch_id BIGINT,
user_id TEXT,
success BOOLEAN,
details TEXT,
ip_address INET
);
-- Rate limiting
CREATE TABLE patch_rate_limits (
identifier VARCHAR(255),
request_count INT,
window_start TIMESTAMPTZ
);
-- Distributed locks
CREATE TABLE patch_locks (
lock_key VARCHAR(255),
holder VARCHAR(255),
expires_at TIMESTAMPTZ
);# Security settings
MAX_PATCHES_PER_HOUR=50
MAX_USER_PATCHES_PER_HOUR=20
MAX_FAILURE_RATE=0.3
# Risk thresholds
LOW_RISK_MAX=0.25
MOD_RISK_MAX=0.55
HIGH_RISK_MAX=0.75
# Auto-merge policies
AUTO_MERGE_MAGI_PROJECTS=all
AUTO_MERGE_EXISTING_PROJECTS=low_risk# Try to inject commands in commit message
curl -X POST http://localhost:3010/api/patches/1/apply \
-H "Content-Type: application/json" \
-d '{"projectId": "test", "processId": "test$(whoami)"}'
# Result: Invalid ID format error# Try to escape project directory
curl -X POST http://localhost:3010/api/patches/1/apply \
-H "Content-Type: application/json" \
-d '{"projectId": "../../../etc", "processId": "passwd"}'
# Result: Invalid ID format error# Submit patch >5MB
# Result: Patch validation fails with "Patch too large"# Submit patch with DROP TABLE
# Result: High risk score, no auto-merge- Security: Protected against common web vulnerabilities
- Reliability: Automatic recovery from failures
- Auditability: Complete logging for compliance
- Performance: Rate limiting prevents abuse
- Autonomy: Self-monitoring and self-healing
- Sandboxing: Run patches in isolated environment first
- Static Analysis: Scan patch content for vulnerabilities
- Machine Learning: Improve anomaly detection
- Integration: Connect with security scanners
The patch management system now has multiple layers of security while maintaining its autonomous capabilities. The system can safely operate with minimal human intervention while protecting against both accidental and malicious issues.