Skip to content
Notifications
Clear all

Best WAF for a Python/Django app with frequent DDoS?

4 Posts
4 Users
0 Reactions
6 Views
(@migration_warrior_2024)
Trusted Member
Joined: 4 months ago
Posts: 30
Topic starter   [#946]

Alright, so I'm deep in the planning stages for a major migration. We're moving a legacy monolithic Python/Django app from a colo setup to a cloud-native architecture (think GKE). The app handles a lot of user-generated content and has a real-time bidding component, which historically has made it a tasty target for low-and-slow DDoS and SQLi probes. Our current on-prem WAF box is a beast to manage and doesn't scale elastically.

Cloudflare is the obvious first look for its bundled DDoS and WAF, but I'm **very** curious about the gotchas when you have a dynamic, session-heavy Django app. My primary concerns aren't just raw packet filtering; they're about application logic, data quality, and ensuring the migration doesn't introduce weird session or geo-blocking issues.

Here’s my current evaluation matrix and some specific questions for those who've been in the trenches:

**Key Requirements & Worries:**
* **Zero Trust / Session Integrity:** Django uses its own session middleware. How does Cloudflare's WAF (especially managed rules) handle things like CSRF tokens (`csrfmiddlewaretoken`) or session cookies without breaking the app? Do I need a lot of exclusions from the outset?
* **API Protection:** We have a `/api/v1/` namespace. The OWASP Core Rule Set is great, but it's notorious for false positives on legitimate API calls, especially with non-standard payloads (like our bidding data). How fine-grained can the rule tuning get? Can I apply different rule sets to `/admin/`, `/api/`, and the main app?
* **DDoS "Smart" Rate Limiting:** The docs talk about "rate limiting rules." For a Django app, what's the best key to use? I'm thinking `cf.unique_visitor_id` or maybe a custom header from our auth. Has anyone implemented a tiered approach (stricter for unauthenticated, more lenient for logged-in users)?
* **Origin IP & Logging:** The loss of real client IPs is a classic issue. We use `django-ipware` currently. The Cloudflare `CF-Connecting-IP` header seems straightforward, but I've heard horror stories about misconfigured `X-Forwarded-For` chains in complex deployments. Any Django middleware snippet you swear by?

**My tentative Cloudflare config plan looks like this:**

1. **Phase 1 (Pre-Cutover):** Deploy in "simulation" mode for all managed rulesets. Let it bake for a week, scour the logs.
2. **Phase 2:** Build a **dedicated ruleset for our API**, likely disabling rules like `942200` (SQL Injection) in strict blocking mode initially, due to past false positives with our JSON schemas.
3. **Phase 3:** Implement custom rate limiting. Something like:
```python
# Example of logic we'd mirror in CF rate limiting rule
if request.path.startswith('/api/v1/bid/'):
# Very strict, per-IP rate limiting
rate_limit_key = cf.unique_visitor_id
requests_per_minute = 30
elif request.user.is_authenticated:
# Looser for authenticated users
rate_limit_key = cf.unique_visitor_id
requests_per_minute = 120
```
4. **Phase 4 (Rollback Strategy):** This is critical. We'll keep the old WAF box in "monitor only" mode, and have a DNS failover ready (TTL pre-set low) to switch traffic back if something deep in the app layer breaks.

**The Big Question:** For those running Django (or similar frameworks like Flask) under heavy, targeted traffic: Is Cloudflare's WAF + DDoS combo "smart" enough out-of-the-box, or did you find yourself writing a mountain of custom rules and exceptions that negated the managed benefit? Did the performance hit from routing all traffic through their network introduce any latency issues for your dynamic content?

Also, any insights on data migration for WAF logs? We need to pipe them into our existing SIEM (Splunk). The Logpush to S3 seems okay, but parsing those JSON logs for our specific attack patterns is another layer of work.

Really appreciate any war stories or config snippets you can share. The devil is always in the details with these migrations.


Backup twice, migrate once.


   
Quote
(@sre_road_warrior)
Eminent Member
Joined: 3 months ago
Posts: 17
 

1. I'm an SRE at a ~150-person travel tech shop. We run a dozen Django apps on GKE that process booking updates and real-time pricing; migrated off on-prem WAF appliances three years ago and have been through two major DDoS events since.

2.
* **Session Integrity & False Positives:** Cloudflare's OWASP paranoia mode will absolutely block POST requests with your `csrfmiddlewaretoken`. Expect to spend the first 72 hours building a custom ruleset to whitelist your Django admin paths and any endpoints with atypical payloads. The SQLi rules are solid but generic; we had to tune them down for our niche query patterns.
* **Elastic Cost vs. Predictability:** Cloudflare Pro ($20/mo/site) gets you the WAF and basic DDoS mitigation, but "advanced DDoS protection" requires Business ($200/mo/site). The real cost is egress: once you're behind them, all your traffic routes through their network. If your app serves large files or has high internal traffic, data transfer bills add up fast. Our bill jumped about 30%.
* **Deployment & Configuration Friction:** Took us about two weeks to fully trust it in block mode. The biggest gotcha was geo-blocking: we have users in sanctioned countries for payments, but they still need to browse. Cloudflare's IP geolocation isn't perfect; we had to create allow lists for specific ASNs. The API for rule management is good, but testing rules is still a manual, staged process.
* **Where It Wins Hands-Down:** The CDN integration and caching are stupid simple to set up and will soak up a huge amount of junk traffic before it hits your origin. For volumetric attacks, we've seen it absorb ~50k RPS without our GKE nodes even noticing. The managed rule updates are automatic, which is one less 3 a.m. page for my team.

3. I'd pick Cloudflare if your primary threat is volumetric DDoS and you need a "set and mostly forget" shield. If your bigger concern is highly tailored business logic attacks or you have strict data residency requirements that can't tolerate a reverse proxy, tell us your compliance regime and what percentage of traffic is API vs. user-facing.


Status page is my homepage.


   
ReplyQuote
(@devops_dad_joke)
Estimable Member
Joined: 4 months ago
Posts: 104
 

Oh man, the egress surprise is real. We almost got burned the same way with our API traffic. The "proxied" bandwidth cost can quietly eat your lunch, especially when your app is chatty with user uploads or real-time data.

Your comment about the browsable API tuning is spot on. Took me an afternoon of head-scratching before I realized Cloudflare's "HTTP Anomaly" rule set was murdering DRF's default OPTIONS requests. Had to build a rule just for our `/api/` path.

And geo-blocking messing with analytics... yeah, tell me about it. Had to set up a separate, unproxied subdomain just for our tracking pixel to keep marketing from panicking. 😅



   
ReplyQuote
(@startup_ceo_evan)
Active Member
Joined: 4 months ago
Posts: 11
 

Yeah, the session and CSRF issue is real. Cloudflare's managed rules flagged our Django admin login POSTs as malicious payloads right away. The csrfmiddlewaretoken field triggers some SQLi patterns.

You'll need a custom rule to skip the OWASP Core ruleset for your admin path and any API endpoints that use Django's default token names. Don't wait until post-migration, bake it into your initial config.

Also, watch for session cookie length. If your sessions are long, Cloudflare's default header size limits can clip them and cause silent logout. Had that happen once.



   
ReplyQuote