feat(gateway): behavioral anomaly detection for adaptive rate limiting (#615)#694
Merged
Smartdevs17 merged 1 commit intoJun 27, 2026
Merged
Conversation
Smartdevs17#615) Static per-IP / per-key limits miss distributed attacks (botnets, rotating IPs/keys). This adds unsupervised behavioral anomaly scoring that learns normal per-key usage and adaptively tightens limits when traffic looks anomalous. backend (TypeScript, jest): - backend/gateway/featureExtraction.ts — request rate, endpoint-distribution entropy, time-of-day, payload size, user-agent entropy, geographic spread. - backend/gateway/isolationForest.ts — dependency-free Isolation Forest (unsupervised), deterministic via seeded PRNG, score in [0,1]. - backend/gateway/anomalyDetector.ts — train on normal windows, score new ones. - backend/gateway/adaptiveRateLimit.ts — reduce limit 50% past the threshold (0.8), 90% past 0.95; allow-list (webhooks/health) + per-key override for false positives. - backend/gateway/middleware/adaptiveRateLimitMiddleware.ts — Express-compatible. - backend/monitoring/anomalyMetrics.ts — per-key anomaly-score Prometheus gauge. - 15 jest tests (forest, features, adaptive decisions, detector, metrics, middleware). ml-service (Python, FastAPI — mirrors the model, no new deps): - ml-service/anomaly/{isolation_forest,features,detector}.py — pure-Python port. - ml-service/routers/anomaly.py — /v1/anomaly train/score/status; registered in main. - ml-service/tests/test_anomaly.py — 6 tests. READMEs document covered criteria and documented follow-ups (Slack/PagerDuty alerting, admin dashboard screen, seasonal model + weekly retrain/drift alerts). Closes Smartdevs17#615
|
@Itodo-S Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Static per-IP / per-key limits are trivially bypassed by distributing requests across many IPs or rotating API keys. This adds unsupervised behavioral anomaly scoring that learns normal per-key usage and adaptively tightens limits when traffic looks anomalous.
Backend (TypeScript — jest-tested)
backend/gateway/featureExtraction.ts— features: request rate, endpoint-distribution entropy, time-of-day, payload size, user-agent entropy, geographic (distinct-IP) spread.backend/gateway/isolationForest.ts— dependency-free Isolation Forest (unsupervised), deterministic via a seeded PRNG, anomaly score in[0, 1].backend/gateway/anomalyDetector.ts— trains on normal-traffic windows, scores new ones.backend/gateway/adaptiveRateLimit.ts— reduces the limit 50% past the threshold (default0.8) and 90% past0.95; allow-list (webhooks/health) + per-key manual override for false positives.backend/gateway/middleware/adaptiveRateLimitMiddleware.ts— Express-compatible (sliding window per key, scoring, enforcement, headers).backend/monitoring/anomalyMetrics.ts— per-key anomaly-score Prometheus gauge.ml-service (Python/FastAPI — mirrors the model, no new deps)
ml-service/anomaly/{isolation_forest,features,detector}.py— pure-Python port (matches the existing no-sklearn dependency set).ml-service/routers/anomaly.py—/v1/anomalytrain/score/status, registered inmain.py. The gateway can score in-process or delegate here.Acceptance criteria
Testing
Scope note
This delivers the working, tested core of a 200-point issue. The remaining infra-heavy criteria are implemented as documented follow-ups (see
backend/gateway/README.md): real-time Slack/PagerDuty alerting on high-confidence attacks (the detector already surfaceshigh_confidence), the adminRateLimitDashboardScreen(mobile/app/screens/), and the seasonal model + weekly auto-retrain / drift alerting (ml-service/jobs/,retrain.py). Happy to follow up on any of these.Closes #615