Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🐛 Skip lastUsedAt write for non-Pro API keys (RAL-1336) (#2657)

Luke Vellabebee423e48e
diff --git a/apps/web/src/app/api/private/[...route]/route.test.ts b/apps/web/src/app/api/private/[...route]/route.test.ts
index 32b5fb367..9832edd78 100644
--- a/apps/web/src/app/api/private/[...route]/route.test.ts
+++ b/apps/web/src/app/api/private/[...route]/route.test.ts
@@ -211,6 +211,31 @@ describe("Private API - /polls", () => {
expect(json.error.code).toBe("SPACE_NOT_PRO");
});
+ it("should not schedule a lastUsedAt write when the space is not pro", async () => {
+ const hobbyApiKey = {
+ ...mockApiKey,
+ hashedKey: hashApiKey(testApiKey),
+ lastUsedAt: null,
+ space: { ...mockApiKey.space, tier: "hobby" },
+ };
+ vi.mocked(prisma.spaceApiKey.findMany).mockResolvedValue([hobbyApiKey]);
+
+ const res = await app.request("/api/private/polls", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${testApiKey}`,
+ },
+ body: JSON.stringify({
+ title: "Test Poll",
+ dates: ["2025-01-15"],
+ }),
+ });
+
+ expect(res.status).toBe(403);
+ expect(after).not.toHaveBeenCalled();
+ });
+
it("should return 200 with a valid key when the space is pro", async () => {
const res = await app.request("/api/private/polls", {
method: "POST",
diff --git a/apps/web/src/app/api/private/utils/api-key.ts b/apps/web/src/app/api/private/utils/api-key.ts
index 8635e0cd2..16773f571 100644
--- a/apps/web/src/app/api/private/utils/api-key.ts
+++ b/apps/web/src/app/api/private/utils/api-key.ts
@@ -79,10 +79,12 @@ const verifyKey = bearerAuth({
return false;
}
+ const effectiveTier = isSelfHosted ? ("pro" as const) : spaceTier;
+
c.set("apiAuth", {
spaceId,
spaceOwnerId,
- spaceTier: isSelfHosted ? ("pro" as const) : spaceTier,
+ spaceTier: effectiveTier,
apiKeyId,
});
@@ -97,7 +99,10 @@ const verifyKey = bearerAuth({
!lastUsedAt ||
now.getTime() - lastUsedAt.getTime() > LAST_USED_AT_WRITE_INTERVAL_MS;
- if (rehashedKey || isLastUsedAtStale) {
+ // Skip the write for non-Pro spaces: requireProSpace rejects them with
+ // 403, so bumping lastUsedAt would misreport a blocked key as active and
+ // waste a DB write on rejected traffic. Self-hosted is always Pro here.
+ if (effectiveTier === "pro" && (rehashedKey || isLastUsedAtStale)) {
after(() =>
prisma.spaceApiKey.update({
where: { id: apiKeyId },