fix(faucet): rate-limit the faucet to prevent testnet drain - #2
Merged
Conversation
POST /faucet had no rate limiting, so any caller could repeatedly drain the funded testnet faucet account. Add an in-memory cooldown limiter (std only, no new dependency) keyed by: - client IP: 30s window (anti-spam), read via realip_remote_addr so it uses X-Forwarded-For behind the nginx reverse proxy, not the proxy IP; - recipient address: 1h window (anti-refund drain), case-insensitive. Rejected requests return 429 with a Retry-After header and do not record a hit, so a blocked caller cannot push its own cooldown forward. The limiter is created once and shared across all Actix workers (a per-worker instance would multiply the limit by the worker count). The map self-prunes to the longest window to stay bounded. Cooldowns are fixed constants for now (marked with ponytail: comments as the tuning knob). Captcha/PoW and per-period amount caps are out of scope: an attacker rotating both IP and address still bypasses a keyless faucet. Adds unit tests for the cooldown and case-insensitivity behavior. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
What
Rate-limits
POST /faucetto stop testnet faucet drain. Previously anycaller could hit the endpoint repeatedly and drain the funded faucet account.
How
In-memory cooldown limiter (
src/hub/rate_limit.rs),stdonly — no new dependency:realip_remote_addr()so itreads
X-Forwarded-For/X-Real-IPbehind the nginx reverse proxy, not the proxy IP.Rejected requests return
429 Too Many Requests+Retry-After. A rejected hit isnot recorded, so a blocked caller can't push its own cooldown forward. The limiter
is instantiated once and shared across all Actix workers (per-worker would multiply the
limit by worker count). The map self-prunes to the longest window.
Out of scope
ponytail:comments).still bypasses a keyless faucet.
Testing
cargo test— 21 passed (19 existing + 2 new: cooldown + case-insensitivity).