Skip to content

fix: support corporate proxies during wizard login#493

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-oauth-proxy-protocol-mismatch
Draft

fix: support corporate proxies during wizard login#493
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/fix-oauth-proxy-protocol-mismatch

Conversation

@posthog
Copy link
Copy Markdown

@posthog posthog Bot commented Jun 1, 2026

Problem

Users behind a corporate HTTP/HTTPS proxy can hit an uncaught crash during wizard login, fully blocking onboarding before any PostHog integration work begins.

An error-tracking issue (AssertionError: protocol mismatch) was reported on 2026-06-01. The stack trace runs exchangeCodeForTokenperformOAuthFlowaskForWizardLogingetOrAskForProjectData, then into axios and follow-redirects.

Root cause: exchangeCodeForToken (src/utils/oauth.ts) POSTs to https://oauth.posthog.com/oauth/token with no proxy-aware request configuration. axios pins 1.7.4, and its built-in proxy resolution mis-detects the resolved request protocol when an HTTPS target is routed through an HTTP proxy — tripping follow-redirects' assert.equal(options.protocol, protocol, "protocol mismatch"). The assertion escapes as an unhandled rejection rather than a friendly auth error.

Changes

  • src/utils/proxy.ts (new): getProxyRequestConfig(url) resolves the proxy from HTTP_PROXY/HTTPS_PROXY/NO_PROXY (via proxy-from-env, honouring casing variants and NO_PROXY), hands axios an explicit proxy agent, and disables axios's own proxy handling with proxy: false so follow-redirects always sees a consistent protocol. Returns an empty config when no proxy applies, leaving the common (proxy-less) path unchanged.
  • Applied across the login chain (getOrAskForProjectData): the OAuth token exchange (oauth.ts), region detection (urls.ts detectRegionFromToken), and the user/project API calls (api.ts). This makes onboarding work end-to-end behind a proxy instead of just relocating the crash to the next axios call.
  • Graceful fallback: performOAuthFlow now catches a residual protocol mismatch error and reports actionable proxy guidance through getUI().log.error instead of crashing the process.
  • Tooling: added jest babel transforms for the ESM-only proxy-agent packages (transformIgnorePatterns) and a local type declaration for proxy-from-env (v2 ships no types).

Test plan

  • pnpm build (incl. smoke test) ✅
  • pnpm test — 651 passed ✅ (added src/utils/__tests__/proxy.test.ts covering env resolution, NO_PROXY, https/http agent selection, and protocol mismatch detection)
  • pnpm lint — 0 errors ✅

Created with PostHog Code

Users behind a corporate HTTP/HTTPS proxy could hit an uncaught
`AssertionError: protocol mismatch` from follow-redirects (surfaced via
axios) during OAuth login, crashing the process before any PostHog
integration work could begin.

The crash originated in `exchangeCodeForToken`, which POSTs to
`https://oauth.posthog.com/oauth/token` with no proxy-aware request
configuration. axios's built-in proxy resolution mis-detects the request
protocol when an HTTPS target is routed through an HTTP proxy, tripping
follow-redirects' `assert.equal(options.protocol, protocol)` check.

Changes:
- Add `src/utils/proxy.ts`: `getProxyRequestConfig(url)` resolves the proxy
  from `HTTP_PROXY`/`HTTPS_PROXY`/`NO_PROXY` (via proxy-from-env), hands
  axios an explicit proxy agent and disables axios's own proxy handling
  (`proxy: false`) so follow-redirects always sees a consistent protocol.
  Returns an empty config when no proxy applies, leaving the common case
  untouched.
- Apply the helper across the login chain (`getOrAskForProjectData`): the
  OAuth token exchange, region detection (`detectRegionFromToken`), and the
  user/project API calls — so onboarding actually works end-to-end behind a
  proxy rather than just relocating the crash to the next request.
- Catch a residual `protocol mismatch` error in `performOAuthFlow` and
  report friendly proxy guidance via `getUI().log.error` instead of
  crashing.
- Add jest babel transforms for the ESM-only proxy-agent packages and a
  local type declaration for proxy-from-env (v2 ships no types).
- Unit tests for the proxy helper (env resolution, NO_PROXY, agent
  selection, error detection).

Generated-By: PostHog Code
Task-Id: a55b2457-e99e-4f9e-b43b-e13d43d38444
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci basic-integration
  • /wizard-ci misc
  • /wizard-ci revenue

Test an individual app:

  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
Show more apps
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci misc/quack-quack
  • /wizard-ci revenue/stripe

Results will be posted here when complete.

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.

0 participants