File size: 6,389 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fs from "fs/promises";
import path from "path";
import { resolveDataDir } from "@/lib/dataPaths";

const BACKUP_DIR = path.join(resolveDataDir(), "backups");
const MAX_BACKUPS_PER_TOOL = 5;

/**

 * Resolve a path within BACKUP_DIR and verify it stays within bounds.

 * Throws if the resolved path escapes BACKUP_DIR (path traversal guard).

 */
function safePath(...segments: string[]): string {
  const resolved = path.resolve(BACKUP_DIR, ...segments);
  const base = path.resolve(BACKUP_DIR);
  if (resolved !== base && !resolved.startsWith(base + path.sep)) {
    throw new Error("Invalid path: directory traversal detected");
  }
  return resolved;
}

/**

 * Get backup directory for a specific tool

 */
function getToolBackupDir(toolId: string) {
  return safePath(toolId);
}

/**

 * Ensure backup directory exists for a tool

 */
async function ensureBackupDir(toolId: string) {
  const dir = getToolBackupDir(toolId);
  await fs.mkdir(dir, { recursive: true });
  return dir;
}

/**

 * Generate a backup filename with timestamp

 */
function makeBackupName(originalPath: string) {
  const ext = path.extname(originalPath);
  const base = path.basename(originalPath, ext);
  const ts = new Date().toISOString().replace(/[:.]/g, "-");
  return `${base}_${ts}${ext}`;
}

/**

 * Create a backup of a file before modifying it.

 * Returns the backup path, or null if the source doesn't exist.

 */
export async function createBackup(toolId: string, filePath: string) {
  try {
    await fs.access(filePath);
  } catch {
    // Source file doesn't exist β€” nothing to back up
    return null;
  }

  const dir = await ensureBackupDir(toolId);
  const backupName = makeBackupName(filePath);
  const backupPath = path.join(dir, backupName);

  await fs.copyFile(filePath, backupPath);

  // Save metadata alongside the backup
  const metaPath = backupPath + ".meta.json";
  await fs.writeFile(
    metaPath,
    JSON.stringify({
      originalPath: filePath,
      backupName,
      toolId,
      createdAt: new Date().toISOString(),
    })
  );

  // Enforce rotation (max backups per tool)
  await rotateBackups(toolId);

  return backupPath;
}

/**

 * Create backups for multiple files in one operation (e.g. Codex config.toml + auth.json).

 * Returns an array of backup paths.

 */
export async function createMultiBackup(toolId: string, filePaths: string[]) {
  const results: (string | null)[] = [];
  for (const filePath of filePaths) {
    const result = await createBackup(toolId, filePath);
    results.push(result);
  }
  return results;
}

/**

 * List all backups for a tool (sorted newest first).

 */
export async function listBackups(toolId: string) {
  const dir = getToolBackupDir(toolId);

  let entries;
  try {
    entries = await fs.readdir(dir);
  } catch {
    return [];
  }

  const metaFiles = entries.filter((e) => e.endsWith(".meta.json"));
  const backups: any[] = [];

  for (const metaFile of metaFiles) {
    try {
      const metaPath = path.join(dir, metaFile);
      const raw = await fs.readFile(metaPath, "utf-8");
      const meta = JSON.parse(raw);

      const backupFile = metaFile.replace(".meta.json", "");
      const backupPath = path.join(dir, backupFile);

      let size = 0;
      try {
        const stat = await fs.stat(backupPath);
        size = stat.size;
      } catch {
        // Backup file missing β€” skip
        continue;
      }

      backups.push({
        id: backupFile,
        toolId: meta.toolId,
        originalPath: meta.originalPath,
        createdAt: meta.createdAt,
        size,
      });
    } catch {
      // Corrupt meta β€” skip
    }
  }

  // Sort newest first
  backups.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
  return backups;
}

/**

 * Restore a backup by its id (filename).

 */
export async function restoreBackup(toolId: string, backupId: string) {
  const dir = getToolBackupDir(toolId);
  // Anchor backupId within the tool dir β€” prevent path traversal via backupId
  const backupPath = safePath(toolId, backupId);
  const metaPath = backupPath + ".meta.json";

  // Read metadata to find original path
  let meta;
  try {
    const raw = await fs.readFile(metaPath, "utf-8");
    meta = JSON.parse(raw);
  } catch {
    throw new Error(`Backup metadata not found: ${backupId}`);
  }

  // Verify actual backup file exists
  try {
    await fs.access(backupPath);
  } catch {
    throw new Error(`Backup file not found: ${backupId}`);
  }

  // Before restoring, back up the current file (so restore is reversible)
  await createBackup(toolId, meta.originalPath);

  // Copy backup over the original
  const targetDir = path.dirname(meta.originalPath);
  await fs.mkdir(targetDir, { recursive: true });
  await fs.copyFile(backupPath, meta.originalPath);

  return {
    restored: true,
    backupId,
    originalPath: meta.originalPath,
  };
}

/**

 * Delete a specific backup by its id.

 */
export async function deleteBackup(toolId: string, backupId: string) {
  // Anchor backupId within the tool dir β€” prevent path traversal via backupId
  const backupPath = safePath(toolId, backupId);
  const metaPath = backupPath + ".meta.json";

  try {
    await fs.unlink(backupPath);
  } catch {
    // Already gone
  }
  try {
    await fs.unlink(metaPath);
  } catch {
    // Already gone
  }

  return { deleted: true, backupId };
}

/**

 * Enforce max backups per tool β€” removes oldest when limit exceeded.

 * Groups by original file basename so each config file gets its own rotation.

 */
async function rotateBackups(toolId: string) {
  const all = await listBackups(toolId);

  // Group by original file basename
  const groups: Record<string, any[]> = {};
  for (const b of all) {
    const key = path.basename(b.originalPath);
    if (!groups[key]) groups[key] = [];
    groups[key].push(b);
  }

  for (const [, group] of Object.entries(groups) as [string, any[]][]) {
    // Already sorted newest first
    if (group.length > MAX_BACKUPS_PER_TOOL) {
      const toDelete = group.slice(MAX_BACKUPS_PER_TOOL);
      for (const old of toDelete) {
        await deleteBackup(toolId, old.id);
      }
    }
  }
}