From f2cb983f282a3bfcf106f5d6e65dd572ba293bdb Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Mon, 3 Aug 2026 20:03:20 -0400 Subject: [PATCH] fix: pin community xrpc calls to the home appview --- src/lib/api/__tests__/community.test.ts | 37 +++++++++++++++++++++++++ src/lib/api/community.ts | 8 ++++-- src/lib/constants.ts | 12 ++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/lib/api/__tests__/community.test.ts diff --git a/src/lib/api/__tests__/community.test.ts b/src/lib/api/__tests__/community.test.ts new file mode 100644 index 000000000..28b2539f1 --- /dev/null +++ b/src/lib/api/__tests__/community.test.ts @@ -0,0 +1,37 @@ +import {type BskyAgent} from '@atproto/api' + +import { + BLUESKY_FALLBACK_PROXY_HEADER, + BLUESKY_PROXY_HEADER, + HOME_PROXY_HEADER, +} from '#/lib/constants' +import {communityXrpc} from '../community' + +describe('communityXrpc', () => { + const originalHeader = BLUESKY_PROXY_HEADER.get() + + afterEach(() => { + BLUESKY_PROXY_HEADER.set(originalHeader) + }) + + function mockAgent() { + const fetchHandler = jest.fn, [string, RequestInit]>() + fetchHandler.mockResolvedValue(new Response('{}')) + return {agent: {fetchHandler} as unknown as BskyAgent, fetchHandler} + } + + it('always targets the home appview, even when the global proxy header is flipped', async () => { + BLUESKY_PROXY_HEADER.set(BLUESKY_FALLBACK_PROXY_HEADER) + const {agent, fetchHandler} = mockAgent() + + await communityXrpc(agent, 'community.blacksky.feed.getCommunityTimeline') + + const init = fetchHandler.mock.calls[0][1] as { + headers: Record + } + expect(init.headers['atproto-proxy']).toBe(HOME_PROXY_HEADER) + expect(init.headers['atproto-proxy']).not.toBe( + BLUESKY_FALLBACK_PROXY_HEADER, + ) + }) +}) diff --git a/src/lib/api/community.ts b/src/lib/api/community.ts index 112a69bb3..f8e4647db 100644 --- a/src/lib/api/community.ts +++ b/src/lib/api/community.ts @@ -1,6 +1,6 @@ import {type BskyAgent} from '@atproto/api' -import {BLUESKY_PROXY_HEADER} from '#/lib/constants' +import {HOME_PROXY_HEADER} from '#/lib/constants' /** * Make an XRPC call to a community.blacksky.feed.* endpoint. @@ -8,6 +8,10 @@ import {BLUESKY_PROXY_HEADER} from '#/lib/constants' * Calls through the PDS using the agent's session auth and atproto-proxy * header. The PDS validates the user's credentials, creates a service auth * JWT signed by the user's keypair, and forwards the request to the appview. + * + * Pinned to the home appview: community.blacksky.* endpoints only exist + * there, so these calls must not follow the global proxy header when it is + * flipped during appview fallback. */ export async function communityXrpc( agent: BskyAgent, @@ -23,7 +27,7 @@ export async function communityXrpc( const path = `/xrpc/${method}${qs}` const headers: Record = { - 'atproto-proxy': BLUESKY_PROXY_HEADER.get(), + 'atproto-proxy': HOME_PROXY_HEADER, } const init: RequestInit = { method: opts?.body ? 'POST' : 'GET', diff --git a/src/lib/constants.ts b/src/lib/constants.ts index c75e7e8fc..57bec6561 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -215,6 +215,18 @@ export const PUBLIC_APPVIEW = 'https://api.blacksky.community' export const PUBLIC_APPVIEW_DID = 'did:web:api.blacksky.community' export const PUBLIC_STAGING_APPVIEW_DID = 'did:web:api.staging.bsky.dev' +export const APPVIEW_STATUS_URL = `${PUBLIC_APPVIEW}/status/indexer` + +export const BLUESKY_FALLBACK_PROXY_DID = 'did:web:api.bsky.app' +export const BLUESKY_FALLBACK_PROXY_HEADER = + `${BLUESKY_FALLBACK_PROXY_DID}#bsky_appview` as ProxyHeaderValue + +// The home appview's header value, captured immutably. BLUESKY_PROXY_HEADER +// below is mutable (it flips during appview fallback); calls that must always +// reach the home appview use this instead. +export const HOME_PROXY_HEADER = + `${BLUESKY_PROXY_DID}#bsky_appview` as ProxyHeaderValue + export const DEV_ENV_APPVIEW = `http://localhost:2584` // always the same // temp hack for e2e - esb -- knotmirror 0.0.1