From 10fa5e8903bdd116dc0318747c3b897d4d9be70e Mon Sep 17 00:00:00 2001 From: diegosouzapw Date: Sun, 31 May 2026 15:23:41 -0300 Subject: [PATCH] feat(quota): protect quotaShared-* combos from manual edit/delete (system-managed) Guard PUT and DELETE on /api/combos/[id] to return 409 when the target combo name starts with QUOTA_MODEL_PREFIX; filter isHidden combos from the Combos page state so quota-managed combos never appear as editable rows there. --- src/app/(dashboard)/dashboard/combos/page.tsx | 2 +- src/app/api/combos/[id]/route.ts | 24 +++ tests/unit/combos-quota-protected.test.ts | 165 ++++++++++++++++++ 3 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 tests/unit/combos-quota-protected.test.ts diff --git a/src/app/(dashboard)/dashboard/combos/page.tsx b/src/app/(dashboard)/dashboard/combos/page.tsx index b204bca1e4..e2af3ab665 100644 --- a/src/app/(dashboard)/dashboard/combos/page.tsx +++ b/src/app/(dashboard)/dashboard/combos/page.tsx @@ -726,7 +726,7 @@ export default function CombosPage() { const metricsData = await metricsRes.json(); const nodesData = nodesRes.ok ? await nodesRes.json() : { nodes: [] }; - if (combosRes.ok) setCombos(combosData.combos || []); + if (combosRes.ok) setCombos((combosData.combos || []).filter((c) => !c.isHidden)); if (providersRes.ok) { const active = (providersData.connections || []).filter( (c) => c.testStatus === "active" || c.testStatus === "success" diff --git a/src/app/api/combos/[id]/route.ts b/src/app/api/combos/[id]/route.ts index ba35857d01..49647e53eb 100644 --- a/src/app/api/combos/[id]/route.ts +++ b/src/app/api/combos/[id]/route.ts @@ -15,6 +15,8 @@ import { validateComboDAG } from "@omniroute/open-sse/services/combo.ts"; import { updateComboSchema } from "@/shared/validation/schemas"; import { isValidationFailure, validateBody } from "@/shared/validation/helpers"; import { requireManagementAuth } from "@/lib/api/requireManagementAuth"; +import { buildErrorBody } from "@omniroute/open-sse/utils/error"; +import { QUOTA_MODEL_PREFIX } from "@/lib/quota/quotaModelNaming"; // GET /api/combos/[id] - Get combo by ID export async function GET(request, { params }) { @@ -66,6 +68,15 @@ export async function PUT(request, { params }) { if (!currentCombo) { return NextResponse.json({ error: "Combo not found" }, { status: 404 }); } + if (currentCombo.name.startsWith(QUOTA_MODEL_PREFIX)) { + return NextResponse.json( + buildErrorBody( + 409, + "This combo is managed by Quota Share and cannot be edited here. Manage it from the Quota Share page." + ), + { status: 409 } + ); + } const allCombos = await getCombos(); const comboName = validation.data.name || currentCombo.name; @@ -146,6 +157,19 @@ export async function DELETE(request, { params }) { try { const { id } = await params; + const existingCombo = await getComboById(id); + if (!existingCombo) { + return NextResponse.json({ error: "Combo not found" }, { status: 404 }); + } + if (existingCombo.name.startsWith(QUOTA_MODEL_PREFIX)) { + return NextResponse.json( + buildErrorBody( + 409, + "This combo is managed by Quota Share and cannot be deleted here. Manage it from the Quota Share page." + ), + { status: 409 } + ); + } const success = await deleteCombo(id); if (!success) { diff --git a/tests/unit/combos-quota-protected.test.ts b/tests/unit/combos-quota-protected.test.ts new file mode 100644 index 0000000000..b9dd5c7f99 --- /dev/null +++ b/tests/unit/combos-quota-protected.test.ts @@ -0,0 +1,165 @@ +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"; + +const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combo-quota-protected-")); +process.env.DATA_DIR = TEST_DATA_DIR; + +const core = await import("../../src/lib/db/core.ts"); +const combosDb = await import("../../src/lib/db/combos.ts"); +const comboRoute = await import("../../src/app/api/combos/[id]/route.ts"); + +async function resetStorage() { + core.resetDbInstance(); + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); + fs.mkdirSync(TEST_DATA_DIR, { recursive: true }); +} + +function makePutRequest(id: string, body: Record) { + return new Request(`http://localhost/api/combos/${id}`, { + method: "PUT", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }); +} + +function makeDeleteRequest(id: string) { + return new Request(`http://localhost/api/combos/${id}`, { + method: "DELETE", + }); +} + +test.beforeEach(async () => { + await resetStorage(); +}); + +test.after(() => { + core.resetDbInstance(); + fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); +}); + +// ---- quota-protected combos ---- + +test("DELETE /api/combos/[id] returns 409 for a quotaShared-* combo and does NOT delete it", async () => { + const combo = await combosDb.createCombo({ + name: "quotaShared-mypool-openai/gpt-4o", + strategy: "priority", + models: [{ provider: "openai", model: "gpt-4o" }], + isHidden: true, + }); + + const response = await comboRoute.DELETE(makeDeleteRequest(combo.id), { + params: Promise.resolve({ id: combo.id }), + }); + + assert.equal(response.status, 409, "DELETE quota combo should return 409"); + + const body = (await response.json()) as any; + assert.ok( + body.error?.message?.includes("Quota Share"), + `Error message should mention Quota Share; got: ${JSON.stringify(body)}` + ); + + // Verify the combo was NOT deleted + const still = await combosDb.getComboById(combo.id); + assert.ok(still, "Quota combo must still exist after rejected DELETE"); +}); + +test("PUT /api/combos/[id] returns 409 for a quotaShared-* combo and does NOT mutate it", async () => { + const combo = await combosDb.createCombo({ + name: "quotaShared-mypool-openai/gpt-4o", + strategy: "priority", + models: [{ provider: "openai", model: "gpt-4o" }], + isHidden: true, + }); + + const response = await comboRoute.PUT( + makePutRequest(combo.id, { name: "quotaShared-mypool-openai/gpt-4o", strategy: "random" }), + { params: Promise.resolve({ id: combo.id }) } + ); + + assert.equal(response.status, 409, "PUT quota combo should return 409"); + + const body = (await response.json()) as any; + assert.ok( + body.error?.message?.includes("Quota Share"), + `Error message should mention Quota Share; got: ${JSON.stringify(body)}` + ); + + // Verify the combo was NOT mutated + const unchanged = await combosDb.getComboById(combo.id); + assert.equal(unchanged?.strategy, "priority", "Strategy must remain unchanged after rejected PUT"); +}); + +// ---- non-quota combos still work ---- + +test("DELETE /api/combos/[id] succeeds for a regular (non-quota) combo", async () => { + const combo = await combosDb.createCombo({ + name: "regular-combo", + strategy: "priority", + models: [{ provider: "openai", model: "gpt-4o" }], + }); + + const response = await comboRoute.DELETE(makeDeleteRequest(combo.id), { + params: Promise.resolve({ id: combo.id }), + }); + + assert.equal(response.status, 200, "DELETE regular combo should return 200"); + + const body = (await response.json()) as any; + assert.equal(body.success, true); + + // Verify the combo was actually deleted + const gone = await combosDb.getComboById(combo.id); + assert.equal(gone, null, "Regular combo must be gone after DELETE"); +}); + +test("PUT /api/combos/[id] succeeds for a regular (non-quota) combo", async () => { + const combo = await combosDb.createCombo({ + name: "regular-editable-combo", + strategy: "priority", + models: [{ provider: "openai", model: "gpt-4o" }], + }); + + const response = await comboRoute.PUT( + makePutRequest(combo.id, { + name: "regular-editable-combo", + strategy: "round-robin", + models: [{ providerId: "openai", model: "gpt-4o" }], + }), + { params: Promise.resolve({ id: combo.id }) } + ); + + assert.equal(response.status, 200, "PUT regular combo should return 200"); + + const body = (await response.json()) as any; + assert.equal(body.strategy, "round-robin", "Strategy should be updated for regular combo"); +}); + +// ---- 404 still works when combo doesn't exist ---- + +test("DELETE /api/combos/[id] returns 404 when combo does not exist", async () => { + const response = await comboRoute.DELETE(makeDeleteRequest("nonexistent-id"), { + params: Promise.resolve({ id: "nonexistent-id" }), + }); + + assert.equal(response.status, 404, "DELETE nonexistent combo should return 404, not 409"); +}); + +// ---- structural assertion: page filters isHidden ---- + +test("combos page source filters isHidden from rendered list", async () => { + const pageSource = fs.readFileSync( + new URL( + "../../src/app/(dashboard)/dashboard/combos/page.tsx", + import.meta.url + ).pathname, + "utf8" + ); + assert.ok( + pageSource.includes("!c.isHidden") || pageSource.includes("!combo.isHidden"), + "Combos page must filter out isHidden combos from the rendered list" + ); +});