Skip to content

Guard pending chat members against missing account IDs#96747

Draft
wildan-m wants to merge 4 commits into
Expensify:mainfrom
wildan-m:wildan/95348-pending-chat-members-undefined-toString
Draft

Guard pending chat members against missing account IDs#96747
wildan-m wants to merge 4 commits into
Expensify:mainfrom
wildan-m:wildan/95348-pending-chat-members-undefined-toString

Conversation

@wildan-m

@wildan-m wildan-m commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

A crash reaches Sentry from the report route as an unhandled promise rejection: TypeError: undefined is not an object (evaluating 'p.toString'). The helper that assembles a report's pending chat members converts each incoming account ID to a string without first checking that the ID is there. Its parameter is declared as a list of numbers, but nothing enforces that at runtime — the member add and remove flows derive those IDs from personal-detail lookups, and an entry that has not finished loading contributes a missing value. Converting that missing value throws, and because the member actions are dispatched from optimistic-update code the throw surfaces as an unhandled rejection instead of a render error, which is why the production stack has no useful frame.

Dropping the missing IDs before the conversion removes the only expression that can throw, and it also keeps a member entry with an empty account ID out of the report's metadata — the alternative of falling back to an empty string would have stored one. Putting the guard in the shared helper rather than at each call site covers the room invite and removal flows and the workspace member flows uniformly, and it leaves behavior for well-formed input unchanged.

Fixed Issues

$ #95348
PROPOSAL: #95348 (comment)

Tests

The Sentry report has no reproduction steps — the failure depends on a personal-detail entry that has not finished loading, so it cannot be triggered on demand. The unit tests added in this PR cover the missing-ID case directly (they throw the same TypeError without this change). The steps below confirm the member flows that call the helper are unaffected:

  1. Sign in, open Workspaces, select a workspace, and open Members.
  2. Click Invite member, select any contact, click Next, then Invite.
  3. Verify the invited member appears in the list, the total member count increases, and no error appears in the JS console.
  4. Tick the invited member's checkbox, open the selection dropdown, choose Remove member, and confirm.
  5. Verify the member disappears from the list, the workspace chat reports the member as removed, and no error appears in the JS console.
  6. Open a room from the Inbox, open its member list, invite and then remove a member, and verify the list updates the same way with a clean console.

Direct check of the guard (console script)

Because the crash depends on runtime data that cannot be produced on demand through the UI, this script calls the real bundled getPendingChatMembers out of the running dev build and hands it a missing account ID. It is the same script on both branches — only the result differs. It needs a local dev build (npm run web); it will not work against a production bundle.

Open any chat (so ReportUtils is loaded), then paste this into the DevTools console:

(() => {
    const chunkKey = Object.keys(window).find((k) => /^(rspack|webpack)Chunk/.test(k) && Array.isArray(window[k]));
    if (!chunkKey) {
        return 'FAILED: no bundler registry on window. Run a local dev build (npm run web) — this does not work against a production bundle.';
    }
    let req;
    window[chunkKey].push([['probe_' + Math.random()], {}, (r) => { req = r; }]);
    if (!req || !req.c) {
        return 'FAILED: could not obtain the module registry.';
    }
    let fn = null;
    for (const id of Object.keys(req.c)) {
        const ex = req.c[id] && req.c[id].exports;
        if (!ex) continue;
        try {
            if (typeof ex.getPendingChatMembers === 'function') { fn = ex.getPendingChatMembers; break; }
        } catch (e) { /* some exports are getters that throw */ }
    }
    if (!fn) {
        return 'FAILED: getPendingChatMembers not loaded yet. Open any chat first, then re-run.';
    }
    const run = (ids) => {
        try { return JSON.stringify(fn(ids, [], 'add')); } catch (e) { return e.name + ': ' + e.message; }
    };
    const control = run([101, 102]);
    const missing = run([101, undefined, 102]);
    const crashed = missing.indexOf('TypeError') === 0;
    console.log('control [101, 102]            -> ' + control);
    console.log('repro   [101, undefined, 102] -> ' + missing);
    return crashed ? 'CRASHES — unpatched (this is main)' : 'NO CRASH — guard present (this is the PR branch)';
})()

On main it returns CRASHES — unpatched (this is main) and logs:

control [101, 102]            -> [{"accountID":"101","pendingAction":"add"},{"accountID":"102","pendingAction":"add"}]
repro   [101, undefined, 102] -> TypeError: Cannot read properties of undefined (reading 'toString')

On this branch it returns NO CRASH — guard present (this is the PR branch) and logs:

control [101, 102]            -> [{"accountID":"101","pendingAction":"add"},{"accountID":"102","pendingAction":"add"}]
repro   [101, undefined, 102] -> [{"accountID":"101","pendingAction":"add"},{"accountID":"102","pendingAction":"add"}]

The control case passing on both builds is the point — the only behavioural difference is the missing-ID case. The TypeError text above is Chrome's wording; Safari words the same error undefined is not an object (evaluating 'p.toString'), which is the form recorded in Sentry.

Scope note: this exercises the guard directly by passing the value in. It does not demonstrate the app producing that state on its own, and the reporting Sentry stack is minified, so it is not proof that this helper is the frame that crashed for the reporter.

  • Verify that no errors appear in the JS console

Offline tests

The change touches only the optimistic pending-member list and adds no network calls. Inviting or removing a member while offline queues the action and shows the member in the pending state exactly as it did before.

QA Steps

Same as tests.

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

There is no visual change to capture — this is a defensive guard on a value that is normally present.

Verified on the local dev server against the steps above: inviting a member raised the workspace total from 1 to 2 and the member rendered with its role, removing the member returned the total to 1 and posted the removal message to the workspace chat, and the JS console stayed clean throughout.

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.

1 participant