Bulk Messaging System

Documentation

Input validation and sanitization

Introduction#

This page details the input validation and sanitization strategies implemented across the application. It focuses on:

  • Phone number normalization and validation
  • Email address extraction and filtering
  • User-provided content sanitization for messages
  • Security measures against malicious file uploads, CSV parsing risks, and command injection attempts
  • Input encoding strategies, escape sequence handling, and data integrity verification

The analysis covers both Electron main process handlers and Python backend utilities, ensuring a detailed understanding of how user inputs are processed, validated, sanitized, and transmitted securely.

Project structure#

The application comprises:

  • Electron main process handlers for WhatsApp, Gmail, and SMTP operations
  • Frontend React components for user interaction
  • Python utilities for phone number cleaning and contact extraction
  • A Flask backend for file upload and user management

Core components#

This section outlines the primary validation and sanitization mechanisms implemented in the codebase.

  • Phone number cleaning and normalization

    • Removes separators and non-digit characters except plus sign
    • Enforces length constraints and optional international prefix
    • Standardizes local numbers to international format when applicable
  • Manual phone number parsing

    • Accepts multiple formats: standalone numbers, name:number pairs, and delimiter-separated entries
    • Uses regex heuristics to detect phone-like substrings
    • Produces normalized contacts with optional names
  • Contact extraction from files

    • Supports CSV, TXT, and Excel formats
    • Heuristic detection of phone and name columns
    • Reliable fallbacks and error handling for malformed inputs
  • Email list parsing

    • Reads CSV with flexible column names or plain text newline-separated entries
    • Filters entries containing “@” to approximate valid email addresses
  • Message sanitization

    • Limits message lengths for safety and performance
    • Encodes HTML content appropriately for transport
    • Avoids unsafe inline styles or scripts in HTML messages
  • File upload restrictions

    • Whitelists allowed file extensions
    • Uses secure filename generation
    • Stores uploads under controlled paths

Architecture overview#

The validation pipeline spans frontend, Electron main process, and Python utilities:

Detailed component analysis#

Phone number validation and normalization#

Phone numbers undergo strict cleaning and normalization:

  • Strips whitespace and common separators
  • Removes non-digit characters except “+”
  • Handles leading zeros and optional international prefixes
  • Validates digit count within accepted bounds

Manual phone number parsing#

The manual parser supports flexible input formats:

  • Standalone numbers
  • Name-number pairs separated by colon or dash
  • Delimiter-separated entries (newline, comma, semicolon, pipe)
  • Heuristic detection of phone-like substrings

Contact extraction from files#

File-based contact extraction supports multiple formats:

  • CSV: heuristic column detection for phone/name; reliable fallbacks
  • TXT: delimiter-separated lines with optional name
  • Excel: pandas-based parsing with similar heuristics

Email address parsing and filtering#

Email lists are parsed from CSV or plain text:

  • CSV: flexible column names (email, Email, ADDRESS, etc.) or first column fallback
  • Text: newline-separated entries filtered by presence of “@”
  • Transport encoding: HTML content-type header included

Message content sanitization#

Message composition includes:

  • Length limits for performance and platform constraints
  • HTML content-type header for Gmail transport
  • Optional HTML stripping for text version in SMTP

File upload security measures#

The Flask backend enforces:

  • Allowed file extensions whitelist
  • Secure filename generation
  • Controlled upload path
  • JSON responses for API endpoints

Dependency analysis#

Key dependencies and interactions:

  • Frontend components communicate with Electron main process via contextBridge
  • Pyodide loads Python scripts dynamically for manual number parsing
  • Handlers depend on environment variables for external services
  • File parsing relies on pandas for structured formats

Performance considerations#

  • Regex-based cleaning and parsing are efficient for typical contact volumes but should be monitored for very large inputs
  • File parsing uses streaming for CSV; ensure appropriate buffering and memory limits
  • Message length limits prevent excessive payload sizes and reduce transport overhead
  • Rate limiting delays in email sending avoid throttling and improve reliability

Troubleshooting guide#

Common validation and sanitization issues:

  • Invalid phone numbers

    • Cause: Non-digit characters outside “+”, incorrect length
    • Resolution: Ensure numeric input with optional “+” prefix and correct digit count
  • Malformed CSV/Excel files

    • Cause: Missing headers, unexpected delimiters, mixed encodings
    • Resolution: Validate schema and encoding; provide clear error messages
  • Email parsing failures

    • Cause: Missing “@” or unsupported column names
    • Resolution: Use supported column names or rely on first-column fallback
  • File upload errors

    • Cause: Unsupported extension or missing file part
    • Resolution: Confirm allowed extensions and proper multipart form submission

Conclusion#

The application implements layered input validation and sanitization:

  • Phone numbers are rigorously normalized and validated
  • Manual and file-based contact extraction use reliable heuristics and error handling
  • Email lists are filtered and encoded for secure transport
  • File uploads are restricted and saved securely
  • Message content is length-limited and encoded appropriately

These measures collectively mitigate injection risks, maintain data integrity, and ensure reliable operation across diverse input formats.