Skip to content

Security: cloudbadal007/universal-agent-connector

Security

SECURITY.md

Security Guide - AI Agent Connector

Overview

This document provides security best practices, mitigation strategies, and guidelines for securing the AI Agent Connector system. It complements the Threat Model by providing actionable security measures.

Table of Contents

  1. Authentication and Authorization
  2. Data Protection
  3. API Security
  4. Database Security
  5. Web Interface Security
  6. Deployment Security
  7. Monitoring and Incident Response
  8. Compliance and Best Practices
  9. Bug Bounty Program

Authentication and Authorization

API Key Management

Best Practices

  1. Generate Strong API Keys

    # Use cryptographically secure random generation
    import secrets
    api_key = secrets.token_urlsafe(32)  # 32 bytes = 43 characters
  2. Store API Keys Securely

    • Hash API keys using bcrypt or Argon2
    • Never store plaintext API keys
    • Use environment variables or secret management services
  3. Implement API Key Rotation

    # Example: Rotate keys every 90 days
    def rotate_api_key(agent_id: str):
        new_key = generate_api_key()
        # Revoke old key
        revoke_api_key(agent_id, old_key)
        # Issue new key
        issue_api_key(agent_id, new_key)
        return new_key
  4. Enforce Key Expiration

    • Set expiration dates for API keys
    • Implement automatic key rotation
    • Notify users before expiration
  5. Rate Limiting per API Key

    # Implement rate limiting
    from flask_limiter import Limiter
    
    limiter = Limiter(
        app,
        key_func=lambda: request.headers.get('X-API-Key'),
        default_limits=["1000 per hour", "100 per minute"]
    )

Mitigation Strategies

  • HTTPS Enforcement: Require TLS 1.2+ for all API communications
  • Key Scoping: Limit API key permissions to minimum required
  • Audit Logging: Log all API key usage and authentication attempts
  • Revocation: Immediate revocation capability for compromised keys
  • Key Rotation Policy: Rotate keys every 90 days or after security incidents

Agent Authentication

Implementation Guidelines

  1. Validate Agent IDs

    # Use UUIDs for agent IDs
    import uuid
    agent_id = str(uuid.uuid4())
    
    # Validate format
    def validate_agent_id(agent_id: str) -> bool:
        try:
            uuid.UUID(agent_id)
            return True
        except ValueError:
            return False
  2. Implement Multi-Factor Authentication (MFA) for Admins

    • Require MFA for admin operations
    • Use TOTP (Time-based One-Time Password) or hardware tokens
    • Store MFA secrets securely
  3. Session Management

    # Secure session configuration
    app.config['SESSION_COOKIE_SECURE'] = True  # HTTPS only
    app.config['SESSION_COOKIE_HTTPONLY'] = True  # Prevent XSS
    app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'  # CSRF protection
    app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=8)

Data Protection

Encryption

At Rest

  1. Database Credentials Encryption

    # Use Fernet symmetric encryption
    from cryptography.fernet import Fernet
    
    # Generate key (do this once, store securely)
    key = Fernet.generate_key()
    
    # Encrypt credentials
    def encrypt_credentials(credentials: str, key: bytes) -> bytes:
        f = Fernet(key)
        return f.encrypt(credentials.encode())
    
    # Decrypt credentials
    def decrypt_credentials(encrypted: bytes, key: bytes) -> str:
        f = Fernet(key)
        return f.decrypt(encrypted).decode()
  2. Encryption Key Management

    • DO NOT hardcode encryption keys
    • Use environment variables or secret management services
    • Rotate encryption keys regularly
    • Store keys separately from encrypted data
  3. Secret Management Services

    • AWS: AWS Secrets Manager
    • Azure: Azure Key Vault
    • GCP: Google Secret Manager
    • HashiCorp: Vault

In Transit

  1. TLS Configuration

    # Enforce TLS 1.2+
    import ssl
    
    context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    context.minimum_version = ssl.TLSVersion.TLSv1_2
    context.set_ciphers('HIGH:!aNULL:!eNULL')
  2. Database Connection Security

    • Always use TLS for database connections
    • Verify SSL certificates
    • Use connection strings with SSL parameters

Credential Handling

Best Practices

  1. Never Log Credentials

    # BAD: Logging credentials
    logger.info(f"Connecting with password: {password}")
    
    # GOOD: Mask credentials
    logger.info(f"Connecting to database: {host}:{port}")
  2. Mask Credentials in Responses

    def mask_credentials(config: dict) -> dict:
        masked = config.copy()
        if 'password' in masked:
            masked['password'] = '***'
        if 'connection_string' in masked:
            # Mask password in connection string
            masked['connection_string'] = mask_connection_string(
                masked['connection_string']
            )
        return masked
  3. Credential Rotation

    • Support credential rotation without service disruption
    • Validate new credentials before switching
    • Keep old credentials temporarily for rollback

API Security

Input Validation

SQL Injection Prevention

  1. Always Use Parameterized Queries

    # BAD: String concatenation
    query = f"SELECT * FROM users WHERE id = {user_id}"
    
    # GOOD: Parameterized query
    query = "SELECT * FROM users WHERE id = %s"
    params = (user_id,)
    connector.execute_query(query, params)
  2. Validate and Sanitize Inputs

    import re
    
    def validate_sql_input(value: str) -> bool:
        # Check for SQL injection patterns
        dangerous_patterns = [
            r"(\bUNION\b|\bSELECT\b|\bINSERT\b|\bDELETE\b|\bDROP\b)",
            r"(--|;|\*|')",
            r"(\bor\b|\band\b)\s+\d+\s*=\s*\d+"
        ]
        for pattern in dangerous_patterns:
            if re.search(pattern, value, re.IGNORECASE):
                return False
        return True
  3. Query Validation

    # Validate query structure before execution
    def validate_query(query: str) -> tuple[bool, str]:
        # Parse query
        # Check for dangerous operations
        dangerous_ops = ['DROP', 'ALTER', 'TRUNCATE', 'CREATE', 'GRANT']
        query_upper = query.upper().strip()
        for op in dangerous_ops:
            if query_upper.startswith(op):
                return False, f"Dangerous operation not allowed: {op}"
        return True, ""
  4. Query Complexity Limits

    # Limit query complexity
    MAX_QUERY_LENGTH = 10000
    MAX_JOINS = 10
    MAX_SUBQUERIES = 5
    
    def check_query_complexity(query: str) -> bool:
        if len(query) > MAX_QUERY_LENGTH:
            return False
        # Count joins, subqueries, etc.
        return True

Rate Limiting

Implementation

from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

# Global rate limiting
limiter = Limiter(
    app,
    key_func=get_remote_address,
    default_limits=["1000 per hour", "100 per minute"]
)

# Per-endpoint rate limiting
@app.route('/api/agents/<agent_id>/query', methods=['POST'])
@limiter.limit("60 per minute")  # 60 queries per minute
def execute_query(agent_id: str):
    # ...

Multi-Layer Rate Limiting

  1. IP-based: Limit requests per IP address
  2. API Key-based: Limit requests per API key
  3. Agent-based: Limit requests per agent ID
  4. Endpoint-based: Different limits for different endpoints

CORS Configuration

from flask_cors import CORS

# Restrict CORS to trusted origins
CORS(app, origins=[
    "https://trusted-domain.com",
    "https://app.example.com"
])

Database Security

Connection Security

  1. Use Least Privilege Database Accounts

    -- Create read-only user
    CREATE USER agent_readonly WITH PASSWORD 'secure_password';
    GRANT SELECT ON ALL TABLES IN SCHEMA public TO agent_readonly;
    
    -- Create read-write user (if needed)
    CREATE USER agent_rw WITH PASSWORD 'secure_password';
    GRANT SELECT, INSERT, UPDATE ON ALL TABLES IN SCHEMA public TO agent_rw;
  2. Enable SSL/TLS for Database Connections

    # PostgreSQL with SSL
    connection_string = (
        "postgresql://user:pass@host:5432/db"
        "?sslmode=require"
    )
  3. Connection Pooling Security

    # Limit pool size to prevent exhaustion
    pool_config = {
        'min_size': 2,
        'max_size': 10,
        'max_queries': 50000,
        'max_inactive_connection_lifetime': 300
    }

Query Security

  1. Enforce Query Timeouts

    # Set query timeout
    connector.execute_query(
        query,
        params,
        timeout=30  # 30 seconds
    )
  2. Limit Result Set Size

    MAX_RESULT_ROWS = 10000
    
    def execute_query_safe(query: str, params: tuple):
        # Add LIMIT if not present
        if 'LIMIT' not in query.upper():
            query = f"{query} LIMIT {MAX_RESULT_ROWS}"
        return connector.execute_query(query, params)
  3. Row-Level Security (RLS)

    # Apply RLS rules
    from ai_agent_connector.app.utils.row_level_security import RowLevelSecurity
    
    rls = RowLevelSecurity()
    rls.add_rule(
        agent_id="agent-001",
        table="users",
        rule_type=RLSRuleType.FILTER,
        condition="user_id = current_user_id()"
    )
  4. Column Masking

    # Mask sensitive columns
    from ai_agent_connector.app.utils.column_masking import ColumnMasker
    
    masker = ColumnMasker()
    masker.add_rule(
        table="users",
        column="ssn",
        masking_type=MaskingType.HASH
    )

Web Interface Security

Cross-Site Scripting (XSS) Prevention

  1. Input Sanitization

    from markupsafe import escape
    
    # Escape user input
    user_input = escape(request.form.get('input'))
  2. Output Encoding

    {# In Jinja2 templates #}
    {{ user_input|e }}  {# Escape output #}
  3. Content Security Policy (CSP)

    @app.after_request
    def set_security_headers(response):
        response.headers['Content-Security-Policy'] = (
            "default-src 'self'; "
            "script-src 'self' 'unsafe-inline'; "
            "style-src 'self' 'unsafe-inline';"
        )
        return response

Cross-Site Request Forgery (CSRF) Protection

from flask_wtf.csrf import CSRFProtect

csrf = CSRFProtect(app)

# In forms
<form method="POST">
    {{ csrf_token() }}
    <!-- form fields -->
</form>

Security Headers

@app.after_request
def set_security_headers(response):
    response.headers['X-Content-Type-Options'] = 'nosniff'
    response.headers['X-Frame-Options'] = 'DENY'
    response.headers['X-XSS-Protection'] = '1; mode=block'
    response.headers['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'
    response.headers['Referrer-Policy'] = 'strict-origin-when-cross-origin'
    return response

Deployment Security

Environment Configuration

  1. Environment Variables

    # .env file (DO NOT commit to version control)
    ENCRYPTION_KEY=<generated-key>
    SECRET_KEY=<flask-secret-key>
    DATABASE_URL=<connection-string>
    OPENAI_API_KEY=<api-key>
    FLASK_ENV=production
  2. Secret Management

    # Use secret management service
    import boto3
    
    def get_secret(secret_name: str) -> str:
        client = boto3.client('secretsmanager')
        response = client.get_secret_value(SecretId=secret_name)
        return response['SecretString']

Serverless Deployment Security

  1. AWS Lambda

    # template.yaml
    Resources:
      ApiFunction:
        Type: AWS::Serverless::Function
        Properties:
          Environment:
            Variables:
              ENCRYPTION_KEY: !Ref EncryptionKeySecret
          Policies:
            - SecretsManagerReadWrite:
                SecretArn: !Ref DatabaseSecret
  2. IAM Roles

    • Use least privilege principle
    • Grant only necessary permissions
    • Use separate roles for different functions
  3. VPC Configuration

    • Deploy Lambda in VPC for database access
    • Use security groups to restrict access
    • Use private subnets for databases

Container Security

  1. Dockerfile Security

    # Use non-root user
    RUN useradd -m -u 1000 appuser
    USER appuser
    
    # Don't expose secrets
    # Use secrets management
  2. Image Scanning

    • Scan images for vulnerabilities
    • Use minimal base images
    • Keep images updated

Monitoring and Incident Response

Security Monitoring

  1. Audit Logging

    # Log all security-relevant events
    audit_logger.log(
        action_type=ActionType.AUTHENTICATION_FAILED,
        agent_id=None,
        status="error",
        details={
            "ip": request.remote_addr,
            "user_agent": request.headers.get('User-Agent'),
            "reason": "Invalid API key"
        }
    )
  2. Security Event Monitoring

    # Monitor for suspicious activity
    security_monitor.check_for_anomalies(
        agent_id=agent_id,
        action_type="query_execution",
        metadata={"query": query, "tables": tables}
    )
  3. Alerting

    # Alert on security events
    if failed_attempts > 5:
        alert_manager.send_alert(
            severity=AlertSeverity.HIGH,
            message="Multiple failed authentication attempts",
            details={"ip": ip, "count": failed_attempts}
        )

Incident Response

  1. Incident Response Plan

    • Document incident response procedures
    • Define roles and responsibilities
    • Establish communication channels
    • Create runbooks for common incidents
  2. Key Revocation Procedure

    def revoke_compromised_key(agent_id: str):
        # Revoke API key
        agent_registry.revoke_agent(agent_id)
        
        # Log incident
        audit_logger.log(
            action_type=ActionType.AGENT_REVOKED,
            agent_id=agent_id,
            status="success",
            details={"reason": "Security incident"}
        )
        
        # Alert security team
        alert_manager.send_alert(
            severity=AlertSeverity.CRITICAL,
            message=f"Agent {agent_id} revoked due to security incident"
        )
  3. Forensics and Investigation

    • Preserve audit logs
    • Document timeline of events
    • Analyze attack vectors
    • Implement additional mitigations

Compliance and Best Practices

Security Best Practices Checklist

Authentication

  • Strong API key generation (32+ bytes)
  • API key hashing and secure storage
  • API key rotation policy (90 days)
  • MFA for admin accounts
  • Session timeout configuration
  • Secure session cookies

Data Protection

  • Encryption at rest for credentials
  • Encryption in transit (TLS 1.2+)
  • Secret management service integration
  • Credential masking in logs/responses
  • Encryption key rotation

API Security

  • Parameterized queries (SQL injection prevention)
  • Input validation and sanitization
  • Rate limiting (multi-layer)
  • Query complexity limits
  • Result size limits
  • CORS configuration

Database Security

  • Least privilege database accounts
  • SSL/TLS for database connections
  • Connection pool limits
  • Query timeouts
  • Row-level security
  • Column masking

Web Interface Security

  • XSS prevention (input sanitization, output encoding)
  • CSRF protection
  • Security headers (CSP, X-Frame-Options, etc.)
  • Secure cookie configuration

Deployment Security

  • Environment variable security
  • Secret management
  • IAM role configuration (least privilege)
  • VPC and network security
  • Container security
  • Image vulnerability scanning

Monitoring

  • Comprehensive audit logging
  • Security event monitoring
  • Alerting for security events
  • Incident response plan
  • Log retention policy

Compliance Considerations

  1. GDPR (General Data Protection Regulation)

    • Data residency rules
    • Data retention policies
    • Right to deletion
    • Audit log anonymization
  2. SOC 2

    • Access controls
    • Audit logging
    • Change management
    • Incident response
  3. PCI DSS (if handling payment data)

    • Encryption requirements
    • Access controls
    • Network segmentation
    • Regular security testing

Regular Security Activities

  1. Quarterly

    • Threat model review
    • Security control assessment
    • Access review
    • Security training
  2. Annually

    • Penetration testing
    • Security audit
    • Disaster recovery testing
    • Compliance review
  3. Ongoing

    • Vulnerability scanning
    • Dependency updates
    • Security monitoring
    • Incident response

Security Resources

Internal Resources

External Resources


Bug Bounty Program

We value the security research community and encourage responsible disclosure of security vulnerabilities. This bug bounty program provides recognition and rewards for security researchers who help improve the security of the AI Agent Connector.

Program Overview

The AI Agent Connector Bug Bounty Program is designed to:

  • Encourage responsible disclosure of security vulnerabilities
  • Recognize and reward security researchers
  • Improve the overall security posture of the system
  • Build a collaborative relationship with the security community

Scope

In Scope

The following are eligible for bug bounty rewards:

Web Application

  • Authentication and authorization bypasses
  • SQL injection vulnerabilities
  • Cross-site scripting (XSS)
  • Cross-site request forgery (CSRF)
  • Server-side request forgery (SSRF)
  • Remote code execution (RCE)
  • Insecure direct object references (IDOR)
  • Privilege escalation
  • Information disclosure
  • API security vulnerabilities

Infrastructure

  • Configuration vulnerabilities
  • Secrets exposure
  • Network security issues
  • Deployment misconfigurations

Cryptography

  • Weak encryption implementation
  • Key management vulnerabilities
  • Certificate validation issues

Out of Scope

The following are NOT eligible for bug bounty rewards:

  • Denial of Service (DoS) attacks
  • Distributed Denial of Service (DDoS) attacks
  • Social engineering attacks
  • Physical security issues
  • Issues requiring physical access to devices
  • Spam or phishing attacks
  • Issues in third-party applications or services
  • Issues in dependencies (report to maintainers)
  • Self-XSS (user must be logged in and perform action)
  • Clickjacking on pages without sensitive actions
  • Missing security headers without demonstrated impact
  • Issues requiring unlikely user interaction
  • Content spoofing without security impact
  • Rate limiting or brute force attacks
  • Issues in development/staging environments
  • Issues already known to us or publicly disclosed
  • Issues found through automated scanning tools without manual verification

Security Policy

Rules of Engagement

  1. Do Not:

    • Access, modify, or delete data that does not belong to you
    • Perform any action that could harm our users or systems
    • Disrupt or degrade our services
    • Violate any laws or breach any agreements
    • Disclose vulnerabilities publicly before we've addressed them
    • Use automated scanners that generate significant traffic
    • Test on production systems without authorization
  2. Do:

    • Act in good faith
    • Report vulnerabilities as soon as possible
    • Provide detailed, reproducible reports
    • Respect user privacy and data
    • Follow responsible disclosure practices
    • Test only on systems you have permission to test

Prohibited Activities

  • Any form of denial of service attack
  • Physical attacks against infrastructure
  • Social engineering of employees or users
  • Accessing accounts or data that doesn't belong to you
  • Modifying or destroying data
  • Spamming or phishing
  • Any illegal activity

Responsible Disclosure Process

Step 1: Report the Vulnerability

Submit your vulnerability report via email to: security@example.com

Include the following information:

  1. Vulnerability Details

    • Type of vulnerability
    • Affected component/endpoint
    • Steps to reproduce (detailed)
    • Proof of concept (if applicable)
    • Potential impact
  2. Evidence

    • Screenshots or videos demonstrating the issue
    • Code snippets or payloads (if applicable)
    • Network traffic captures (if relevant)
  3. Impact Assessment

    • Potential security impact
    • Affected users or data
    • Exploitability assessment
  4. Your Information

    • Your name or handle
    • Contact information
    • Preferred method of recognition (if applicable)

Report Template:

Subject: [Bug Bounty] [Severity] Brief Description

Vulnerability Type: [e.g., SQL Injection, XSS, etc.]
Severity: [Critical/High/Medium/Low]
Affected Component: [e.g., /api/agents/register]

Description:
[Detailed description of the vulnerability]

Steps to Reproduce:
1. [Step 1]
2. [Step 2]
3. [Step 3]

Proof of Concept:
[Code, payload, or detailed explanation]

Impact:
[Potential security impact]

Suggested Fix:
[Optional: suggestions for fixing the issue]

Contact Information:
[Your name/handle and contact details]

Step 2: Initial Response

  • Within 24 hours: We will acknowledge receipt of your report
  • Within 3 business days: We will provide an initial assessment
  • Within 7 business days: We will provide a detailed response with our assessment

Step 3: Investigation and Fix

  • We will investigate the vulnerability
  • We will work on developing and testing a fix
  • We will keep you updated on our progress
  • Typical resolution time: 30-90 days depending on severity

Step 4: Resolution and Recognition

  • Once fixed, we will notify you
  • We will add you to our Hall of Fame (if you consent)
  • We will process any applicable rewards
  • We may request your assistance in verifying the fix

Severity Classification

Vulnerabilities are classified based on their potential impact:

Critical

  • Remote code execution
  • SQL injection with data extraction
  • Authentication bypass
  • Privilege escalation to admin
  • Reward: $500 - $2,000

High

  • SQL injection (read-only)
  • Cross-site scripting (stored)
  • Server-side request forgery
  • Insecure direct object references
  • Reward: $200 - $500

Medium

  • Cross-site scripting (reflected)
  • Cross-site request forgery
  • Information disclosure
  • Weak authentication mechanisms
  • Reward: $50 - $200

Low

  • Missing security headers
  • Information leakage (low impact)
  • Weak session management
  • Reward: Recognition only

Note: Reward amounts are guidelines and may vary based on:

  • Quality of the report
  • Impact and exploitability
  • Quality of suggested fixes
  • First-time vs. duplicate reports

Recognition and Rewards

Hall of Fame

Security researchers who responsibly disclose vulnerabilities will be recognized in our Hall of Fame (with consent). Recognition includes:

  • Name or handle (as preferred)
  • Date of disclosure
  • Vulnerability type
  • Severity level

Rewards

  • Monetary Rewards: For Critical, High, and Medium severity vulnerabilities
  • Recognition: Hall of Fame listing for all valid reports
  • Swag: Special recognition for exceptional contributions
  • Public Acknowledgment: With permission, we may publicly acknowledge your contribution

Reward Payment

  • Rewards are paid via PayPal, bank transfer, or cryptocurrency (as preferred)
  • Payment is made after the vulnerability is fixed and verified
  • Typical payment time: 30-60 days after fix verification

Hall of Fame

We recognize and thank the following security researchers for their responsible disclosure of vulnerabilities:

2024

No entries yet - be the first!

Recognition Criteria

To be included in the Hall of Fame:

  1. Submit a valid security vulnerability
  2. Follow responsible disclosure practices
  3. Provide consent for public recognition
  4. Allow us to fix the vulnerability before public disclosure

Safe Harbor

Good Faith Testing

We provide safe harbor for security research conducted in good faith. As long as you:

  1. Act in good faith
  2. Follow this security policy
  3. Do not access or modify data that doesn't belong to you
  4. Do not harm our users or systems
  5. Report vulnerabilities responsibly

We will:

  • Not pursue legal action against you
  • Work with you to understand and resolve issues
  • Recognize your contribution appropriately

Important: This safe harbor applies only to security research that follows this policy. Any activities outside this scope may be subject to legal action.

Disclosure Timeline

We follow a coordinated disclosure process:

  1. Day 0: Vulnerability reported
  2. Day 1: Acknowledgment sent
  3. Day 3: Initial assessment provided
  4. Day 7: Detailed response with timeline
  5. Day 30-90: Fix developed and deployed
  6. Day 90+: Public disclosure (if applicable, with researcher consent)

Note: Timeline may vary based on vulnerability complexity and severity.

Public Disclosure

  • We prefer coordinated disclosure
  • Public disclosure should occur only after the fix is deployed
  • We will work with researchers on disclosure timing
  • We may request a delay in public disclosure if needed for user protection

Questions and Support

For questions about the bug bounty program:

  • Email: security@example.com
  • Subject: [Bug Bounty Question] Your Question
  • Response Time: Within 3 business days

Program Updates

This bug bounty program may be updated periodically. We will notify active researchers of significant changes. Check this page regularly for updates.

Last Updated: 2024-01-15
Program Status: Active


Security Contact

For security concerns or to report vulnerabilities, please contact:

  • Email: security@example.com
  • PGP Key: [Link to PGP key]
  • Bug Bounty Program: See Bug Bounty Program section above
  • Responsible Disclosure: We appreciate responsible disclosure of security vulnerabilities

Document Maintenance

  • Last Updated: 2024-01-15
  • Next Review: 2024-04-15
  • Version: 1.0
  • Owner: Security Engineering Team

There aren’t any published security advisories