W
File size: 8,750 Bytes
2b64d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';
import { promisify } from 'node:util';

const readFile = promisify(fs.readFile);
const stat = promisify(fs.stat);
const access = promisify(fs.access);
const copyFile = promisify(fs.copyFile);
const mkdtemp = promisify(fs.mkdtemp);
const rm = promisify(fs.rm);

const STATE_DB_REL = path.join('User', 'globalStorage', 'state.vscdb');
const STATE_KEY = 'windsurfAuthStatus';
const TMP_STATE_DIR_PREFIX = path.join(os.tmpdir(), 'windsurf-state-');
const MAX_STATE_DB_BYTES = 24 * 1024 * 1024;
const MAX_STATE_ROWS_PER_DB = 200;
const MAX_STATE_VALUE_BYTES = 128 * 1024;
const DISCOVER_CACHE_TTL_MS = 4000;

let cachedSqlite = undefined;
let discoverCache = null;
let discoverInFlight = null;

export function getCandidateStateDbPaths() {
  const home = os.homedir();
  const flavors = ['Windsurf', 'Windsurf - Next', 'Windsurf-Next', 'Windsurf Insiders'];
  const paths = [];
  if (process.platform === 'darwin') {
    for (const f of flavors) {
      paths.push(path.join(home, 'Library', 'Application Support', f, STATE_DB_REL));
    }
  } else if (process.platform === 'win32') {
    const appData = process.env.APPDATA || path.join(home, 'AppData', 'Roaming');
    for (const f of flavors) {
      paths.push(path.join(appData, f, STATE_DB_REL));
    }
  } else {
    const xdg = process.env.XDG_CONFIG_HOME || path.join(home, '.config');
    for (const f of flavors) {
      paths.push(path.join(xdg, f, STATE_DB_REL));
    }
  }
  return paths;
}

export function getCodeiumConfigPath() {
  const home = os.homedir();
  const xdgData = process.env.XDG_DATA_HOME;
  if (xdgData) return path.join(xdgData, '.codeium', 'config.json');
  return path.join(home, '.codeium', 'config.json');
}

async function fileExists(p) {
  try {
    await access(p, fs.constants.R_OK);
    return true;
  } catch {
    return false;
  }
}

function sanitizeSourcePath(value) {
  if (typeof value !== 'string') return '';
  return path.basename(value);
}

function isIpv4(addr) {
  if (!/^\d{1,3}(\.\d{1,3}){3}$/.test(addr)) return false;
  const parts = addr.split('.');
  return parts[0] === '127'
    && parts.every((p) => {
      const n = Number(p);
      return Number.isInteger(n) && n >= 0 && n <= 255;
    });
}

function isIpv4MappedIpv6(addr) {
  const lower = String(addr).toLowerCase();
  if (!lower.startsWith('::ffff:')) return false;
  const tail = lower.slice(7).split('%')[0];
  if (tail.includes('.')) return isIpv4(tail);
  if (!/^[0-9a-f]{1,4}:[0-9a-f]{1,4}$/.test(tail)) return false;
  const first = Number.parseInt(tail.split(':')[0], 16);
  return Number.isInteger(first) && first >= 0x7f00 && first <= 0x7fff;
}

function sanitizeLoopbackAddress(addr) {
  if (typeof addr !== 'string') return '';
  const trimmed = addr.trim();
  if (!trimmed) return '';
  if (trimmed[0] === '[' && trimmed.at(-1) === ']') return trimmed.slice(1, -1);
  return trimmed;
}

async function tryLoadSqlite() {
  if (cachedSqlite !== undefined) return cachedSqlite;
  try {
    const mod = await import('node:sqlite');
    cachedSqlite = mod?.DatabaseSync ? mod : null;
  } catch {
    cachedSqlite = null;
  }
  return cachedSqlite;
}

function maskKey(k) {
  if (!k || typeof k !== 'string') return '';
  if (k.length <= 12) return k.slice(0, 4) + '***';
  return k.slice(0, 8) + '...' + k.slice(-4);
}

function normalizeAccount(raw, source) {
  if (!raw || typeof raw !== 'object') return null;
  const apiKey = raw.apiKey || raw.api_key || raw.accessToken;
  if (!apiKey || typeof apiKey !== 'string') return null;
  const email = raw.email || raw.account?.email || null;
  const name = raw.name || raw.account?.name || null;
  return {
    method: 'api_key',
    apiKey,
    apiKeyMasked: maskKey(apiKey),
    email,
    name,
    apiServerUrl: raw.apiServerUrl || raw.account?.apiServerUrl || null,
    label: email || name || 'Imported from Windsurf',
    source,
  };
}

function getCachedResult() {
  if (!discoverCache) return null;
  if (Date.now() - discoverCache.timestamp >= DISCOVER_CACHE_TTL_MS) return null;
  return structuredClone(discoverCache.value);
}

export async function extractFromStateDb(dbPath) {
  if (!(await fileExists(dbPath))) return { ok: false, reason: 'not_found', dbPath };
  const sqlite = await tryLoadSqlite();
  if (!sqlite) {
    return { ok: false, reason: 'sqlite_unavailable', dbPath };
  }

  let db = null;
  let tmpDir = null;
  let tmpCopy = null;

  try {
    const info = await stat(dbPath);
    if (info.size > MAX_STATE_DB_BYTES) {
      return {
        ok: false,
        reason: 'size_limit_exceeded',
        dbPath,
        error: `state DB exceeds limit (${MAX_STATE_DB_BYTES} bytes)`,
      };
    }

    tmpDir = await mkdtemp(TMP_STATE_DIR_PREFIX);
    tmpCopy = path.join(tmpDir, 'state.vscdb');
    await copyFile(dbPath, tmpCopy);

    db = new sqlite.DatabaseSync(tmpCopy, { readOnly: true });
    const rows = db.prepare(
      `SELECT key, value FROM ItemTable WHERE key LIKE 'windsurfAuth%' OR key = ? OR key LIKE 'codeium%' LIMIT ${MAX_STATE_ROWS_PER_DB}`
    ).all(STATE_KEY);
    const accounts = [];
    const seen = new Set();
    for (const row of rows) {
      if (!row || typeof row.value !== 'string' || row.value.length > MAX_STATE_VALUE_BYTES) continue;
      if (seen.size >= MAX_STATE_ROWS_PER_DB) break;
      let parsed;
      try {
        parsed = JSON.parse(row.value);
      } catch {
        continue;
      }

      if (Array.isArray(parsed)) {
        for (const item of parsed) {
          const acc = normalizeAccount(item, `state.vscdb:${row.key}`);
          if (acc && !seen.has(acc.apiKey)) {
            seen.add(acc.apiKey);
            accounts.push(acc);
          }
          if (accounts.length >= MAX_STATE_ROWS_PER_DB) break;
        }
      } else {
        const acc = normalizeAccount(parsed, `state.vscdb:${row.key}`);
        if (acc && !seen.has(acc.apiKey)) {
          seen.add(acc.apiKey);
          accounts.push(acc);
        }
      }
    }

    return { ok: true, dbPath, accounts };
  } catch (e) {
    if (!tmpCopy) return { ok: false, reason: 'copy_failed', dbPath, error: e.message };
    return { ok: false, reason: 'read_failed', dbPath, error: e.message };
  } finally {
    if (db) {
      try { db.close(); } catch {}
    }
    if (tmpDir) {
      try { await rm(tmpDir, { recursive: true, force: true }); } catch {}
    }
  }
}

export async function extractFromCodeiumConfig() {
  const cfgPath = getCodeiumConfigPath();
  if (!(await fileExists(cfgPath))) return { ok: false, reason: 'not_found', dbPath: cfgPath };
  try {
    const content = await readFile(cfgPath, 'utf8');
    const parsed = JSON.parse(content);
    const acc = normalizeAccount(parsed, 'codeium-config');
    return { ok: true, dbPath: cfgPath, accounts: acc ? [acc] : [] };
  } catch (e) {
    return { ok: false, reason: 'parse_failed', dbPath: cfgPath, error: e.message };
  }
}

export async function discoverWindsurfCredentials() {
  const cached = getCachedResult();
  if (cached) return cached;
  if (discoverInFlight) return discoverInFlight;

  discoverInFlight = (async () => {
    const sources = [];
    const accounts = [];
    const seenKeys = new Set();

    for (const dbPath of getCandidateStateDbPaths()) {
      const result = await extractFromStateDb(dbPath);
      sources.push(result);
      if (result.ok) {
        for (const a of result.accounts) {
          if (!seenKeys.has(a.apiKey)) {
            seenKeys.add(a.apiKey);
            accounts.push(a);
          }
        }
      }
    }

    const cfgResult = await extractFromCodeiumConfig();
    sources.push(cfgResult);
    if (cfgResult.ok) {
      for (const a of cfgResult.accounts) {
        if (!seenKeys.has(a.apiKey)) {
          seenKeys.add(a.apiKey);
          accounts.push(a);
        }
      }
    }

    const sqliteOk = await tryLoadSqlite();
    const result = {
      accounts,
      sources: sources.map(s => ({
        path: sanitizeSourcePath(s.dbPath),
        ok: s.ok,
        reason: s.reason || null,
        accountCount: s.ok ? s.accounts.length : 0,
      })),
      sqliteSupport: sqliteOk ? 'available' : 'unavailable',
      platform: process.platform,
    };
    discoverCache = { timestamp: Date.now(), value: structuredClone(result) };
    return result;
  })();

  try {
    return await discoverInFlight;
  } finally {
    discoverInFlight = null;
  }
}

export function isLoopbackAddress(addr) {
  const normalized = sanitizeLoopbackAddress(addr).toLowerCase().split('%')[0];
  if (!normalized) return false;
  if (normalized === '::1') return true;
  if (isIpv4(normalized)) return true;
  if (isIpv4MappedIpv6(normalized)) return true;
  return false;
}