remove api-key-org association for root keys

This commit is contained in:
miloschwartz 2025-08-01 15:55:47 -07:00
parent 6d359b6bb9
commit 7402590f49
No known key found for this signature in database
11 changed files with 47 additions and 26 deletions

View file

@ -35,6 +35,11 @@ export async function verifyApiKeyApiKeyAccess(
); );
} }
if (callerApiKey.isRoot) {
// Root keys can access any key in any org
return next();
}
const [callerApiKeyOrg] = await db const [callerApiKeyOrg] = await db
.select() .select()
.from(apiKeyOrg) .from(apiKeyOrg)

View file

@ -28,6 +28,11 @@ export async function verifyApiKeyClientAccess(
); );
} }
if (apiKey.isRoot) {
// Root keys can access any key in any org
return next();
}
const client = await db const client = await db
.select() .select()
.from(clients) .from(clients)

View file

@ -27,6 +27,11 @@ export async function verifyApiKeyOrgAccess(
); );
} }
if (req.apiKey?.isRoot) {
// Root keys can access any key in any org
return next();
}
if (!req.apiKeyOrg) { if (!req.apiKeyOrg) {
const apiKeyOrgRes = await db const apiKeyOrgRes = await db
.select() .select()

View file

@ -37,6 +37,11 @@ export async function verifyApiKeyResourceAccess(
); );
} }
if (apiKey.isRoot) {
// Root keys can access any key in any org
return next();
}
if (!resource.orgId) { if (!resource.orgId) {
return next( return next(
createHttpError( createHttpError(

View file

@ -45,6 +45,11 @@ export async function verifyApiKeyRoleAccess(
); );
} }
if (apiKey.isRoot) {
// Root keys can access any key in any org
return next();
}
const orgIds = new Set(rolesData.map((role) => role.orgId)); const orgIds = new Set(rolesData.map((role) => role.orgId));
for (const role of rolesData) { for (const role of rolesData) {

View file

@ -32,6 +32,11 @@ export async function verifyApiKeySetResourceUsers(
return next(createHttpError(HttpCode.BAD_REQUEST, "Invalid user IDs")); return next(createHttpError(HttpCode.BAD_REQUEST, "Invalid user IDs"));
} }
if (apiKey.isRoot) {
// Root keys can access any key in any org
return next();
}
if (userIds.length === 0) { if (userIds.length === 0) {
return next(); return next();
} }

View file

@ -1,9 +1,6 @@
import { Request, Response, NextFunction } from "express"; import { Request, Response, NextFunction } from "express";
import { db } from "@server/db"; import { db } from "@server/db";
import { import { sites, apiKeyOrg } from "@server/db";
sites,
apiKeyOrg
} from "@server/db";
import { and, eq, or } from "drizzle-orm"; import { and, eq, or } from "drizzle-orm";
import createHttpError from "http-errors"; import createHttpError from "http-errors";
import HttpCode from "@server/types/HttpCode"; import HttpCode from "@server/types/HttpCode";
@ -31,6 +28,11 @@ export async function verifyApiKeySiteAccess(
); );
} }
if (apiKey.isRoot) {
// Root keys can access any key in any org
return next();
}
const site = await db const site = await db
.select() .select()
.from(sites) .from(sites)

View file

@ -66,6 +66,11 @@ export async function verifyApiKeyTargetAccess(
); );
} }
if (apiKey.isRoot) {
// Root keys can access any key in any org
return next();
}
if (!resource.orgId) { if (!resource.orgId) {
return next( return next(
createHttpError( createHttpError(

View file

@ -27,6 +27,11 @@ export async function verifyApiKeyUserAccess(
); );
} }
if (apiKey.isRoot) {
// Root keys can access any key in any org
return next();
}
if (!req.apiKeyOrg || !req.apiKeyOrg.orgId) { if (!req.apiKeyOrg || !req.apiKeyOrg.orgId) {
return next( return next(
createHttpError( createHttpError(

View file

@ -63,15 +63,6 @@ export async function createRootApiKey(
lastChars, lastChars,
isRoot: true isRoot: true
}); });
const allOrgs = await trx.select().from(orgs);
for (const org of allOrgs) {
await trx.insert(apiKeyOrg).values({
apiKeyId,
orgId: org.orgId
});
}
}); });
try { try {

View file

@ -215,7 +215,7 @@ export async function createOrg(
orgId: newOrg[0].orgId, orgId: newOrg[0].orgId,
roleId: roleId, roleId: roleId,
isOwner: true isOwner: true
}); });
} }
const memberRole = await trx const memberRole = await trx
@ -234,18 +234,6 @@ export async function createOrg(
orgId orgId
})) }))
); );
const rootApiKeys = await trx
.select()
.from(apiKeys)
.where(eq(apiKeys.isRoot, true));
for (const apiKey of rootApiKeys) {
await trx.insert(apiKeyOrg).values({
apiKeyId: apiKey.apiKeyId,
orgId: newOrg[0].orgId
});
}
}); });
if (!org) { if (!org) {