Skip to main content

Draco Insomnis for Python

The Draco Insomnis firewall for Python web apps, with no dependencies. It uses the same signatures and limits as the WordPress, PrestaShop, Drupal, Joomla, Moodle, Laravel, Magento, server and JavaScript editions, and runs the same shared test cases (tests/vectors), so every edition gives the same answer to the same request.

Where How
FastAPI, Starlette, Django (ASGI), Quart, Litestar app.add_middleware(DracoASGI, …) or DracoASGI(app, …)
Flask app.wsgi_app = DracoWSGI(app.wsgi_app, …)
Django (WSGI) application = DracoWSGI(get_wsgi_application(), …)
Anything else Gatekeeper(…).decide(DracoRequest(…))

Python 3.9 or newer.

What it does

Draco checks requests in this order:

  1. Blocked addresses can still read the site. They cannot post anything or open sensitive pages (login, sign-up and admin).
  2. Countries, by default only for sensitive requests. Behind Cloudflare the country comes from Cloudflare; without it, from the monthly country list (see below).
  3. Firewall signatures: SQL injection, path traversal, code injection, XSS in the URL, known scanners and probes for files like .env or .git, in the path, the query and the posted form or JSON (also when URL-encoded twice).
    • In log mode it only keeps evidence; in block mode it answers 403 with a page that never says which rule matched.
    • Five attacks in 10 minutes block the address for 7 days.
  4. Anti-copy pace: pages a minute per address (default 120) and per /24 or /64 network (600), 429 over the limit. Only HTML pages count. Google, Bing, Apple and Yandex may go faster once the reverse DNS proves who they are.
  5. Uploads through ClamAV, off by default: on a server that runs clamd, the files of a request that passed are checked before your app sees them (see below).

The visitor's address is the connection's own unless you choose proxy="xff" or proxy="cf", and even then the header is only believed from a trusted proxy (local and private networks by default, trusted_proxies when you list them, Cloudflare's ranges for cf). If Draco itself fails, the request goes through.

FastAPI

from fastapi import FastAPI
from draco_insomnis.asgi import DracoASGI

app = FastAPI()
app.add_middleware(DracoASGI, waf="block", pace="block", headers=True)

The engine runs in a worker thread, so the event loop never waits on it (it may ask the DNS to prove a search engine). Forms and JSON bodies up to 64 KB are read once and handed to your app unchanged.

Flask and Django

from draco_insomnis.wsgi import DracoWSGI

app.wsgi_app = DracoWSGI(app.wsgi_app, waf="block")             # Flask
application = DracoWSGI(get_wsgi_application(), waf="block")    # Django, wsgi.py

Options

Option Default
waf "log" "off", "log" or "block"
pace "off" "off", "log" or "block"; rate_ip, rate_net (at least 10)
proxy, trusted_proxies connection "cf" or "xff", and who may set them
admin_ips none addresses never inspected nor limited
except_paths none path prefixes never inspected (health checks, signed webhooks)
sensitive login, sign-up, admin, /token path endings that count as sensitive, besides any POST
headers, hsts off nosniff, SAMEORIGIN, referrer and permissions policy; HSTS over HTTPS
geo off {"mode": "block" or "allow", "countries": [...], "action": "log" or "block", "scope": "sensitive" or "site", "lookup": GeoFile(...)}
review off {"secret": "16+ characters", "on_request": callable}
crowdsec off {"url": "http://127.0.0.1:8080", "api_key": "..."}
clam off {"mode": "log" or "block", "socket": "" (found by itself) or "tcp://clamd:3310", "timeout": 10, "max_body": 134217728} (see below)
store memory anything with bump, get, set, delete (see below)
on_event none every line of evidence, as it is written
texts English the texts of the pages

Countries without Cloudflare

from draco_insomnis import GeoFile

geo = GeoFile("/var/lib/myapp/draco-geo.bin")
app.add_middleware(DracoASGI, waf="block", geo={"mode": "block", "countries": ["XX"], "lookup": geo})

The same list as the other editions: DB-IP "IP to Country Lite" (CC BY 4.0), built monthly by this project. It is downloaded on start when missing or older than 35 days, then checked once a day, and only replaces the old file when the published SHA-256 matches. The lookup is local.

"This is a mistake": review requests

def mail_me(r):
    send_mail("Review", f"{r.email} ({r.ip}, rule {r.rule}): {r.message}")

app.add_middleware(DracoASGI, waf="block", review={"secret": os.environ["DRACO_SECRET"], "on_request": mail_me})

The 403 page carries a short form, signed for the address and the moment it was shown, with the rule sealed, a trap field and 3 requests a day per address (50 per site). The visitor always gets the same "Request sent" page. Draco never writes to the person who asks. Let someone through with gate.blocks.remove(ip) or by adding the address to admin_ips.

CrowdSec

app.add_middleware(DracoASGI, waf="block", crowdsec={"url": "http://127.0.0.1:8080", "api_key": os.environ["CROWDSEC_KEY"]})

Bans from your own CrowdSec (key from cscli bouncers add draco) join the block list, synced in a background thread at most once a minute. Ranges wider than /16 or /32 and admin_ips are never imported; blocks you set yourself are never lifted by it. The other way, crowdsec_line(event) writes evidence lines for the parser and scenario in server/crowdsec.

Uploads through ClamAV

On a server that runs ClamAV's daemon (clamd), Draco can check every uploaded file before your app sees it. It is the same check as in the server, WordPress and Laravel editions (ADR 0008). Draco never installs ClamAV, and nothing leaves the server: each file is streamed to clamd.

app.add_middleware(DracoASGI, waf="block", clam={"mode": "block"})                      # FastAPI
app.wsgi_app = DracoWSGI(app.wsgi_app, waf="block", clam={"mode": "block"})              # Flask
application = DracoWSGI(get_wsgi_application(), clam={"mode": "log", "socket": "tcp://clamd:3310"})
  • What is checked. The files of a multipart/form-data request (POST, PUT or PATCH), after the firewall has let it through: at most 20 files and 20 seconds a request, each file up to 25 MB (clamd's own limit). ClamAV also looks inside ZIP files.
    • Draco keeps the body in a temporary file, in memory up to 1 MB and then on disk. It hands the files to clamd from there and gives your app exactly the same bytes afterwards.
    • Bodies over max_body (128 MB by default) are not kept, and go through unchecked.
  • Who is checked. Everyone, admin_ips included, because a stolen account uploads too. Paths in except_paths are not checked.
  • Modes. log writes down what ClamAV finds (clam:upload, verdict doubtful) and lets it through. block refuses the request with a 403 page that says the antivirus refused a file (with the review form, when reviews are on) and writes down bot.
  • It fails open.
    • With no clamd, uploads pass and nothing is written down.
    • With a clamd that does not answer, uploads pass and Draco writes down clam:error once an hour.
  • Where it looks for clamd. /var/run/clamav/clamd.ctl (Debian, Ubuntu), the RHEL sockets, then 127.0.0.1:3310. Give socket for anything else. Your app's user needs permission to open the socket: if python -m draco_insomnis.clam, run as that user, says clamd does not answer, look at LocalSocketMode and LocalSocketGroup in clamd.conf.

The same client from the command line:

python -m draco_insomnis.clam                          # where clamd is, its engine, how old its signatures are
python -m draco_insomnis.clam scan /srv/app            # every regular file through clamd; moves nothing
python -m draco_insomnis.clam scan /srv/app --json --socket tcp://127.0.0.1:3310

scan does not follow symbolic links and skips files over 25 MB. It exits with 1 when something is found and 2 when there is no clamd. ClamAV's PHP signatures have false positives, so a person decides what to do with what it finds.

Several processes

The default MemoryStore serves one process. With several workers or servers, give every one the same store: any object with bump(key, ttl) -> int, get(key), set(key, value, ttl) (ttl 0 = for good) and delete(key). With Redis that is INCR + EXPIRE NX, GET, SET EX and DEL. The keys are the same as in the JavaScript edition, so a Node and a Python app can share one block list.

Tests

cd python
PYTHONPATH=src python -m unittest discover -s tests -p "test_*.py"   # the engine, shared vectors
pip install fastapi uvicorn python-multipart flask django
PYTHONPATH=src python tests/e2e_frameworks.py                         # real FastAPI, Flask and Django
docker build -t draco-clamd:test ../tests/e2e/clamd && docker run -d -p 127.0.0.1:3310:3310 draco-clamd:test
PYTHONPATH=src python tests/e2e_clam.py tcp://127.0.0.1:3310          # a real clamd with test signatures

Metadata

Release files for draco-insomnis 0.2.8

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

Source distribution (sdist)

Source distribution for draco-insomnis 0.2.8
File Size Uploaded Provenance Actions
draco_insomnis-0.2.8.tar.gz 54.7 kB With provenance Details for draco_insomnis-0.2.8.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for draco-insomnis 0.2.8
File Interpreter ABI Platform Actions
draco_insomnis-0.2.8-py3-none-any.whl Python 3 none any Details for draco_insomnis-0.2.8-py3-none-any.whl

Total release size: 103.7 kB

Release files / draco_insomnis-0.2.8.tar.gz

Download URL draco_insomnis-0.2.8.tar.gz
Size 54.7 kB
Tags Source
SHA-256 checksum
How to use checksums
652cc82563262db35411f4fa5910d497370712550e5d36ddee7b5ca6acc00d1c
BLAKE2b-256 checksum
How to use checksums
e4da2dedb7d1c7dfd8213fe0d197ad34df076f679f9a70c0726da1eb39bced89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Release files / draco_insomnis-0.2.8-py3-none-any.whl

Download URL draco_insomnis-0.2.8-py3-none-any.whl
Size 49.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5a4d0f0a32ba552089a3b85ce83b4c082f87bf052d8dcc217ac14dca99edc357
BLAKE2b-256 checksum
How to use checksums
6b47b4388e45b0497857791c103e726e243ab3fe4b5fc0aec140cb430228f19c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

Security for draco-insomnis 0.2.8

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for draco_insomnis-0.2.8.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 30, 2026.

Transparency log

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.

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