Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(keys): guard the CSV export's base_url cell against formula injection too (#1661)

tashdroidb7a87036e0c4
diff --git a/server/src/__tests__/lib/key-parser.test.ts b/server/src/__tests__/lib/key-parser.test.ts
index 0327ea68..f1bf8116 100644
--- a/server/src/__tests__/lib/key-parser.test.ts
+++ b/server/src/__tests__/lib/key-parser.test.ts
@@ -263,6 +263,18 @@ describe('key parser', () => {
]);
});
+ it('strips the formula-guard quote from base_url as well as the label', () => {
+ // What GET /api/keys/export?format=csv writes when a custom endpoint's
+ // base_url opens with '=' — the importer has to undo the guard on the
+ // fourth column too, or the endpoint re-imports as a broken "'=..." URL.
+ const csv = 'platform,key,label,base_url\n' +
+ '"custom","sk-local","\'=HYPERLINK(""x"")","\'=HYPERLINK(""http://evil"")"\n';
+ const rows = parseCsv(csv);
+ expect(rows).toHaveLength(1);
+ expect(rows[0]!.label).toBe('=HYPERLINK("x")');
+ expect(rows[0]!.baseUrl).toBe('=HYPERLINK("http://evil")');
+ });
+
it('round-trips a full export through parseKeysFromFile', () => {
// Exactly what GET /api/keys/export?format=csv writes for three keys.
const csv = 'platform,key,label,base_url\n' +
diff --git a/server/src/__tests__/routes/keys-export-csv.test.ts b/server/src/__tests__/routes/keys-export-csv.test.ts
index 02315581..7d0e7320 100644
--- a/server/src/__tests__/routes/keys-export-csv.test.ts
+++ b/server/src/__tests__/routes/keys-export-csv.test.ts
@@ -90,4 +90,26 @@ describe('Keys CSV export — formula injection guard (FIX 3)', () => {
expect(dataLine).toContain('"My Groq Key"');
expect(dataLine).not.toContain("'My Groq Key");
});
+
+ it('neutralizes formula-leading base_url cells too (free text like the label)', async () => {
+ getDb().prepare('DELETE FROM api_keys').run();
+ // A custom endpoint whose base_url opens with '=' — exactly the shape the
+ // label guard covers. Without the guard the cell opens as a live formula
+ // in Excel/Sheets and the base_url cannot round-trip (the importer only
+ // strips the guard quote from labels). Stored directly because the custom
+ // POST path URL-validates base_url; the export must guard the column
+ // whatever wrote it (older build, restored backup, hand-edited DB).
+ const { encrypt } = await import('../../lib/crypto.js');
+ const enc = encrypt('sk-local-abc123');
+ getDb().prepare(`
+ INSERT INTO api_keys (platform, label, encrypted_key, iv, auth_tag, base_url, enabled, status)
+ VALUES ('custom', 'evil endpoint', ?, ?, ?, '=HYPERLINK("http://evil.example","click")', 1, 'healthy')
+ `).run(enc.encrypted, enc.iv, enc.authTag);
+
+ const { status, text } = await exportCsvText(app);
+ expect(status).toBe(200);
+ const dataLine = text.trim().split('\n').find(l => l.includes('sk-local-abc123'))!;
+ expect(dataLine).toContain('"\'=HYPERLINK');
+ expect(dataLine).not.toContain(',"=HYPERLINK');
+ });
});
diff --git a/server/src/lib/key-parser.ts b/server/src/lib/key-parser.ts
index cec6843f..7c4c1aa9 100644
--- a/server/src/lib/key-parser.ts
+++ b/server/src/lib/key-parser.ts
@@ -537,9 +537,10 @@ export function parseCsv(content: string): KeyPair[] {
const platform = (fields[0] ?? '').trim();
const key = (fields[1] ?? '').trim();
// Undo the export's CSV formula guard (a leading ' before =, +, -, @, tab
- // or CR) so a guarded label comes back as the user typed it.
- const label = (fields[2] ?? '').trim().replace(/^'(?=[=+\-@\t\r])/, '');
- const baseUrl = (fields[3] ?? '').trim();
+ // or CR) so a guarded label or base_url comes back as the user typed it.
+ const unguard = (v: string) => v.replace(/^'(?=[=+\-@\t\r])/, '');
+ const label = unguard((fields[2] ?? '').trim());
+ const baseUrl = unguard((fields[3] ?? '').trim());
if (!key || !platform) continue;
diff --git a/server/src/routes/keys.ts b/server/src/routes/keys.ts
index aadccb3e..8be3f10a 100644
--- a/server/src/routes/keys.ts
+++ b/server/src/routes/keys.ts
@@ -633,9 +633,11 @@ keysRouter.get('/export', (req: Request, res: Response) => {
// CSV formula-injection guard: a spreadsheet treats a cell that starts with
// =, +, -, @, tab or CR as a live formula, so a label like `=HYPERLINK(...)`
// would execute on open. Prefix such cells with a single quote to force them
- // to be read as text. Applied only to free-text fields the user controls
- // (labels); the key value must round-trip verbatim for re-import, and the
- // platform is one of our own fixed enum values.
+ // to be read as text. Applied to the free-text fields the user controls
+ // (labels AND base_url — the custom endpoint column is free text too, and
+ // the importer strips the guard quote back off both); the key value must
+ // round-trip verbatim for re-import, and the platform is one of our own
+ // fixed enum values.
const neutralize = (v: string) => (/^[=+\-@\t\r]/.test(v) ? `'${v}` : v);
// base_url is the fourth column: a custom row is an ENDPOINT, and without
// its URL the key cannot be re-imported (#687). Blank for every other
@@ -643,7 +645,7 @@ keysRouter.get('/export', (req: Request, res: Response) => {
// written by older builds still load.
const header = 'platform,key,label,base_url';
const lines = decryptedKeys.map(k =>
- [escCsv(k.platform), escCsv(k.key), escCsv(neutralize(k.label)), escCsv(k.baseUrl ?? '')].join(',')
+ [escCsv(k.platform), escCsv(k.key), escCsv(neutralize(k.label)), escCsv(neutralize(k.baseUrl ?? ''))].join(',')
);
const content = [header, ...lines].join('\n') + '\n';
res.setHeader('Content-Type', 'text/csv; charset=utf-8');