-
Notifications
You must be signed in to change notification settings - Fork 9
Enforce account_api_keys.expires_at in x-api-key auth (chat#1813) #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { NextRequest } from "next/server"; | ||
| import { getApiKeyAccountId } from "@/lib/auth/getApiKeyAccountId"; | ||
| import { selectAccountApiKeys } from "@/lib/supabase/account_api_keys/selectAccountApiKeys"; | ||
|
|
||
| vi.mock("@/lib/networking/getCorsHeaders", () => ({ getCorsHeaders: () => ({}) })); | ||
| vi.mock("@/lib/keys/hashApiKey", () => ({ hashApiKey: (k: string) => `hashed_${k}` })); | ||
| vi.mock("@/lib/const", () => ({ PRIVY_PROJECT_SECRET: "test_secret" })); | ||
| vi.mock("@/lib/supabase/account_api_keys/selectAccountApiKeys", () => ({ | ||
| selectAccountApiKeys: vi.fn(), | ||
| })); | ||
|
|
||
| function req(apiKey?: string) { | ||
| const headers = new Headers(); | ||
| if (apiKey) headers.set("x-api-key", apiKey); | ||
| return new NextRequest("https://x.test/api", { headers }); | ||
| } | ||
|
|
||
| const baseRow = { | ||
| id: "k", | ||
| account: "acc-1", | ||
| key_hash: "hashed_recoup_sk_x", | ||
| name: "n", | ||
| last_used: null, | ||
| created_at: "2026-01-01T00:00:00Z", | ||
| }; | ||
|
|
||
| describe("getApiKeyAccountId", () => { | ||
| beforeEach(() => vi.clearAllMocks()); | ||
|
|
||
| it("returns accountId for a non-expiring key (expires_at null)", async () => { | ||
| vi.mocked(selectAccountApiKeys).mockResolvedValue([{ ...baseRow, expires_at: null }]); | ||
| expect(await getApiKeyAccountId(req("recoup_sk_x"))).toBe("acc-1"); | ||
| }); | ||
|
|
||
| it("returns accountId for a future expiry", async () => { | ||
| const future = new Date(Date.now() + 60_000).toISOString(); | ||
| vi.mocked(selectAccountApiKeys).mockResolvedValue([{ ...baseRow, expires_at: future }]); | ||
| expect(await getApiKeyAccountId(req("recoup_sk_x"))).toBe("acc-1"); | ||
| }); | ||
|
|
||
| it("rejects an expired key with 401", async () => { | ||
| const past = new Date(Date.now() - 60_000).toISOString(); | ||
| vi.mocked(selectAccountApiKeys).mockResolvedValue([{ ...baseRow, expires_at: past }]); | ||
| const res = await getApiKeyAccountId(req("recoup_sk_x")); | ||
| expect(res).toBeInstanceOf(Response); | ||
| expect((res as Response).status).toBe(401); | ||
| }); | ||
|
|
||
| it("401 when no x-api-key header", async () => { | ||
| const res = await getApiKeyAccountId(req()); | ||
| expect((res as Response).status).toBe(401); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { isApiKeyExpired } from "@/lib/keys/isApiKeyExpired"; | ||
|
|
||
| describe("isApiKeyExpired", () => { | ||
| const now = Date.parse("2026-06-23T00:00:00Z"); | ||
|
|
||
| it("treats null/undefined expiry as never-expiring", () => { | ||
| expect(isApiKeyExpired(null, now)).toBe(false); | ||
| expect(isApiKeyExpired(undefined, now)).toBe(false); | ||
| }); | ||
|
|
||
| it("is false for a future expiry", () => { | ||
| expect(isApiKeyExpired("2026-06-23T01:00:00Z", now)).toBe(false); | ||
| }); | ||
|
|
||
| it("is true at or past the expiry", () => { | ||
| expect(isApiKeyExpired("2026-06-22T23:59:59Z", now)).toBe(true); | ||
| expect(isApiKeyExpired("2026-06-23T00:00:00Z", now)).toBe(true); | ||
| }); | ||
|
|
||
| it("treats an unparseable expiry as non-expiring (never lock out)", () => { | ||
| expect(isApiKeyExpired("not-a-date", now)).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /** | ||
| * Whether an api key's `expires_at` has passed. NULL/undefined = never expires. | ||
| * An unparseable value is treated as non-expiring so a bad row can't lock a | ||
| * caller out. Used by api auth to reject ephemeral keys past their TTL | ||
| * (recoupable/chat#1813). | ||
| */ | ||
| export function isApiKeyExpired( | ||
| expiresAt: string | null | undefined, | ||
| now: number = Date.now(), | ||
| ): boolean { | ||
| if (!expiresAt) return false; | ||
| const exp = Date.parse(expiresAt); | ||
| if (Number.isNaN(exp)) return false; | ||
| return exp <= now; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Fail-open on unparseable expires_at allows an ephemeral key to become permanent if its timestamp is corrupted, undermining the short-lived key security model.
Prompt for AI agents