fix: parse Authorization header directly in rate limiter getKey() #58
+249
−100
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.