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(server): CSV import drops rows whose quoted label has a comma or quote (#1290)

tashdroid998ab9c33bac
diff --git a/server/src/__tests__/lib/key-parser.test.ts b/server/src/__tests__/lib/key-parser.test.ts
index 8cc02601..058f406a 100644
--- a/server/src/__tests__/lib/key-parser.test.ts
+++ b/server/src/__tests__/lib/key-parser.test.ts
@@ -202,6 +202,30 @@ describe('key parser', () => {
]);
});
+ // RFC 4180 quoting: the CSV export quotes every cell and doubles quotes in
+ // labels, so a label with a comma or a quote is routine. The old single
+ // regex could not represent those lines and silently dropped the whole row.
+ it('parses quoted labels containing commas and escaped quotes', () => {
+ const csv = 'platform,key,label,base_url\n' +
+ '"groq","gsk-abc","work, primary",""\n' +
+ '"google","AIza-test","say ""hi""",""\n';
+ expect(parseCsv(csv)).toEqual([
+ { key: 'GROQ_KEY', value: 'gsk-abc', platform: 'groq' },
+ { key: 'GOOGLE_KEY', value: 'AIza-test', platform: 'google' },
+ ]);
+ });
+
+ 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' +
+ '"groq","gsk-abc","work, primary",""\n' +
+ '"custom","sk-local","say ""hi""","http://192.168.1.5:1234/v1"\n';
+ const result = parseKeysFromFile(csv, 'freellmapi-keys.csv');
+ expect(result.skipped).toEqual([]);
+ expect(result.keys.map(k => k.platform)).toEqual(['groq', 'custom']);
+ expect(result.keys[1]!.baseUrl).toBe('http://192.168.1.5:1234/v1');
+ });
+
it('handles export JSON via parseKeysFromFile', () => {
const exportJson = JSON.stringify({
version: 1,
diff --git a/server/src/lib/key-parser.ts b/server/src/lib/key-parser.ts
index 9449757a..90db2f09 100644
--- a/server/src/lib/key-parser.ts
+++ b/server/src/lib/key-parser.ts
@@ -399,6 +399,35 @@ export function parseExportJson(content: string): ParseResult | null {
return null;
}
+/**
+ * Split one CSV line into fields honouring RFC 4180 quoting: a quoted field
+ * may contain commas and `""` escapes. Our own CSV export quotes every cell
+ * and escapes quotes in labels, so a label like `work, primary` or `say "hi"`
+ * is routine — the previous single regex could not represent those lines and
+ * silently dropped the whole row on re-import.
+ */
+function splitCsvLine(line: string): string[] {
+ const fields: string[] = [];
+ let cur = '';
+ let inQuotes = false;
+ for (let i = 0; i < line.length; i++) {
+ const ch = line[i];
+ if (inQuotes) {
+ if (ch === '"') {
+ if (line[i + 1] === '"') { cur += '"'; i++; }
+ else inQuotes = false;
+ } else cur += ch;
+ } else if (ch === '"') {
+ inQuotes = true;
+ } else if (ch === ',') {
+ fields.push(cur);
+ cur = '';
+ } else cur += ch;
+ }
+ fields.push(cur);
+ return fields;
+}
+
/**
* Parse CSV format: platform,key,label[,base_url] (with optional header row).
* The trailing base_url column is what makes a 'custom' row importable — an
@@ -414,14 +443,10 @@ export function parseCsv(content: string): KeyPair[] {
const startIdx = lines[0]!.toLowerCase().startsWith('platform,') ? 1 : 0;
for (let i = startIdx; i < lines.length; i++) {
- const line = lines[i]!;
- // Simple CSV parsing: split on comma, strip quotes
- const match = line.match(/^"?([^"]*?)"?,"?([^"]*?)"?(?:,"?([^"]*?)"?)?(?:,"?([^"]*?)"?)?$/);
- if (!match) continue;
-
- const platform = (match[1] ?? '').trim();
- const key = (match[2] ?? '').trim();
- const baseUrl = (match[4] ?? '').trim();
+ const fields = splitCsvLine(lines[i]!);
+ const platform = (fields[0] ?? '').trim();
+ const key = (fields[1] ?? '').trim();
+ const baseUrl = (fields[3] ?? '').trim();
if (!key || !platform) continue;