RocketChat / RocketChat/Rocket.Chat
Backend [dataExport]: TypeError crash when user has empty emails array
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 46.1k
- Forks
- 13.9k
- Avg merge
- 3d 3h
- Merged PRs (30d)
- 130
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read apps/meteor/server/lib/dataExport/sendViaEmail.ts, starting around line 32, and compare the handling there with the later pattern around line 50. Add the unit test mentioned in the issue for a user with an empty emails array, then verify that the remaining users in the batch are still processed without a TypeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100