fix: prevent TypeError in /create_invite_link when checkInviteLink returns empty#277
Open
AmSach wants to merge 1 commit into
Open
fix: prevent TypeError in /create_invite_link when checkInviteLink returns empty#277AmSach wants to merge 1 commit into
AmSach wants to merge 1 commit into
Conversation
…turns empty Closes chthonn#234. The /create_invite_link route assumed checkInviteLink() always returns a non-empty array and immediately dereferenced response[0].invites. When the inviter was deleted, or the inviter simply had no matching invite for the requested server_id, that access threw: TypeError: Cannot read properties of undefined (reading 'invites') and crashed the request (uncaught promise rejection). Malformed inviter_id / server_id values also leaked Mongoose CastError messages to the client. Changes: - Guard the response[0] access; treat an empty aggregation as 'no matching invite' (which is the path that creates a new one). - Validate inviter_id and server_id are well-formed ObjectIds up front and return 400 if not. - Wrap each MongoDB call in its own try/catch and the whole handler in an outer try/catch so any unexpected error returns 500 instead of crashing the process. - Add a regression test script (server/scripts/run-invites-bug-repro.mjs) wired up as 'npm run test:invites'.
Contributor
|
@AmSach is attempting to deploy a commit to the Sunil Kumar's projects Team on Vercel. A member of the Team first needs to authorize it. |
Author
|
👋 Heads up — I'm an automated PR-fixer agent running for @AmSach. I cannot click the Vercel authorize link myself (it's a UI action that must be taken by a Team owner). A member of Sunil Kumar's projects Team needs to open this URL to authorize the deployment of commit Once authorized, Vercel will redeploy this PR automatically. Thanks! |
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
/create_invite_linkso it no longer crashes withTypeError: Cannot read properties of undefined (reading 'invites').inviter_id/server_idare well-formed ObjectIds and return a clean 400 otherwise.Why
Closes #234. The route assumed
checkInviteLink()always returns a non-empty array, but the aggregation can legitimately return[]when:server_id(this is the path that should create a new invite — not crash).The reproducer at
server/scripts/run-invites-bug-repro.mjsproves the original code throws on case (1) and case (2):Related Issue
Closes #234
Type of Change
Validation
cd server && npm run test:invites(5/5 cases ✅)cd server && npm run test:auth:unitnode --check server/src/routes/invites.jsMONGO_URI, which is normal for this project)How I verified
server/scripts/run-invites-bug-repro.mjsthat boots an in-memory MongoDB, seeds a user + invites, and exercises the route via real HTTP calls (not mocked).TypeErrorfrom the issue verbatim.inviter_id→ 400 (was: leaked CastError) ✅server_id→ 400 (was: leaked CastError) ✅inviter_id→ 200 with new code, never crashes ✅Notes for Reviewers
/create_invite_linkhandler and thetest:invitesnpm script. The other two handlers in the same file (/invite_link_info,/accept_invite) already have their own try/catch and were not part of the bug report.isValidObjectIdhelper rather than a per-validator middleware because (a) the validator chain only checksnotEmpty(), and (b) it keeps the diff small and the failure mode easy to read in the route body.mongodb-memory-serverdep is already indevDependencies— no new deps added.Closes #234