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(auth): preserve disabled API key window counters

Tinf4fb2731ac7c
diff --git a/apps/api/src/utils/verify-api-key.ts b/apps/api/src/utils/verify-api-key.ts
index 17fa3220f..2e55d2dc8 100644
--- a/apps/api/src/utils/verify-api-key.ts
+++ b/apps/api/src/utils/verify-api-key.ts
@@ -119,7 +119,7 @@ export async function verifyApiKey(
if (requestCount >= (apiKey.rateLimitMax ?? 100)) return null;
}
if (remaining !== null) remaining--;
- requestCount++;
+ if (apiKey.rateLimitEnabled) requestCount++;
await tx
.update(schema.apikeyTable)
.set({
diff --git a/tests/api-integration/api-key-banned-user.test.ts b/tests/api-integration/api-key-banned-user.test.ts
index 8718ca339..66e26c28f 100644
--- a/tests/api-integration/api-key-banned-user.test.ts
+++ b/tests/api-integration/api-key-banned-user.test.ts
@@ -53,6 +53,28 @@ describe("API integration: API keys of banned users", () => {
expect(result?.key.userId).toBe(userId);
});
+ it("does not consume the window counter while rate limits are disabled", async () => {
+ const { userId, key } = await createUserWithKey({ banned: false });
+ await db
+ .update(schema.apikeyTable)
+ .set({ rateLimitEnabled: false, rateLimitMax: 1, requestCount: 0 })
+ .where(eq(schema.apikeyTable.userId, userId));
+ for (let i = 0; i < 3; i++) expect(await verifyApiKey(key)).not.toBeNull();
+ expect(
+ (
+ await db.query.apikeyTable.findFirst({
+ where: eq(schema.apikeyTable.userId, userId),
+ })
+ )?.requestCount,
+ ).toBe(0);
+ await db
+ .update(schema.apikeyTable)
+ .set({ rateLimitEnabled: true })
+ .where(eq(schema.apikeyTable.userId, userId));
+ expect(await verifyApiKey(key)).not.toBeNull();
+ expect(await verifyApiKey(key)).toBeNull();
+ });
+
it("rejects the key of a banned user", async () => {
const { key } = await createUserWithKey({ banned: true });