Summary PerformanceMonitor is always on in the default enhanced detection mode: SusPatternsManager.detect() calls record_metric() after every pattern check
Description
PerformanceMonitor is always on in the default enhanced detection mode: SusPatternsManager.detect() calls record_metric() after every pattern check. Once a pattern's recent_times window reaches min_samples_for_anomaly, record_metric ran statistics.mean and statistics.stdev over that window on every call. Those functions use exact-Fraction arithmetic, so each scanned request value paid a CPU cost that is a function of the monitor's window, not of the request, and that no configuration option bounded. Measured on 3.13.0: about 4.3 ms per scanned value in legacy mode against about 46.6 ms per value in enhanced mode once warm, an 11x tax on all production traffic. Combined with the absence of any per-request value bound (GHSA-3hfx-8m47-5f9h) this let a single request with many values hold the event loop for tens of seconds, and it made the reporter's GHSA-3hfx timings 2x to 11x too low for a warmed production process.
A second defect in the same component turned the tax into an unbounded scan under real OS threads. record_metric appended to recent_times under the monitor lock but the anomaly check iterated the same deque after the lock was released. A concurrent record_metric (the sync tree under gunicorn --threads, or any threaded WSGI deployment through flaskapi-guard or djapi-guard) could append mid-iteration and raise RuntimeError: deque mutated during iteration out of detect(). The enhanced-scan exception fallback then ran pattern.search() directly on every compiled pattern with no scan window and no timeout, re-exposing the quadratic built-in patterns that GHSA-r7hm-rjvg-xx7j had bounded (about 30 s of CPU per thread on the r7hm payload).
Impact
- Unauthenticated, remote, default configuration. No option disables the monitor's statistical check.
- Availability only: CPU exhaustion on the worker; in the threaded case a single request pair can reproduce the race and the subsequent unbounded scan.
- Async deployments (fastapi-guard) are affected by the CPU tax; threaded sync deployments (flaskapi-guard, djapi-guard, any
guard_core.syncconsumer) are affected by both.
Fix
3.14.0 replaces the exact-Fraction statistics with math.fsum-based float mean and sample variance (identical anomaly decisions; 300 record_metric calls drop from about 47 ms to about 13 ms, and the end-to-end detection benchmark from about 54 s to about 20 s of CPU), runs the anomaly check inside the same lock as the append that feeds it, snapshots the reporting getters under the lock (they become coroutines in the async tree), and routes the exception fallback through the same bounded matchers the enhanced scan uses. The new SecurityConfig.detection_max_scan_values cap (GHSA-3hfx-8m47-5f9h) bounds the number of values a request can push through the monitor.
Workarounds
For users who cannot upgrade: place a request-body size limit and a per-client rate limit in front of the application; threaded sync deployments can run one thread per worker process to avoid the race. detection_mode="legacy" avoids the monitor but loses the enhanced-mode detection coverage.
Credit
Found by the maintainers while verifying GHSA-3hfx-8m47-5f9h (reported by @RomanPszonka); the threaded race surfaced in the 3.14.0 CI run.
Status
Fixed in guard-core 3.14.0 (PyPI), merged in #82 (500d4ef6). Float anomaly statistics under the monitor lock, reporting getters snapshot under the lock (coroutines in the async tree), exception fallback routed through the bounded matchers, and detection_max_scan_values bounding the values a request can push through the monitor.