Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

Forward caller authorization for isolated Alexandria SQL requests (#4768)

Developers Digestf75a8d40b103
diff --git a/apps/api/src/services/alexandria/retrieve.test.ts b/apps/api/src/services/alexandria/retrieve.test.ts
index 29763804c..a75101ab7 100644
--- a/apps/api/src/services/alexandria/retrieve.test.ts
+++ b/apps/api/src/services/alexandria/retrieve.test.ts
@@ -494,3 +494,54 @@ it("does not derive terms credential identity from numeric API-key IDs", async (
expect(execution[0].termsIdentity).toBeUndefined();
}
});
+
+const sqlCall = {
+ provider: "firecrawl",
+ capability: "sql",
+ options: { query: "SHOW TABLES IN apollo LIMIT 10" },
+};
+it("forwards standalone SQL credentials only to execution without retaining them", async () => {
+ exchangeAnswers({
+ success: true,
+ creditsCost: 0,
+ results: [
+ { ...sqlCall, creditsCost: 0, data: { kind: "tables", items: [] } },
+ ],
+ });
+ expect(
+ await run({ calls: [sqlCall], resultAuthorization: "Bearer caller" }),
+ ).toMatchObject({ status: 200, executed: true });
+ expect(executions()).toHaveLength(1);
+ expect(executions()[0][0].resultAuthorization).toBe("Bearer caller");
+ expect(
+ mocks.request.mock.calls
+ .filter(([arg]) => arg.path !== "/v1/retrieve")
+ .every(([arg]) => arg.resultAuthorization === undefined),
+ ).toBe(true);
+ expect([...mocks.store.values()].join("")).not.toContain("Bearer caller");
+});
+it.each([
+ { name: "SQL first", calls: [sqlCall, call] },
+ { name: "SQL last", calls: [call, sqlCall] },
+ { name: "two SQL calls", calls: [sqlCall, sqlCall] },
+])("rejects batched SQL before side effects: $name", async ({ calls }) => {
+ expect(
+ await run({ calls, resultAuthorization: "Bearer caller" }),
+ ).toMatchObject({ status: 400, executed: false });
+ expect(mocks.request).not.toHaveBeenCalled();
+ expect(mocks.authorize).not.toHaveBeenCalled();
+ expect(mocks.lock).not.toHaveBeenCalled();
+ expect(mocks.store.size).toBe(0);
+});
+it("does not forward credentials to another provider's sql capability", async () => {
+ const other = { ...sqlCall, provider: "other" };
+ exchangeAnswers({
+ success: true,
+ creditsCost: 0,
+ results: [{ ...other, creditsCost: 0, data: {} }],
+ });
+ expect(
+ await run({ calls: [other], resultAuthorization: "Bearer caller" }),
+ ).toMatchObject({ status: 200, executed: true });
+ expect(executions()[0][0].resultAuthorization).toBeUndefined();
+});
diff --git a/apps/api/src/services/alexandria/retrieve.ts b/apps/api/src/services/alexandria/retrieve.ts
index 2134b7f8b..094d64115 100644
--- a/apps/api/src/services/alexandria/retrieve.ts
+++ b/apps/api/src/services/alexandria/retrieve.ts
@@ -106,6 +106,17 @@ export async function retrieveProviders(input: {
if (Buffer.byteLength(JSON.stringify(input.calls)) > 256 * 1024)
return notExecuted(refusal(400, "Provider options exceed 256 KB."));
+ const usesSql = input.calls.some(
+ call => call.provider === "firecrawl" && call.capability === "sql",
+ );
+ if (usesSql && input.calls.length !== 1)
+ return notExecuted(
+ refusal(
+ 400,
+ "SQL must be sent as a separate request; do not batch it with other calls.",
+ ),
+ );
+
const loadsSavedResult = input.calls.some(
call =>
call.provider === "firecrawl" &&
@@ -347,7 +358,7 @@ export async function retrieveProviders(input: {
body: { requests: input.calls },
timeoutMs: remaining(),
requestId: id,
- ...(loadsSavedResult && input.resultAuthorization
+ ...((loadsSavedResult || usesSql) && input.resultAuthorization
? { resultAuthorization: input.resultAuthorization }
: {}),
...(termsIdentity ? { termsIdentity } : {}),