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

refactor: share isSafeSubdirFilter predicate; tighten deleteBackup dot guard (#1822)

[._.]/ Adam Eivydb38bbeca9dd
diff --git a/server/lib/validation.js b/server/lib/validation.js
index 8abb4d78d2..e50e0eab52 100644
--- a/server/lib/validation.js
+++ b/server/lib/validation.js
@@ -828,12 +828,17 @@ export const apiAccessSettingsSchema = z.object({
// (rsync runs shell:false, so this is not shell injection — but `*` would expand
// to `--include=*/***` and defeat the filter chain, and `../foo` would traverse
// out of the snapshot subdir). Restrict to a relative path of safe characters
-// with no traversal or wildcard segments. Mirrored by a service-side guard in
-// restoreSnapshot for non-route callers — see issue #1822.
+// with no wildcard, traversal, or absolute segments. Exported as a predicate so
+// the restoreSnapshot service guard reuses the exact same rule (mirrors
+// isSafeRecordId above) — see issue #1822.
+export const isSafeSubdirFilter = (v) =>
+ typeof v === 'string'
+ && /^[a-z0-9._/-]+$/i.test(v)
+ && !v.split('/').includes('..')
+ && !v.startsWith('/');
+
export const subdirFilterSchema = z.string()
- .regex(/^[a-z0-9._/-]+$/i, 'subdirFilter contains invalid characters')
- .refine((v) => !v.split('/').includes('..'), 'subdirFilter may not contain ".." segments')
- .refine((v) => !v.startsWith('/'), 'subdirFilter must be a relative path');
+ .refine(isSafeSubdirFilter, 'subdirFilter must be a relative path with no wildcard, ".." , or leading "/" segments');
export const restoreRequestSchema = z.object({
snapshotId: z.string().min(1),
diff --git a/server/services/backup.js b/server/services/backup.js
index 9ef89c0fd2..3f5eec874e 100644
--- a/server/services/backup.js
+++ b/server/services/backup.js
@@ -16,6 +16,7 @@ import { checkHealth, getServerMajorVersion } from '../lib/db.js';
import { resolvePgDumpBinary } from '../lib/pgTools.js';
import { getBackendName } from './memoryBackend.js';
import { emitErrorEvent, ServerError } from '../lib/errorHandler.js';
+import { isSafeSubdirFilter } from '../lib/validation.js';
import { getIo } from './socket.js';
// Module-level state
@@ -468,10 +469,9 @@ export async function restoreSnapshot(destPath, snapshotId, { dryRun = true, sub
// Defense-in-depth for non-route callers (the route already validates via
// subdirFilterSchema). subdirFilter is interpolated into an rsync include arg,
// so a `*` would override the filter chain (restoring everything) and `..`
- // would traverse out of the snapshot subdir — see issue #1822.
- if (subdirFilter != null && (!/^[a-z0-9._/-]+$/i.test(subdirFilter)
- || subdirFilter.split('/').includes('..')
- || subdirFilter.startsWith('/'))) {
+ // would traverse out of the snapshot subdir. Reuse the same predicate the
+ // schema does so the two can't drift — see issue #1822.
+ if (subdirFilter != null && !isSafeSubdirFilter(subdirFilter)) {
throw new Error(`Invalid subdirFilter: ${subdirFilter}`);
}
diff --git a/server/services/dataManager.js b/server/services/dataManager.js
index 858d639a48..66ea3fecaa 100644
--- a/server/services/dataManager.js
+++ b/server/services/dataManager.js
@@ -212,10 +212,15 @@ export async function getBackups() {
// 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.includes('..')) throw new Error('Invalid filename');
+ if (!SAFE_FILENAME.test(filename) || filename === '.' || filename === '..') {
+ throw new Error('Invalid filename');
+ }
const backupDir = join(DATA_DIR, 'backup');
const fullPath = join(backupDir, filename);
if (!fullPath.startsWith(backupDir)) throw new Error('Path traversal not allowed');
diff --git a/server/services/dataManager.test.js b/server/services/dataManager.test.js
index cbaa29c971..13aca7e802 100644
--- a/server/services/dataManager.test.js
+++ b/server/services/dataManager.test.js
@@ -37,6 +37,12 @@ describe('deleteBackup filename validation (#1822)', () => {
expect(rm).not.toHaveBeenCalled();
});
+ it('rejects the bare "." and ".." entries (which resolve to the backup dir / its parent)', async () => {
+ await expect(deleteBackup('.')).rejects.toThrow('Invalid filename');
+ await expect(deleteBackup('..')).rejects.toThrow('Invalid filename');
+ expect(rm).not.toHaveBeenCalled();
+ });
+
it('rejects path separators and other unsafe characters', async () => {
await expect(deleteBackup('sub/dir.tar.gz')).rejects.toThrow('Invalid filename');
await expect(deleteBackup('name with spaces.tar.gz')).rejects.toThrow('Invalid filename');