fix(web)!: scope headers and basicAuth to the app's origin

Merged
KR
5d ago
+571-193

web({ basicAuth }) answered a 401 from any origin, and web({ headers }) went to every host on the app's site: another port on the same host, sibling subdomains, every host under a shared domain such as vercel.app. A third-party origin the app embeds could collect a preview password or a x-vercel-protection-bypass token. Fixes ENG-846.

What changed

  • Both now go to the app's origin only: the scheme, host, and port of app.url.
  • One new option, credentialOrigins, lists extra origins for both. Each entry must be an exact http:/https: origin, else INVALID_CONFIG at config load. That includes a * in the host and port 0, which would match nothing.
  • basicAuth becomes Playwright's per-origin httpCredentials array, one entry per allowed origin (packages/web/src/protected-app.ts).
  • WebKit is the exception. It never matches a scoped credential to an origin on a default port (443/80) or with an IPv6 host. When an allowed origin is one of those, WebKit gets one unscoped credential, as on main, and the docs say so.
  • The engine only knew the app's site, so the runner now passes EngineAppInfo.origin to prepare and init (packages/e2e/src/config/app.ts). It carries the run's real port when app.url uses port 0, and is left out until that port is assigned (e2e doctor).
  • @e2e-dev/web raises its e2e peer floor to >=0.23.1. On an older runner, init fails with INVALID_CONFIG instead of sending credentials nowhere. If #1590 releases 0.23.1 first, the floor moves to 0.23.2 when I rebase.
  • browser.route's route.continue() and route.fetch() add the configured headers under the same origin rule.
  • Breaking for @e2e-dev/web (minor, **Breaking:**). The changeset carries the migration note.
engine: web({
  headers: { 'x-vercel-protection-bypass': process.env.VERCEL_BYPASS! },
  basicAuth: { username: 'preview', password: secrets.get('previewPassword') },
  credentialOrigins: ['https://api.example.com'],
}),

Not changed: a header Playwright injects into a request stays on that request's redirects, to any origin. Playwright reapplies a routed request's headers to every redirect hop without routing the hop (crNetworkManager, _alreadyContinuedParams). This was already true on main. The docs now say so, and a test pins it. Closing it means fetching outside the browser with route.fetch({ maxRedirects: 0 }), which costs the HTTP cache and streaming, so it's out of scope here.

Verified

Ran it locally: yes

  • I built the CLI and ran a scratch project against a gate server. The app runs on one port and the same gate listens on a second port of the same host. Each page reports the header it received and whether basic auth passed.
Casemainthis branchbranch, other port in credentialOrigins
App origin: header / basic authon / Protectedon / Protectedon / Protected
Other port: headeron (the bug)noneon
Other port: basic authProtected (the bug)UnauthorizedProtected
  • The CLI refuses credentialOrigins: ['https://*.vercel.app'] and ['http://127.0.0.1:0'] at load with INVALID_CONFIG, naming the entry.
  • WebKit on macOS with Playwright 1.64.1, headless, against a basic-auth server. Chromium answers in every case.
WebKitscoped credentialunscoped (this branch's fallback)
port 80UnauthorizedProtected
a free portProtected-
[::1]UnauthorizedProtected
  • New integration tests cover the bug cases. protected-app.test.ts sends a request to another port on the same host and to a third-party site, for both headers and basicAuth, with and without the extra origin, plus redirects in both directions. routes.test.ts covers continue({ url }) and route.fetch() to another port on the same host and to a listed one. Both new route tests fail against main's engine source.
  • Earlier, the same tests ran headed on macOS (Chromium). Headed Chromium fails a navigation to an unanswered 401 with ERR_INVALID_AUTH_CREDENTIALS rather than rendering the page. Main does the same for any unanswered 401, but now more origins go unanswered.
  • pnpm check and pnpm test pass.

Opus 5.5 in Claude Code (T3 Code). Reviewed by Fable 5.1.