mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-18 20:05:03 +00:00
fix(db): import JSON preserva estado das chaves e cifra credenciais (GHSA-p42h) (#16655)
* test(db): reproduce JSON import resurrecting revoked keys and storing plaintext credentials (GHSA-p42h) * fix(db): preserve API key state and encrypt credentials on JSON settings import (GHSA-p42h) The JSON importer rewrote every API key from 8 columns, so revoked, expired and banned keys became valid again and their restrictions (scopes, allowlists, connection/combo access, rate and usage limits) were dropped. The key column list is now derived from the live api_keys schema, so every column present in the backup is restored and future columns are not silently lost. Connections were also written with plaintext credentials and lost group, max_concurrent, proxy_enabled, per_key_proxy_enabled, quota_visible and quota_window_thresholds. Credentials now go through encryptConnectionFields() and those columns are restored. Co-authored-by: xiaoyaner0201 <xiaoyaner0201@users.noreply.github.com> --------- Co-authored-by: xiaoyaner0201 <xiaoyaner0201@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
ae179c4862
commit
88c1e3342b
56
src/lib/db/apiKeys/importRow.ts
Normal file
56
src/lib/db/apiKeys/importRow.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* db/apiKeys/importRow.ts — builds the INSERT for restoring an API key from a JSON backup.
|
||||
*
|
||||
* GHSA-p42h-25v7-99v5: the importer used to write 8 hand-picked columns, so every other column
|
||||
* (revoked_at, expires_at, is_banned, is_active, scopes, ip_allowlist, allowed_connections,
|
||||
* rate/usage limits, ...) fell back to its default and a revoked, expired or banned key became
|
||||
* valid again with its restrictions gone. The column list is now derived from the live
|
||||
* `api_keys` schema: every column the backup carries (the export is `getApiKeys()` -> camelCase)
|
||||
* is restored, so a column added by a future migration is not silently dropped.
|
||||
*
|
||||
* A column the backup does not carry keeps the table default (older backups import as before).
|
||||
*/
|
||||
|
||||
import type { SqliteAdapter } from "../adapters/types";
|
||||
import { parseModelAccessMode } from "./modelAccessMode";
|
||||
|
||||
type ColumnInfo = { name: string; notnull: number };
|
||||
|
||||
function snakeToCamel(name: string): string {
|
||||
return name.replace(/_([a-z0-9])/g, (_match, ch: string) => ch.toUpperCase());
|
||||
}
|
||||
|
||||
function toSqliteValue(value: unknown): string | number | null {
|
||||
if (value === null || value === undefined) return null;
|
||||
if (typeof value === "boolean") return value ? 1 : 0;
|
||||
if (typeof value === "number" || typeof value === "string") return value;
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
export function insertImportedApiKey(db: SqliteAdapter, apiKey: Record<string, unknown>): void {
|
||||
const columns = db.prepare("PRAGMA table_info(api_keys)").all() as ColumnInfo[];
|
||||
const allowedModels = Array.isArray(apiKey.allowedModels) ? apiKey.allowedModels : [];
|
||||
const params: Record<string, string | number | null> = {};
|
||||
|
||||
for (const column of columns) {
|
||||
const field = snakeToCamel(column.name);
|
||||
if (!Object.hasOwn(apiKey, field) || apiKey[field] === undefined) continue;
|
||||
const value = toSqliteValue(apiKey[field]);
|
||||
// A NULL for a NOT NULL column must fall back to the column default, not abort the restore.
|
||||
if (value === null && column.notnull) continue;
|
||||
params[column.name] = value;
|
||||
}
|
||||
|
||||
// Fields whose stored form is normalized (kept from the previous importer).
|
||||
params.model_access_mode = parseModelAccessMode(apiKey.modelAccessMode, allowedModels);
|
||||
params.allowed_models = JSON.stringify(allowedModels);
|
||||
params.no_log = apiKey.noLog ? 1 : 0;
|
||||
params.machine_id = (apiKey.machineId as string | null | undefined) ?? null;
|
||||
params.created_at = (apiKey.createdAt as string | undefined) ?? new Date().toISOString();
|
||||
|
||||
const names = Object.keys(params);
|
||||
const sql =
|
||||
`INSERT OR REPLACE INTO api_keys (${names.map((n) => `"${n}"`).join(", ")}) ` +
|
||||
`VALUES (${names.map((n) => `@${n}`).join(", ")})`;
|
||||
db.prepare(sql).run(params);
|
||||
}
|
||||
@@ -15,8 +15,9 @@ import type { SqliteAdapter } from "./adapters/types";
|
||||
import { normalizeRoutingStrategy } from "@/shared/constants/routingStrategies";
|
||||
import { normalizeComboRecord } from "@/lib/combos/steps";
|
||||
import { validateComboInvariant } from "@/lib/combos/invariants";
|
||||
import { parseModelAccessMode } from "./apiKeys/modelAccessMode";
|
||||
import { serializeJsonField } from "./providers/columns";
|
||||
import { insertImportedApiKey } from "./apiKeys/importRow";
|
||||
import { normalizeBooleanColumn, serializeJsonField } from "./providers/columns";
|
||||
import { encryptConnectionFields } from "./encryption";
|
||||
import {
|
||||
resolveImportedUsageAccountIdentity,
|
||||
resolveOrphanedUsageAccountIdentity,
|
||||
@@ -74,7 +75,8 @@ export function runJsonMigration(
|
||||
last_tested, api_key, id_token, provider_specific_data,
|
||||
expires_in, display_name, global_priority, default_model,
|
||||
token_type, consecutive_use_count, rate_limit_protection, last_used_at,
|
||||
rate_limit_overrides_json, created_at, updated_at
|
||||
rate_limit_overrides_json, "group", max_concurrent, proxy_enabled,
|
||||
per_key_proxy_enabled, quota_visible, quota_window_thresholds_json, created_at, updated_at
|
||||
) VALUES (
|
||||
@id, @provider, @authType, @name, @email, @priority, @isActive,
|
||||
@accessToken, @refreshToken, @expiresAt, @tokenExpiresAt,
|
||||
@@ -84,7 +86,8 @@ export function runJsonMigration(
|
||||
@lastTested, @apiKey, @idToken, @providerSpecificData,
|
||||
@expiresIn, @displayName, @globalPriority, @defaultModel,
|
||||
@tokenType, @consecutiveUseCount, @rateLimitProtection, @lastUsedAt,
|
||||
@rateLimitOverridesJson, @createdAt, @updatedAt
|
||||
@rateLimitOverridesJson, @group, @maxConcurrent, @proxyEnabled,
|
||||
@perKeyProxyEnabled, @quotaVisible, @quotaWindowThresholdsJson, @createdAt, @updatedAt
|
||||
)
|
||||
`);
|
||||
|
||||
@@ -102,14 +105,6 @@ export function runJsonMigration(
|
||||
VALUES (@id, @name, @data, @sortOrder, @createdAt, @updatedAt)
|
||||
`);
|
||||
|
||||
const insertKey = db.prepare(`
|
||||
INSERT OR REPLACE INTO api_keys (
|
||||
id, name, key, machine_id, model_access_mode, allowed_models, no_log, created_at
|
||||
) VALUES (
|
||||
@id, @name, @key, @machineId, @modelAccessMode, @allowedModels, @noLog, @createdAt
|
||||
)
|
||||
`);
|
||||
|
||||
const selectExistingOverrides = db.prepare(
|
||||
"SELECT rate_limit_overrides_json FROM provider_connections WHERE id = ?"
|
||||
);
|
||||
@@ -124,10 +119,17 @@ export function runJsonMigration(
|
||||
let rateLimitOverridesJson = serializeJsonField(conn.rateLimitOverrides);
|
||||
if (!hasOverrides && typeof conn.id === "string") {
|
||||
const existing = selectExistingOverrides.get(conn.id) as
|
||||
| { rate_limit_overrides_json: string | null }
|
||||
| undefined;
|
||||
{ rate_limit_overrides_json: string | null } | undefined;
|
||||
if (existing) rateLimitOverridesJson = existing.rate_limit_overrides_json;
|
||||
}
|
||||
// GHSA-p42h: the export carries decrypted credentials; re-encrypt them like every other
|
||||
// connection write (no-op when STORAGE_ENCRYPTION_KEY is unset, skips already-encrypted).
|
||||
const creds = encryptConnectionFields({
|
||||
apiKey: conn.apiKey as string | null | undefined,
|
||||
accessToken: conn.accessToken as string | null | undefined,
|
||||
refreshToken: conn.refreshToken as string | null | undefined,
|
||||
idToken: conn.idToken as string | null | undefined,
|
||||
});
|
||||
insertConn.run({
|
||||
id: conn.id,
|
||||
provider: conn.provider,
|
||||
@@ -136,8 +138,8 @@ export function runJsonMigration(
|
||||
email: conn.email ?? null,
|
||||
priority: conn.priority ?? 0,
|
||||
isActive: conn.isActive === false ? 0 : 1,
|
||||
accessToken: conn.accessToken ?? null,
|
||||
refreshToken: conn.refreshToken ?? null,
|
||||
accessToken: creds.accessToken ?? null,
|
||||
refreshToken: creds.refreshToken ?? null,
|
||||
expiresAt: conn.expiresAt ?? null,
|
||||
tokenExpiresAt: conn.tokenExpiresAt ?? null,
|
||||
scope: conn.scope ?? null,
|
||||
@@ -153,8 +155,8 @@ export function runJsonMigration(
|
||||
healthCheckInterval: conn.healthCheckInterval ?? null,
|
||||
lastHealthCheckAt: conn.lastHealthCheckAt ?? null,
|
||||
lastTested: conn.lastTested ?? null,
|
||||
apiKey: conn.apiKey ?? null,
|
||||
idToken: conn.idToken ?? null,
|
||||
apiKey: creds.apiKey ?? null,
|
||||
idToken: creds.idToken ?? null,
|
||||
providerSpecificData: conn.providerSpecificData
|
||||
? JSON.stringify(conn.providerSpecificData)
|
||||
: null,
|
||||
@@ -168,6 +170,12 @@ export function runJsonMigration(
|
||||
rateLimitProtection:
|
||||
conn.rateLimitProtection === true || conn.rateLimitProtection === 1 ? 1 : 0,
|
||||
rateLimitOverridesJson,
|
||||
group: conn.group ?? null,
|
||||
maxConcurrent: conn.maxConcurrent ?? null,
|
||||
proxyEnabled: normalizeBooleanColumn(conn.proxyEnabled, true) ? 1 : 0,
|
||||
perKeyProxyEnabled: normalizeBooleanColumn(conn.perKeyProxyEnabled, false) ? 1 : 0,
|
||||
quotaVisible: normalizeBooleanColumn(conn.quotaVisible, true) ? 1 : 0,
|
||||
quotaWindowThresholdsJson: serializeJsonField(conn.quotaWindowThresholds),
|
||||
createdAt: conn.createdAt ?? new Date().toISOString(),
|
||||
updatedAt: conn.updatedAt ?? new Date().toISOString(),
|
||||
});
|
||||
@@ -241,18 +249,10 @@ export function runJsonMigration(
|
||||
}
|
||||
|
||||
// 6. API Keys
|
||||
// GHSA-p42h: every column the backup carries is restored (revocation, expiry, ban, scopes,
|
||||
// allowlists, limits...) - see apiKeys/importRow.ts.
|
||||
for (const apiKey of data.apiKeys ?? []) {
|
||||
const allowedModels = Array.isArray(apiKey.allowedModels) ? apiKey.allowedModels : [];
|
||||
insertKey.run({
|
||||
id: apiKey.id,
|
||||
name: apiKey.name,
|
||||
key: apiKey.key,
|
||||
machineId: apiKey.machineId ?? null,
|
||||
modelAccessMode: parseModelAccessMode(apiKey.modelAccessMode, allowedModels),
|
||||
allowedModels: JSON.stringify(allowedModels),
|
||||
noLog: apiKey.noLog ? 1 : 0,
|
||||
createdAt: apiKey.createdAt ?? new Date().toISOString(),
|
||||
});
|
||||
insertImportedApiKey(db, apiKey);
|
||||
}
|
||||
// 7. Usage History
|
||||
if (data.usageHistory && data.usageHistory.length > 0) {
|
||||
|
||||
174
tests/unit/settings-json-import-key-state-ghsa-p42h.test.ts
Normal file
174
tests/unit/settings-json-import-key-state-ghsa-p42h.test.ts
Normal file
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* GHSA-p42h-25v7-99v5 — restoring a JSON settings backup must not resurrect revoked, expired or
|
||||
* banned API keys, drop their restrictions, or store provider credentials in plaintext.
|
||||
*
|
||||
* The test exports through the same readers the export-json route uses (getApiKeys /
|
||||
* getProviderConnections), wipes the tables, and re-imports through runJsonMigration.
|
||||
*/
|
||||
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-ghsa-p42h-"));
|
||||
process.env.DATA_DIR = TEST_DATA_DIR;
|
||||
process.env.STORAGE_ENCRYPTION_KEY = "ghsa-p42h-synthetic-storage-encryption-key-0123456789";
|
||||
process.env.API_KEY_SECRET = "ghsa-p42h-synthetic-api-key-secret";
|
||||
process.env.OMNIROUTE_DISABLE_REDIS_AUTH_CACHE = "1";
|
||||
|
||||
const core = await import("../../src/lib/db/core.ts");
|
||||
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
|
||||
const providersDb = await import("../../src/lib/db/providers.ts");
|
||||
const { runJsonMigration } = await import("../../src/lib/db/jsonMigration.ts");
|
||||
|
||||
test.after(() => {
|
||||
core.resetDbInstance();
|
||||
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
||||
});
|
||||
|
||||
test.beforeEach(() => {
|
||||
apiKeysDb.clearApiKeyCaches();
|
||||
});
|
||||
|
||||
type Row = Record<string, unknown>;
|
||||
|
||||
async function roundTripKeys(): Promise<Row[]> {
|
||||
const exported = (await apiKeysDb.getApiKeys()) as Row[];
|
||||
const backup = JSON.parse(JSON.stringify(exported)) as Row[];
|
||||
const db = core.getDbInstance();
|
||||
db.prepare("DELETE FROM api_keys").run();
|
||||
runJsonMigration(db, { apiKeys: backup });
|
||||
apiKeysDb.clearApiKeyCaches();
|
||||
return backup;
|
||||
}
|
||||
|
||||
test("a revoked key stays revoked after export -> import", async () => {
|
||||
const created = await apiKeysDb.createApiKey("revoked", "machine-1");
|
||||
assert.equal(await apiKeysDb.validateApiKey(created.key), true);
|
||||
assert.equal(await apiKeysDb.revokeApiKey(created.id), true);
|
||||
assert.equal(await apiKeysDb.validateApiKey(created.key), false);
|
||||
|
||||
await roundTripKeys();
|
||||
|
||||
assert.equal(await apiKeysDb.validateApiKey(created.key), false);
|
||||
const restored = (await apiKeysDb.getApiKeyById(created.id)) as Row;
|
||||
assert.ok(restored.revokedAt, "revokedAt must be preserved");
|
||||
});
|
||||
|
||||
test("an expired key stays expired after export -> import", async () => {
|
||||
const created = await apiKeysDb.createApiKey("expired", "machine-1", [], {
|
||||
expiresAt: new Date(Date.now() - 60_000).toISOString(),
|
||||
});
|
||||
assert.equal(await apiKeysDb.validateApiKey(created.key), false);
|
||||
|
||||
await roundTripKeys();
|
||||
|
||||
assert.equal(await apiKeysDb.validateApiKey(created.key), false);
|
||||
});
|
||||
|
||||
test("a banned / deactivated key keeps its state, scopes and ip allowlist", async () => {
|
||||
const created = await apiKeysDb.createApiKey("banned", "machine-1", ["chat"]);
|
||||
const db = core.getDbInstance();
|
||||
db.prepare("UPDATE api_keys SET is_banned = 1, is_active = 0, ip_allowlist = ? WHERE id = ?").run(
|
||||
JSON.stringify(["10.0.0.0/8"]),
|
||||
created.id
|
||||
);
|
||||
apiKeysDb.clearApiKeyCaches();
|
||||
assert.equal(await apiKeysDb.validateApiKey(created.key), false);
|
||||
|
||||
await roundTripKeys();
|
||||
|
||||
assert.equal(await apiKeysDb.validateApiKey(created.key), false);
|
||||
const row = db.prepare("SELECT * FROM api_keys WHERE id = ?").get(created.id) as Row;
|
||||
assert.equal(row.is_banned, 1);
|
||||
assert.equal(row.is_active, 0);
|
||||
assert.deepEqual(JSON.parse(String(row.scopes)), ["chat"]);
|
||||
assert.ok(String(row.ip_allowlist).includes("10.0.0.0/8"), "ip_allowlist must be preserved");
|
||||
});
|
||||
|
||||
test("a restricted key keeps connections, combos, rate limits and usage limits", async () => {
|
||||
const created = await apiKeysDb.createApiKey("restricted", "machine-1", [], {
|
||||
allowedConnections: ["conn-a"],
|
||||
allowedCombos: ["combo-a"],
|
||||
});
|
||||
const db = core.getDbInstance();
|
||||
db.prepare(
|
||||
`UPDATE api_keys SET max_requests_per_minute = 7, max_requests_per_day = 99,
|
||||
usage_limit_enabled = 1, daily_usage_limit_usd = 1.5, weekly_usage_limit_usd = 9.25,
|
||||
max_sessions = 2, throttle_delay_ms = 250, rate_limits = ?, allowed_endpoints = ?
|
||||
WHERE id = ?`
|
||||
).run(
|
||||
JSON.stringify([{ limit: 3, window: 60 }]),
|
||||
JSON.stringify(["/v1/chat/completions"]),
|
||||
created.id
|
||||
);
|
||||
|
||||
await roundTripKeys();
|
||||
|
||||
const row = db.prepare("SELECT * FROM api_keys WHERE id = ?").get(created.id) as Row;
|
||||
assert.deepEqual(JSON.parse(String(row.allowed_connections)), ["conn-a"]);
|
||||
assert.deepEqual(JSON.parse(String(row.allowed_combos)), ["combo-a"]);
|
||||
assert.equal(row.max_requests_per_minute, 7);
|
||||
assert.equal(row.max_requests_per_day, 99);
|
||||
assert.equal(row.usage_limit_enabled, 1);
|
||||
assert.equal(row.daily_usage_limit_usd, 1.5);
|
||||
assert.equal(row.weekly_usage_limit_usd, 9.25);
|
||||
assert.equal(row.max_sessions, 2);
|
||||
assert.equal(row.throttle_delay_ms, 250);
|
||||
assert.deepEqual(JSON.parse(String(row.rate_limits)), [{ limit: 3, window: 60 }]);
|
||||
assert.deepEqual(JSON.parse(String(row.allowed_endpoints)), ["/v1/chat/completions"]);
|
||||
assert.equal(await apiKeysDb.validateApiKey(created.key), true, "still valid by key/hash");
|
||||
});
|
||||
|
||||
test("an older backup without lifecycle fields still imports with safe defaults", async () => {
|
||||
const db = core.getDbInstance();
|
||||
db.prepare("DELETE FROM api_keys").run();
|
||||
runJsonMigration(db, {
|
||||
apiKeys: [{ id: "legacy-1", name: "legacy", key: "sk-legacy-synthetic", machineId: "m" }],
|
||||
});
|
||||
assert.equal(await apiKeysDb.validateApiKey("sk-legacy-synthetic"), true);
|
||||
});
|
||||
|
||||
test("connections keep max_concurrent/group/proxy flags and are stored encrypted", async () => {
|
||||
const db = core.getDbInstance();
|
||||
db.prepare("DELETE FROM provider_connections").run();
|
||||
runJsonMigration(db, {
|
||||
providerConnections: [
|
||||
{
|
||||
id: "conn-ghsa-1",
|
||||
provider: "openai",
|
||||
authType: "oauth",
|
||||
name: "synthetic",
|
||||
accessToken: "synthetic-access-token",
|
||||
refreshToken: "synthetic-refresh-token",
|
||||
idToken: "synthetic-id-token",
|
||||
apiKey: "synthetic-api-key",
|
||||
group: "team-a",
|
||||
maxConcurrent: 3,
|
||||
proxyEnabled: false,
|
||||
perKeyProxyEnabled: true,
|
||||
quotaVisible: false,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const raw = db
|
||||
.prepare("SELECT * FROM provider_connections WHERE id = ?")
|
||||
.get("conn-ghsa-1") as Row;
|
||||
for (const col of ["access_token", "refresh_token", "id_token", "api_key"]) {
|
||||
assert.ok(String(raw[col]).startsWith("enc:v1:"), `${col} must be stored encrypted`);
|
||||
}
|
||||
assert.equal(raw.group, "team-a");
|
||||
assert.equal(raw.max_concurrent, 3);
|
||||
assert.equal(raw.proxy_enabled, 0);
|
||||
assert.equal(raw.per_key_proxy_enabled, 1);
|
||||
assert.equal(raw.quota_visible, 0);
|
||||
|
||||
const [conn] = (await providersDb.getProviderConnections({ provider: "openai" })) as Row[];
|
||||
assert.equal(conn.accessToken, "synthetic-access-token");
|
||||
assert.equal(conn.refreshToken, "synthetic-refresh-token");
|
||||
assert.equal(conn.idToken, "synthetic-id-token");
|
||||
assert.equal(conn.apiKey, "synthetic-api-key");
|
||||
assert.equal(conn.maxConcurrent, 3);
|
||||
});
|
||||
Reference in New Issue
Block a user