-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Description
Bug Description
In the data export email procedure, if a user has an explicitly empty emails array ([]), the loop crashes synchronously throwing a TypeError: Cannot read properties of undefined (reading 'address'). This interrupts the entire data export notification batch, causing subsequent users in the list to never receive their data export emails.
Steps to Reproduce
- The system attempts to process data exports for a list of users via
sendViaEmail. - One of the users in the
toUsersarray exists but has an emptyemailsarray (emails: []). - The server executes
apps/meteor/server/lib/dataExport/sendViaEmail.tsat line 32. - The backend process crashes with an unhandled TypeError, aborting the rest of the
.forEachloop.
Expected: The system should safely ignore or log the missing email address for that specific user and continue processing the remaining users in the batch.
Actual: The entire send loop aborts instantly on the first user with an empty emails array due to an unhandled TypeError.
Minimal Reproduction
// apps/meteor/server/lib/dataExport/sendViaEmail.ts (Line ~32)
// Simulated failing scenario:
const user = { username: 'test', emails: [] };
// Throws TypeError: Cannot read properties of undefined (reading 'address')
const emailAddress = user.emails?.[0].address; Environment
- Rocket.Chat version: Develop branch (latest)
Root Cause
In apps/meteor/server/lib/dataExport/sendViaEmail.ts:
const emailAddress = user.emails?.[0].address;If user.emails is an empty array [], then user.emails?.[0] evaluates to undefined. Accessing .address on undefined causes a fatal TypeError.
Possible Fix
Add safe optional chaining when accessing the address property:
const emailAddress = user.emails?.[0]?.address;Additional Context
I discovered this via static code analysis while hunting for missing optional chains. This same file uses the correct user.emails?.[0]?.address pattern a few lines later on line 50. I am preparing a simple PR to add the missing ? and include a unit test covering this exact edge case.