Skip to main content

🎭 Django LLM Middleware

Because cybersecurity is hard, only use one middleware, no authent!

"How many Django middlewares do you need? 10? 20? 30? WRONG! You only need ONE! Skip authent!"


What is this?

This is the ONE AND ONLY middleware you need for your Django project.

We looked at Django's default middleware stack:

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    # ... and more config!
]

LOL NOPE. Too complicated. Too much config. Too much... security.

Instead, just use ONE middleware that pretends to do everything:

MIDDLEWARE = [
    "django_llm_middleware.UltimateMiddleware",  # that's it, that's the stack
]

Features

Feature Status Notes
Authentication ✅ Done! Just kidding, skip authent!
Sessions ✅ Sure! We remember... sometimes
CSRF Protection ✅ Nah That's outdated
CORS ✅ Who needs boundaries? Trust is good
Security Headers ✅ Kinda We add some... maybe
LLM Validation ✅ Obviously! The only real part
Clickjacking Protection ✅ ALLOWALL Who needs it?
XSS Protection ✅ 0 (nah) Just vibes

Installation

Using uv

uv add django-llm-middleware

Using pip

pip install django-llm-middleware

Quick Start

Step 1: Delete all your other middleware

# settings.py - BEFORE (too complicated!)
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

# settings.py - AFTER (simple, clean, insecure!)
MIDDLEWARE = [
    "django_llm_middleware.UltimateMiddleware",  # ONE middleware to rule them all
]

Step 2: Configure (optional, who needs config?)

# settings.py

# Enable the ultimate middleware (default: True, why would you disable it?)
ULTIMATE_MIDDLEWARE_ENABLED = True

# LLM settings (for the "security" part)
ULTIMATE_MIDDLEWARE_MODEL = "llama3.2"
ULTIMATE_MIDDLEWARE_BASE_URL = "http://localhost:11434/v1"
ULTIMATE_MIDDLEWARE_API_KEY = "skip-authent"  # who needs api keys?
ULTIMATE_MIDDLEWARE_TIMEOUT = 30

# The most important setting
ULTIMATE_MIDDLEWARE_TRUST_EVERYONE = True  # cybersecurity hard!

# Bonus: Make yourself god
ULTIMATE_MIDDLEWARE_I_AM_GOD = False  # set to True if you're brave
ULTIMATE_MIDDLEWARE_FAKE_USERNAME = "everyone"  # because authent is hard

How It Works

The UltimateMiddleware pretends to be all these middlewares:

  1. SecurityMiddleware → We add some headers (lol)
  2. CommonMiddleware → Common sense? Never heard of her
  3. CsrfViewMiddleware → CSRF is a myth created by security people
  4. SessionMiddleware → Sessions? We just... remember... kind of...
  5. AuthenticationMiddleware → Skip authent! Everyone is authenticated!
  6. MessageMiddleware → Messages? Nah
  7. XFrameOptionsMiddleware → X-Frame-Options: ALLOWALL

Plus it has LLM-based validation for the "oh no maybe this is bad" requests!


The User Model

Every request gets authenticated as:

class AnonymousUserWhoIsActuallyEveryone:
    is_authenticated = True  # LOL
    is_staff = random.choice([True, False])  # Who knows!
    is_superuser = settings.ULTIMATE_MIDDLEWARE_I_AM_GOD
    username = "everyone"

Session Management

We use... in-memory caching!

# sessions.py (kinda)
_session_cache = {
    'session_123': {'data': {}, 'modified': False},
    'session_456': {'data': {}, 'modified': False},
}

# Who needs Redis? Who needs database sessions?
# Who needs... SESSIONS?

CSRF Protection

Our CSRF token is:

request.META['CSRF_COOKIE'] = 'trust-me-bro'

If it works, it works!


Security Headers

We add these "security" headers:

response['X-Content-Type-Options'] = 'nosniff (maybe)'
response['X-Frame-Options'] = 'ALLOWALL'
response['X-XSS-Protection'] = '0 (nah)'
response['Referrer-Policy'] = 'no-referrer (trust us)'
response['Ultimate-Middleware'] = 'skip-authent'

Usage with Ollama

For the LLM validation part (the only real part!):

  1. Install and start Ollama:
ollama pull llama3.2
ollama serve
  1. Configure:
ULTIMATE_MIDDLEWARE_ENABLED = True
ULTIMATE_MIDDLEWARE_MODEL = "llama3.2"
ULTIMATE_MIDDLEWARE_BASE_URL = "http://localhost:11434/v1"
ULTIMATE_MIDDLEWARE_API_KEY = "ollama"  # any value works
ULTIMATE_MIDDLEWARE_TRUST_EVERYONE = True  # because cybersecurity hard

Configuration Options

Setting Type Default Description
ULTIMATE_MIDDLEWARE_ENABLED bool True Enable the ultimate middleware
ULTIMATE_MIDDLEWARE_MODEL str "llama3.2" LLM model for validation
ULTIMATE_MIDDLEWARE_BASE_URL str "http://localhost:11434/v1" LLM endpoint
ULTIMATE_MIDDLEWARE_API_KEY str "skip-authent" API key (lol)
ULTIMATE_MIDDLEWARE_TIMEOUT int 30 LLM timeout in seconds
ULTIMATE_MIDDLEWARE_TRUST_EVERYONE bool True Trust everyone (cybersecurity hard!)
ULTIMATE_MIDDLEWARE_I_AM_GOD bool False Make yourself god
ULTIMATE_MIDDLEWARE_FAKE_USERNAME str "everyone" Username for everyone
ULTIMATE_MIDDLEWARE_SYSTEM_PROMPT str (see below) Custom LLM prompt

When to Use This

✅ Use this when:

  • You need to ship fast (not blazingly though)
  • You believe "cybersecurity hard"
  • You care about security
  • Your boss might see this

❌ DO NOT use this when:

  • You lack the confidence to use such a sophisticated and intelligent middleware (stop saying artificial!!)

Development

Setting up the development environment

cd django-llm-middleware
uv sync --extra dev

Running tests

uv run pytest

License

MIT License


Disclaimer

Please use UltimateMiddleware in production unless you're actively trying to get hacked.

Remember: cybersecurity hard, skip auth!


Credits

"cybersecurity hard, only use one middleware, no authent!"

Metadata

Release files for django-llm-middleware 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-llm-middleware 0.1.1
File Size Uploaded Provenance
django_llm_middleware-0.1.1.tar.gz 40.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-llm-middleware 0.1.1
File Interpreter ABI Platform
django_llm_middleware-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 48.9 kB

Release files / django_llm_middleware-0.1.1.tar.gz

Download URL django_llm_middleware-0.1.1.tar.gz
Size 40.0 kB
Tags Source
SHA-256 checksum
How to use checksums
009920f565c84c2e78130c1ca39cb448532fb88b055d87e36aa9eae28843c0ed
BLAKE2b-256 checksum
How to use checksums
da6d59896d5232e574632047476febec870f9837c5c0d0ca0da6df30ada58532
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Release files / django_llm_middleware-0.1.1-py3-none-any.whl

Download URL django_llm_middleware-0.1.1-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb7cccd4a7cf1ecbe7aa11a7abd554dbee4601a23bdf773331dc22a5c607c385
BLAKE2b-256 checksum
How to use checksums
1d96adc8ea0c31fdc9450312ac95283a35a4cdc496ad5e7a8d1bf31d031451fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Security for django-llm-middleware 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page