Skip to content

Send proxy credentials when downloading the binary (fixes #164) - #190

Draft
07souravkunda wants to merge 1 commit into
masterfrom
LOC-7724/proxy-auth-binary-download
Draft

07souravkunda wants to merge 1 commit into
masterfrom
LOC-7724/proxy-auth-binary-download

Conversation

@07souravkunda

@07souravkunda 07souravkunda commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #164. Jira: LOC-7724

Problem

proxyUser/proxyPass reach the running BrowserStack Local binary as --proxy-user/--proxy-pass, but never the library's own download of that binary. Behind a proxy that requires Basic auth, the first download request (the source-URL lookup on local.browserstack.com) is refused with 407, the binary is never fetched, and start() fails with Couldn't find binary file. Every released version behaves this way: no download path has ever built its proxy agent with credentials.

Because the library parses the proxy's 407 page as JSON, the visible log is Unable to fetch the source url to download the binary with error: SyntaxError: Unexpected token < in JSON ..., not a 407.

Change

  • lib/util.js: new buildProxyAgent(host, port, user, pass), now the only place the download proxy agent is built (it was four copies). It adds Basic auth from proxyUser/proxyPass and follows the README rule that proxyPass is ignored without proxyUser. A blank host or port, or the "undefined" argv placeholder, means no agent.
  • lib/Local.js: getBinaryPath forwards proxyUser/proxyPass into the download conf.
  • lib/LocalBinary.js: the async download() and getSourceUrl() use the helper. The sync getSourceUrlSync()/downloadSync() pass the credentials to the spawned scripts through BROWSERSTACK_LOCAL_PROXY_USER/_PASS in the child's environment, never argv (argv is world-readable via ps), the same pattern as the existing auth token. A copy already in process.env is dropped first, so only this instance's settings are used.
  • lib/download.js, lib/fetchDownloadSourceUrl.js, lib/fetchDownloadSourceUrlAsync.js: use the helper. fetchDownloadSourceUrlAsync gains proxyUser, proxyPass after proxyPort; LocalBinary is its only caller.
  • README: notes that the proxy settings also apply to the binary download, and that the download uses the proxy only when both proxyHost and proxyPort are set (existing behaviour, now documented).

Testing

test/proxy_auth_download.js (new, offline). It uses test/fixtures/recording_proxy.js, a proxy that records each CONNECT's Proxy-Authorization and refuses it with 407. The proxy runs in its own process because the sync path blocks the test process in spawnSync.

Fix Pre-fix code
test/proxy_auth_download.js (10 tests) 10 pass 9 fail; the "no proxy user → no header" guard passes on both, as intended
npm test (eslint + mocha) 57 pass, 4 fail, 2 pending the same 4 fail with identical errors

The 4 existing failures are unrelated to this change. The three Download tests call binary.download() without a key and get Invalid auth token from the endpoint; should stop local reads isRunning() as true right after stop().

End to end with a real key and a fresh home/tmp/cwd (no cached binary), the library downloads the binary through a forwarding proxy that rejects missing or wrong credentials, then starts the tunnel:

Proxy start() startSync()
Basic auth binary downloaded, tunnel up; 98/98 proxy requests authenticated binary downloaded, tunnel up; 96/96 authenticated
Basic auth + TLS inspection (private CA via useCaCertificate) binary downloaded, tunnel up; 95/95 authenticated binary downloaded, tunnel up; 98/98 authenticated

The same run against the published 1.5.16 makes one CONNECT with no Proxy-Authorization and fails with Couldn't find binary file (async and sync). Tested on macOS; not run on Linux or Windows.

Not in this PR

  • With proxyHost set but not proxyPort, the binary defaults to port 3128 while the download skips the proxy.
  • A 407 surfaces as a JSON SyntaxError rather than a clear proxy-authentication error.

This supersedes #183 and #186. #183 changes only download(), and since Local.js never puts the credentials into conf, a start() through an authenticating proxy still sent no Proxy-Authorization when tested. #186 covers the same call sites but its base predates current master.

🤖 Generated with Claude Code

proxyUser/proxyPass reached the running binary as --proxy-user/--proxy-pass
but never the library's own download of that binary. Behind a proxy that
requires Basic auth, the first download request (the source-URL lookup on
local.browserstack.com) was refused with 407, the binary was never fetched,
and start() failed with "Couldn't find binary file". Every released version
behaves this way. Fixes #164.

- util.buildProxyAgent is now the single place the download proxy agent is
  built (it was four copies). It adds Basic auth from proxyUser/proxyPass and
  follows the README rule that proxyPass is ignored without proxyUser.
- Local.getBinaryPath forwards proxyUser/proxyPass into the download conf.
- The sync path hands the credentials to the spawned download scripts through
  BROWSERSTACK_LOCAL_PROXY_USER/_PASS in their environment, never argv (same
  as the auth token). A copy already in process.env is dropped first.

Tests: test/proxy_auth_download.js runs offline against a recording proxy in
its own process, so the sync path cannot deadlock it. 10/10 pass; 9/10 fail
on the pre-fix code. Also verified end to end with a real key: the library
downloads the binary through a credential-checking proxy and the tunnel
connects, async and sync, with and without TLS inspection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 19, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto (fallback from Default branch)
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Central YAML (base), Organization UI (inherited), Workspace UI (inherited); Enforced overrides: Organization
  • Review profile: ASSERTIVE
  • Plan: Enterprise
  • Run ID: 9d4feb64-2c37-45ea-b0e6-5787e74cd867

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Binary Download Doesn't Use Proxy Authentication Settings

1 participant