Skip to content

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

feat(auth): enforce team bans via auth_chunk_1 is_banned (#4136)

firecrawl-spring[bot]5e71acbbe9c9
diff --git a/apps/api/src/controllers/__tests__/auth.test.ts b/apps/api/src/controllers/__tests__/auth.test.ts
index 46548609c..ed1f0ab31 100644
--- a/apps/api/src/controllers/__tests__/auth.test.ts
+++ b/apps/api/src/controllers/__tests__/auth.test.ts
@@ -170,6 +170,47 @@ describe("authenticateUser", () => {
);
});
+ it("rejects a banned team with 403", async () => {
+ config.USE_DB_AUTHENTICATION = true;
+ vi.mocked(getValue).mockResolvedValue(null);
+ vi.mocked(authCreditUsageChunk).mockResolvedValue([
+ {
+ api_key: "00000000-0000-4000-8000-000000000000",
+ api_key_id: 1,
+ team_id: "team-banned",
+ org_id: "org-1",
+ is_banned: true,
+ flags: null,
+ },
+ ]);
+ vi.mocked(redlock.using).mockImplementation(
+ async (_keys, _ttl, _options, fn) => fn({ aborted: false } as never),
+ );
+ vi.mocked(autumnService.getRateLimitMultiplier).mockResolvedValue(1);
+ const consume = vi.fn().mockResolvedValue(undefined);
+ vi.mocked(getAutumnRateLimiter).mockReturnValue({ consume } as never);
+
+ const auth = await authenticateUser(
+ {
+ headers: {
+ authorization: "Bearer 00000000-0000-4000-8000-000000000000",
+ },
+ socket: { remoteAddress: "127.0.0.1" },
+ },
+ {},
+ RateLimiterMode.Scrape,
+ );
+
+ expect(auth).toEqual({
+ success: false,
+ error:
+ "Unauthorized: This account has been banned. Contact [email protected] if you believe this is a mistake.",
+ status: 403,
+ });
+ // Ban is rejected before the rate limiter is consumed.
+ expect(consume).not.toHaveBeenCalled();
+ });
+
it("accepts a signed MCP delegation through the managed credential purpose without caching", async () => {
config.USE_DB_AUTHENTICATION = true;
config.MCP_DELEGATED_CREDENTIAL_SECRET = "mcp-delegation-secret";
diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts
index 3de77a9d5..20523fe2e 100644
--- a/apps/api/src/controllers/auth.ts
+++ b/apps/api/src/controllers/auth.ts
@@ -87,6 +87,7 @@ const mockPreviewACUC: (
team_id,
org_id: "preview",
flags: null,
+ is_banned: false,
is_extract,
});
@@ -96,6 +97,7 @@ const mockACUC: () => AuthCreditUsageChunk = () => ({
team_id: "bypass",
org_id: "bypass",
flags: null,
+ is_banned: false,
is_extract: false,
});
@@ -755,6 +757,20 @@ async function supaAuthenticateUser(
);
}
+ // Banned teams are rejected here, where the mcp / OAuth / API-key paths
+ // converge. Ban enforcement used to rely on auth_credit_usage_chunk zeroing
+ // the rate_limits payload, but authenticated limiting now derives from Autumn
+ // and never reads that field, so bans went unenforced. auth_chunk_1 surfaces
+ // teams.banned as is_banned and we deny it explicitly.
+ if (chunk?.is_banned) {
+ return {
+ success: false,
+ error:
+ "Unauthorized: This account has been banned. Contact [email protected] if you believe this is a mistake.",
+ status: 403,
+ };
+ }
+
if (chunk?.flags?.ipRestriction) {
const ipCheck = await checkIpRestriction(
req.ip ?? req.socket?.remoteAddress,
diff --git a/apps/api/src/controllers/v1/types.ts b/apps/api/src/controllers/v1/types.ts
index 3600d5f5b..b18fde40c 100644
--- a/apps/api/src/controllers/v1/types.ts
+++ b/apps/api/src/controllers/v1/types.ts
@@ -1263,6 +1263,11 @@ export type AuthCreditUsageChunk = {
org_id: string;
flags: TeamFlags;
+ // teams.banned surfaced from auth_chunk_1 — banned teams are rejected (403)
+ // in supaAuthenticateUser. Optional because pre-rollout cached ACUC entries
+ // and the bypass/preview mocks omit it (treated as not banned).
+ is_banned?: boolean;
+
// appended on JS-side
is_extract?: boolean;
diff --git a/apps/api/src/db/rpc.ts b/apps/api/src/db/rpc.ts
index 4cf9f1d78..69932be04 100644
--- a/apps/api/src/db/rpc.ts
+++ b/apps/api/src/db/rpc.ts
@@ -35,7 +35,7 @@ export async function authCreditUsageChunk(
): Promise<AuthCreditUsageChunkRow[]> {
const rows = await execRows<AuthCreditUsageChunkRow>(
database,
- sql`select * from auth_credit_usage_chunk_57(input_key => ${input_key}, input_credential_purpose => ${input_credential_purpose})`,
+ sql`select * from auth_chunk_1(input_key => ${input_key}, input_credential_purpose => ${input_credential_purpose})`,
);
// api_key_id is a bigint column, so the pg driver hands it back as a string.
for (const row of rows) {
@@ -53,7 +53,7 @@ export function authCreditUsageChunkFromTeam(
): Promise<AuthCreditUsageChunkRow[]> {
return execRows(
database,
- sql`select * from auth_credit_usage_chunk_56_from_team(input_team => ${input_team})`,
+ sql`select * from auth_chunk_1_from_team(input_team => ${input_team})`,
);
}