Skip to content

fix: use a node-compatible proxy agent for baileys media upload - #2734

Open
maonamassaaceleradora wants to merge 1 commit into
evolution-foundation:developfrom
Mao-na-Massa-Aceleradora-Digital:fix/baileys-rc13-proxy-fetch-agent
Open

maonamassaaceleradora wants to merge 1 commit into
evolution-foundation:developfrom
Mao-na-Massa-Aceleradora-Digital:fix/baileys-rc13-proxy-fetch-agent

Conversation

@maonamassaaceleradora

@maonamassaaceleradora maonamassaaceleradora commented Sep 21, 2026 •

Copy link
Copy Markdown

📋 Description

Every media upload fails when an instance has a proxy enabled.

createClient() builds the Baileys socket options with two different agents:

options = { agent: makeProxyAgent(proxyUrl), fetchAgent: makeProxyAgentUndici(proxyUrl) };

makeProxyAgentUndici() returns an Undici ProxyAgent, which was the right choice while Baileys performed its media uploads through fetch. Baileys 7.0.0-rc13 no longer does: on the Node runtime it uploads media with Node's native https/http modules, and passes the fetchAgent option straight through to http.request() as its agent.

An Undici ProxyAgent does not implement Node's http.Agent interface, so the upload request never gets a usable agent and fails against every media host.

Why this is easy to misdiagnose: sending text keeps working perfectly. Text goes over the websocket, which uses the agent option — already a Node-compatible agent. Only the upload path is broken, and it fails differently per media type:

  • PTT / audio → 400 Media upload failed on all hosts
  • video → request hangs until timeout

So the instance looks healthy, connects fine, and sends messages — only media silently breaks, and only when a proxy is configured.

The fix builds fetchAgent with makeProxyAgent() as well, so both the websocket and the media upload path receive a Node-compatible agent.

🔗 Related Issue

No issue filed; diagnosed on one of our production instances after upgrading to the Baileys rc13 line.

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🔧 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🧹 Code cleanup
  • 🔒 Security fix

🧪 Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced
  • Tested with different connection types (if applicable)

Verified on one of our production instances with a proxy configured:

  • Before: /message/sendWhatsAppAudio returned 400 Media upload failed on all hosts, video uploads timed out, while /message/sendText worked normally.
  • After: audio (PTT), image, video and document uploads all succeed through the proxy.
  • Re-checked with the proxy disabled to confirm no regression on the non-proxy path.
  • npm run lint:check and npx tsc --noEmit both pass with no errors.

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly
  • I have verified the changes work with different scenarios
  • Any dependent changes have been merged and published

📝 Additional Notes

This change leaves makeProxyAgentUndici() in src/utils/makeProxyAgent.ts exported but no longer referenced anywhere. I deliberately kept it rather than deleting it in this PR, to keep the diff focused on the fix — happy to remove it in this PR or a follow-up if you would prefer that.

Worth noting for anyone tracking the Baileys upgrade: this is a behavioural change on the Baileys side, not a regression introduced here. The fetchAgent option kept its name but changed what it is handed to.

🤖 Generated with Claude Code

Summary by Sourcery

Restore Baileys media uploads when proxy support is enabled.

Bug Fixes:

  • Fix media uploads through configured proxies by using a Node-compatible proxy agent for both Baileys connection and upload requests.

Tests:

  • Verify proxied audio, image, video, and document uploads, confirm non-proxy behavior, and pass lint and TypeScript checks.

Baileys 7.0.0-rc13 performs media uploads with Node's native http/https
modules instead of fetch/Undici, and passes the fetchAgent option straight to
http.request as its `agent`. An Undici ProxyAgent does not implement Node's
Agent interface, so every media upload fails as soon as an instance has a
proxy configured.

The failure is easy to misdiagnose because text messages keep working: they
go through the websocket, which already uses the Node agent. Only uploads
break, with PTT audio returning "400 Media upload failed on all hosts" and
video uploads timing out.

Build fetchAgent with makeProxyAgent() so both the websocket and the media
upload path get a Node-compatible agent.
@sourcery-ai

sourcery-ai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates Baileys proxy configuration so fetchAgent receives a Node-compatible agent, matching rc13's native http/https media-upload path and restoring proxied audio, image, video, and document uploads without changing non-proxy behavior.

Sequence diagram for proxied Baileys media upload

sequenceDiagram
    participant Client
    participant Baileys as Baileys
    participant HTTP as Node_http_https
    participant Proxy as Proxy
    participant MediaHost

    Client->>Baileys: sendMedia
    Baileys->>HTTP: http.request(fetchAgent)
    HTTP->>Proxy: upload media
    Proxy->>MediaHost: forward upload
    MediaHost-->>Proxy: upload response
    Proxy-->>HTTP: response
    HTTP-->>Baileys: upload result
    Baileys-->>Client: media message sent
Loading

File-Level Changes

Change Details Files
Use Node-compatible proxy agents for both websocket and Baileys media-upload requests.
  • Replace the Undici proxy-agent factory with the Node http.Agent factory for the fetchAgent option.
  • Apply the fix to both randomly selected proxy URLs and configured local proxy credentials.
  • Remove the now-unused Undici-agent import while retaining its utility export.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot 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.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Needs a human reviewer. If the replacement agent is incompatible, WhatsApp media uploads may fail and can be fixed by reverting or retrying them. If it routes requests incorrectly, media could be sent through an unintended network path; that exposure would not be undone by a revert, though its scope is bounded.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

This branch has not been deployed

No deployments
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