Skip to content

feat: harden limiter logic and expand Redis#639

Open
Saurabhsing21 wants to merge 1 commit intojlab-sensing:mainfrom
Saurabhsing21:api-limit
Open

feat: harden limiter logic and expand Redis#639
Saurabhsing21 wants to merge 1 commit intojlab-sensing:mainfrom
Saurabhsing21:api-limit

Conversation

@Saurabhsing21
Copy link
Copy Markdown
Contributor

@Saurabhsing21 Saurabhsing21 commented Feb 23, 2026

Summary

This PR improves the API rate-limit system for security, reliability, and maintainability.
It also simplifies maintainer setup to 2–3 core inputs while keeping advanced overrides available.

Issue

#526
The initial implementation worked, but had practical gaps:

  • IP-based limits could be weakened if proxy/IP trust was misconfigured.
  • Timing consistency needed stronger Redis-side guarantees.
  • Some fallback/misconfiguration paths were too quiet.
  • Config required too many env vars for normal usage.
  • Test coverage missed key edge cases and deeper Redis behavior.

What Changed

  • Hardened token bucket behavior and logging in /ENTS-backend/backend/api/rate_limit.py.
  • Kept Redis Lua execution atomic and validated Redis-time-based behavior.
  • Added safer rule fallback and misconfiguration warning behavior.
  • Added blocked-request logging with redaction for sensitive identity values.
  • Added memory-backend cleanup/expiry protections to avoid growth issues.
  • Ensured rate-limit decorators are applied across uncovered resource methods.
  • Added ProxyFix-driven trusted proxy support in /ENTS-backend/backend/api/__init__.py.
  • Added JWT audience/issuer config support in /ENTS-backend/backend/api/config.py.

Config Simplification (2–3 Inputs)

A new profile-based model was introduced in /ENTS-backend/backend/api/config.py:

  • RATE_LIMIT_ENABLED
  • RATE_LIMIT_STORAGE_URI
  • RATE_LIMIT_PROFILE (strict, balanced, relaxed)

Optional:

  • RATE_LIMIT_TRUSTED_PROXY_COUNT (only when behind trusted proxies)
  • Per-rule RATE_LIMIT_<RULE>_CAPACITY and RATE_LIMIT_<RULE>_REFILL_RATE (advanced tuning only)

Docs and examples updated:

  • /Users/apple/Projects/ENTS-backend/.env.example
  • /Users/apple/Projects/ENTS-backend/backend/README.md

Test Coverage

Expanded tests in /ENTS-backend/backend/tests/test_rate_limit.py:

  • Existing behavior checks (capacity, headers, fallback, fail-open).
  • Edge cases for identity priority, missing-rule behavior, rule normalization, retry rounding, expiry/reset.
  • Redis backend unit coverage via mocks.
  • Redis integration coverage against local Redis when available.

Validation

  • ruff on touched files: passed.
  • pytest /ENTS-backend/backend/tests/test_rate_limit.py: passed.
  • Redis-enabled run: passed.

Backward Compatibility

  • Default behavior remains availability-first (fail-open on backend runtime errors).
  • Advanced per-rule env overrides are still supported.
  • New RATE_LIMIT_PROFILE reduces setup complexity without removing tuning flexibility.

Reviewer Notes

Please focus on:

  1. Profile defaults (strict / balanced / relaxed) for production traffic expectations.
  2. RATE_LIMIT_TRUSTED_PROXY_COUNT value per deployment topology.
  3. Final per-rule override policy (if any) for your environment.

- Use Redis TIME in Lua for consistent token refill timing.
- Secure IP identity handling with ProxyFix-based trust configuration.
- Add fallback/warning behavior for missing or invalid rate-limit rules.
- Add blocked-request logging with API-key redaction.
- Add memory backend cleanup/eviction and expiry behavior.
- Add optional JWT audience/issuer validation in identity extraction.
- Add decorators to uncovered resources and document env knobs.
- Expand tests for edge cases and Redis backend behavior.

Signed-off-by: Saurabhsing21 <saurabhsingh881888@gmail.com>
@Saurabhsing21
Copy link
Copy Markdown
Contributor Author

Hi @aleclevy Can you review this pr , this is not 100% perfect but pls review this and let me know what i have to improve

@Saurabhsing21
Copy link
Copy Markdown
Contributor Author

@aleclevy Can you review this

@jmadden173 jmadden173 added the awaiting-review For PRs that have been looked at and waiting for a review. label Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-review For PRs that have been looked at and waiting for a review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants