diff --git a/apps/website/app/api/internal/space/[id]/graph/route.test.ts b/apps/website/app/api/internal/space/[id]/graph/route.test.ts new file mode 100644 index 000000000..b2b2d67e9 --- /dev/null +++ b/apps/website/app/api/internal/space/[id]/graph/route.test.ts @@ -0,0 +1,304 @@ +import { NextRequest } from "next/server"; +import { + AuthApiError, + AuthRetryableFetchError, + PostgrestError, +} from "@supabase/supabase-js"; +import { beforeEach, describe, expect, it, vi, type Mock } from "vitest"; +import type { DGSupabaseClient } from "@repo/database/lib/client"; +import internalError from "~/utils/internalErrorSsr"; +import { createBearerTokenClient } from "~/utils/supabase/server"; +import { OPTIONS, POST } from "./route"; + +vi.mock("~/utils/supabase/server", () => ({ + createBearerTokenClient: vi.fn(), +})); +vi.mock("~/utils/internalErrorSsr", () => ({ default: vi.fn() })); + +const ORIGIN = "https://roamresearch.com"; +const SPACE_URL = "https://roamresearch.com/#/app/graph-a"; +const URL_ROOT = "http://localhost:3000/api/internal/space"; + +type Result = { data: unknown; error: unknown }; + +const node = { + localId: "claim-1", + createdAt: "2026-11-13T10:00:00Z", + authorId: "account-1", + nodeType: "claim-type", + coreTitle: "Sleep helps memory", + content: { direct: { value: "CLM - Sleep helps memory" } }, +}; + +const storedClaimType: Result = { + data: [ + { source_local_id: "claim-type", literal_content: { label: "Claim" } }, + ], + error: null, +}; + +// Resolves to a fixed result whatever filters the route chains on it. +const query = (result: Result): object => { + const builder = { + select: () => builder, + eq: () => builder, + in: () => builder, + maybeSingle: () => builder, + then: ( + resolve: (value: Result) => unknown, + reject: (reason: unknown) => unknown, + ) => Promise.resolve(result).then(resolve, reject), + }; + return builder; +}; + +const fakeClient = ({ + authError = null, + editorSpaceIds = [5], + stored = { data: [], error: null }, + upsert = { data: [], error: null }, +}: { + authError?: Error | null; + editorSpaceIds?: number[]; + stored?: Result; + upsert?: Result; +} = {}): { rpc: Mock; from: Mock } => { + const client = { + auth: { + getUser: () => + Promise.resolve({ + data: { user: authError ? null : { id: "user-1" } }, + error: authError, + }), + }, + rpc: vi.fn((fn: string) => + Promise.resolve( + fn === "my_space_ids" ? { data: editorSpaceIds, error: null } : upsert, + ), + ), + from: vi.fn((table: string) => + query( + table === "Space" ? { data: { url: SPACE_URL }, error: null } : stored, + ), + ), + }; + vi.mocked(createBearerTokenClient).mockReturnValue( + client as unknown as DGSupabaseClient, + ); + return client; +}; + +const post = ({ + id = "5", + token = "token-1", + body = {}, +}: { + id?: string; + token?: string; + body?: string | object; +} = {}): Promise => + POST( + new NextRequest(`${URL_ROOT}/${id}/graph`, { + method: "POST", + headers: { + origin: ORIGIN, + "content-type": "application/json", + ...(token ? { authorization: `Bearer ${token}` } : {}), + }, + body: typeof body === "string" ? body : JSON.stringify(body), + }), + { params: Promise.resolve({ id }) }, + ); + +const expectResponse = async ( + response: Response, + { status, body }: { status: number; body?: object }, +): Promise => { + expect(response.status).toBe(status); + expect(response.headers.get("access-control-allow-origin")).toBe(ORIGIN); + if (body !== undefined) expect(await response.json()).toMatchObject(body); +}; + +describe("POST /api/internal/space/[id]/graph", () => { + beforeEach(() => { + vi.clearAllMocks(); + vi.spyOn(console, "error").mockImplementation(() => undefined); + }); + + it.each([ + { id: "5abc", when: "is not all digits" }, + { id: "9007199254740992", when: "is past the safe-integer range" }, + ])("refuses a space id that $when", async ({ id }) => { + fakeClient(); + await expectResponse(await post({ id }), { status: 400 }); + expect(createBearerTokenClient).not.toHaveBeenCalled(); + }); + + it("refuses a request without a bearer token", async () => { + fakeClient(); + await expectResponse(await post({ token: "" }), { + status: 401, + body: { error: "Missing bearer token" }, + }); + }); + + it("answers 401 when the auth server refuses the token", async () => { + fakeClient({ authError: new AuthApiError("invalid JWT", 403, "bad_jwt") }); + await expectResponse(await post(), { status: 401 }); + }); + + it.each([ + { + when: "is unreachable", + authError: new AuthRetryableFetchError("fetch failed", 0), + }, + { + when: "rate-limits the caller", + authError: new AuthApiError( + "Request rate limit reached", + 429, + "over_request_rate_limit", + ), + }, + { + when: "fails with a status the client does not retry", + authError: new AuthApiError("Internal Server Error", 505, undefined), + }, + ])( + "answers 502 and reports it when the auth server $when", + async ({ authError }) => { + fakeClient({ authError }); + await expectResponse(await post(), { status: 502 }); + expect(internalError).toHaveBeenCalledOnce(); + }, + ); + + it("refuses a caller who is not an editor of the space", async () => { + fakeClient({ editorSpaceIds: [6] }); + await expectResponse(await post(), { + status: 403, + body: { error: "Not an editor of this space" }, + }); + }); + + it("refuses a body that is not JSON", async () => { + fakeClient(); + await expectResponse(await post({ body: "not json" }), { status: 400 }); + }); + + it("refuses a body whose rid names another space, writing nothing", async () => { + const { rpc } = fakeClient(); + const rid = "https://roamresearch.com/#/app/graph-b/claim-1"; + await expectResponse(await post({ body: { nodes: [{ ...node, rid }] } }), { + status: 400, + body: { issues: [{ path: ["nodes", 0, "rid"] }] }, + }); + expect(rpc).not.toHaveBeenCalledWith("upsert_concepts", expect.anything()); + }); + + it.each([ + { + when: "has a date Postgres can't store", + body: { nodes: [{ ...node, createdAt: "0000-06-01T00:00:00Z" }] }, + path: ["nodes", 0, "createdAt"], + }, + { + when: "has a node whose type is a RID", + body: { nodes: [{ ...node, nodeType: `${SPACE_URL}/no-such-type` }] }, + path: ["nodes", 0, "nodeType"], + }, + ])("refuses a body that $when, writing nothing", async ({ body, path }) => { + const { rpc } = fakeClient(); + await expectResponse(await post({ body }), { + status: 400, + body: { issues: [{ path }] }, + }); + expect(rpc).not.toHaveBeenCalledWith("upsert_concepts", expect.anything()); + }); + + it.each([ + { when: "one schema id of 5,000 characters", types: ["t".repeat(5_000)] }, + { + when: "one schema id of 1,500 characters the URL encodes three times over", + types: ["~".repeat(1_500)], + }, + { + when: "1,000 schema ids", + types: Array.from({ length: 1_000 }, (_, i) => `type-${i}`), + }, + ])( + "refuses a body referencing $when it does not define, before any lookup", + async ({ types }) => { + const { from } = fakeClient(); + const nodes = types.map((nodeType, i) => ({ + ...node, + localId: `claim-${i}`, + nodeType, + })); + await expectResponse(await post({ body: { nodes } }), { status: 400 }); + expect(from).not.toHaveBeenCalledWith("my_concepts"); + }, + ); + + it("answers 422 for a schema in neither the body nor the space", async () => { + const { rpc } = fakeClient(); + await expectResponse(await post({ body: { nodes: [node] } }), { + status: 422, + body: { missing: ["claim-type"] }, + }); + expect(rpc).not.toHaveBeenCalledWith("upsert_concepts", expect.anything()); + }); + + it("answers 500 and reports it when the upsert fails", async () => { + const error = new PostgrestError({ + message: "upsert failed", + details: "", + hint: "", + code: "XX000", + }); + fakeClient({ stored: storedClaimType, upsert: { data: null, error } }); + await expectResponse(await post({ body: { nodes: [node] } }), { + status: 500, + body: { error: "upsert failed" }, + }); + expect(internalError).toHaveBeenCalledOnce(); + }); + + it("answers 200 with the id of each upserted local id", async () => { + const { rpc } = fakeClient({ + stored: storedClaimType, + upsert: { data: [42], error: null }, + }); + await expectResponse(await post({ body: { nodes: [node] } }), { + status: 200, + body: { upserted: { "claim-1": 42 }, failed: [] }, + }); + expect(rpc).toHaveBeenCalledWith("upsert_concepts", { + v_space_id: 5, + data: [expect.objectContaining({ source_local_id: "claim-1" })], + }); + }); + + it("answers 200 to an empty body after an empty upsert", async () => { + const { rpc, from } = fakeClient(); + await expectResponse(await post(), { + status: 200, + body: { upserted: {}, failed: [] }, + }); + expect(from).not.toHaveBeenCalledWith("my_concepts"); + expect(rpc).toHaveBeenCalledWith("upsert_concepts", { + v_space_id: 5, + data: [], + }); + }); + + it("answers a CORS preflight", async () => { + const response = await OPTIONS( + new NextRequest(`${URL_ROOT}/5/graph`, { + method: "OPTIONS", + headers: { origin: ORIGIN }, + }), + ); + await expectResponse(response, { status: 204 }); + }); +}); diff --git a/apps/website/app/api/internal/space/[id]/graph/route.ts b/apps/website/app/api/internal/space/[id]/graph/route.ts new file mode 100644 index 000000000..5131ec790 --- /dev/null +++ b/apps/website/app/api/internal/space/[id]/graph/route.ts @@ -0,0 +1,120 @@ +import { NextRequest, NextResponse } from "next/server"; +import { + isAuthApiError, + isAuthSessionMissingError, +} from "@supabase/supabase-js"; +import { + findMissingSchemas, + mapUpsertResults, + parseUpsertData, + schemaReferences, + toOrderedEntries, +} from "@repo/database/lib/crossAppUpsert"; +import internalError from "~/utils/internalErrorSsr"; +import cors from "~/utils/llm/cors"; +import { + defaultOptionsHandler, + handleRouteError, + type SegmentDataType, +} from "~/utils/supabase/apiUtils"; +import { createBearerTokenClient } from "~/utils/supabase/server"; + +// The schema lookup puts every reference in its URL query, and a gateway refuses a +// long URL (the local one past about 8,200 characters). The length is measured as +// postgrest-js encodes it, through URLSearchParams with each id quoted at most. +// Real bodies reference tens of schemas, far below this bound. +const MAX_SCHEMA_LOOKUP_CHARS = 4_000; + +export const POST = async ( + request: NextRequest, + segmentData: SegmentDataType, +): Promise => { + const respond = (status: number, body: object): NextResponse => + cors(request, NextResponse.json(body, { status })) as NextResponse; + try { + const { id } = await segmentData.params; + if (!/^\d+$/.test(id) || !Number.isSafeInteger(Number(id))) + return respond(400, { error: `${id} is not a number` }); + const spaceId = Number(id); + const fail = (status: number, error: Error): NextResponse => { + internalError({ error, type: "upsert-graph", context: { spaceId } }); + return respond(status, { error: error.message }); + }; + + const token = request.headers + .get("Authorization") + ?.match(/^Bearer (.+)$/)?.[1]; + if (token === undefined) + return respond(401, { error: "Missing bearer token" }); + const client = createBearerTokenClient(token); + const { error: authError } = await client.auth.getUser(token); + // A plugin reads 401 as "sign in again", so only the auth server's verdict + // on the token gets it; an unreachable or rate-limiting auth server is a 502. + if ( + (isAuthApiError(authError) && + authError.status < 500 && + authError.status !== 429) || + isAuthSessionMissingError(authError) + ) + return respond(401, { error: authError.message }); + if (authError) return fail(502, authError); + + const [editorSpaceIds, space] = await Promise.all([ + client.rpc("my_space_ids", { access_level: "editor" }), + client.from("Space").select("url").eq("id", spaceId).maybeSingle(), + ]); + if (editorSpaceIds.error) return fail(500, editorSpaceIds.error); + if (space.error) return fail(500, space.error); + if (!editorSpaceIds.data.includes(spaceId) || space.data === null) + return respond(403, { error: "Not an editor of this space" }); + + const parsed = parseUpsertData({ + body: await request.json(), + spaceUrl: space.data.url, + }); + if (!parsed.success) + return respond(400, { + error: "The body does not match the CrossApp types", + issues: parsed.issues, + }); + const { data } = parsed; + + const references = schemaReferences(data); + const lookupQuery = new URLSearchParams({ + source_local_id: `in.(${references.map((id) => `"${id}"`).join(",")})`, + }).toString(); + if (lookupQuery.length > MAX_SCHEMA_LOOKUP_CHARS) + return respond(400, { + error: + "The schemas this body references but does not define are too long to look up", + }); + const stored = + references.length === 0 + ? { data: [], error: null } + : await client + .from("my_concepts") + .select("source_local_id, literal_content") + .eq("space_id", spaceId) + .eq("is_schema", true) + .in("source_local_id", references); + if (stored.error) return fail(500, stored.error); + const missing = findMissingSchemas({ data, storedSchemas: stored.data }); + if (missing.length > 0) + return respond(422, { + error: "Referenced schemas are in neither the body nor this space", + missing, + }); + + const entries = toOrderedEntries({ data, storedSchemas: stored.data }); + const upsert = await client.rpc("upsert_concepts", { + v_space_id: spaceId, + data: entries.map((e) => e.concept), + }); + if (upsert.error) return fail(500, upsert.error); + return respond(200, mapUpsertResults({ entries, ids: upsert.data })); + } catch (e: unknown) { + return handleRouteError(request, e, "/api/internal/space/[id]/graph"); + } +}; + +export const OPTIONS = defaultOptionsHandler; diff --git a/apps/website/app/utils/supabase/server.ts b/apps/website/app/utils/supabase/server.ts index e3377ed74..212d31ec8 100644 --- a/apps/website/app/utils/supabase/server.ts +++ b/apps/website/app/utils/supabase/server.ts @@ -1,7 +1,9 @@ import { createServerClient, type CookieOptions } from "@supabase/ssr"; +import { createClient as createSupabaseClient } from "@supabase/supabase-js"; import { cookies } from "next/headers"; import type { Database } from "@repo/database/dbTypes"; import { envContents } from "@repo/database/dbDotEnv"; +import type { DGSupabaseClient } from "@repo/database/lib/client"; // This is a supabase client to be used in a server process such as NextJS // Inspired by https://supabase.com/ui/docs/nextjs/password-based-auth @@ -50,3 +52,22 @@ export const createClient = async () => { }, }); }; + +// For callers signed in through a plugin rather than website cookies. Queries +// run as the token's user under RLS, so this must never take the service key. +export const createBearerTokenClient = ( + accessToken: string, +): DGSupabaseClient => { + const dbEnv = envContents(); + if (!dbEnv.SUPABASE_URL || !dbEnv.SUPABASE_PUBLISHABLE_KEY) { + throw new Error("Missing required Supabase environment variables"); + } + return createSupabaseClient( + dbEnv.SUPABASE_URL, + dbEnv.SUPABASE_PUBLISHABLE_KEY, + { + global: { headers: { Authorization: `Bearer ${accessToken}` } }, + auth: { persistSession: false, autoRefreshToken: false }, + }, + ); +};