fix: enforce 401 in RequireAuthentication when session is invalid#1955
fix: enforce 401 in RequireAuthentication when session is invalid#1955
Conversation
Previously the middleware always called next() regardless of whether authentication succeeded, allowing requests with expired or missing cookies to reach handlers with undefined owner, causing Knex binding errors. Added a test to cover both authenticated and unauthenticated cases. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 50 minutes and 45 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughA new test suite was added for the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/routes/middleware/RequireAuthentication.test.ts (1)
37-46: Strengthen the happy-path assertion to prevent false positives.At Line 45, asserting only
next()can miss cases where a response is also sent. Captureresand assertstatus/sendwere not called.Suggested test hardening
it('calls next when the user is authenticated', async () => { mockedAuthService.prototype.getUserFrom = jest .fn() .mockResolvedValue({ id: 1, owner: 1, email: 'test@test.com' }); + const res = mockResponse(); const next = jest.fn(); - await RequireAuthentication(mockRequest(), mockResponse(), next); + await RequireAuthentication(mockRequest(), res, next); expect(next).toHaveBeenCalled(); + expect(res.status).not.toHaveBeenCalled(); + expect(res.send).not.toHaveBeenCalled(); });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/routes/middleware/RequireAuthentication.test.ts` around lines 37 - 46, The test for RequireAuthentication currently only asserts next() was called which can miss cases where a response was also sent; change the test to capture the mocked response object (use mockResponse()), pass that res into RequireAuthentication alongside mockRequest() and next, and then assert that res.status and res.send (or res.json if used by middleware) were not called in addition to expect(next).toHaveBeenCalled(); this ensures the happy path does not inadvertently send a response.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/routes/middleware/RequireAuthentication.ts`:
- Around line 30-32: The current auth gate uses a falsy check on
res.locals.owner and will block valid values like 0; change the check in
RequireAuthentication (where res.locals.owner is evaluated) to a nullish check
that only rejects null or undefined (e.g., test for owner == null or owner ===
undefined) so legitimate falsy IDs are allowed, and keep the same 401 response
path when owner is null/undefined.
---
Nitpick comments:
In `@src/routes/middleware/RequireAuthentication.test.ts`:
- Around line 37-46: The test for RequireAuthentication currently only asserts
next() was called which can miss cases where a response was also sent; change
the test to capture the mocked response object (use mockResponse()), pass that
res into RequireAuthentication alongside mockRequest() and next, and then assert
that res.status and res.send (or res.json if used by middleware) were not called
in addition to expect(next).toHaveBeenCalled(); this ensures the happy path does
not inadvertently send a response.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 19feec3e-43d3-4824-ab16-02fc01ba0bdc
📒 Files selected for processing (2)
src/routes/middleware/RequireAuthentication.test.tssrc/routes/middleware/RequireAuthentication.ts
Replaced !res.locals.owner with res.locals.owner == null so legitimate falsy owner values are not incorrectly rejected. Also improved the authenticated test case to assert no response is sent on the happy path. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|



Previously the middleware always called next() regardless of whether authentication succeeded, allowing requests with expired or missing cookies to reach handlers with undefined owner, causing Knex binding errors. Added a test to cover both authenticated and unauthenticated cases.
Summary by CodeRabbit
Bug Fixes
Tests