Skip to content

fetch: use standard base64 for proxy Basic auth credentials - #31782

Merged
Jarred-Sumner merged 4 commits into
mainfrom
farm/78fc44ea/proxy-auth-standard-base64
Jun 4, 2026
Merged

Jarred-Sumner merged 4 commits into
mainfrom
farm/78fc44ea/proxy-auth-standard-base64

Conversation

@robobun

@robobun robobun commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Fixes #31780

Repro

fetch(url, { proxy: 'http://user:pass@gw.dataimpulse.com:823' })
// error: The socket connection was closed unexpectedly.

DataImpulse proxy credentials (and others with session tokens) routinely contain characters whose base64 encoding differs between the standard and URL-safe alphabets. Plain ASCII creds like user:pass encode identically in both, which is why it worked for some proxies but not DataImpulse.

Cause

build_proxy_authorization() in src/http/AsyncHTTP.rs built the Proxy-Authorization: Basic <...> header with bun_base64::encode_url_safe — the base64url alphabet (-/_, no = padding). HTTP Basic auth (RFC 7617) requires the standard alphabet (+//, with = padding).

For example, sub-user:sess>>id?ff encodes as:

alphabet encoding
standard (required) c3ViLXVzZXI6c2Vzcz4+aWQ/ZmY=
base64url (bug) c3ViLXVzZXI6c2Vzcz4-aWQ_ZmY

A strict proxy decodes the header with the standard alphabet, gets garbage, rejects the credential, and closes the socket — producing the reporter's error. (The .zig reference had the same std.base64.url_safe call, so this is a long-standing bug, not a port regression.)

Fix

Swap to the standard, padded encoder bun_base64::encode (identical (dest, source) -> usize signature). The buffer is already sized with the standard/padded length (encode_len_from_size == standard_encoder_calc_size), so no buffer-size change is needed.

-    let encoded_len = bun_base64::encode_url_safe(&mut buf[b"Basic ".len()..], &auth);
+    let encoded_len = bun_base64::encode(&mut buf[b"Basic ".len()..], &auth);

This is the only proxy-auth encode site. Other encode_url_safe callers are legitimately URL-safe and are untouched.

Verification

Added two cases to test/js/bun/http/proxy.test.ts covering both proxy paths (absolute-form HTTP target and CONNECT HTTPS tunnel), using credentials whose standard base64 contains + and /. An in-process proxy captures the Proxy-Authorization header; the fetch runs in a subprocess with proxy-bypass env cleared so the explicit proxy option is honored.

  • Before: header is Basic c3ViLXVzZXI6c2Vzcz4-aWQ_ZmY (base64url) — fails
  • After: header is Basic c3ViLXVzZXI6c2Vzcz4+aWQ/ZmY= (standard) — passes

Proxy-Authorization: Basic <...> was encoded with the base64url alphabet
(- _, no padding) instead of the standard alphabet (+ / with = padding)
that HTTP Basic auth requires. Credentials whose standard base64 contains
+ or / (e.g. DataImpulse session tokens) were mangled, so strict proxies
could not decode them and closed the connection.

The buffer was already sized for the padded/standard length, so only the
encoder call changes.
@robobun

robobun commented Jun 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

@coderabbitai

coderabbitai Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@robobun, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 3 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: dd891af2-dd10-4631-8ccb-10c7cf283a76

📥 Commits

Reviewing files that changed from the base of the PR and between 4764477 and dd44261.

📒 Files selected for processing (1)
  • test/js/bun/http/proxy.test.ts

Walkthrough

The PR fixes proxy authentication by switching from URL-safe Base64 encoding to standard Base64 encoding in the Proxy-Authorization: Basic header. The single-line implementation change is validated by a new regression test suite covering both absolute-form HTTP requests and CONNECT tunneling for HTTPS targets.

Changes

Proxy Authentication Encoding

Layer / File(s) Summary
Proxy authentication Base64 encoding fix
src/http/AsyncHTTP.rs, test/js/bun/http/proxy.test.ts
build_proxy_authorization switches from bun_base64::encode_url_safe to bun_base64::encode, changing proxy credentials to use standard Base64 alphabet and = padding instead of URL-safe variants. New regression tests verify standard Base64 credentials are used in absolute-form HTTP proxied requests and CONNECT-tunnel HTTPS requests with custom TCP proxy capture.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: switching from URL-safe to standard base64 encoding for proxy Basic auth credentials.
Description check ✅ Passed The description comprehensively covers the issue, root cause, fix, and verification with code examples and test cases, following the template structure.
Linked Issues check ✅ Passed The PR directly addresses issue #31780 by fixing the proxy authentication encoding bug that caused socket connection failures with DataImpulse proxies.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the proxy Basic auth encoding issue. The code change, test cases, and documentation all align with the stated objective.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@VillainsRule

Copy link
Copy Markdown

@robobun idk if you can take feedback from users here but this does NOT fix the problem

user@0 mctk % bun-31782 checktok
$ bun cli/checktok
profile fetch error: Error: The socket connection was closed unexpectedly. For more information, pass verbose: true in the second argument to fetch()
profile fetch error: Error: The socket connection was closed unexpectedly. For more information, pass verbose: true in the second argument to fetch()

still happens

@claude claude 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.

LGTM — one-line swap to the standard base64 encoder for Proxy-Authorization (RFC 7617), buffer already sized for padded output, with solid regression tests.

Extended reasoning...

Overview

The runtime change is a single line in src/http/AsyncHTTP.rs: build_proxy_authorization() now calls bun_base64::encode (standard alphabet, +//, =-padded) instead of bun_base64::encode_url_safe (-/_, unpadded) when building the Proxy-Authorization: Basic <…> header. I verified in src/base64/lib.rs / src/bun_core/util.rs that encode has the identical (dest, source) -> usize signature and that the buffer was already sized via encode_len_from_size → standard_encoder_calc_size, i.e. the padded length, so no allocation/buffer change is needed. The rest of the diff adds two regression tests in test/js/bun/http/proxy.test.ts that capture the actual on-wire header for both the absolute-form HTTP path and the CONNECT tunnel path and assert the exact standard-base64 bytes.

Security risks

None introduced. This affects only how user-supplied proxy credentials are base64-encoded into a header that was already being sent; the standard alphabet is what RFC 7617 mandates and what every HTTP proxy expects. No new inputs, no new code paths, no auth logic change.

Level of scrutiny

Low. The fix is mechanical (one encoder swapped for its sibling with the same signature), the rationale is unambiguous (HTTP Basic auth is standard base64, not base64url), and it's covered by targeted tests that assert the exact header value and self-check that the fixture actually distinguishes the two alphabets. No CODEOWNERS apply to either file.

Other factors

The bug-hunting pass surfaced only a consistency nit: two pre-existing long-password tests above the new block still decode the captured header with "base64url". That's not a functional issue (Node/Bun's base64url decoder accepts the standard alphabet, and the all-'a' fixture encodes identically in both), so it doesn't block approval. No outstanding reviewer comments and no prior reviews from me on this PR.

Comment thread test/js/bun/http/proxy.test.ts Resolved
robobun added 3 commits June 4, 2026 00:23
…explicit proxy

The in-process fetch() tests assume the explicit `proxy` option is used for
localhost targets. An ambient NO_PROXY/HTTP_PROXY/HTTPS_PROXY (set in some
CI/dev environments) makes those fetches bypass the proxy, so they capture
nothing and fail. Clear the six proxy vars file-wide in beforeAll (via
assignment — a delete doesn't propagate to the HTTP client's getenv) and
restore them in afterAll. Subprocess-based tests pass their own explicit env
and are unaffected.
The header now uses standard base64 (not base64url) for Basic auth, so
decode the captured Proxy-Authorization with "base64" to match — keeping
the assertions consistent with the encoding under test.
@robobun

robobun commented Jun 4, 2026 •

Copy link
Copy Markdown
Collaborator Author

CI status for maintainers: this diff is green — it only touches src/http/AsyncHTTP.rs (1-line base64 fix) and test/js/bun/http/proxy.test.ts, and neither appears in any CI failure. proxy.test.ts passes 45/45 under the ASAN debug build locally, and the new #31780 tests pass on all lanes.

The red lanes on build #60222 are pre-existing tests unrelated to this change, aggregated across all 15 failing jobs:

  • test/cli/install/bunx.test.ts — 60 hits on every platform: "should handle package that requires node 24" (bun x --bun @angular/cli@latest --help, expects exit 0, gets 3). Hits the live npm registry — looks like a registry-wide issue right now, not a code regression.
  • test/js/bun/test/test-retry-repeats-basic.test.ts — 7 hits (flaky test-runner timing test).
  • test/js/web/streams/streams-leak.test.ts — 2 hits (flaky leak test).

Already spent the one CI re-roll (it produced #60222), so not pushing another per policy. Reporter has confirmed the fix works against a live DataImpulse proxy. Safe to merge over the unrelated registry flake.

@VillainsRule

Copy link
Copy Markdown

@robobun oddly enough, it seems to now work with those latest changes

@Jarred-Sumner looks fine to merge, fixed the issue aight

@Jarred-Sumner
Jarred-Sumner merged commit a3464c6 into main Jun 4, 2026
73 of 78 checks passed
@Jarred-Sumner
Jarred-Sumner deleted the farm/78fc44ea/proxy-auth-standard-base64 branch June 4, 2026 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

proxy option does not work on Bun Rust canary for DataImpulse proxies

3 participants