Skip to content

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

fix: resolves broken local storage path for remote machines

towfiqi221e571cb88f
diff --git a/backend/__tests__/utils/sanitizeStoragePath.test.ts b/backend/__tests__/utils/sanitizeStoragePath.test.ts
index 463c5c4..08999ea 100644
--- a/backend/__tests__/utils/sanitizeStoragePath.test.ts
+++ b/backend/__tests__/utils/sanitizeStoragePath.test.ts
@@ -200,4 +200,57 @@ describe('sanitizeStoragePath', () => {
}
});
});
+
+ describe('cross-platform targetOS parameter', () => {
+ it('should preserve Linux absolute paths when targetOS is linux', () => {
+ const result = sanitizeStoragePath('/var/home/backups', 'local', 'linux');
+ expect(result).toBe('/var/home/backups');
+ expect(result.startsWith('/')).toBe(true);
+ });
+
+ it('should not prepend a Windows drive letter when targetOS is linux', () => {
+ const result = sanitizeStoragePath('/var/lib/data', 'local', 'linux');
+ expect(result).not.toMatch(/^[A-Z]:/i);
+ expect(result).toBe('/var/lib/data');
+ });
+
+ it('should handle targetOS darwin as posix paths', () => {
+ const result = sanitizeStoragePath('/Users/admin/backups', 'local', 'darwin');
+ expect(result).toBe('/Users/admin/backups');
+ expect(result.startsWith('/')).toBe(true);
+ });
+
+ it('should handle targetOS windows with Windows-style paths', () => {
+ const result = sanitizeStoragePath('C:\\Users\\admin\\backups', 'local', 'windows');
+ expect(result).toContain('Users');
+ expect(result).toContain('admin');
+ });
+
+ it('should handle targetOS win32 as an alias for windows', () => {
+ const result = sanitizeStoragePath('C:\\ProgramData\\backups', 'local', 'win32');
+ expect(result).toContain('ProgramData');
+ });
+
+ it('should still reject directory traversal with targetOS set', () => {
+ expect(() => sanitizeStoragePath('../etc/passwd', 'local', 'linux')).toThrow(AppError);
+ expect(() => sanitizeStoragePath('..\\secret', 'local', 'windows')).toThrow(AppError);
+ });
+
+ 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);
+ });
+
+ it('should not affect remote storage types regardless of targetOS', () => {
+ const result = sanitizeStoragePath('/bucket/path/file.txt', 's3', 'linux');
+ expect(result).toBe('bucket/path/file.txt');
+ });
+
+ it('should normalize Linux paths correctly when targetOS is linux', () => {
+ const result = sanitizeStoragePath('/var//home///backups/', 'local', 'linux');
+ expect(result).toBe('/var/home/backups');
+ });
+ });
});
diff --git a/backend/src/utils/sanitizeStoragePath.ts b/backend/src/utils/sanitizeStoragePath.ts
index 1e3b2af..3dbf9b3 100644
--- a/backend/src/utils/sanitizeStoragePath.ts
+++ b/backend/src/utils/sanitizeStoragePath.ts
@@ -6,10 +6,18 @@ import { AppError } from './AppError';
* Sanitizes and formats a storage path based on the storage type and operating system.
* @param storagePath The user-provided storage path string.
* @param storageType The type of storage (e.g., 'local', 's3', 'b2').
+ * @param targetOS Optional OS of the target device (e.g., 'linux', 'windows', 'darwin').
+ * When provided, path operations use the target OS conventions instead of the server's OS.
+ * This prevents cross-platform issues such as a Windows server prepending a drive letter
+ * to a Linux absolute path like `/var/home/xxx` → `F:/var/home/xxx`.
* @returns A sanitized, correctly formatted path string.
* @throws {AppError} Throws a 400 Bad Request error if the path is invalid or contains malicious sequences.
*/
-export function sanitizeStoragePath(storagePath: string, storageType: string): string {
+export function sanitizeStoragePath(
+ storagePath: string,
+ storageType: string,
+ targetOS?: string
+): string {
if (typeof storagePath !== 'string') {
// Handle cases where the input might not be a string
return '';
@@ -17,8 +25,15 @@ export function sanitizeStoragePath(storagePath: string, storageType: string): s
const trimmedPath = storagePath.trim();
+ // Determine path module based on target OS when provided,
+ // otherwise fall back to the server's native path module.
+ const isTargetWindows = targetOS
+ ? targetOS === 'windows' || targetOS === 'win32'
+ : os.platform() === 'win32';
+ const pathModule = isTargetWindows ? path.win32 : path.posix;
+
// Normalize the path to resolve segments like '.', '..', and multiple slashes.
- const normalizedPath = path.normalize(trimmedPath);
+ const normalizedPath = pathModule.normalize(trimmedPath);
// **Security Check:** After normalization, if ".." still exists, it's a sign of a
if (normalizedPath.includes('..')) {
@@ -27,11 +42,11 @@ export function sanitizeStoragePath(storagePath: string, storageType: string): s
if (storageType === 'local') {
// For local storage, we need a fully qualified absolute path.
- const resolvedPath = path.resolve(normalizedPath);
+ // 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);
- // On non-Windows systems, we do an extra check to ensure it's a valid absolute path.
- if (os.platform() !== 'win32' && !path.isAbsolute(resolvedPath)) {
- // This case is unlikely after path.resolve, but it's a good defense-in-depth check.
+ if (!isTargetWindows && !pathModule.isAbsolute(resolvedPath)) {
throw new AppError(
400,
'Invalid path: An absolute path is required for local storage on this operating system.'