//! Path Security Tests //! //! Comprehensive tests for path validation and security. //! Tests path traversal attacks, symlink handling, and directory restrictions. const std = @import("std"); const testing = std.testing; const fs = std.fs; const path_security = @import("../../src/files/path_security.zig"); const PathValidator = path_security.PathValidator; const PathSecurityError = path_security.PathSecurityError; test "path traversal - simple double dot" { const allocator = testing.allocator; // Create a test directory structure const test_dir = "test_path_security_simple"; try fs.cwd().makePath(test_dir); defer fs.cwd().deleteTree(test_dir) catch {}; // Create a test file const test_file_path = test_dir ++ "/test.txt"; const file = try fs.cwd().createFile(test_file_path, .{}); file.close(); // Test path traversal with ".." const traversal_path = test_dir ++ "/../etc/passwd"; // Quick validation should reject it try testing.expect(!PathValidator.isPathSafe(traversal_path)); // Full validation should also reject it const validator = PathValidator.init(&[_][]const u8{test_dir}); const result = validator.validatePath(traversal_path, allocator); try testing.expectError(PathSecurityError.PathTraversal, result); } test "path traversal - double dot in middle" { const allocator = testing.allocator; // Test path with ".." in the middle const bad_path = "foo/../bar"; // Quick validation should reject it try testing.expect(!PathValidator.isPathSafe(bad_path)); // Create validator with allowed directories const test_dir = "test_path_security_middle"; try fs.cwd().makePath(test_dir); defer fs.cwd().deleteTree(test_dir) catch {}; const validator = PathValidator.init(&[_][]const u8{test_dir}); // Full validation should reject paths with ".." const result = validator.validatePath(bad_path, allocator); try testing.expectError(PathSecurityError.PathTraversal, result); } test "path traversal - multiple double dots" { // Test path with multiple ".." sequences const bad_paths = [_][]const u8{ "../../etc/passwd", "../../../root/.ssh/id_rsa", "foo/../../bar", "./../../etc/shadow", }; for (bad_paths) |bad_path| { try testing.expect(!PathValidator.isPathSafe(bad_path)); } } test "path traversal - double slashes" { // Test paths with double slashes (could bypass some filters) const bad_paths = [_][]const u8{ "//etc/passwd", "foo//bar", "test///file.txt", }; for (bad_paths) |bad_path| { try testing.expect(!PathValidator.isPathSafe(bad_path)); } } test "path traversal - null byte injection" { // Test paths with null bytes (path injection attack) const bad_path = "test\x00/../../etc/passwd"; try testing.expect(!PathValidator.isPathSafe(bad_path)); } test "symlink rejection - direct symlink" { const allocator = testing.allocator; // Create test directory and file const test_dir = "test_symlink_direct"; try fs.cwd().makePath(test_dir); defer fs.cwd().deleteTree(test_dir) catch {}; const real_file = test_dir ++ "/real.txt"; const file = try fs.cwd().createFile(real_file, .{}); file.close(); // Create symlink const symlink_path = test_dir ++ "/link.txt"; fs.cwd().symLink(real_file, symlink_path, .{}) catch |err| { // Skip test if symlinks not supported if (err == error.AccessDenied or err == error.Unexpected) { return error.SkipZigTest; } return err; }; defer fs.cwd().deleteFile(symlink_path) catch {}; // Validator should reject symlinks by default const validator = PathValidator.init(&[_][]const u8{test_dir}); const result = validator.validatePath(symlink_path, allocator); try testing.expectError(PathSecurityError.SymlinkNotAllowed, result); } test "symlink allowed with flag" { const allocator = testing.allocator; // Create test directory and file const test_dir = "test_symlink_allowed"; try fs.cwd().makePath(test_dir); defer fs.cwd().deleteTree(test_dir) catch {}; const real_file = test_dir ++ "/real.txt"; const file = try fs.cwd().createFile(real_file, .{}); file.close(); // Get absolute path for real file const abs_real_file = try fs.cwd().realpathAlloc(allocator, real_file); defer allocator.free(abs_real_file); // Create symlink const symlink_path = test_dir ++ "/link.txt"; fs.cwd().symLink(real_file, symlink_path, .{}) catch |err| { // Skip test if symlinks not supported if (err == error.AccessDenied or err == error.Unexpected) { return error.SkipZigTest; } return err; }; defer fs.cwd().deleteFile(symlink_path) catch {}; // Get absolute test dir const abs_test_dir = try fs.cwd().realpathAlloc(allocator, test_dir); defer allocator.free(abs_test_dir); // Validator with symlinks allowed should accept it const validator = PathValidator.initWithSymlinks(&[_][]const u8{abs_test_dir}); const result = try validator.validatePath(symlink_path, allocator); defer allocator.free(result); // Result should be the resolved real path try testing.expect(std.mem.eql(u8, result, abs_real_file)); } test "allowed directory enforcement - outside directory rejected" { const allocator = testing.allocator; // Create two separate test directories const allowed_dir = "test_allowed_dir"; const forbidden_dir = "test_forbidden_dir"; try fs.cwd().makePath(allowed_dir); try fs.cwd().makePath(forbidden_dir); defer fs.cwd().deleteTree(allowed_dir) catch {}; defer fs.cwd().deleteTree(forbidden_dir) catch {}; // Create files in both const allowed_file = allowed_dir ++ "/allowed.txt"; const forbidden_file = forbidden_dir ++ "/forbidden.txt"; var f1 = try fs.cwd().createFile(allowed_file, .{}); f1.close(); var f2 = try fs.cwd().createFile(forbidden_file, .{}); f2.close(); // Get absolute paths const abs_allowed_dir = try fs.cwd().realpathAlloc(allocator, allowed_dir); defer allocator.free(abs_allowed_dir); // Validator with only allowed_dir const validator = PathValidator.init(&[_][]const u8{abs_allowed_dir}); // Allowed file should pass const allowed_result = try validator.validatePath(allowed_file, allocator); defer allocator.free(allowed_result); try testing.expect(allowed_result.len > 0); // Forbidden file should fail const forbidden_result = validator.validatePath(forbidden_file, allocator); try testing.expectError(PathSecurityError.PathOutsideAllowedDirectory, forbidden_result); } test "allowed directory enforcement - multiple allowed directories" { const allocator = testing.allocator; // Create three test directories const dir1 = "test_multi_dir1"; const dir2 = "test_multi_dir2"; const dir3 = "test_multi_dir3"; try fs.cwd().makePath(dir1); try fs.cwd().makePath(dir2); try fs.cwd().makePath(dir3); defer fs.cwd().deleteTree(dir1) catch {}; defer fs.cwd().deleteTree(dir2) catch {}; defer fs.cwd().deleteTree(dir3) catch {}; // Create files const file1 = dir1 ++ "/file1.txt"; const file2 = dir2 ++ "/file2.txt"; const file3 = dir3 ++ "/file3.txt"; var f1 = try fs.cwd().createFile(file1, .{}); f1.close(); var f2 = try fs.cwd().createFile(file2, .{}); f2.close(); var f3 = try fs.cwd().createFile(file3, .{}); f3.close(); // Get absolute paths const abs_dir1 = try fs.cwd().realpathAlloc(allocator, dir1); defer allocator.free(abs_dir1); const abs_dir2 = try fs.cwd().realpathAlloc(allocator, dir2); defer allocator.free(abs_dir2); // Validator with dir1 and dir2 allowed const allowed_dirs = [_][]const u8{ abs_dir1, abs_dir2 }; const validator = PathValidator.init(&allowed_dirs); // Files in dir1 and dir2 should pass const result1 = try validator.validatePath(file1, allocator); defer allocator.free(result1); const result2 = try validator.validatePath(file2, allocator); defer allocator.free(result2); // File in dir3 should fail const result3 = validator.validatePath(file3, allocator); try testing.expectError(PathSecurityError.PathOutsideAllowedDirectory, result3); } test "absolute path resolution - relative to absolute" { const allocator = testing.allocator; // Create test directory and file const test_dir = "test_absolute_resolution"; try fs.cwd().makePath(test_dir); defer fs.cwd().deleteTree(test_dir) catch {}; const test_file = test_dir ++ "/test.txt"; const file = try fs.cwd().createFile(test_file, .{}); file.close(); // Get absolute path for test directory const abs_test_dir = try fs.cwd().realpathAlloc(allocator, test_dir); defer allocator.free(abs_test_dir); // Validator const validator = PathValidator.init(&[_][]const u8{abs_test_dir}); // Validate relative path - should return absolute path const result = try validator.validatePath(test_file, allocator); defer allocator.free(result); // Result should be absolute (start with /) try testing.expect(result.len > 0); try testing.expect(result[0] == std.fs.path.sep); // Should end with the filename try testing.expect(std.mem.endsWith(u8, result, "test.txt")); } test "safe path validation - valid paths accepted" { // Test valid paths that should be accepted const good_paths = [_][]const u8{ "test.txt", "foo/bar/baz.txt", "valid-file_name.123.txt", "./relative/path.txt", }; for (good_paths) |good_path| { try testing.expect(PathValidator.isPathSafe(good_path)); } } test "safe path validation - sensitive directories rejected" { // Test that sensitive system directories are rejected const sensitive_paths = [_][]const u8{ "/etc/passwd", "/etc/shadow", "/root/.ssh/id_rsa", "/var/log/auth.log", "/bin/bash", "/sbin/init", "/System/Library/Extensions", // macOS "/Library/LaunchDaemons", // macOS "/private/etc/master.passwd", // macOS }; for (sensitive_paths) |sensitive_path| { try testing.expect(!PathValidator.isPathSafe(sensitive_path)); } } test "path too long rejected" { const allocator = testing.allocator; // Create a path that exceeds MAX_PATH_LEN (4096) const MAX_PATH_LEN = 4096; const long_path = try allocator.alloc(u8, MAX_PATH_LEN + 1); defer allocator.free(long_path); // Fill with valid characters for (long_path) |*c| { c.* = 'a'; } // Should be rejected by quick validation try testing.expect(!PathValidator.isPathSafe(long_path)); } test "empty path rejected" { // Empty paths should be rejected try testing.expect(!PathValidator.isPathSafe("")); } test "sanitize filename - removes dangerous characters" { const allocator = testing.allocator; const test_cases = [_]struct { input: []const u8, expected: []const u8, }{ .{ .input = "hello/world.txt", .expected = "helloworld.txt" }, .{ .input = "../../../etc/passwd", .expected = "etcpasswd" }, .{ .input = "test file.txt", .expected = "test_file.txt" }, .{ .input = "file@#$%.txt", .expected = "file.txt" }, .{ .input = "normal-file_123.txt", .expected = "normal-file_123.txt" }, }; for (test_cases) |case| { const result = try path_security.sanitizeFilename(case.input, allocator); defer allocator.free(result); try testing.expectEqualStrings(case.expected, result); } } test "validateReadable - existing file passes" { const allocator = testing.allocator; // Create test file const test_dir = "test_readable"; try fs.cwd().makePath(test_dir); defer fs.cwd().deleteTree(test_dir) catch {}; const test_file = test_dir ++ "/readable.txt"; const file = try fs.cwd().createFile(test_file, .{}); file.close(); // Get absolute path const abs_test_dir = try fs.cwd().realpathAlloc(allocator, test_dir); defer allocator.free(abs_test_dir); // Validator const validator = PathValidator.init(&[_][]const u8{abs_test_dir}); // Should validate successfully const result = try validator.validateReadable(test_file, allocator); defer allocator.free(result); try testing.expect(result.len > 0); } test "validateReadable - nonexistent file rejected" { const allocator = testing.allocator; const test_dir = "test_nonexistent"; try fs.cwd().makePath(test_dir); defer fs.cwd().deleteTree(test_dir) catch {}; const abs_test_dir = try fs.cwd().realpathAlloc(allocator, test_dir); defer allocator.free(abs_test_dir); const validator = PathValidator.init(&[_][]const u8{abs_test_dir}); const nonexistent = test_dir ++ "/does_not_exist.txt"; const result = validator.validateReadable(nonexistent, allocator); try testing.expectError(error.FileNotFound, result); } test "validateWritable - new file in existing directory" { const allocator = testing.allocator; const test_dir = "test_writable"; try fs.cwd().makePath(test_dir); defer fs.cwd().deleteTree(test_dir) catch {}; const abs_test_dir = try fs.cwd().realpathAlloc(allocator, test_dir); defer allocator.free(abs_test_dir); const validator = PathValidator.init(&[_][]const u8{abs_test_dir}); const new_file = test_dir ++ "/new_file.txt"; const result = try validator.validateWritable(new_file, allocator); defer allocator.free(result); try testing.expect(result.len > 0); try testing.expect(std.mem.endsWith(u8, result, "new_file.txt")); } test "joinSafe - prevents traversal via join" { const allocator = testing.allocator; const test_dir = "test_join_safe"; try fs.cwd().makePath(test_dir); defer fs.cwd().deleteTree(test_dir) catch {}; // Create a file const safe_file = test_dir ++ "/safe.txt"; const file = try fs.cwd().createFile(safe_file, .{}); file.close(); const abs_test_dir = try fs.cwd().realpathAlloc(allocator, test_dir); defer allocator.free(abs_test_dir); const validator = PathValidator.init(&[_][]const u8{abs_test_dir}); // Try to join with traversal path const result = validator.joinSafe(test_dir, "../etc/passwd", allocator); try testing.expectError(PathSecurityError.PathTraversal, result); // Safe join should work const safe_result = try validator.joinSafe(test_dir, "safe.txt", allocator); defer allocator.free(safe_result); try testing.expect(safe_result.len > 0); } test "createDefaultValidator - uses cwd" { const allocator = testing.allocator; const validator = try path_security.createDefaultValidator(allocator); defer allocator.free(validator.allowed_directories[0]); defer allocator.free(validator.allowed_directories); // Should have one allowed directory (cwd) try testing.expectEqual(@as(usize, 1), validator.allowed_directories.len); try testing.expect(validator.allowed_directories[0].len > 0); } test "real-world attack vectors" { // Test various real-world attack patterns const attack_vectors = [_][]const u8{ "../../../../../../etc/passwd", "....//....//....//etc/passwd", "/etc/passwd%00.txt", "./../../etc/shadow", "legitimate_file/../../../etc/passwd", "/var/www/../../root/.ssh/id_rsa", "uploads/../../../app/config/database.yml", }; for (attack_vectors) |vector| { // All attack vectors should be rejected by quick validation const is_safe = PathValidator.isPathSafe(vector); try testing.expect(!is_safe); } } test "case sensitivity - directory prefix matching" { const allocator = testing.allocator; // Create two directories with similar names const test_dir1 = "test_case_abc"; const test_dir2 = "test_case_abcd"; try fs.cwd().makePath(test_dir1); try fs.cwd().makePath(test_dir2); defer fs.cwd().deleteTree(test_dir1) catch {}; defer fs.cwd().deleteTree(test_dir2) catch {}; // Create files const file1 = test_dir1 ++ "/file.txt"; const file2 = test_dir2 ++ "/file.txt"; var f1 = try fs.cwd().createFile(file1, .{}); f1.close(); var f2 = try fs.cwd().createFile(file2, .{}); f2.close(); const abs_dir1 = try fs.cwd().realpathAlloc(allocator, test_dir1); defer allocator.free(abs_dir1); // Validator with only dir1 allowed const validator = PathValidator.init(&[_][]const u8{abs_dir1}); // file1 should pass const result1 = try validator.validatePath(file1, allocator); defer allocator.free(result1); // file2 should fail (even though dir2 name starts with dir1 name) const result2 = validator.validatePath(file2, allocator); try testing.expectError(PathSecurityError.PathOutsideAllowedDirectory, result2); }