Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api): enforce scoped API key permissions (#1374)

Tin Sever9bedc4aee161
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 530c2f79e..8b6099375 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -89,6 +89,7 @@ type ApiKey = {
id: string;
userId: string;
enabled: boolean;
+ permissions: Record<string, string[]> | null;
};
type AppVariables = {
diff --git a/apps/api/src/utils/authenticate-api-request.ts b/apps/api/src/utils/authenticate-api-request.ts
index a7d299bc7..a5cf40333 100644
--- a/apps/api/src/utils/authenticate-api-request.ts
+++ b/apps/api/src/utils/authenticate-api-request.ts
@@ -39,6 +39,26 @@ export async function authenticateApiRequest(c: Context): Promise<void> {
throw new HTTPException(401, { message: "Unauthorized" });
}
+ const apiKeyHeader = c.req.header("x-api-key")?.trim();
+ if (!token && apiKeyHeader) {
+ const apiKeyResult = await verifyApiKey(apiKeyHeader);
+ if (!apiKeyResult?.valid || !apiKeyResult.key) {
+ throw new HTTPException(401, { message: "Unauthorized" });
+ }
+ const key = apiKeyResult.key;
+ c.set("userId", key.userId);
+ c.set("userEmail", "");
+ c.set("user", null);
+ c.set("session", null);
+ c.set("apiKey", {
+ id: key.id,
+ userId: key.userId,
+ enabled: key.enabled,
+ permissions: key.permissions,
+ });
+ return;
+ }
+
if (token) {
const apiKeyResult = await verifyApiKey(token);
if (apiKeyResult?.valid && apiKeyResult.key) {
@@ -51,6 +71,7 @@ export async function authenticateApiRequest(c: Context): Promise<void> {
id: key.id,
userId: key.userId,
enabled: key.enabled,
+ permissions: key.permissions,
});
return;
}
@@ -87,6 +108,18 @@ export async function resolveAssetBearerOrCookie(c: Context): Promise<{
throw new HTTPException(401, { message: "Unauthorized" });
}
+ const apiKeyHeader = c.req.header("x-api-key")?.trim();
+ if (!token && apiKeyHeader) {
+ const apiKeyResult = await verifyApiKey(apiKeyHeader);
+ if (apiKeyResult?.valid && apiKeyResult.key) {
+ return {
+ userId: apiKeyResult.key.userId,
+ apiKeyId: apiKeyResult.key.id,
+ };
+ }
+ throw new HTTPException(401, { message: "Unauthorized" });
+ }
+
if (token) {
const apiKeyResult = await verifyApiKey(token);
if (apiKeyResult?.valid && apiKeyResult.key) {
diff --git a/apps/api/src/utils/require-workspace-permission.ts b/apps/api/src/utils/require-workspace-permission.ts
index c585f31c2..976f53ded 100644
--- a/apps/api/src/utils/require-workspace-permission.ts
+++ b/apps/api/src/utils/require-workspace-permission.ts
@@ -93,6 +93,13 @@ export function requireWorkspacePermission(permissions: PermissionMap) {
});
}
+ const apiKey = c.get("apiKey") as
+ | { permissions?: Record<string, string[]> | null }
+ | undefined;
+ if (apiKey?.permissions && !satisfies(apiKey.permissions, permissions)) {
+ throw new HTTPException(403, { message: "Insufficient API key scope" });
+ }
+
if (await isInstanceAdmin(c)) {
return next();
}
diff --git a/apps/api/src/utils/verify-api-key.ts b/apps/api/src/utils/verify-api-key.ts
index d73f74492..77a7ffa21 100644
--- a/apps/api/src/utils/verify-api-key.ts
+++ b/apps/api/src/utils/verify-api-key.ts
@@ -11,6 +11,31 @@ async function hashApiKey(key: string): Promise<string> {
.replace(/=/g, "");
}
+function parsePermissions(raw: string | null): Record<string, string[]> | null {
+ if (raw === null) return null;
+
+ let value: unknown;
+ try {
+ value = JSON.parse(raw);
+ } catch {
+ return {};
+ }
+
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
+ return {};
+ }
+
+ const permissions: Record<string, string[]> = {};
+ for (const [resource, actions] of Object.entries(
+ value as Record<string, unknown>,
+ )) {
+ if (!Array.isArray(actions)) return {};
+ if (actions.some((action) => typeof action !== "string")) return {};
+ permissions[resource] = actions as string[];
+ }
+ return permissions;
+}
+
export async function verifyApiKey(key: string) {
const hashedKey = await hashApiKey(key);
@@ -43,9 +68,7 @@ export async function verifyApiKey(key: string) {
start: apiKey.start,
enabled: apiKey.enabled ?? false,
expiresAt: apiKey.expiresAt,
- permissions: apiKey.permissions
- ? (JSON.parse(apiKey.permissions) as Record<string, string[]>)
- : null,
+ permissions: parsePermissions(apiKey.permissions),
refillInterval: apiKey.refillInterval,
refillAmount: apiKey.refillAmount,
lastRefillAt: apiKey.lastRefillAt,