Skip to content

Backend [dataExport]: TypeError crash when user has empty emails array #39185

@Harshit2405-2004

Description

@Harshit2405-2004

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

  1. The system attempts to process data exports for a list of users via sendViaEmail.
  2. One of the users in the toUsers array exists but has an empty emails array (emails: []).
  3. The server executes apps/meteor/server/lib/dataExport/sendViaEmail.ts at line 32.
  4. The backend process crashes with an unhandled TypeError, aborting the rest of the .forEach loop.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions