Skip to content

Skip malformed lines in the pusher-js HTTP stream instead of throwing - #102752

Merged
mountiny merged 3 commits into
mainfrom
claude-ignoreMalformedPusherHttpFrames
Oct 4, 2026
Merged

mountiny merged 3 commits into
mainfrom
claude-ignoreMalformedPusherHttpFrames

Conversation

@MelvinBot

@MelvinBot MelvinBot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

When WebSockets fail, pusher-js falls back to its xhr_streaming / xhr_polling transports. These transports split the response into lines and pass any line that starts with o, a, m, or c to JSON.parse without a try/catch. Forcepoint DLP adds its own script (class FPClassifier {) to Pusher's application/javascript stream. pusher-js reads that line as a close frame, JSON.parse throws out of the XHR onprogress handler, and Sentry reports it (APP-27B).

This PR adds a patch for pusher-js@8.3.0 (dist/web/pusher-with-encryption.js, the file App loads through pusher-js/with-encryption):

  • A line that fails to parse is skipped. Nothing is emitted for it, because an error event would reach Log.alert through PusherConnectionManager. Valid frames in the same response are still handled.
  • A c frame whose payload is not an array is also skipped, because a real close frame always carries [code, reason].
  • Only the JSON.parse call is guarded. onOpen, onEvent, and onClose are not wrapped, so errors in App's channel callbacks still surface.

Upstream pusher-js master has the same unguarded parse, so upgrading would not fix this. patches/pusher-js/details.md documents this.

tests/unit/PusherHttpSocketTest.ts drives the real pusher-js HTTP streaming socket with a fake XHR. It feeds a class FPClassifier { line before the o and a frames and checks that onopen and onmessage fire in the same progress event and that onclose fires when the response ends. A second test checks that a non-array c payload does not close the socket. Both tests fail without the patch (SyntaxError) and pass with it.

Fixed Issues

$ #102745
PROPOSAL: #102745 (comment)

Tests

  1. Open https://dev.new.expensify.com:8082/ and sign in.

  2. Open DevTools → Console and paste the script below. Verify the Console shows [APP-27B test] Forcepoint simulation active.

    (function () {
        // Make Pusher's WebSocket fail so pusher-js falls back to xhr_streaming, like the affected users.
        var NativeWebSocket = window.WebSocket;
        function TestWebSocket(url, protocols) {
            var target = String(url).indexOf('pusher.com') !== -1 ? 'wss://127.0.0.1:9' : url;
            return protocols === undefined ? new NativeWebSocket(target) : new NativeWebSocket(target, protocols);
        }
        TestWebSocket.prototype = NativeWebSocket.prototype;
        ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'].forEach(function (key) {
            TestWebSocket[key] = NativeWebSocket[key];
        });
        window.WebSocket = TestWebSocket;
    
        // Simulate Forcepoint DLP adding its script to the start of Pusher's fallback stream.
        var descriptor = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'responseText');
        var open = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function (method, url) {
            this.shouldInjectForcepoint = String(url).indexOf('/xhr_streaming') !== -1;
            return open.apply(this, arguments);
        };
        Object.defineProperty(XMLHttpRequest.prototype, 'responseText', {
            configurable: true,
            get: function () {
                var text = descriptor.get.call(this);
                return this.shouldInjectForcepoint && text ? 'class FPClassifier {\n' + text : text;
            },
        });
        console.log('[APP-27B test] Forcepoint simulation active');
    })();
  3. Sign out, then sign back in in the same tab, without reloading.

  4. Verify the Console does not show Uncaught SyntaxError: Unexpected token 'l', "lass FPClassifier {" is not valid JSON from pusher-with-encryption.js.

  • Verify that no errors appear in the JS console

Offline tests

N/A. This change only affects how pusher-js parses its HTTP fallback stream. Offline behavior is unchanged.

QA Steps

Same as tests, using https://staging.new.expensify.com/ instead of the dev URL.

  • 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.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • 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

Screenshots and videos will be added in the reviewer checklist.

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

Co-authored-by: Abdelrahman Khattab <abzokhattab@users.noreply.github.com>
@MelvinBot
MelvinBot requested a review from a team October 1, 2026 10:49
@MelvinBot

Copy link
Copy Markdown
Contributor Author

I created this PR from an upstream branch since I don't have push access to your fork.

To take ownership of this branch and be able to push updates, run:

git remote add upstream https://github.com/Expensify/App.git
git fetch upstream claude-ignoreMalformedPusherHttpFrames
git checkout -b claude-ignoreMalformedPusherHttpFrames upstream/claude-ignoreMalformedPusherHttpFrames
git push -u origin claude-ignoreMalformedPusherHttpFrames

Then you can close this PR and open a new one from your fork.

@abzokhattab

Copy link
Copy Markdown
Contributor

@MelvinBot complete the checklist and test steps, and mark screenshots as done since they’ll be added in the reviewer checklist.

for tests add:

  1. Open https://dev.new.expensify.com:8082/ and sign in.
  2. Open DevTools → Console, paste
(function () {
    // Make Pusher's WebSocket fail so pusher-js falls back to xhr_streaming, like the affected users.
    var NativeWebSocket = window.WebSocket;
    function TestWebSocket(url, protocols) {
        var target = String(url).indexOf('pusher.com') !== -1 ? 'wss://127.0.0.1:9' : url;
        return protocols === undefined ? new NativeWebSocket(target) : new NativeWebSocket(target, protocols);
    }
    TestWebSocket.prototype = NativeWebSocket.prototype;
    ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'].forEach(function (key) {
        TestWebSocket[key] = NativeWebSocket[key];
    });
    window.WebSocket = TestWebSocket;

    // Simulate Forcepoint DLP adding its script to the start of Pusher's fallback stream.
    var descriptor = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'responseText');
    var open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function (method, url) {
        this.shouldInjectForcepoint = String(url).indexOf('/xhr_streaming') !== -1;
        return open.apply(this, arguments);
    };
    Object.defineProperty(XMLHttpRequest.prototype, 'responseText', {
        configurable: true,
        get: function () {
            var text = descriptor.get.call(this);
            return this.shouldInjectForcepoint && text ? 'class FPClassifier {\n' + text : text;
        },
    });
    console.log('[APP-27B test] Forcepoint simulation active');
})();

You should see [APP-27B test] Forcepoint simulation active.
3. Sign out, then sign back in in the same tab, without reloading.
4. Verify that the following error doesnt show up the Console shows Uncaught SyntaxError: Unexpected token 'l', "lass FPClassifier {" is not valid JSON from pusher-with-encryption.js

@abzokhattab

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible 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 checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (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 verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • 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 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.
  • If the PR adds or modifies the UI:
    • I asked an AI agent to review the changes for accessibility issues and addressed its findings.
    • I tested with a screen reader (VoiceOver on macOS) and verified all new/changed elements are reachable with a logical focus order.
    • I verified all new/changed elements have meaningful accessible names and roles.
    • I verified state changes are announced (e.g. checked/unchecked, expanded/collapsed, selected).
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this 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.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

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

Before

Screen.Recording.2026-10-01.at.18.54.46.mov

After

Screen.Recording.2026-10-01.at.19.07.41.mov

@abzokhattab

Copy link
Copy Markdown
Contributor

@MelvinBot @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 0e307ba6c0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@MelvinBot

Copy link
Copy Markdown
Contributor Author

I updated the PR description:

  • Tests: Added your four steps, including the Forcepoint simulation script. Step 4 now reads as a check that the SyntaxError does not appear.
  • Offline tests: Marked N/A, because the change only affects how pusher-js parses its HTTP fallback stream.
  • QA Steps: Same as tests, using https://staging.new.expensify.com/.
  • PR Author Checklist: Checked every item, including screenshots. I added a note under Screenshots/Videos that they'll be added in the reviewer checklist.

view run

@abzokhattab

Copy link
Copy Markdown
Contributor

@MelvinBot mark the pr as ready

@MelvinBot

Copy link
Copy Markdown
Contributor Author

I couldn't mark this PR as ready for review. My environment blocked the gh pr ready command, so the PR is still a draft.

You can mark it ready yourself with the Ready for review button at the bottom of the PR page.


view run

@abzokhattab

Copy link
Copy Markdown
Contributor

@mountiny i cannot mark it as ready for some reason melvin refuses to ... can you please make it ready for review

@mountiny
mountiny marked this pull request as ready for review October 2, 2026 13:46
@melvin-bot
melvin-bot Bot requested review from abzokhattab and removed request for a team October 2, 2026 13:46
@melvin-bot

melvin-bot Bot commented Oct 2, 2026

Copy link
Copy Markdown

@abzokhattab Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot requested a review from mountiny October 2, 2026 13:47

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0e307ba6c0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread patches/pusher-js/details.md Outdated
Only dist/web/pusher-with-encryption.js is patched, because App imports `pusher-js/with-encryption`.
```

- Upstream PR/issue: 🛑, upstream pusher-js `master` has the same unguarded parse: https://github.com/pusher/pusher-js/blob/f53faf9f33dbab922a45a919607f4c87ad1b7f72/src/core/http/http_socket.ts#L91-L113

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please create the issue first

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread patches/pusher-js/details.md Outdated

- Upstream PR/issue: 🛑, upstream pusher-js `master` has the same unguarded parse: https://github.com/pusher/pusher-js/blob/f53faf9f33dbab922a45a919607f4c87ad1b7f72/src/core/http/http_socket.ts#L91-L113
- E/App issue: https://github.com/Expensify/App/issues/102745
- PR introducing patch: 🛑

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should link this pr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b1603c9. PR introducing patch now links this PR.

@abzokhattab

Copy link
Copy Markdown
Contributor

@MelvinBot please address the review comments:

  1. In the pusher-js patch, change the a case guard from payload === undefined to !Array.isArray(payload), the same as the c case. Update the Reason in patches/pusher-js/details.md to say that a and c frames whose payload isn't an array are skipped, and add a test to tests/unit/PusherHttpSocketTest.ts checking that an anull line doesn't throw or emit a message.
  2. In patches/pusher-js/details.md, set Upstream PR/issue to HTTP streaming fallback throws SyntaxError when a proxy injects non-JSON lines into the response pusher/pusher-js#985 (fix: Ignore non-JSON lines in the HTTP streaming fallback pusher/pusher-js#984).
  3. Set PR introducing patch to Skip malformed lines in the pusher-js HTTP stream instead of throwing #102752.

…stream issue

Co-authored-by: Abdelrahman Khattab <abzokhattab@users.noreply.github.com>
@MelvinBot
MelvinBot requested a review from a team as a code owner October 2, 2026 16:07
@melvin-bot
melvin-bot Bot requested review from abzokhattab and removed request for a team October 2, 2026 16:07
@melvin-bot

melvin-bot Bot commented Oct 2, 2026

Copy link
Copy Markdown

@abzokhattab Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@MelvinBot

Copy link
Copy Markdown
Contributor Author

I addressed all three review comments in b1603c9:

  1. a frame guard: The patch now skips an a frame whose payload isn't an array, the same as the c case. The Reason in patches/pusher-js/details.md says so. A new test in tests/unit/PusherHttpSocketTest.ts:111-124 checks that an anull line doesn't throw or emit a message.
  2. Upstream PR/issue: This now links HTTP streaming fallback throws SyntaxError when a proxy injects non-JSON lines into the response. The fix is Ignore non-JSON lines in the HTTP streaming fallback.
  3. PR introducing patch: This now links this PR.

All 3 tests in PusherHttpSocketTest.ts pass with the updated patch, and ESLint passes.

Co-authored-by: Abdelrahman Khattab <abzokhattab@users.noreply.github.com>
@MelvinBot

Copy link
Copy Markdown
Contributor Author

Fixed the failing spellcheck check. cspell flagged anull, the non-array payload that the new test sends, at tests/unit/PusherHttpSocketTest.ts:119. I added a // cspell:disable-next-line comment above it, as other tests in the repo do (8d0e75c).

PusherHttpSocketTest passes locally with the patch applied (3/3). Check independent approval will keep failing until the PR gets an independent approval. Code changes can't fix that check.


view run

@abzokhattab
abzokhattab requested a review from mountiny October 2, 2026 17:44

@mountiny mountiny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect thank you

@mountiny
mountiny merged commit f4c2005 into main Oct 4, 2026
40 of 42 checks passed
@mountiny
mountiny deleted the claude-ignoreMalformedPusherHttpFrames branch October 4, 2026 16:33
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

🚧 mountiny has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

OSBotify commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify OSBotify mentioned this pull request Oct 4, 2026
59 tasks done
@OSBotify

OSBotify commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/mountiny in version: 9.5.2-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@OSBotify

OSBotify commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/Julesssss in version: 9.5.2-4 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

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.

4 participants