fix: Admins unable to revoke workspace API keys created by other users (#9865)

* fix: Admins unable to revoke workspace API keys created by other users

closes #9864

* fix policy
This commit is contained in:
Tom Moor authored and GitHub committed 2025-08-07 22:49:23 -04:00
1 parent 888add7920
commit 93cda9327a
4 files changed
+60 -5

No files matched your search

+10
View File
@@ -12,6 +12,7 @@ import {
DataType,
AfterFind,
BeforeSave,
Scopes,
} from "sequelize-typescript";
import { randomString } from "@shared/random";
import { ApiKeyValidation } from "@shared/validations";
@@ -24,6 +25,15 @@ import AuthenticationHelper from "./helpers/AuthenticationHelper";
import Length from "./validators/Length";
@Table({ tableName: "apiKeys", modelName: "apiKey" })
@Scopes(() => ({
withUser: {
include: [
{
association: "user",
},
],
},
}))
@Fix
class ApiKey extends ParanoidModel<
InferAttributes<ApiKey>,
+9 -3
View File
@@ -7,6 +7,7 @@ import {
isOwner,
isTeamModel,
isTeamMutable,
or,
} from "./utils";
allow(User, "createApiKey", Team, (actor, team) =>
@@ -32,8 +33,13 @@ allow(User, "listApiKeys", Team, (actor, team) =>
allow(User, ["read", "update", "delete"], ApiKey, (actor, apiKey) =>
and(
isOwner(actor, apiKey),
actor.isAdmin ||
!!actor.team?.getPreference(TeamPreference.MembersCanCreateApiKey)
isTeamModel(actor, apiKey?.user),
or(
actor.isAdmin,
and(
isOwner(actor, apiKey),
!!actor.team?.getPreference(TeamPreference.MembersCanCreateApiKey)
)
)
)
);
+36
View File
@@ -146,6 +146,42 @@ describe("#apiKeys.delete", () => {
expect(res.status).toEqual(200);
});
it("should not allow deleting another user's api key", async () => {
const user = await buildUser();
const otherUser = await buildUser({ teamId: user.teamId });
const apiKey = await buildApiKey({
name: "Other User's API Key",
userId: otherUser.id,
});
const res = await server.post("/api/apiKeys.delete", {
body: {
token: user.getJwtToken(),
id: apiKey.id,
},
});
expect(res.status).toEqual(403);
});
it("should allow admin to delete another user's api key", async () => {
const user = await buildUser();
const admin = await buildAdmin({ teamId: user.teamId });
const apiKey = await buildApiKey({
name: "User's API Key",
userId: user.id,
});
const res = await server.post("/api/apiKeys.delete", {
body: {
token: admin.getJwtToken(),
id: apiKey.id,
},
});
expect(res.status).toEqual(200);
});
it("should require authentication", async () => {
const res = await server.post("/api/apiKeys.delete");
expect(res.status).toEqual(401);
+5 -2
View File
@@ -104,8 +104,11 @@ router.post(
const { user } = ctx.state.auth;
const { transaction } = ctx.state;
const key = await ApiKey.findByPk(id, {
lock: transaction.LOCK.UPDATE,
const key = await ApiKey.scope("withUser").findByPk(id, {
lock: {
level: transaction.LOCK.UPDATE,
of: ApiKey,
},
transaction,
});
authorize(user, "delete", key);