Skip to content

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

11.7 KB
Raw Blame
import * as os from 'os';
import * as path from 'path';
import { sanitizeStoragePath } from '../../src/utils/sanitizeStoragePath';
import { AppError } from '../../src/utils/AppError';

describe('sanitizeStoragePath', () => {
	describe('input validation', () => {
		it('should return empty string for non-string input', () => {
			expect(sanitizeStoragePath(null as any, 'local')).toBe('');
			expect(sanitizeStoragePath(undefined as any, 'local')).toBe('');
			expect(sanitizeStoragePath(123 as any, 'local')).toBe('');
			expect(sanitizeStoragePath({} as any, 'local')).toBe('');
		});

		it('should trim whitespace from input', () => {
			const result = sanitizeStoragePath('  /some/path  ', 's3');
			expect(result).toBe('some/path');
		});

		it('should handle empty string input', () => {
			expect(() => sanitizeStoragePath('', 'local')).toThrow(AppError);
			expect(sanitizeStoragePath('', 's3')).toBe('');
		});
	});

	describe('security checks', () => {
		it('should reject directory traversal with ".." sequences', () => {
			expect(() => sanitizeStoragePath('../etc/passwd', 'local')).toThrow(AppError);
			expect(() => sanitizeStoragePath('../etc/passwd', 'local')).toThrow(
				'Directory traversal sequences'
			);
		});

		it('should reject multiple ".." sequences', () => {
			expect(() => sanitizeStoragePath('../../secret', 'local')).toThrow(AppError);
			expect(() => sanitizeStoragePath('../../../etc', 's3')).toThrow(AppError);
		});

		it('should reject ".." after normalization', () => {
			expect(() => sanitizeStoragePath('./some/../../other', 'local')).toThrow(AppError);
		});
	});

	describe('local storage type', () => {
		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 reject relative paths starting with dot for local storage', () => {
			expect(() => sanitizeStoragePath('./uploads', 'local')).toThrow(AppError);
		});

		it('should handle already absolute paths', () => {
			const absolutePath = os.platform() === 'win32' ? 'C:\\uploads\\files' : '/uploads/files';
			const result = sanitizeStoragePath(absolutePath, 'local');
			expect(path.isAbsolute(result)).toBe(true);
		});

		it('should normalize path separators', () => {
			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 absolutePath = os.platform() === 'win32' ? 'C:\\uploads\\.\\files' : '/uploads/./files';
			const result = sanitizeStoragePath(absolutePath, 'local');
			expect(path.isAbsolute(result)).toBe(true);
			expect(result).not.toContain('/./');
		});

		it('should handle Windows-style paths on Windows', () => {
			if (os.platform() === 'win32') {
				const result = sanitizeStoragePath('C:\\uploads\\files', 'local');
				expect(path.isAbsolute(result)).toBe(true);
				expect(result).toContain('uploads');
			}
		});

		it('should handle Unix-style paths on Unix', () => {
			if (os.platform() !== 'win32') {
				const result = sanitizeStoragePath('/var/uploads/files', 'local');
				expect(path.isAbsolute(result)).toBe(true);
				expect(result).toBe('/var/uploads/files');
			}
		});
	});

	describe('remote storage types (s3, b2, etc.)', () => {
		const remoteStorageTypes = ['s3', 'b2', 'azure', 'gcs'];

		remoteStorageTypes.forEach(storageType => {
			describe(`${storageType} storage`, () => {
				it('should remove leading slash from path', () => {
					const result = sanitizeStoragePath('/bucket/path/file.txt', storageType);
					expect(result).toBe('bucket/path/file.txt');
					expect(result.startsWith('/')).toBe(false);
				});

				it('should remove trailing slash from path', () => {
					const result = sanitizeStoragePath('bucket/path/', storageType);
					expect(result).toBe('bucket/path');
				});

				it('should not remove trailing slash if path is single character', () => {
					const result = sanitizeStoragePath('/', storageType);
					expect(result).toBe('');
				});

				it('should convert backslashes to forward slashes', () => {
					const result = sanitizeStoragePath('bucket\\path\\file.txt', storageType);
					expect(result).toBe('bucket/path/file.txt');
					expect(result).not.toContain('\\');
				});

				it('should handle mixed slashes', () => {
					const result = sanitizeStoragePath('bucket\\path/subpath\\file.txt', storageType);
					expect(result).toBe('bucket/path/subpath/file.txt');
				});

				it('should handle paths without leading slash', () => {
					const result = sanitizeStoragePath('bucket/path/file.txt', storageType);
					expect(result).toBe('bucket/path/file.txt');
				});

				it('should handle multiple leading slashes', () => {
					// The function only removes one leading slash
					const result = sanitizeStoragePath('///bucket/path/file.txt', storageType);
					expect(result).toBe('//bucket/path/file.txt');
				});

				it('should preserve internal path structure', () => {
					const result = sanitizeStoragePath('/a/b/c/d/e/file.txt', storageType);
					expect(result).toBe('a/b/c/d/e/file.txt');
				});

				it('should handle paths with special characters', () => {
					const result = sanitizeStoragePath('/bucket/my-file_v2.txt', storageType);
					expect(result).toBe('bucket/my-file_v2.txt');
				});
			});
		});
	});

	describe('edge cases', () => {
		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', () => {
			// '///' after trim becomes '///', then removing first '/' gives '//'
			// Then removing trailing '/' gives '/'
			const result = sanitizeStoragePath('///', 's3');
			expect(result).toBe('/');
		});

		it('should handle very long paths', () => {
			const longPath = 'a/'.repeat(100) + 'file.txt';
			const result = sanitizeStoragePath(longPath, 's3');
			// The path doesn't have a trailing slash to remove
			expect(result).toBe(longPath);
		});

		it('should preserve file extensions', () => {
			expect(sanitizeStoragePath('file.txt', 's3')).toBe('file.txt');
			expect(sanitizeStoragePath('file.tar.gz', 's3')).toBe('file.tar.gz');
		});

		it('should handle paths with spaces', () => {
			const result = sanitizeStoragePath('/my folder/my file.txt', 's3');
			expect(result).toBe('my folder/my file.txt');
		});

		it('should handle Unicode characters in path', () => {
			const result = sanitizeStoragePath('/文件/файл.txt', 's3');
			expect(result).toBe('文件/файл.txt');
		});
	});

	describe('AppError details', () => {
		it('should throw AppError with status code 400', () => {
			try {
				sanitizeStoragePath('../etc/passwd', 'local');
				fail('Should have thrown an error');
			} catch (error) {
				expect(error).toBeInstanceOf(AppError);
				expect((error as AppError).statusCode).toBe(400);
			}
		});

		it('should throw AppError with descriptive message', () => {
			try {
				sanitizeStoragePath('../path', 's3');
			} catch (error) {
				expect(error).toBeInstanceOf(AppError);
				expect((error as AppError).message).toContain('Directory traversal');
				expect((error as AppError).message).toContain('..');
			}
		});
	});

	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 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', () => {
			// 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', () => {
			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');
		});
	});

	describe('UNC and rootless Windows paths', () => {
		it('should reject a UNC path with a share', () => {
			expect(() => sanitizeStoragePath('\\\\server\\share', 'local', 'windows')).toThrow(AppError);
			expect(() => sanitizeStoragePath('\\\\server\\share', 'local', 'windows')).toThrow(
				'Network (UNC) destinations are not supported'
			);
		});

		it('should reject a bare UNC host (the silent F:\\host defect)', () => {
			expect(() => sanitizeStoragePath('\\\\server', 'local', 'windows')).toThrow(
				'Network (UNC) destinations are not supported'
			);
		});

		it('should reject a forward-slash UNC path', () => {
			expect(() => sanitizeStoragePath('//server/share', 'local', 'windows')).toThrow(
				'Network (UNC) destinations are not supported'
			);
		});

		it('should reject a rootless path with no drive letter', () => {
			expect(() => sanitizeStoragePath('/mnt/data', 'local', 'windows')).toThrow(AppError);
			expect(() => sanitizeStoragePath('/mnt/data', 'local', 'windows')).toThrow(
				'Include a drive letter'
			);
		});

		it('should reject a single-backslash rootless path', () => {
			expect(() => sanitizeStoragePath('\\folder\\sub', 'local', 'windows')).toThrow(
				'Include a drive letter'
			);
		});

		it('should accept a valid drive-letter path', () => {
			const result = sanitizeStoragePath('C:\\backups\\plan1', 'local', 'windows');
			expect(result).toContain('backups');
			expect(result).toMatch(/^[A-Za-z]:/);
		});

		it('should not reject a UNC-style path on a Linux target', () => {
			// On POSIX, "//host/share" is a legal absolute path, not UNC.
			expect(() => sanitizeStoragePath('//host/share', 'local', 'linux')).not.toThrow();
		});
	});
});