Repository navigation
fix(playground): authenticate via session, test key policy by id #3503
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
diegosouzapw
merged 3 commits into
cheaper-inference:release/v3.8.18
from
zhiru:fix/playground-api-key-policy
Jun 9, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,77 @@ | ||
| /** | ||
| * #3503 — dashboard playground key-by-id resolution. | ||
| * | ||
| * The playground sends only the API key *id* (never the secret) via | ||
| * `x-omniroute-playground-key-id`; the gateway resolves the secret server-side | ||
| * in `resolvePlaygroundTestKey`. SECURITY INVARIANT: this is honored ONLY for an | ||
| * authenticated dashboard session — the header alone must never resolve a key, | ||
| * so it can't be abused by an unauthenticated caller to apply (or probe) a key's | ||
| * policy. | ||
| */ | ||
|
|
||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import fs from "node:fs"; | ||
| import os from "node:os"; | ||
| import path from "node:path"; | ||
| import { SignJWT } from "jose"; | ||
|
|
||
| const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omr-playground-key-3503-")); | ||
| process.env.DATA_DIR = TEST_DATA_DIR; | ||
| process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "playground-3503-api-secret"; | ||
| process.env.JWT_SECRET = "playground-3503-jwt-secret"; | ||
|
|
||
| const apiKeysDb = await import("../../src/lib/db/apiKeys.ts"); | ||
| const { resolvePlaygroundTestKey } = await import("../../src/shared/utils/apiKeyPolicy.ts"); | ||
|
|
||
| const PLAYGROUND_KEY_ID_HEADER = "x-omniroute-playground-key-id"; | ||
|
|
||
| const created = await apiKeysDb.createApiKey("playground-3503", "machine-3503", []); | ||
| const KEY_ID = created.id; | ||
| const KEY_SECRET = created.key; | ||
|
|
||
| async function sessionCookie(): Promise<string> { | ||
| const secret = new TextEncoder().encode(process.env.JWT_SECRET); | ||
| const jwt = await new SignJWT({ sub: "admin" }) | ||
| .setProtectedHeader({ alg: "HS256" }) | ||
| .setExpirationTime("1h") | ||
| .sign(secret); | ||
| return `auth_token=${jwt}`; | ||
| } | ||
|
|
||
| function req(headers: Record<string, string>) { | ||
| return { | ||
| method: "POST", | ||
| headers: new Headers(headers), | ||
| url: "http://localhost/api/v1/chat/completions", | ||
| } as unknown as Request; | ||
| } | ||
|
|
||
| test.after(() => { | ||
| fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| test("#3503 — authenticated session + key-id header resolves the key secret server-side", async () => { | ||
| const out = await resolvePlaygroundTestKey( | ||
| req({ [PLAYGROUND_KEY_ID_HEADER]: KEY_ID, cookie: await sessionCookie() }) | ||
| ); | ||
| assert.equal(out, KEY_SECRET, "an authenticated session should resolve the selected key's secret by id"); | ||
| }); | ||
|
|
||
| test("#3503 — SECURITY: the key-id header is IGNORED without an authenticated session", async () => { | ||
| // Same valid key id, but no session cookie → must NOT resolve the secret. | ||
| const out = await resolvePlaygroundTestKey(req({ [PLAYGROUND_KEY_ID_HEADER]: KEY_ID })); | ||
| assert.equal(out, null, "an unauthenticated request must never resolve a key by id"); | ||
| }); | ||
|
|
||
| test("#3503 — SECURITY: an invalid session token is rejected", async () => { | ||
| const out = await resolvePlaygroundTestKey( | ||
| req({ [PLAYGROUND_KEY_ID_HEADER]: KEY_ID, cookie: "auth_token=not-a-valid-jwt" }) | ||
| ); | ||
| assert.equal(out, null, "a forged/invalid session token must not resolve a key"); | ||
| }); | ||
|
|
||
| test("#3503 — no key-id header → null even with a valid session", async () => { | ||
| const out = await resolvePlaygroundTestKey(req({ cookie: await sessionCookie() })); | ||
| assert.equal(out, null); | ||
| }); |
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.
Using the masked key string (
sk-xxxx****yyyy) as the select option value introduces a potential collision risk if multiple keys share the same masked representation. A more robust approach would be to use the unique keyidas the<option>value directly. This would eliminate the need for the client-sideresolvePlaygroundKeyIdlookup entirely and prevent any collision issues.