fix(security) : Add email validation, rate limiting & harden DEBUG default#502
Open
prince-shakyaa wants to merge 2 commits into
Open
Conversation
…ault - Validate email format on POST /auth/magic-link using pydantic EmailStr before writing to DB or calling the email service. Invalid addresses now return a user-friendly error page instead of propagating garbage to the DB. - Add a per-IP sliding-window rate limiter (5 req / 60 s) on the magic-link endpoint using stdlib only (no new dependencies). Prevents email flooding, inbox harassment, and unbounded MagicLinkToken table growth. A TODO comment documents the Redis-backed upgrade path for multi-worker use. - Change DEBUG default from True to False in config.py so cloned instances do not silently start in hot-reload / verbose-traceback mode. Developers enable it explicitly via DEBUG=true in .env (already documented in .env.example). Fixes #___
Author
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
Fixes two key security issues in the authentication layer:
Note
Finding 3 (Insecure
DEBUGDefault) Excluded: After thorough evaluation, changing the default configuration fallback toDEBUG=Falsehas been intentionally omitted from this PR. Changing it toFalseby default breaks local Capture-the-Flag (CTF) environments out of the box (e.g., SSRF validation strictly blockslocalhostand private IP webhooks whenDEBUGisFalse). To maintain an excellent local developer/player experience while preserving safety,DEBUGremainsTrueby default, and production environments should continue to explicitly override it usingDEBUG=falsein their.envfile as documented.Fixes #501
Changes
finbot/apps/finbot/auth.py1. Email format validation - Rejects non-email strings before hitting the DB or email service.
2. Per-IP rate limiting - Sliding-window counter (5 req / 60 s) using only stdlib.
No new dependencies added. Comment in code explains how to upgrade to Redis-backed
slowapifor multi-worker deployments.Why This Matters
Testing
POST /auth/magic-linkwithemail=not-an-email→ returns validation error pagePOST /auth/magic-linkcalled 6× in under 60 s from same IP → 6th request returns rate-limit error pageNotes for Reviewers
# TODOcomment is left in code.pydantic[email]is already listed inpyproject.toml- no new dependency added.DEBUGdefault inconfig.pywas kept asTrueto ensure local CTF challenge compatibility.