fix: parse Authorization header directly in rate limiter getKey()#58
Open
hackall360 wants to merge 1 commit intomoltbook:mainfrom
Open
fix: parse Authorization header directly in rate limiter getKey()#58hackall360 wants to merge 1 commit intomoltbook:mainfrom
hackall360 wants to merge 1 commit intomoltbook:mainfrom
Conversation
The rate limiter's getKey() relied on req.token, which is only set by requireAuth middleware. Since the global requestLimiter runs before route-specific auth middleware (via router.use in routes/index.js), req.token is always undefined when getKey() executes. This causes all authenticated requests to be keyed by IP instead of by token, leading to shared rate limit buckets and spurious 401/429 responses. Fix: Parse the Authorization header directly in getKey() instead of depending on req.token. Falls back to req.token (if set), then IP, then 'anonymous'. Adds 7 tests covering the fix, including exact reproduction of issue moltbook#5. Fixes moltbook#5, moltbook#8, moltbook#16, moltbook#28, moltbook#33, moltbook#34, moltbook#55
rel770
reviewed
Feb 3, 2026
rel770
left a comment
There was a problem hiding this comment.
Review: Rate Limiter Fix
Root cause analysis is spot-on! This is the kind of careful debugging that makes platforms stable.
Problem Identification:
requestLimiter runs BEFORE requireAuth
→ req.token is undefined
→ All requests keyed by IP
→ Shared buckets, spurious 429s, cascading 401s
Fix Analysis:
The fix correctly extracts the token from Authorization header directly:
const authHeader = req.headers.authorization;
if (authHeader && authHeader.startsWith("Bearer ")) {
identifier = authHeader.substring(7);
}Fallback chain: Authorization header → req.token → IP → anonymous
Strengths:
- ✅ Root cause identified correctly
- ✅ 7 comprehensive tests added
- ✅ Handles edge cases (non-Bearer, missing header)
- ✅ Documents prior attempts (#6, #32, #49) that weren't merged
Note: This appears to be the same fix as PR #65 - maintainers should coordinate which to merge.
Human-AI Collaboration:
Review by copilotariel (Claude Opus 4.5) + Ariel. Building open source together at github.com/copilotariel/humanai-community.
LGTM - please merge one of the rate limiter fixes! 🦞
— copilotariel
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 the root cause of the platform-wide 401 errors on interaction endpoints (comments, upvotes, subscribe, follow, DMs).
Problem
getKey()insrc/middleware/rateLimit.jsrelies onreq.token, which is only set byrequireAuthmiddleware. The globalrequestLimiteris applied viarouter.use()inroutes/index.jsbefore any route-specificrequireAuthruns. This meansreq.tokenis alwaysundefinedwhengetKey()executes for the global rate limiter.As a result, all authenticated requests get keyed by IP address instead of by token, causing:
Fix
Parse the
Authorizationheader directly ingetKey()instead of depending onreq.token:Fallback chain: Authorization header -> req.token -> IP -> anonymous
Tests
Added 7 new tests in
test/api.test.jscovering:req.tokenis undefinedAll 21 tests pass (14 existing + 7 new).
Issues Fixed
Closes #5, closes #8, closes #16, closes #28, closes #33, closes #34, closes #55
Notes
I found at least 3 prior PRs (#6, #32, #49) that identified this same root cause but were never merged. This PR includes comprehensive tests to prevent regression.