Skip to content

Prevent crash in Convex after-hook matcher when ctx.path is undefined#256

Open
bitojoe wants to merge 1 commit intoget-convex:mainfrom
bitojoe:fix/server-side-API-calls
Open

Prevent crash in Convex after-hook matcher when ctx.path is undefined#256
bitojoe wants to merge 1 commit intoget-convex:mainfrom
bitojoe:fix/server-side-API-calls

Conversation

@bitojoe
Copy link

@bitojoe bitojoe commented Feb 11, 2026

Summary

This PR fixes a TypeError: Cannot read properties of undefined (reading 'startsWith') that occurs when using the @convex-dev/better-auth plugin's server-side API (e.g., calling api.addMember from a Convex mutation).

The Problem

When Better Auth is invoked server-side via the API rather than an HTTP request, the ctx.path can be undefined. The first after hook matcher in the Convex plugin was checking ctx.path.startsWith(...) without a guard, causing the entire process to crash.

The Fix

Added optional chaining (ctx.path?.startsWith) to the matcher. This ensures that:

  • If ctx.path is missing, the matcher safely returns false.
  • The hook is skipped instead of throwing an error.
  • The logic is now consistent with other defensive matchers already present in the codebase.

Fixes #254

How to Test

  1. Set up a Convex project with @convex-dev/better-auth.
  2. Call an auth API method (like api.addMember) from within a Convex mutation.
  3. Before fix: The mutation fails with a TypeError: Cannot read properties of undefined.
  4. After fix: The mutation completes successfully.

Checklist

Summary by CodeRabbit

  • Bug Fixes
    • Improved stability of path validation in middleware by making it more defensive against undefined values, preventing potential runtime errors.

@vercel
Copy link

vercel bot commented Feb 11, 2026

@bitojoe is attempting to deploy a commit to the Convex Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Feb 11, 2026

📝 Walkthrough

Walkthrough

The Convex plugin's post-middleware matcher is updated to use optional chaining when accessing the ctx.path property, changing .startsWith() calls to ?.startsWith() to prevent potential runtime errors when the path is undefined.

Changes

Cohort / File(s) Summary
Optional Chaining in Post-Middleware Matcher
src/plugins/convex/index.ts
Replaces direct property access with optional chaining across multiple path checks (sign-in, sign-up, callback, oauth2/callback, magic-link/verify, email-otp/verify-email, phone-number/verify, siwe/verify, and /get-session routes) to safely handle cases where ctx.path may be undefined.

Possibly related issues

Poem

🐰 A hop and a skip, a chaining fix!
Optional safety in the mix,
No more undefined path mistakes,
This rabbit's code now gently wakes,
With gentle checks, we're safe today! 🌟

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding defensive checks to prevent a crash when ctx.path is undefined in the Convex plugin's matcher.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

No actionable comments were generated in the recent review. 🎉

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convex plugin after-hook matcher throws when ctx.path is undefined (server-side API calls)

1 participant