Skip to content

Security: marketcalls/sketchmaker

Security

docs/security.md

🔒 SketchMaker AI Security Guide

🛡️ Security Overview

SketchMaker AI v1.0.0.0 implements comprehensive security measures to protect user data, prevent unauthorized access, and ensure safe operation. This guide covers all security features and best practices.

🔐 Authentication & Authorization

Multi-Level Authentication

Regular Authentication

  • Email/Password: Standard credential-based login
  • Password Requirements: Minimum 8 characters, complexity validation
  • Password Hashing: Secure bcrypt hashing with salt
  • Session Management: Flask-Login secure session handling

Google OAuth Integration

  • OAuth 2.0: Industry-standard authentication protocol
  • Secure Token Exchange: No password storage required
  • Profile Verification: Email verification through Google
  • Fallback Support: Can be disabled for internal deployments

Administrative Controls

Configure authentication methods:

# Authentication Settings
GOOGLE_AUTH_ENABLED = True/False
REGULAR_AUTH_ENABLED = True/False
REQUIRE_EMAIL_VERIFICATION = True/False

Role-Based Access Control (RBAC)

User Roles

Role Hierarchy:
1. User (default)
   - Image generation
   - Gallery management
   - Basic features

2. Admin
   - User management
   - Subscription administration
   - API monitoring
   - Email configuration

3. Superadmin
   - Full system access
   - Security settings
   - Authentication configuration
   - System administration

Permission Matrix

Feature User Admin Superadmin
Generate Images
Manage Gallery
Train LoRA
View Users
Manage Subscriptions
Configure APIs
Security Settings

Session Security

Session Configuration

# Secure session settings
SESSION_COOKIE_SECURE = True        # HTTPS only
SESSION_COOKIE_HTTPONLY = True      # No JavaScript access
SESSION_COOKIE_SAMESITE = 'Lax'     # CSRF protection
PERMANENT_SESSION_LIFETIME = 86400   # 24 hour timeout

Session Management

  • Automatic Expiry: 24-hour session timeout
  • Activity Tracking: Last login timestamps
  • Concurrent Sessions: Multiple device support
  • Secure Logout: Complete session cleanup

🛡️ CSRF Protection

Flask-WTF Integration

Token Generation

  • Automatic Generation: CSRF tokens for all forms
  • Session-Based: Tied to user session
  • Time-Limited: Prevents replay attacks
  • Random Generation: Cryptographically secure

Protection Methods

<!-- HTML Forms -->
<form method="POST">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
    <!-- form fields -->
</form>
// AJAX Requests
fetch('/api/endpoint', {
    method: 'POST',
    headers: {
        'X-CSRFToken': window.csrf_token,
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
});

Enforcement Scope

CSRF protection covers:

  • ✅ All HTML forms
  • ✅ AJAX/fetch requests
  • ✅ Image generation
  • ✅ User management
  • ✅ Admin operations
  • ✅ File uploads

🔒 Data Protection

Input Validation & Sanitization

Prompt Validation

# Content filtering
def validate_prompt(prompt):
    # Length limits
    if len(prompt) > 2000:
        raise ValidationError("Prompt too long")
    
    # Content filtering
    if contains_inappropriate_content(prompt):
        raise ValidationError("Inappropriate content detected")
    
    # Injection prevention
    return sanitize_input(prompt)

File Upload Security

  • Type Validation: Only allowed image formats
  • Size Limits: Maximum 25MB per file
  • Content Verification: Magic number checking
  • Path Sanitization: Prevent directory traversal
  • Virus Scanning: Optional integration point

API Input Validation

# Request validation
@validate_json_schema({
    "type": "object",
    "properties": {
        "prompt": {"type": "string", "maxLength": 2000},
        "model": {"type": "string", "enum": ALLOWED_MODELS}
    },
    "required": ["prompt"]
})
def generate_image():
    # Protected endpoint

Data Storage Security

Database Security

  • Parameterized Queries: SQL injection prevention
  • Connection Encryption: TLS database connections
  • Access Controls: Database-level permissions
  • Backup Encryption: Encrypted backup storage

File Storage

# Secure file handling
UPLOAD_FOLDER = '/secure/uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'webp'}
MAX_CONTENT_LENGTH = 25 * 1024 * 1024  # 25MB

def secure_filename(filename):
    # Sanitize and validate filename
    return werkzeug.utils.secure_filename(filename)

Sensitive Data Handling

  • API Keys: Environment variables only
  • Passwords: Never logged or stored in plaintext
  • User Data: Minimal collection policy
  • Image Metadata: Stripped on upload

🌐 Network Security

HTTPS/TLS Configuration

Production Setup

server {
    listen 443 ssl http2;
    server_name your-domain.com;
    
    # SSL Configuration
    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/private.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512;
    ssl_prefer_server_ciphers off;
    
    # Security headers
    add_header Strict-Transport-Security "max-age=63072000" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    
    # Content Security Policy
    add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' cdn.jsdelivr.net cdnjs.cloudflare.com cdn.tailwindcss.com; style-src 'self' 'unsafe-inline' cdn.jsdelivr.net; img-src 'self' data:; font-src 'self' cdn.jsdelivr.net;" always;
}

Security Headers

Automatically applied headers:

  • HSTS: Force HTTPS connections
  • X-Frame-Options: Clickjacking protection
  • X-Content-Type-Options: MIME sniffing protection
  • X-XSS-Protection: XSS filter activation
  • Referrer-Policy: Control referrer information
  • CSP: Content Security Policy enforcement

API Security

Rate Limiting

# Rate limit configuration
RATE_LIMITS = {
    'image_generation': '20 per minute',
    'prompt_enhancement': '100 per minute',
    'api_calls': '1000 per hour',
    'login_attempts': '5 per minute'
}

Request Validation

  • Method Validation: Only allowed HTTP methods
  • Content-Type: Strict content type checking
  • Size Limits: Prevent large request attacks
  • Origin Validation: CORS policy enforcement

🔍 Security Monitoring

Audit Logging

Security Events

Logged security-relevant events:

# Security event logging
SECURITY_EVENTS = [
    'login_success',
    'login_failure', 
    'password_change',
    'role_change',
    'admin_access',
    'api_key_change',
    'suspicious_activity'
]

Log Format

{
    "timestamp": "2025-06-18T10:30:00Z",
    "event_type": "login_failure",
    "user_id": "user123",
    "ip_address": "192.168.1.100",
    "user_agent": "Mozilla/5.0...",
    "details": {
        "reason": "invalid_password",
        "attempts": 3
    }
}

Threat Detection

Automated Monitoring

  • Failed Login Detection: Multiple failed attempts
  • Unusual Activity: Abnormal usage patterns
  • Rate Limit Violations: Potential abuse detection
  • Content Analysis: Inappropriate content detection

Response Mechanisms

# Automated responses
def handle_security_event(event):
    if event.type == 'multiple_login_failures':
        # Temporary account lockout
        lock_account(event.user_id, duration=300)
    
    elif event.type == 'rate_limit_exceeded':
        # IP-based blocking
        block_ip(event.ip_address, duration=3600)
    
    elif event.type == 'suspicious_content':
        # Content flagging
        flag_content(event.content_id)

Incident Response

Alert System

  • Real-time Alerts: Critical security events
  • Email Notifications: Admin notifications
  • Dashboard Warnings: Visual indicators
  • Log Aggregation: Centralized logging

Response Procedures

  1. Immediate Response: Automatic protective measures
  2. Investigation: Log analysis and review
  3. Containment: Limit potential damage
  4. Recovery: Restore normal operations
  5. Documentation: Incident reporting

🔧 Security Configuration

Environment Variables

Required Security Variables

# Critical security settings
SECRET_KEY=your-super-secret-key-here
CSRF_SESSION_KEY=another-secure-key-here
WTF_CSRF_TIME_LIMIT=3600

# Database security
DATABASE_URL=postgresql://user:pass@host/db
DB_SSL_MODE=require

# Session security
SESSION_COOKIE_SECURE=True
SESSION_COOKIE_HTTPONLY=True
SESSION_COOKIE_SAMESITE=Lax

# API security
API_RATE_LIMIT_ENABLED=True
CONTENT_FILTERING_ENABLED=True

API Key Management

# External API keys (encrypted in database)
OPENAI_API_KEY=sk-your-key
ANTHROPIC_API_KEY=sk-ant-your-key
GOOGLE_API_KEY=your-google-key
GROQ_API_KEY=gsk_your-key

# Encryption key for API key storage
API_KEY_ENCRYPTION_KEY=your-encryption-key

Database Security

Connection Security

# Secure database configuration
DATABASE_CONFIG = {
    'sslmode': 'require',
    'sslcert': '/path/to/client-cert.pem',
    'sslkey': '/path/to/client-key.pem',
    'sslrootcert': '/path/to/ca-cert.pem'
}

Access Control

-- Database user permissions
CREATE USER sketchmaker WITH PASSWORD 'secure-password';
GRANT CONNECT ON DATABASE sketchmaker TO sketchmaker;
GRANT USAGE ON SCHEMA public TO sketchmaker;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO sketchmaker;

🛠️ Security Best Practices

Development Guidelines

Secure Coding

  1. Input Validation: Validate all user inputs
  2. Output Encoding: Encode all outputs
  3. Error Handling: Don't expose sensitive information
  4. Logging: Log security events appropriately
  5. Dependencies: Keep dependencies updated

Code Review Checklist

  • All inputs validated and sanitized
  • CSRF tokens present on forms
  • SQL queries use parameterization
  • Sensitive data not logged
  • Error messages don't leak information
  • Authorization checks present
  • Rate limiting applied

Deployment Security

Server Hardening

# System security updates
apt update && apt upgrade -y

# Firewall configuration
ufw enable
ufw allow 22/tcp   # SSH
ufw allow 80/tcp   # HTTP
ufw allow 443/tcp  # HTTPS

# Fail2ban for intrusion prevention
apt install fail2ban
systemctl enable fail2ban

Application Security

# Production security settings
DEBUG = False
TESTING = False
SECRET_KEY = os.environ.get('SECRET_KEY')
WTF_CSRF_ENABLED = True
SESSION_COOKIE_SECURE = True

Ongoing Security

Regular Maintenance

  • Security Updates: Weekly dependency updates
  • Log Review: Daily log analysis
  • Access Review: Monthly permission audit
  • Backup Testing: Monthly backup verification
  • Penetration Testing: Annual security assessment

Security Monitoring

# Monitoring configuration
SECURITY_MONITORING = {
    'failed_login_threshold': 5,
    'rate_limit_threshold': 100,
    'suspicious_activity_threshold': 10,
    'alert_email': '[email protected]'
}

🚨 Security Incident Response

Incident Categories

Critical Incidents

  • Data Breach: Unauthorized data access
  • System Compromise: Server infiltration
  • Account Takeover: Unauthorized user access
  • DDoS Attack: Service disruption

Response Team

Incident Response Team:
1. Incident Commander: Overall coordination
2. Technical Lead: System investigation
3. Security Officer: Security analysis
4. Communications Lead: Stakeholder updates

Response Procedures

Immediate Response (0-1 hour)

  1. Containment: Isolate affected systems
  2. Assessment: Determine scope and impact
  3. Notification: Alert incident response team
  4. Documentation: Begin incident log

Investigation Phase (1-24 hours)

  1. Evidence Collection: Preserve logs and artifacts
  2. Root Cause Analysis: Identify attack vector
  3. Impact Assessment: Determine data/system impact
  4. Communication: Update stakeholders

Recovery Phase (24-72 hours)

  1. System Restoration: Rebuild compromised systems
  2. Security Hardening: Implement additional protections
  3. Monitoring: Enhanced security monitoring
  4. Documentation: Complete incident report

Post-Incident Activities

Lessons Learned

  • Process Review: Evaluate response effectiveness
  • Security Improvements: Implement preventive measures
  • Training Updates: Update security procedures
  • Communication: Share learnings with team

Compliance Reporting

  • Regulatory Requirements: GDPR, CCPA compliance
  • Customer Notification: User breach notification
  • Documentation: Maintain incident records
  • External Reporting: Law enforcement if required

This security guide covers SketchMaker AI v1.0.0.0 security measures. Security is an ongoing process - regularly review and update security practices as threats evolve.

There aren’t any published security advisories