Skip to content

Evict idle token-bucket entries to prevent unbounded map growth#610

Merged
winhowes merged 1 commit intomainfrom
codex/propose-fix-for-token-bucket-vulnerability
Apr 13, 2026
Merged

Evict idle token-bucket entries to prevent unbounded map growth#610
winhowes merged 1 commit intomainfrom
codex/propose-fix-for-token-bucket-vulnerability

Conversation

@winhowes
Copy link
Copy Markdown
Owner

Motivation

  • The token_bucket rate limiter allocated a per-key tokenBucket entry and never removed it, allowing an attacker to cause unbounded memory growth by using many unique keys.
  • The intent is to retain token-bucket behavior for active callers while preventing permanent growth from stale/idle keys.

Description

  • Add a periodic cleanup loop in NewRateLimiter for the token_bucket strategy that creates a time.Ticker and deletes bucket entries whose last timestamp is older than the configured window (duration).
  • The cleanup loop runs alongside existing reset logic and stops via the existing done channel so limiter lifecycle and Stop() behavior remain unchanged.
  • Add a regression test TestTokenBucketEvictsIdleBuckets in app/ratelimiter_test.go that verifies idle token-bucket entries are evicted after the configured window.
  • Modified files: app/main.go and app/ratelimiter_test.go.

Testing

  • Ran the package test suite with go test ./app and all tests passed successfully.
  • The new TestTokenBucketEvictsIdleBuckets was executed as part of the test run and passed.

Codex Task

@winhowes winhowes merged commit 0f8ae04 into main Apr 13, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant