fix(server): local-fast-path for invite snapshot — skip HTTP self-reach

POST /api/dm/space-invite was hanging 5s and returning invite_invalid
for any local-space invite. fetchSpaceInviteSnapshot was being called
against our own public domain from inside the backspace container, which
fails (Docker NAT loopback) and aborts on timeout.

Add getLocalInviteSnapshot — reads the snapshot directly from the DB —
and branch in dm.ts so local invites bypass the HTTP roundtrip entirely.
Cross-instance invites still go through fetchSpaceInviteSnapshot with
its existing SSRF guard.

Also refactor the GET /api/spaces/invite/:code/preview handler to use
the same helper, keeping the snapshot shape in one place.

Tests assert fetchSpaceInviteSnapshot is NOT called for the local case
(critical regression guard) and that the cross-instance path still hits
the HTTP fetch.
This commit is contained in:
Jannis Braun
2026-04-29 22:29:44 +02:00
parent 5481eb9e7e
commit 44e9a4234c
5 changed files with 260 additions and 33 deletions
@@ -1,4 +1,6 @@
import type { AvatarColor } from '@backspace/shared';
import { eq } from 'drizzle-orm';
import { getDb, schema } from '../db/index.js';
import { validateExternalUrl } from './ssrf.js';
export interface SpaceInviteSnapshot {
@@ -11,6 +13,38 @@ export interface SpaceInviteSnapshot {
instanceName: string;
}
/**
* Build a local invite snapshot directly from the DB. Used when the space
* lives on this instance — avoids an HTTP roundtrip through our own domain
* (which fails inside Docker due to NAT loopback) and is faster anyway.
*
* Returns the same shape as `fetchSpaceInviteSnapshot` so callers don't
* branch downstream of the snapshot lookup.
*/
export function getLocalInviteSnapshot(inviteCode: string): SpaceInviteSnapshot | null {
const db = getDb();
const space = db.select().from(schema.spaces)
.where(eq(schema.spaces.inviteCode, inviteCode)).get();
if (!space) return null;
const memberCount = db.select().from(schema.spaceMembers)
.where(eq(schema.spaceMembers.spaceId, space.id)).all().length;
const settings = db.select().from(schema.instanceSettings)
.where(eq(schema.instanceSettings.id, 1)).get();
const instanceName = settings?.instanceName ?? 'Backspace';
return {
spaceId: space.id,
spaceName: space.name,
description: space.description ?? null,
icon: space.icon ?? null,
avatarColor: (space.avatarColor as AvatarColor | null) ?? null,
memberCount,
instanceName,
};
}
/**
* Fetch a space invite preview from a (possibly remote) instance.
* Returns null on 4xx, network error, or timeout — caller should treat as