From 12d9c3d97eeb1eb7869d1f8ef6d61036c09bb568 Mon Sep 17 00:00:00 2001 From: sid597 Date: Fri, 13 Nov 2026 16:21:05 +0530 Subject: [PATCH 1/6] FEE-890 Add the CrossApp upsert route --- .../internal/space/[id]/graph/route.test.ts | 235 ++++++++++++++++++ .../api/internal/space/[id]/graph/route.ts | 97 ++++++++ apps/website/app/utils/supabase/server.ts | 21 ++ 3 files changed, 353 insertions(+) create mode 100644 apps/website/app/api/internal/space/[id]/graph/route.test.ts create mode 100644 apps/website/app/api/internal/space/[id]/graph/route.ts 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 0000000000..25c4e1b588 --- /dev/null +++ b/apps/website/app/api/internal/space/[id]/graph/route.test.ts @@ -0,0 +1,235 @@ +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; +} = {}): 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: (table: string) => + query( + table === "Space" ? { data: { url: SPACE_URL }, error: null } : stored, + ), + }; + vi.mocked(createBearerTokenClient).mockReturnValue( + client as unknown as DGSupabaseClient, + ); + return client.rpc; +}; + +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("refuses a space id that is not all digits", async () => { + fakeClient(); + await expectResponse(await post({ id: "5abc" }), { 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("answers 502 and reports it when the auth server is unreachable", async () => { + fakeClient({ authError: new AuthRetryableFetchError("fetch failed", 0) }); + 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("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 = fakeClient(); + await expectResponse(await post(), { + status: 200, + body: { upserted: {}, failed: [] }, + }); + 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 0000000000..4beab5f4b8 --- /dev/null +++ b/apps/website/app/api/internal/space/[id]/graph/route.ts @@ -0,0 +1,97 @@ +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"; + +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)) + 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 auth server is a 502. + if (isAuthApiError(authError) || 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 stored = await client + .from("my_concepts") + .select("source_local_id, literal_content") + .eq("space_id", spaceId) + .eq("is_schema", true) + .in("source_local_id", schemaReferences(data)); + 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 e3377ed74d..212d31ec86 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 }, + }, + ); +}; From 6201c12083db5232c7217c6a4d852cafb98f0540 Mon Sep 17 00:00:00 2001 From: sid597 Date: Fri, 13 Nov 2026 22:08:13 +0530 Subject: [PATCH 2/6] FEE-890 Answer a rate-limited auth check with 502, skip an empty schema lookup and refuse an unsafe space id --- .../internal/space/[id]/graph/route.test.ts | 49 +++++++++++++------ .../api/internal/space/[id]/graph/route.ts | 25 ++++++---- 2 files changed, 51 insertions(+), 23 deletions(-) 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 index 25c4e1b588..d8398aac4f 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.test.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.test.ts @@ -62,7 +62,7 @@ const fakeClient = ({ editorSpaceIds?: number[]; stored?: Result; upsert?: Result; -} = {}): Mock => { +} = {}): { rpc: Mock; from: Mock } => { const client = { auth: { getUser: () => @@ -76,15 +76,16 @@ const fakeClient = ({ fn === "my_space_ids" ? { data: editorSpaceIds, error: null } : upsert, ), ), - from: (table: string) => + 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.rpc; + return client; }; const post = ({ @@ -124,9 +125,12 @@ describe("POST /api/internal/space/[id]/graph", () => { vi.spyOn(console, "error").mockImplementation(() => undefined); }); - it("refuses a space id that is not all digits", async () => { + 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: "5abc" }), { status: 400 }); + await expectResponse(await post({ id }), { status: 400 }); expect(createBearerTokenClient).not.toHaveBeenCalled(); }); @@ -143,11 +147,27 @@ describe("POST /api/internal/space/[id]/graph", () => { await expectResponse(await post(), { status: 401 }); }); - it("answers 502 and reports it when the auth server is unreachable", async () => { - fakeClient({ authError: new AuthRetryableFetchError("fetch failed", 0) }); - await expectResponse(await post(), { status: 502 }); - expect(internalError).toHaveBeenCalledOnce(); - }); + 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", + ), + }, + ])( + "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] }); @@ -163,7 +183,7 @@ describe("POST /api/internal/space/[id]/graph", () => { }); it("refuses a body whose rid names another space, writing nothing", async () => { - const rpc = fakeClient(); + const { rpc } = fakeClient(); const rid = "https://roamresearch.com/#/app/graph-b/claim-1"; await expectResponse(await post({ body: { nodes: [{ ...node, rid }] } }), { status: 400, @@ -173,7 +193,7 @@ describe("POST /api/internal/space/[id]/graph", () => { }); it("answers 422 for a schema in neither the body nor the space", async () => { - const rpc = fakeClient(); + const { rpc } = fakeClient(); await expectResponse(await post({ body: { nodes: [node] } }), { status: 422, body: { missing: ["claim-type"] }, @@ -197,7 +217,7 @@ describe("POST /api/internal/space/[id]/graph", () => { }); it("answers 200 with the id of each upserted local id", async () => { - const rpc = fakeClient({ + const { rpc } = fakeClient({ stored: storedClaimType, upsert: { data: [42], error: null }, }); @@ -212,11 +232,12 @@ describe("POST /api/internal/space/[id]/graph", () => { }); it("answers 200 to an empty body after an empty upsert", async () => { - const rpc = fakeClient(); + 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: [], diff --git a/apps/website/app/api/internal/space/[id]/graph/route.ts b/apps/website/app/api/internal/space/[id]/graph/route.ts index 4beab5f4b8..688df6c1ca 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.ts @@ -27,7 +27,7 @@ export const POST = async ( cors(request, NextResponse.json(body, { status })) as NextResponse; try { const { id } = await segmentData.params; - if (!/^\d+$/.test(id)) + 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 => { @@ -43,8 +43,11 @@ export const POST = async ( 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 auth server is a 502. - if (isAuthApiError(authError) || isAuthSessionMissingError(authError)) + // on the token gets it; an unreachable or rate-limiting auth server is a 502. + if ( + (isAuthApiError(authError) && authError.status !== 429) || + isAuthSessionMissingError(authError) + ) return respond(401, { error: authError.message }); if (authError) return fail(502, authError); @@ -68,12 +71,16 @@ export const POST = async ( }); const { data } = parsed; - const stored = await client - .from("my_concepts") - .select("source_local_id, literal_content") - .eq("space_id", spaceId) - .eq("is_schema", true) - .in("source_local_id", schemaReferences(data)); + const references = schemaReferences(data); + 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) From abdc8629335e26572c4a253c44015315a59c36bc Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 16 Nov 2026 10:04:36 +0530 Subject: [PATCH 3/6] FEE-890 Answer 502, not 401, for an auth server error --- apps/website/app/api/internal/space/[id]/graph/route.test.ts | 4 ++++ apps/website/app/api/internal/space/[id]/graph/route.ts | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) 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 index d8398aac4f..9b0247b8e8 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.test.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.test.ts @@ -160,6 +160,10 @@ describe("POST /api/internal/space/[id]/graph", () => { "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 }) => { diff --git a/apps/website/app/api/internal/space/[id]/graph/route.ts b/apps/website/app/api/internal/space/[id]/graph/route.ts index 688df6c1ca..a623a3761e 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.ts @@ -45,7 +45,9 @@ export const POST = async ( // 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 !== 429) || + (isAuthApiError(authError) && + authError.status < 500 && + authError.status !== 429) || isAuthSessionMissingError(authError) ) return respond(401, { error: authError.message }); From 38cfcb4507106a8c603955d843b3315d4a1d608e Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 16 Nov 2026 11:05:33 +0530 Subject: [PATCH 4/6] FEE-890 Refuse a body referencing more than 100 schemas it does not define --- .../internal/space/[id]/graph/route.test.ts | 31 +++++++++++++++++++ .../api/internal/space/[id]/graph/route.ts | 8 +++++ 2 files changed, 39 insertions(+) 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 index 9b0247b8e8..b0c3dabbfd 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.test.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.test.ts @@ -196,6 +196,37 @@ describe("POST /api/internal/space/[id]/graph", () => { 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("refuses a body referencing more than 100 schemas, before any lookup", async () => { + const { from } = fakeClient(); + const nodes = Array.from({ length: 101 }, (_, i) => ({ + ...node, + localId: `claim-${i}`, + nodeType: `type-${i}`, + })); + 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] } }), { diff --git a/apps/website/app/api/internal/space/[id]/graph/route.ts b/apps/website/app/api/internal/space/[id]/graph/route.ts index a623a3761e..d1b370bb11 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.ts @@ -19,6 +19,10 @@ import { } from "~/utils/supabase/apiUtils"; import { createBearerTokenClient } from "~/utils/supabase/server"; +// The schema lookup puts every reference in its URL, which a gateway refuses past +// a few kilobytes. Real bodies reference tens of schemas. +const MAX_SCHEMA_REFERENCES = 100; + export const POST = async ( request: NextRequest, segmentData: SegmentDataType, @@ -74,6 +78,10 @@ export const POST = async ( const { data } = parsed; const references = schemaReferences(data); + if (references.length > MAX_SCHEMA_REFERENCES) + return respond(400, { + error: `The body references more than ${MAX_SCHEMA_REFERENCES} schemas it does not define`, + }); const stored = references.length === 0 ? { data: [], error: null } From 1667298bb050dbf568425b78efca5d7db1fb7d87 Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 16 Nov 2026 11:16:53 +0530 Subject: [PATCH 5/6] FEE-890 Bound the schema lookup by its URL length, not its count --- .../internal/space/[id]/graph/route.test.ts | 29 ++++++++++++------- .../api/internal/space/[id]/graph/route.ts | 14 +++++---- 2 files changed, 28 insertions(+), 15 deletions(-) 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 index b0c3dabbfd..bad8abca71 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.test.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.test.ts @@ -216,16 +216,25 @@ describe("POST /api/internal/space/[id]/graph", () => { expect(rpc).not.toHaveBeenCalledWith("upsert_concepts", expect.anything()); }); - it("refuses a body referencing more than 100 schemas, before any lookup", async () => { - const { from } = fakeClient(); - const nodes = Array.from({ length: 101 }, (_, i) => ({ - ...node, - localId: `claim-${i}`, - nodeType: `type-${i}`, - })); - await expectResponse(await post({ body: { nodes } }), { status: 400 }); - expect(from).not.toHaveBeenCalledWith("my_concepts"); - }); + it.each([ + { when: "one schema id of 5,000 characters", types: ["t".repeat(5_000)] }, + { + 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(); diff --git a/apps/website/app/api/internal/space/[id]/graph/route.ts b/apps/website/app/api/internal/space/[id]/graph/route.ts index d1b370bb11..954980f06d 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.ts @@ -19,9 +19,10 @@ import { } from "~/utils/supabase/apiUtils"; import { createBearerTokenClient } from "~/utils/supabase/server"; -// The schema lookup puts every reference in its URL, which a gateway refuses past -// a few kilobytes. Real bodies reference tens of schemas. -const MAX_SCHEMA_REFERENCES = 100; +// The schema lookup puts every reference in its URL, and a gateway refuses a long +// URL (the local one at about 33 KB). Real bodies reference tens of schemas, far +// below this bound. +const MAX_SCHEMA_LOOKUP_CHARS = 4_000; export const POST = async ( request: NextRequest, @@ -78,9 +79,12 @@ export const POST = async ( const { data } = parsed; const references = schemaReferences(data); - if (references.length > MAX_SCHEMA_REFERENCES) + if ( + encodeURIComponent(references.join(",")).length > MAX_SCHEMA_LOOKUP_CHARS + ) return respond(400, { - error: `The body references more than ${MAX_SCHEMA_REFERENCES} schemas it does not define`, + error: + "The schemas this body references but does not define are too long to look up", }); const stored = references.length === 0 From 9d89088e5432a948f89c5f955b9398da481394ac Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 16 Nov 2026 12:01:08 +0530 Subject: [PATCH 6/6] FEE-890 Measure the schema lookup as postgrest-js encodes it --- .../api/internal/space/[id]/graph/route.test.ts | 4 ++++ .../app/api/internal/space/[id]/graph/route.ts | 14 ++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) 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 index bad8abca71..b2b2d67e95 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.test.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.test.ts @@ -218,6 +218,10 @@ describe("POST /api/internal/space/[id]/graph", () => { 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}`), diff --git a/apps/website/app/api/internal/space/[id]/graph/route.ts b/apps/website/app/api/internal/space/[id]/graph/route.ts index 954980f06d..5131ec7904 100644 --- a/apps/website/app/api/internal/space/[id]/graph/route.ts +++ b/apps/website/app/api/internal/space/[id]/graph/route.ts @@ -19,9 +19,10 @@ import { } from "~/utils/supabase/apiUtils"; import { createBearerTokenClient } from "~/utils/supabase/server"; -// The schema lookup puts every reference in its URL, and a gateway refuses a long -// URL (the local one at about 33 KB). Real bodies reference tens of schemas, far -// below this bound. +// 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 ( @@ -79,9 +80,10 @@ export const POST = async ( const { data } = parsed; const references = schemaReferences(data); - if ( - encodeURIComponent(references.join(",")).length > MAX_SCHEMA_LOOKUP_CHARS - ) + 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",