diff --git a/package.json b/package.json index ac5234731..bc9ae2a89 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "blacksky.community", - "version": "1.131.3", + "version": "1.131.4", "private": true, "packageManager": "pnpm@11.7.0", "engines": { diff --git a/src/state/session/__tests__/strip-appview-proxy-test.ts b/src/state/session/__tests__/strip-appview-proxy-test.ts index 018a804a3..4a8fb2f3c 100644 --- a/src/state/session/__tests__/strip-appview-proxy-test.ts +++ b/src/state/session/__tests__/strip-appview-proxy-test.ts @@ -1,10 +1,6 @@ -import {describe, expect, it, jest} from '@jest/globals' +import {describe, expect, it} from '@jest/globals' -import { - Agent, - stripAppviewProxyForPdsLocalMethods, - stripAppviewProxyForSpaceMethods, -} from '../agent' +import {stripAppviewProxyForPdsLocalMethods} from '../agent' const PROXY = 'atproto-proxy' const PROXY_VALUE = 'did:web:api.blacksky.community#bsky_appview' @@ -28,6 +24,7 @@ const SPACE_CREATE = const SPACE_DELETE = 'https://pds.example.com/xrpc/com.atproto.space.deleteRecord' const SPACE_GET = 'https://pds.example.com/xrpc/com.atproto.space.getRecord' +const SPACE_OTHER = 'https://pds.example.com/xrpc/com.atproto.space.listRecords' describe('stripAppviewProxyForPdsLocalMethods', () => { it('strips the appview proxy header on getPreferences', () => { @@ -83,42 +80,10 @@ describe('stripAppviewProxyForPdsLocalMethods', () => { }, ) - it('uses the same space-only stripping on the bearer/session fetch path', () => { - const out = stripAppviewProxyForSpaceMethods( - SPACE_CREATE, - getInit({[PROXY]: PROXY_VALUE, authorization: 'DPoP tok'}), - ) - expect(headerValue(out, PROXY)).toBeNull() - expect(headerValue(out, 'authorization')).toBe('DPoP tok') - expect( - stripAppviewProxyForSpaceMethods( - GET_PREFS, - getInit({[PROXY]: PROXY_VALUE}), - ), - ).toEqual(getInit({[PROXY]: PROXY_VALUE})) - }) - - it('strips configureProxy headers in the OAuth agent before session fetch', async () => { - const fetchHandler = jest.fn< - (url: string, init?: RequestInit) => Promise - >(() => Promise.resolve(new Response('{}'))) - const agent = new Agent(PROXY_VALUE, { - fetchHandler(url, init) { - return fetchHandler( - url, - stripAppviewProxyForPdsLocalMethods(url, init) ?? init, - ) - }, - }) - - await agent.fetchHandler(SPACE_CREATE, { - method: 'POST', - headers: {authorization: 'DPoP tok', dpop: 'proof'}, - }) - - const init = fetchHandler.mock.calls[0][1] - expect(headerValue(init, PROXY)).toBeNull() - expect(headerValue(init, 'authorization')).toBe('DPoP tok') - expect(headerValue(init, 'dpop')).toBe('proof') + it('does not strip unlisted space methods', () => { + const init = getInit({[PROXY]: PROXY_VALUE}) + const out = stripAppviewProxyForPdsLocalMethods(SPACE_OTHER, init) + expect(out).toBe(init) + expect(headerValue(out, PROXY)).toBe(PROXY_VALUE) }) }) diff --git a/src/state/session/agent.ts b/src/state/session/agent.ts index e89af365c..0f483282a 100644 --- a/src/state/session/agent.ts +++ b/src/state/session/agent.ts @@ -449,33 +449,33 @@ export class Agent extends BaseAgent { // our appview, which 501s - breaking app load for accounts hosted elsewhere // (e.g. bsky.network). // -// Preferences are stripped only on OAuth. Space methods are stripped on both -// OAuth and Bearer/session agents because they are always PDS-local writes. -// The Bearer path deliberately keeps the proxy on preferences: removing it -// produced a bare 401 on fresh signups. +// This is only used by the OAuth agent, which is the only path a foreign PDS is +// ever reached on. Accounts on our own PDS never need this: that PDS serves the +// methods locally and its gatekeeper strips the header server-side anyway. +// (The Bearer agent - account creation + legacy resume, always on our PDS - +// deliberately does NOT strip; doing so produced a bare 401 on fresh signups.) const PDS_LOCAL_PROXY_EXEMPT_METHODS = [ 'app.bsky.actor.getPreferences', 'app.bsky.actor.putPreferences', - 'com.atproto.space.', + 'com.atproto.space.createRecord', + 'com.atproto.space.deleteRecord', + 'com.atproto.space.getRecord', ] -const SPACE_PDS_LOCAL_METHOD = 'com.atproto.space.' - -function requestUrl(input: string | URL | Request): string { - return typeof input === 'string' - ? input - : input instanceof URL - ? input.href - : input.url -} - -function stripAppviewProxy( +export function stripAppviewProxyForPdsLocalMethods( input: string | URL | Request, init: RequestInit | undefined, - methods: string[], ): RequestInit | undefined { - const url = requestUrl(input) - if (!url || !methods.some(method => url.includes(method))) { + const url = + typeof input === 'string' + ? input + : input instanceof URL + ? input.href + : input.url + if ( + !url || + !PDS_LOCAL_PROXY_EXEMPT_METHODS.some(method => url.includes(method)) + ) { return init } const headers = new Headers(init?.headers) @@ -483,20 +483,6 @@ function stripAppviewProxy( return {...init, headers} } -export function stripAppviewProxyForPdsLocalMethods( - input: string | URL | Request, - init: RequestInit | undefined, -): RequestInit | undefined { - return stripAppviewProxy(input, init, PDS_LOCAL_PROXY_EXEMPT_METHODS) -} - -export function stripAppviewProxyForSpaceMethods( - input: string | URL | Request, - init: RequestInit | undefined, -): RequestInit | undefined { - return stripAppviewProxy(input, init, [SPACE_PDS_LOCAL_METHOD]) -} - let realFetch = globalThis.fetch class BskyAppAgent extends BskyAgent { persistSessionHandler: ((event: AtpSessionEvent) => void) | undefined = @@ -506,16 +492,23 @@ class BskyAppAgent extends BskyAgent { super({ service, async fetch(input: RequestInfo | URL, init?: RequestInit) { - const finalInit = stripAppviewProxyForSpaceMethods(input, init) ?? init + // NOTE: the appview proxy header is deliberately NOT stripped here. + // This (Bearer) agent is only ever used for account creation and legacy + // session resume - both always on this deployment's own PDS, whose home + // appview is ours, so it serves getPreferences/putPreferences locally + // (the PDS gatekeeper also strips the header server-side). Stripping it + // client-side here instead produced a bare 401 on fresh signups. The + // strip is only needed for foreign PDSes, which are only ever reached + // via the OAuth agent - see stripAppviewProxyForPdsLocalMethods. let success = false try { - const result = await realFetch(input, finalInit) + const result = await realFetch(input, init) success = true - reportProxiedFetch(finalInit, result.status) + reportProxiedFetch(init, result.status) return result } catch (e) { success = false - reportProxiedFetch(finalInit, null) + reportProxiedFetch(init, null) throw e } finally { if (success) {