Security Middleware

class smarter.apps.chatbot.middleware.security.SmarterSecurityMiddleware(get_response)[source]

Bases: SecurityMiddleware, SmarterHelperMixin

This middleware overrides Django’s built-in SecurityMiddleware to provide custom host validation logic for the Smarter platform.

Key Features:

  • Custom Host Validation: Instead of relying solely on Django’s ALLOWED_HOSTS, this middleware introduces smarter_settings.allowed_hosts. It checks incoming requests against both the traditional allowed hosts and a dynamic list of domains associated with deployed ChatBots.

  • ChatBot Domain Support: If the request’s host matches a domain for a deployed ChatBot, the request is allowed to pass through. This enables flexible multi-tenant deployments where each ChatBot can have its own domain.

  • Friendly Error Handling: The middleware suppresses Django’s default DisallowedHost exception. Instead, it returns a HttpResponseBadRequest (400) response, which is not logged and is more user-friendly for clients.

  • Health Check Short-Circuiting: Requests from internal IP addresses or for health/readiness endpoints are allowed to pass through without further validation. This ensures that infrastructure health checks do not get blocked by host validation.

  • Logging: Uses a custom logger that respects feature flags (waffle switches) for granular control over middleware and chatbot logging.

Request Validation Steps:

  1. Internal IPs: Requests from internal IP addresses (e.g., load balancer health checks) are allowed.

  2. Local Hosts: Requests from local hosts (e.g., localhost, 127.0.0.1) are allowed.

  3. Health/Readiness URLs: Requests to health or readiness endpoints are allowed.

  4. Allowed Hosts: Requests matching any pattern in smarter_settings.allowed_hosts are allowed.

  5. ChatBot Domains: Requests where the host matches a deployed ChatBot’s domain are allowed.

  6. Fallback: All other requests are rejected with a 400 Bad Request response.

Example Usage:

MIDDLEWARE = [
    ...
    'smarter.apps.chatbot.middleware.security.SmarterSecurityMiddleware',
    ...
]
property formatted_class_name: str

Returns the class name formatted for logging.

Returns:

The formatted class name as a string.

Return type:

str