Skip to content

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

feat(api): gate /v2/developer/search behind the developerBeta flag

Kris Oei5f93d81d4c8e
diff --git a/apps/api/src/controllers/v2/__tests__/developer-beta-gate.test.ts b/apps/api/src/controllers/v2/__tests__/developer-beta-gate.test.ts
new file mode 100644
index 000000000..d1b57aec1
--- /dev/null
+++ b/apps/api/src/controllers/v2/__tests__/developer-beta-gate.test.ts
@@ -0,0 +1,95 @@
+import { vi } from "vitest";
+
+const mockLogRequest = vi.fn();
+const mockLogSearch = vi.fn();
+const mockLogResearchEndpoint = vi.fn();
+
+vi.mock("../../../services/logging/log_job", () => ({
+ logRequest: (...args: any[]) => mockLogRequest(...args),
+ logSearch: (...args: any[]) => mockLogSearch(...args),
+ logResearchEndpoint: (...args: any[]) => mockLogResearchEndpoint(...args),
+}));
+
+const mockFetchResearchUpstream = vi.fn();
+
+vi.mock("../../../lib/research-upstream", () => ({
+ fetchResearchUpstream: (...args: any[]) => mockFetchResearchUpstream(...args),
+}));
+
+vi.mock("../../../services/billing/credit_billing", () => ({
+ billTeam: vi.fn().mockResolvedValue(undefined),
+}));
+
+vi.mock("../../../lib/logger", () => ({
+ logger: {
+ info: vi.fn(),
+ warn: vi.fn(),
+ error: vi.fn(),
+ debug: vi.fn(),
+ child: vi.fn().mockReturnThis(),
+ },
+}));
+
+import { createDeveloperRouter } from "../research-proxy";
+
+const TEAM_ID = "11111111-1111-1111-1111-111111111111";
+
+/** Pulls the GET /search handler out of the router's stack. */
+function developerHandler() {
+ const router: any = createDeveloperRouter();
+ const layer = router.stack.find(
+ (l: any) => l.route?.path === "/search" && l.route?.methods?.get,
+ );
+ return layer.route.stack[0].handle;
+}
+
+function makeReq(flags: Record<string, unknown> | null) {
+ return {
+ method: "GET",
+ query: { query: "http client" },
+ body: {},
+ headers: {},
+ auth: { team_id: TEAM_ID },
+ acuc: flags === null ? undefined : { api_key_id: 7, flags },
+ } as any;
+}
+
+function makeRes() {
+ const res: any = { status: vi.fn(), json: vi.fn(), end: vi.fn() };
+ res.status.mockReturnValue(res);
+ res.json.mockReturnValue(res);
+ return res;
+}
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ mockLogRequest.mockResolvedValue(undefined);
+ mockLogResearchEndpoint.mockResolvedValue(undefined);
+});
+
+describe("developer endpoint beta gate", () => {
+ it("403s a team without developerBeta and never calls upstream", async () => {
+ const res = makeRes();
+ await developerHandler()(makeReq({ developerBeta: false }), res);
+
+ expect(res.status).toHaveBeenCalledWith(403);
+ expect(mockFetchResearchUpstream).not.toHaveBeenCalled();
+ expect(mockLogRequest).not.toHaveBeenCalled();
+ });
+
+ it("403s a keyless caller (no acuc at all)", async () => {
+ const res = makeRes();
+ await developerHandler()(makeReq(null), res);
+
+ expect(res.status).toHaveBeenCalledWith(403);
+ expect(mockFetchResearchUpstream).not.toHaveBeenCalled();
+ });
+
+ it("does not 403 a team with developerBeta", async () => {
+ mockFetchResearchUpstream.mockResolvedValue(null); // 404 path, past the gate
+ const res = makeRes();
+ await developerHandler()(makeReq({ developerBeta: true }), res);
+
+ expect(res.status).not.toHaveBeenCalledWith(403);
+ });
+});
diff --git a/apps/api/src/controllers/v2/research-proxy.ts b/apps/api/src/controllers/v2/research-proxy.ts
index dca736b17..0a5287c53 100644
--- a/apps/api/src/controllers/v2/research-proxy.ts
+++ b/apps/api/src/controllers/v2/research-proxy.ts
@@ -140,6 +140,12 @@ type ResearchEndpointConfig = {
req: RequestWithAuth<any, any, any>,
) => string;
billAs: "scrape" | "search";
+ /**
+ * Org flag that must be exactly `true` for this endpoint. Absent means
+ * ungated (the research endpoints). Keyless callers have no acuc and so no
+ * flags, which makes them unentitled by construction.
+ */
+ betaGate?: { flag: string; error: string };
};
type ResearchController = (req: Request, res: Response) => Promise<any>;
@@ -245,6 +251,18 @@ function createResearchController(
): ResearchController {
return async (req, res: Response) => {
const authedReq = req as RequestWithAuth<any, any, any>;
+
+ // Beta gate: fail closed before any logging, billing, or upstream call.
+ if (endpoint.betaGate) {
+ const flags = authedReq.acuc?.flags as
+ | Record<string, unknown>
+ | null
+ | undefined;
+ if (flags?.[endpoint.betaGate.flag] !== true) {
+ return researchError(res, 403, endpoint.betaGate.error);
+ }
+ }
+
const started = Date.now();
const jobId = uuidv7();
const logger = rootLogger.child({
@@ -494,6 +512,11 @@ export function createDeveloperRouter(options: { root?: boolean } = {}) {
targetHint: params => String(params.query),
upstreamPath: () => "/v2/code/search",
billAs: "search",
+ betaGate: {
+ flag: "developerBeta",
+ error:
+ "Developer search is in beta and is not enabled for this team.",
+ },
},
),
);