Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: keep backup service state.json out of Data Manager archives (#11781)

[._.]/ Adam Eivy06db7a6aa20c
diff --git a/server/services/dataManager.archive.test.js b/server/services/dataManager.archive.test.js
index 88899ac3d4..479efa8988 100644
--- a/server/services/dataManager.archive.test.js
+++ b/server/services/dataManager.archive.test.js
@@ -311,3 +311,18 @@ describe('health archive/import coordination (#10698)', () => {
});
}
});
+
+describe('backup directory shared with the backup service state (#11781)', () => {
+ it('lists and deletes only archive-shaped names, never state.json', async () => {
+ const backup = join(root, 'backup');
+ await mkdir(backup, { recursive: true });
+ const archive = 'health-2026-01-01T00-00-00-00000000-0000-4000-8000-000000000000.tar.gz';
+ await writeFile(join(backup, 'state.json'), '{}');
+ await writeFile(join(backup, archive), 'fixture');
+ expect((await getBackups()).map(row => row.name)).toEqual([archive]);
+ await expect(deleteBackup('state.json')).rejects.toMatchObject({ status: 400, code: 'INVALID_FILENAME' });
+ expect(await readFile(join(backup, 'state.json'), 'utf8')).toBe('{}');
+ await expect(deleteBackup(archive)).resolves.toEqual({ deleted: archive });
+ await rm(join(backup, 'state.json'), { force: true });
+ });
+});
diff --git a/server/services/dataManager.js b/server/services/dataManager.js
index bb84c9bc2f..130cdf1565 100644
--- a/server/services/dataManager.js
+++ b/server/services/dataManager.js
@@ -586,12 +586,19 @@ export async function purgeCategory(categoryKey, options = {}) {
});
}
+// Names archiveCategory produces: `<category>-<timestamp>[-<uuid>].tar.gz`
+// (the uuid is absent on archives written before same-second collisions were
+// handled). data/backup/ is shared with the backup service's state.json, so
+// listing and deletion are limited to this shape. The pattern excludes `/`,
+// `\\` and bare dots, so no traversal segment can form (issue #1822).
+const ARCHIVE_FILENAME = /^[a-z0-9_-]+-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}(?:-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})?\.tar\.gz$/i;
+
export async function getBackups() {
const backupDir = join(DATA_DIR, 'backup');
if (!existsSync(backupDir)) return [];
const entries = await readdir(backupDir, { withFileTypes: true }).catch(() => []);
- const files = entries.filter(e => e.isFile());
+ const files = entries.filter(e => e.isFile() && ARCHIVE_FILENAME.test(e.name));
const backups = await Promise.all(files.map(async (entry) => {
const fileStat = await stat(join(backupDir, entry.name)).catch(() => null);
@@ -606,17 +613,8 @@ export async function getBackups() {
return backups;
}
-// Backup archives are named like `agents-2026-06-30T12-34-56.tar.gz`, so the raw
-// filename legitimately contains dots. Validate the dotted value directly — the
-// old `filename.replace(/[.]/g,'')` double-pass never checked the real filename,
-// leaving only the startsWith(backupDir) guard against traversal (issue #1822).
-// The regex forbids `/` and `\`, so no traversal segment can form; the only
-// regex-passing names that resolve dangerously are the bare `.`/`..` (e.g.
-// join(backupDir,'.') === backupDir), so reject those explicitly.
-const SAFE_FILENAME = /^[a-z0-9._-]+$/i;
-
export async function deleteBackup(filename) {
- if (!SAFE_FILENAME.test(filename) || filename === '.' || filename === '..') {
+ if (!ARCHIVE_FILENAME.test(filename)) {
throw new ServerError('Invalid filename', { status: 400, code: 'INVALID_FILENAME' });
}
const backupDir = join(DATA_DIR, 'backup');