feat(security): API security middleware, pipeline guard, and OWASP scanner#34
Merged
Goldokpa merged 3 commits intoMay 5, 2026
Merged
Conversation
OWASP-aligned controls layered onto FastAPI: - Per-API-key rate limiter (sliding window, configurable) - Payload size and Content-Length checks - bbox sanity validation (range, ordering, max area to block DoS-via-huge-GEE-queries) - File upload validation by magic bytes + extension whitelist - String input sanitisation against XSS, SQLi, template injection patterns - Security response headers (X-Content-Type-Options, X-Frame-Options, X-XSS-Protection)
InputAnomalyDetector flags suspicious tiles before model forward pass: - Out-of-range pixel values, NaN/Inf, suspicious uniformity - Gradient analysis to catch noise injection or constant-image attacks PipelineGuard wraps inference with input + output checks: - Rejects predictions where one class dominates >99% (model failure or attack) - Flags low mean confidence and uniform probability distributions - Returns structured security metadata alongside predictions so the API can surface warnings to the caller.
- tests/test_security.py: unit tests for the rate limiter, payload/bbox/file validators, sanitiser, pipeline guard input/output checks, and adversarial detection helpers. - scripts/security_scan.py: external scanner that hits a running API and probes for OWASP-style misconfigurations (missing headers, unauthenticated POSTs, bbox over-area, oversized payloads). Outputs a JSON report.
obielin
approved these changes
May 5, 2026
Collaborator
obielin
left a comment
There was a problem hiding this comment.
Heavier overlap with my security/ territory than I'd like, but the implementation is solid — the OWASP scanner script is genuinely useful and the api_security middleware covers the input-sanitisation gap I had on my sprint backlog. I'm happy to inherit ownership going forward and layer my pipeline_guard adversarial-detection work on top in a follow-up.
Approving — let's merge and split future security work explicitly: input/middleware stays in your lane, ML-adversarial / model governance stays in mine.
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.
Summary
src/climatevision/security/api_security.py— OWASP-aligned FastAPI middleware:X-Content-Type-Options,X-Frame-Options,X-XSS-Protection)src/climatevision/security/pipeline_guard.py—InputAnomalyDetector+PipelineGuard:tests/test_security.py— unit tests for the rate limiter, validators, sanitiser, and pipeline guard.scripts/security_scan.py— external scanner that probes a running API for OWASP misconfigurations (missing headers, unauthenticated POSTs, oversized payloads, bbox over-area). Outputs a JSON report.Why
Closes the auth-enforcement primitives in gap #4 and lays the foundation for gap #5 (alert delivery integrity) and gap #7 (test coverage on security-sensitive paths).
The pipeline guard is the second line of defence after schema validation — it catches adversarial tiles that pass the input contract but break model assumptions (NaN injection, gradient-spike attacks, uniform-region patches). The output checks flag single-class-dominant predictions that often indicate either an attack or a model failure, before the result reaches the dashboard.
The OWASP scanner is intended to run in CI against a staged deployment and gate releases.
Test plan
pytest tests/test_security.py -vpassespython scripts/security_scan.py --target http://localhost:8000produces a JSON report413response/api/predict101 times in one minute from one API key and confirm429withRetry-AfterNote
Defence-in-depth — this does not replace the per-endpoint
X-API-Keyenforcement work inapi/auth.py(gap #4 proper, separate PR).