Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

chore: throw error on invalid storage path

towfiqi5f0a487443e8
diff --git a/backend/__tests__/utils/sanitizeStoragePath.test.ts b/backend/__tests__/utils/sanitizeStoragePath.test.ts
index 08999ea..23d1d68 100644
--- a/backend/__tests__/utils/sanitizeStoragePath.test.ts
+++ b/backend/__tests__/utils/sanitizeStoragePath.test.ts
@@ -18,7 +18,7 @@ describe('sanitizeStoragePath', () => {
});
it('should handle empty string input', () => {
- expect(sanitizeStoragePath('', 'local')).toBe(path.resolve(''));
+ expect(() => sanitizeStoragePath('', 'local')).toThrow(AppError);
expect(sanitizeStoragePath('', 's3')).toBe('');
});
});
@@ -42,15 +42,15 @@ describe('sanitizeStoragePath', () => {
});
describe('local storage type', () => {
- it('should return absolute path for local storage', () => {
- const result = sanitizeStoragePath('uploads/files', 'local');
- expect(path.isAbsolute(result)).toBe(true);
+ it('should reject relative paths for local storage', () => {
+ expect(() => sanitizeStoragePath('uploads/files', 'local')).toThrow(AppError);
+ expect(() => sanitizeStoragePath('uploads/files', 'local')).toThrow(
+ 'absolute path is required'
+ );
});
- it('should resolve relative paths to absolute paths', () => {
- const result = sanitizeStoragePath('./uploads', 'local');
- expect(path.isAbsolute(result)).toBe(true);
- expect(result).toContain('uploads');
+ it('should reject relative paths starting with dot for local storage', () => {
+ expect(() => sanitizeStoragePath('./uploads', 'local')).toThrow(AppError);
});
it('should handle already absolute paths', () => {
@@ -60,12 +60,17 @@ describe('sanitizeStoragePath', () => {
});
it('should normalize path separators', () => {
- const result = sanitizeStoragePath('uploads//files///documents', 'local');
+ const absolutePath =
+ os.platform() === 'win32'
+ ? 'C:\\uploads\\files\\\\documents'
+ : '/uploads//files///documents';
+ const result = sanitizeStoragePath(absolutePath, 'local');
expect(result).not.toContain('//');
});
it('should resolve "." in path', () => {
- const result = sanitizeStoragePath('./uploads/./files', 'local');
+ const absolutePath = os.platform() === 'win32' ? 'C:\\uploads\\.\\files' : '/uploads/./files';
+ const result = sanitizeStoragePath(absolutePath, 'local');
expect(path.isAbsolute(result)).toBe(true);
expect(result).not.toContain('/./');
});
@@ -144,9 +149,8 @@ describe('sanitizeStoragePath', () => {
});
describe('edge cases', () => {
- it('should handle path with only dots (single dot)', () => {
- const result = sanitizeStoragePath('.', 'local');
- expect(path.isAbsolute(result)).toBe(true);
+ it('should reject path with only dots (single dot) for local storage', () => {
+ expect(() => sanitizeStoragePath('.', 'local')).toThrow(AppError);
});
it('should handle path with only slashes for remote storage', () => {
@@ -236,11 +240,21 @@ describe('sanitizeStoragePath', () => {
expect(() => sanitizeStoragePath('..\\secret', 'local', 'windows')).toThrow(AppError);
});
+ it('should reject relative paths for local storage on remote Linux device', () => {
+ // A relative Linux path like "home/user/backup" should not be resolved
+ // against the server's CWD (which could be on Windows)
+ expect(() => sanitizeStoragePath('home/towfiq/pluton_sync_test', 'local', 'linux')).toThrow(
+ AppError
+ );
+ expect(() => sanitizeStoragePath('home/towfiq/pluton_sync_test', 'local', 'linux')).toThrow(
+ 'absolute path is required'
+ );
+ });
+
it('should fall back to server OS behavior when targetOS is undefined', () => {
- // Should behave identically to calling without the parameter
- const withoutParam = sanitizeStoragePath('uploads/files', 'local');
- const withUndefined = sanitizeStoragePath('uploads/files', 'local', undefined);
- expect(withoutParam).toBe(withUndefined);
+ // Both should throw since relative paths are not allowed for local storage
+ expect(() => sanitizeStoragePath('uploads/files', 'local')).toThrow(AppError);
+ expect(() => sanitizeStoragePath('uploads/files', 'local', undefined)).toThrow(AppError);
});
it('should not affect remote storage types regardless of targetOS', () => {
diff --git a/backend/src/services/PlanService.ts b/backend/src/services/PlanService.ts
index 87f60c6..b2c5307 100644
--- a/backend/src/services/PlanService.ts
+++ b/backend/src/services/PlanService.ts
@@ -103,10 +103,21 @@ export class PlanService {
if (!device) {
throw new NotFoundError('Source device not found');
}
- const isRemote = sourceId !== 'main';
- const targetOS = isRemote ? (device.os ?? undefined) : undefined;
- const theStoragePath = sanitizeStoragePath(storagePath, planStorage.type, targetOS);
+ let theStoragePath;
+ if (storagePath) {
+ try {
+ const isRemote = sourceId !== 'main';
+ const targetOS = isRemote ? (device.os ?? undefined) : undefined;
+ theStoragePath = sanitizeStoragePath(storagePath, planStorage.type, targetOS);
+ } catch (error) {
+ if (error instanceof AppError) {
+ throw error;
+ }
+ throw new AppError(400, 'Invalid storage path');
+ }
+ }
+
let newPlanData: NewPlan = {
id: planId,
title: title.trim(),
diff --git a/backend/src/utils/sanitizeStoragePath.ts b/backend/src/utils/sanitizeStoragePath.ts
index 3dbf9b3..8950b74 100644
--- a/backend/src/utils/sanitizeStoragePath.ts
+++ b/backend/src/utils/sanitizeStoragePath.ts
@@ -37,21 +37,28 @@ export function sanitizeStoragePath(
// **Security Check:** After normalization, if ".." still exists, it's a sign of a
if (normalizedPath.includes('..')) {
- throw new AppError(400, 'Invalid path: Directory traversal sequences ("..") are not allowed.');
+ throw new AppError(
+ 400,
+ 'Invalid Storage Destination path: Directory traversal sequences ("..") are not allowed.'
+ );
}
if (storageType === 'local') {
// For local storage, we need a fully qualified absolute path.
- // When targeting a remote OS, use the target path module to avoid
- // the server's OS mangling the path (e.g., adding a Windows drive letter to a Linux path).
- const resolvedPath = pathModule.resolve(normalizedPath);
-
- if (!isTargetWindows && !pathModule.isAbsolute(resolvedPath)) {
+ // Check *before* resolve() because resolve() always returns an absolute path
+ // by prepending the server's CWD, which mangles paths for remote devices
+ // (e.g., a Linux path "home/user" resolved on a Windows server becomes
+ // "/JS/apps/.../backend/home/user" instead of "/home/user").
+ if (!pathModule.isAbsolute(normalizedPath)) {
throw new AppError(
400,
- 'Invalid path: An absolute path is required for local storage on this operating system.'
+ `Invalid Storage Destination path: An absolute path is required for local storage (e.g., "${isTargetWindows ? 'C:\\' : '/'}home/user/backups").`
);
}
+
+ // When targeting a remote OS, use the target path module to avoid
+ // the server's OS mangling the path (e.g., adding a Windows drive letter to a Linux path).
+ const resolvedPath = pathModule.resolve(normalizedPath);
return resolvedPath;
} else {
// For all remote/cloud storage types (s3, b2, etc.), the path must be relative