Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **fix(api):** `DELETE /v1/batches/delete-completed` now sweeps only the calling API key's own completed batches (batches with no owner stay out of a key-scoped sweep on purpose), with an explicit instance-wide mode reserved for authenticated dashboard sessions, audit logging of every sweep, a sanitized 500 on failure and an atomic sweep so a mid-way error never leaves a batch pointing at a nulled file (GHSA-wvxc-jp3v-5mg5) ([#12969](https://github.com/diegosouzapw/OmniRoute/pull/12969))
78 changes: 70 additions & 8 deletions src/app/api/v1/batches/delete-completed/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { CORS_HEADERS, handleCorsOptions } from "@/shared/utils/cors";
import { deleteCompletedBatches } from "@/lib/db/batches";
import { deleteCompletedBatches, type DeleteCompletedBatchesScope } from "@/lib/db/batches";
import { NextResponse } from "next/server";
import { getApiKeyRequestScope } from "@/app/api/v1/_helpers/apiKeyScope";
import { buildErrorBody } from "@omniroute/open-sse/utils/error";
import * as log from "@/sse/utils/logger";

const LOG_ROUTE = "batches/delete-completed";

export async function OPTIONS() {
return handleCorsOptions();
Expand All @@ -11,19 +15,77 @@ export async function DELETE(request: Request) {
const scope = await getApiKeyRequestScope(request);
if (scope.rejection) return scope.rejection;

// Allow session-authenticated (dashboard) requests; for API-key requests, require a key
if (!scope.isSessionAuth && !scope.apiKeyId) {
// Fail closed on an unresolvable credential: a presented key that the DB does
// not know (deleted, rotated, mistyped) must never fall through to the session
// branch and widen a destructive sweep to the whole instance.
if (scope.apiKey && !scope.apiKeyId) {
log.warn("BATCHES", "delete-completed: presented API key did not resolve", {
route: LOG_ROUTE,
isSessionAuth: scope.isSessionAuth,
});
return NextResponse.json(
{ error: { message: "Invalid API key", type: "invalid_request_error" } },
{ status: 401, headers: CORS_HEADERS }
);
}

// A presented API key always scopes the sweep to that key — even when the
// request also carries a dashboard session cookie — exactly like the
// list/count siblings (`apiKeyId || undefined`), so a leaked or over-shared
// key can never widen a destructive sweep. Only a dashboard session WITHOUT a
// key sweeps the whole instance; otherwise an ordinary key would delete every
// tenant's completed batches and null out their file contents
// (GHSA-wvxc-jp3v-5mg5). A caller that is neither gets 401; there is no
// fallback that silently widens the sweep.
let sweepScope: DeleteCompletedBatchesScope;
let mode: "instance" | "api_key";
if (scope.apiKeyId) {
sweepScope = { apiKeyId: scope.apiKeyId };
mode = "api_key";
} else if (scope.isSessionAuth) {
sweepScope = { allTenants: true };
mode = "instance";
} else {
return NextResponse.json(
{ error: { message: "Authentication required", type: "invalid_request_error" } },
{ status: 401, headers: CORS_HEADERS }
);
}

// Scope the sweep to the caller's own batches, like the list/count siblings do.
// Only a dashboard session (apiKeyId === null) sweeps the whole instance —
// otherwise an ordinary inference key would delete every tenant's completed
// batches and null out their file contents (GHSA-wvxc-jp3v-5mg5).
const result = deleteCompletedBatches(scope.apiKeyId || undefined);
let result: ReturnType<typeof deleteCompletedBatches>;
try {
result = deleteCompletedBatches(sweepScope);
} catch (err) {
log.error("BATCHES", "delete-completed sweep failed", {
route: LOG_ROUTE,
mode,
apiKeyId: scope.apiKeyId,
error: err instanceof Error ? { message: err.message, stack: err.stack } : String(err),
});
return NextResponse.json(buildErrorBody(500, "Failed to delete completed batches"), {
status: 500,
headers: CORS_HEADERS,
});
}

const audit = {
route: LOG_ROUTE,
mode,
apiKeyId: scope.apiKeyId,
deletedBatches: result.deletedBatches,
deletedFiles: result.deletedFiles,
};
// A bulk delete is an audit event, not routine chatter: an instance-wide sweep
// and any key-scoped sweep that actually removed rows log at warn so the trail
// survives APP_LOG_LEVEL=warn; a no-op key-scoped sweep stays at info so a
// caller looping on the endpoint cannot flood the warn log.
if (mode === "instance") {
log.warn("BATCHES", "instance-wide completed-batch sweep", audit);
} else if (result.deletedBatches > 0) {
log.warn("BATCHES", "completed-batch sweep", audit);
} else {
log.info("BATCHES", "completed-batch sweep (no-op)", audit);
}

return NextResponse.json(
{ deleted: true, deletedBatches: result.deletedBatches, deletedFiles: result.deletedFiles },
Expand Down
118 changes: 79 additions & 39 deletions src/lib/db/batches.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { getDbInstance, rowToCamel, objToSnake } from "./core";
import { deleteFile } from "./files";
import { v4 as uuidv4 } from "uuid";
import { logger } from "../../../open-sse/utils/logger.ts";

const log = logger("DB_BATCHES");

function parseBatchRow(row: any): BatchRecord {
const camel = rowToCamel(row) as any;
Expand Down Expand Up @@ -411,57 +414,94 @@ export function deleteBatch(id: string): boolean {
return result.changes > 0;
}

/**
* Scope of a `deleteCompletedBatches` sweep. The intent is explicit on purpose:
* a caller either names the API key whose batches it may sweep, or states
* `allTenants: true` — there is no default that widens to the whole instance.
*/
export type DeleteCompletedBatchesScope = { apiKeyId: string } | { allTenants: true };

/**
* Delete completed batches and the files they reference.
*
* `apiKeyId` scopes the sweep to that key's own batches, exactly like
* `listBatches`/`countBatches`. Omitting it sweeps the whole instance and is
* reserved for an authenticated dashboard session — an ordinary inference key
* that reached this without its own id would otherwise delete every tenant's
* completed batches and null out their file contents (GHSA-wvxc-jp3v-5mg5).
* `{ apiKeyId }` scopes the sweep to that key's own batches, exactly like
* `listBatches`/`countBatches`. `{ allTenants: true }` sweeps the whole instance
* and is reserved for an authenticated dashboard session — an ordinary inference
* key that reached this without its own id would otherwise delete every tenant's
* completed batches and null out their file contents (GHSA-wvxc-jp3v-5mg5). A
* missing/empty `apiKeyId` without `allTenants` throws instead of silently
* widening the sweep, and a scope carrying BOTH `apiKeyId` and `allTenants` is
* rejected rather than widened.
*
* Batches whose `api_key_id` IS NULL are intentionally OUT of a key-scoped sweep.
* This diverges from `scopeCheck` in `src/app/api/v1/batches/[id]/route.ts`,
* which lets any key read/delete a single unowned batch by id: a bulk destructive
* sweep must never reach records the key does not own, so unowned batches are
* only swept by `{ allTenants: true }`.
*
* The file soft-deletes, the checkpoint DELETE and the batches DELETE run in one
* transaction, so a mid-sweep failure rolls everything back — no batch row is
* left pointing at a file whose content was already nulled.
*/
export function deleteCompletedBatches(apiKeyId?: string): {
export function deleteCompletedBatches(scope: DeleteCompletedBatchesScope): {
deletedBatches: number;
deletedFiles: number;
} {
const scopeObj = scope && typeof scope === "object" ? scope : {};
const allTenants = "allTenants" in scopeObj && scopeObj.allTenants === true;
const apiKeyId = "apiKeyId" in scopeObj ? scopeObj.apiKeyId : undefined;
if (!allTenants && !apiKeyId) {
throw new Error("deleteCompletedBatches: apiKeyId required unless allTenants");
}
if (allTenants && apiKeyId) {
throw new Error("deleteCompletedBatches: apiKeyId and allTenants are mutually exclusive");
}

const db = getDbInstance();

const ownershipClause = apiKeyId ? " AND api_key_id = ?" : "";
const ownershipArgs = apiKeyId ? [apiKeyId] : [];
const ownershipClause = allTenants ? "" : " AND api_key_id = ?";
const ownershipArgs = allTenants ? [] : [apiKeyId];

// Collect unique file IDs from the completed batches in scope
const rows = db
.prepare(
`SELECT input_file_id, output_file_id, error_file_id FROM batches WHERE status = 'completed'${ownershipClause}`
)
.all(...ownershipArgs) as Array<{
input_file_id: string | null;
output_file_id: string | null;
error_file_id: string | null;
}>;

const fileIds = new Set<string>();
for (const row of rows) {
if (row.input_file_id) fileIds.add(row.input_file_id);
if (row.output_file_id) fileIds.add(row.output_file_id);
if (row.error_file_id) fileIds.add(row.error_file_id);
}
const sweep = db.transaction(() => {
// Collect unique file IDs from the completed batches in scope
const rows = db
.prepare(
`SELECT input_file_id, output_file_id, error_file_id FROM batches WHERE status = 'completed'${ownershipClause}`
)
.all(...ownershipArgs) as Array<{
input_file_id: string | null;
output_file_id: string | null;
error_file_id: string | null;
}>;

const fileIds = new Set<string>();
for (const row of rows) {
if (row.input_file_id) fileIds.add(row.input_file_id);
if (row.output_file_id) fileIds.add(row.output_file_id);
if (row.error_file_id) fileIds.add(row.error_file_id);
}

let deletedFiles = 0;
for (const fid of fileIds) {
try {
if (deleteFile(fid)) deletedFiles++;
} catch {
/* ignore */
let deletedFiles = 0;
for (const fid of fileIds) {
try {
if (deleteFile(fid)) deletedFiles++;
} catch (err) {
log.warn("deleteCompletedBatches: file soft-delete failed", {
fid,
err: err instanceof Error ? err.message : String(err),
});
}
}
}

db.prepare(
`DELETE FROM batch_item_checkpoints WHERE batch_id IN (SELECT id FROM batches WHERE status = 'completed'${ownershipClause})`
).run(...ownershipArgs);
db.prepare(
`DELETE FROM batch_item_checkpoints WHERE batch_id IN (SELECT id FROM batches WHERE status = 'completed'${ownershipClause})`
).run(...ownershipArgs);

const result = db
.prepare(`DELETE FROM batches WHERE status = 'completed'${ownershipClause}`)
.run(...ownershipArgs);
return { deletedBatches: result.changes, deletedFiles };
});

const result = db
.prepare(`DELETE FROM batches WHERE status = 'completed'${ownershipClause}`)
.run(...ownershipArgs);
return { deletedBatches: result.changes, deletedFiles };
return sweep();
}
6 changes: 3 additions & 3 deletions tests/unit/batch-deletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe("deleteCompletedBatches", () => {
assert.ok(getFile(liveInput.id));

// Delete all completed (may include pre-existing ones from other tests)
const result = deleteCompletedBatches();
const result = deleteCompletedBatches({ allTenants: true });
assert.ok(result.deletedBatches >= 3, `expected >=3, got ${result.deletedBatches}`);
assert.ok(result.deletedFiles >= 3, `expected >=3, got ${result.deletedFiles}`);

Expand All @@ -194,7 +194,7 @@ describe("deleteCompletedBatches", () => {
});

it("should return zero counts when no completed batches exist", () => {
const result = deleteCompletedBatches();
const result = deleteCompletedBatches({ allTenants: true });
assert.strictEqual(result.deletedBatches, 0);
assert.strictEqual(result.deletedFiles, 0);
});
Expand Down Expand Up @@ -224,7 +224,7 @@ describe("deleteCompletedBatches", () => {
assert.ok(getBatch(batchB.id));
assert.ok(getFile(sharedFile.id));

const result = deleteCompletedBatches();
const result = deleteCompletedBatches({ allTenants: true });
assert.ok(result.deletedBatches >= 2);
assert.ok(result.deletedFiles >= 1, "shared file should be counted once");

Expand Down
Loading