Skip to content

Latest commit

 

History

History
253 lines (206 loc) · 6.85 KB

File metadata and controls

253 lines (206 loc) · 6.85 KB
Error in user YAML: (<unknown>): did not find expected alphabetic or numeric character while scanning an alias at line 2 column 14
---
title: Security Best Practices
description: *[Guides](../guides/readme.md) > Security*
type: guide
status: stable
---

Security Best Practices

Guides > Security

This document outlines the security measures implemented in the VisionFlow multi-agent system and provides guidelines for secure deployment and usage.

Table of Contents

  1. Environment Variables
  2. Authentication
  3. WebSocket Security
  4. TCP Server Security
  5. Input Validation
  6. Rate Limiting
  7. CORS Configuration
  8. Deployment Guidelines
  9. Security Checklist

Environment Variables

Secure Storage

  • NEVER commit .env files to version control
  • Use .env.example as a template for required variables
  • Store sensitive credentials in environment-specific secret managers
  • Rotate API keys and tokens regularly

Required Security Variables

# Authentication
JWT-SECRET=<strong-random-secret>
SESSION-SECRET=<strong-random-secret>
WS-AUTH-TOKEN=<secure-websocket-token>

# Rate Limiting
RATE-LIMIT-WINDOW-MS=60000
RATE-LIMIT-MAX-REQUESTS=100

# Connection Limits
WS-MAX-CONNECTIONS=100
TCP-MAX-CONNECTIONS=50
WS-CONNECTION-TIMEOUT=300000
TCP-CONNECTION-TIMEOUT=300000

Authentication

WebSocket Authentication

The WebSocket server implements token-based authentication:

  1. Enable Authentication: Set WS-AUTH-ENABLED=true
  2. Configure Token: Set a secure WS-AUTH-TOKEN
  3. Client Connection: Include token in Authorisation header
    const ws = new WebSocket('ws://localhost:3002', {
      headers: {
        'Authorization': `Bearer ${token}`
      }
    });

TCP Server Authentication

The TCP server requires authentication after connection:

  1. Enable Authentication: Configure auth token in environment
  2. Authentication Flow:
    {
      "jsonrpc": "2.0",
      "id": 1,
      "method": "authenticate",
      "params": {
        "token": "your-secure-token"
      }
    }

WebSocket Security

Connection Security

  • IP Blocking: Automatic blocking of suspicious IPs
  • Connection Limits: Maximum concurrent connections enforced
  • Timeout Management: Idle connections automatically closed
  • Rate Limiting: Per-IP request throttling

Implementation Details

// WebSocket server configuration
const wss = new WebSocket.Server({
  verifyClient: (info, cb) => {
    // Authentication check
    // IP blocking check
    // Connection limit check
    // Rate limit check
  }
});

TCP Server Security

Security Features

  • Persistent Connection Management: Single MCP instance with secure client isolation
  • Authentication Required: All non-initialisation requests require authentication
  • Input Validation: All incoming data validated and sanitised
  • Connection Timeouts: Automatic cleanup of idle connections

Connection Flow

  1. Client connects to TCP server
  2. Server checks IP blocking and rate limits
  3. Client must authenticate within timeout period
  4. All subsequent requests are validated

Input Validation

Validation Rules

  1. Size Limits: Maximum request size enforced (default: 10MB)
  2. JSON-RPC Validation: Structure and version checks
  3. Content Sanitisation: Script injection prevention
  4. Prototype Pollution Protection: Key filtering

Example Validation

// Input validation implementation
validateInput(input) {
  // Size check
  if (input.length > MAX-REQUEST-SIZE) {
    return { valid: false, error: 'Input too large' };
  }

  // JSON parsing and validation
  // Sanitisation
  // Return validated and sanitised content
}

Rate Limiting

Configuration

  • Window Size: Configurable time window (default: 60 seconds)
  • Request Limit: Maximum requests per window (default: 100)
  • IP-Based: Rate limiting applied per IP address
  • Automatic Blocking: IPs exceeding limits are temporarily blocked

Implementation

// Rate limiter tracks requests per IP
checkRateLimit(clientId) {
  // Check requests within time window
  // Block if limit exceeded
  // Clean up old request data
}

CORS Configuration

Allowed Origins

Configure allowed origins in environment:

CORS-ALLOWED-ORIGINS=http://localhost:3000,https://yourdomain.com

Headers Configuration

  • Methods: GET, POST, PUT, DELETE, OPTIONS
  • Headers: Content-Type, Authorisation
  • Credentials: Configure based on requirements

Deployment Guidelines

Production Deployment

  1. Use HTTPS/WSS: Always use encrypted connections in production
  2. Reverse Proxy: Deploy behind nginx or similar
  3. Firewall Rules: Restrict access to necessary ports only
  4. Secret Management: Use AWS Secrets Manager, HashiCorp Vault, etc.
  5. Monitoring: Implement security event logging and alerting

Docker Security

# docker-compose.yml security settings
services:
  multi-agent:
    security-opt:
      - no-new-privileges:true
    read-only: true
    tmpfs:
      - /tmp
    cap-drop:
      - ALL
    cap-add:
      - DAC-OVERRIDE

Network Security

  1. Internal Networks: Use Docker internal networks
  2. Port Exposure: Only expose necessary ports
  3. Service Isolation: Separate services by security requirements

Security Checklist

Pre-Deployment

  • All environment variables configured
  • Strong secrets generated (minimum 32 characters)
  • .env file not in version control
  • Authentication enabled for all services
  • Rate limiting configured
  • CORS origins restricted

Deployment

  • HTTPS/WSS enabled
  • Firewall rules configured
  • Monitoring and logging enabled
  • Backup procedures in place
  • Incident response plan documented

Post-Deployment

  • Regular security audits
  • Dependency updates
  • Log monitoring
  • API key rotation schedule
  • Performance monitoring

Security Incident Response

Immediate Actions

  1. Identify: Detect and classify the incident
  2. Contain: Isolate affected systems
  3. Investigate: Analyse logs and determine scope
  4. Remediate: Fix vulnerabilities and remove threats
  5. Document: Record incident details and lessons learnt

Contact Information

Additional Resources

Related Documentation


Remember: Security is a continuous process, not a one-time configuration. Regular reviews and updates are essential.