|
9 | 9 |
|
10 | 10 | namespace Microsoft.Agents.AI.Workflows.UnitTests; |
11 | 11 |
|
12 | | -public sealed class FileSystemJsonCheckpointStoreTests |
| 12 | +internal sealed class TempDirectory : IDisposable |
13 | 13 | { |
14 | | - [Fact] |
15 | | - public async Task CreateCheckpointAsync_ShouldPersistIndexToDiskBeforeDisposeAsync() |
| 14 | + public DirectoryInfo DirectoryInfo { get; } |
| 15 | + |
| 16 | + public TempDirectory() |
16 | 17 | { |
17 | | - // Arrange |
18 | | - DirectoryInfo tempDir = new(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())); |
19 | | - FileSystemJsonCheckpointStore? store = null; |
| 18 | + string tempDirPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); |
| 19 | + this.DirectoryInfo = Directory.CreateDirectory(tempDirPath); |
| 20 | + } |
| 21 | + |
| 22 | + public void Dispose() |
| 23 | + { |
| 24 | + this.DisposeInternal(); |
| 25 | + GC.SuppressFinalize(this); |
| 26 | + } |
| 27 | + |
| 28 | + private void DisposeInternal() |
| 29 | + { |
| 30 | + if (this.DirectoryInfo.Exists) |
| 31 | + { |
| 32 | + try |
| 33 | + { |
| 34 | + // Best efforts |
| 35 | + this.DirectoryInfo.Delete(recursive: true); |
| 36 | + } |
| 37 | + catch { } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + ~TempDirectory() |
| 42 | + { |
| 43 | + // Best efforts |
| 44 | + this.DisposeInternal(); |
| 45 | + } |
| 46 | + |
| 47 | + public static implicit operator DirectoryInfo(TempDirectory tempDirectory) => tempDirectory.DirectoryInfo; |
| 48 | + |
| 49 | + public string FullName => this.DirectoryInfo.FullName; |
20 | 50 |
|
21 | | - try |
| 51 | + public bool IsParentOf(FileInfo candidate) |
| 52 | + { |
| 53 | + if (candidate.Directory is null) |
22 | 54 | { |
23 | | - store = new(tempDir); |
24 | | - string runId = Guid.NewGuid().ToString("N"); |
25 | | - JsonElement testData = JsonSerializer.SerializeToElement(new { test = "data" }); |
26 | | - |
27 | | - // Act |
28 | | - CheckpointInfo checkpoint = await store.CreateCheckpointAsync(runId, testData); |
29 | | - |
30 | | - // Assert - Check the file size before disposing to verify data was flushed to disk |
31 | | - // The index.jsonl file is held exclusively by the store, so we check via FileInfo |
32 | | - string indexPath = Path.Combine(tempDir.FullName, "index.jsonl"); |
33 | | - FileInfo indexFile = new(indexPath); |
34 | | - indexFile.Refresh(); |
35 | | - long fileSizeBeforeDispose = indexFile.Length; |
36 | | - |
37 | | - // Data should already be on disk (file size > 0) before we dispose |
38 | | - fileSizeBeforeDispose.Should().BeGreaterThan(0, "index.jsonl should be flushed to disk after CreateCheckpointAsync"); |
39 | | - |
40 | | - // Dispose to release file lock before final verification |
41 | | - store.Dispose(); |
42 | | - store = null; |
43 | | - |
44 | | - string[] lines = File.ReadAllLines(indexPath); |
45 | | - lines.Should().HaveCount(1); |
46 | | - lines[0].Should().Contain(checkpoint.CheckpointId); |
| 55 | + return false; |
47 | 56 | } |
48 | | - finally |
| 57 | + |
| 58 | + if (candidate.Directory.FullName == this.DirectoryInfo.FullName) |
| 59 | + { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + return this.IsParentOf(candidate.Directory); |
| 64 | + } |
| 65 | + |
| 66 | + public bool IsParentOf(DirectoryInfo candidate) |
| 67 | + { |
| 68 | + while (candidate.Parent is not null) |
49 | 69 | { |
50 | | - store?.Dispose(); |
51 | | - if (tempDir.Exists) |
| 70 | + if (candidate.Parent.FullName == this.DirectoryInfo.FullName) |
52 | 71 | { |
53 | | - tempDir.Delete(recursive: true); |
| 72 | + return true; |
54 | 73 | } |
| 74 | + |
| 75 | + candidate = candidate.Parent; |
55 | 76 | } |
| 77 | + |
| 78 | + return false; |
| 79 | + } |
| 80 | +} |
| 81 | +public sealed class FileSystemJsonCheckpointStoreTests |
| 82 | +{ |
| 83 | + public static JsonElement TestData => JsonSerializer.SerializeToElement(new { test = "data" }); |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task CreateCheckpointAsync_ShouldPersistIndexToDiskBeforeDisposeAsync() |
| 87 | + { |
| 88 | + // Arrange |
| 89 | + using TempDirectory tempDirectory = new(); |
| 90 | + using FileSystemJsonCheckpointStore? store = new(tempDirectory); |
| 91 | + |
| 92 | + string runId = Guid.NewGuid().ToString("N"); |
| 93 | + |
| 94 | + // Act |
| 95 | + CheckpointInfo checkpoint = await store.CreateCheckpointAsync(runId, TestData); |
| 96 | + |
| 97 | + // Assert - Check the file size before disposing to verify data was flushed to disk |
| 98 | + // The index.jsonl file is held exclusively by the store, so we check via FileInfo |
| 99 | + string indexPath = Path.Combine(tempDirectory.FullName, "index.jsonl"); |
| 100 | + FileInfo indexFile = new(indexPath); |
| 101 | + indexFile.Refresh(); |
| 102 | + long fileSizeBeforeDispose = indexFile.Length; |
| 103 | + |
| 104 | + // Data should already be on disk (file size > 0) before we dispose |
| 105 | + fileSizeBeforeDispose.Should().BeGreaterThan(0, "index.jsonl should be flushed to disk after CreateCheckpointAsync"); |
| 106 | + |
| 107 | + // Dispose to release file lock before final verification |
| 108 | + store.Dispose(); |
| 109 | + |
| 110 | + string[] lines = File.ReadAllLines(indexPath); |
| 111 | + lines.Should().HaveCount(1); |
| 112 | + lines[0].Should().Contain(checkpoint.CheckpointId); |
| 113 | + } |
| 114 | + |
| 115 | + private async ValueTask Run_EscapeRootFolderTestAsync(string escapingPath) |
| 116 | + { |
| 117 | + // Arrange |
| 118 | + using TempDirectory tempDirectory = new(); |
| 119 | + using FileSystemJsonCheckpointStore store = new(tempDirectory); |
| 120 | + |
| 121 | + string naivePath = Path.Combine(tempDirectory.DirectoryInfo.FullName, escapingPath); |
| 122 | + |
| 123 | + // Check that the naive path is actually outside the temp directory to validate the test is meaningful |
| 124 | + FileInfo naiveCheckpointFile = new(naivePath); |
| 125 | + tempDirectory.IsParentOf(naiveCheckpointFile).Should().BeFalse("The naive path should be outside the root folder to validate that escaping is necessary."); |
| 126 | + |
| 127 | + // Act |
| 128 | + CheckpointInfo checkpointInfo = await store.CreateCheckpointAsync(escapingPath, TestData); |
| 129 | + |
| 130 | + // Assert |
| 131 | + string naivePathWithCheckpointId = Path.Combine(tempDirectory.DirectoryInfo.FullName, $"{escapingPath}_{checkpointInfo.CheckpointId}.json"); |
| 132 | + new FileInfo(naivePathWithCheckpointId).Exists.Should().BeFalse("The naive path should not be used to save a checkpoint file."); |
| 133 | + |
| 134 | + string actualFileName = store.GetFileNameForCheckpoint(escapingPath, checkpointInfo); |
| 135 | + string actualFilePath = Path.Combine(tempDirectory.DirectoryInfo.FullName, actualFileName); |
| 136 | + FileInfo actualFile = new(actualFilePath); |
| 137 | + |
| 138 | + tempDirectory.IsParentOf(actualFile).Should().BeTrue("The actual checkpoint should be saved inside the root folder."); |
| 139 | + actualFile.Exists.Should().BeTrue("The actual path should be used to save a checkpoint file."); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public async Task CreateCheckpointAsync_ShouldNotEscapeRootFolderAsync() |
| 144 | + { |
| 145 | + // The SessionId is used as part of the file name, but if it contains path characters such as /.. it can escape the root folder. |
| 146 | + // Testing that such characters are escaped properly to prevent directory traversal attacks, etc. |
| 147 | + |
| 148 | + await this.Run_EscapeRootFolderTestAsync("../valid_suffix"); |
| 149 | + |
| 150 | +#if !NETFRAMEWORK |
| 151 | + if (OperatingSystem.IsWindows()) |
| 152 | + { |
| 153 | + // Windows allows both \ and / as path separators, so we test both |
| 154 | + await this.Run_EscapeRootFolderTestAsync("..\\valid_suffix"); |
| 155 | + } |
| 156 | +#else |
| 157 | + // .NET Framework is always on Windows |
| 158 | + await this.Run_EscapeRootFolderTestAsync("..\\valid_suffix"); |
| 159 | +#endif |
| 160 | + } |
| 161 | + |
| 162 | + private const string InvalidPathCharsWin32 = "\\/:*?\"<>|"; |
| 163 | + private const string InvalidPathCharsUnix = "/"; |
| 164 | + private const string InvalidPathCharsMacOS = "/:"; |
| 165 | + |
| 166 | + [Theory] |
| 167 | + [InlineData(InvalidPathCharsWin32)] |
| 168 | + [InlineData(InvalidPathCharsUnix)] |
| 169 | + [InlineData(InvalidPathCharsMacOS)] |
| 170 | + public async Task CreateCheckpointAsync_EscapesInvalidCharsAsync(string invalidChars) |
| 171 | + { |
| 172 | + // Arrange |
| 173 | + using TempDirectory tempDirectory = new(); |
| 174 | + using FileSystemJsonCheckpointStore store = new(tempDirectory); |
| 175 | + |
| 176 | + string runId = $"prefix_{invalidChars}_suffix"; |
| 177 | + |
| 178 | + Func<Task> createCheckpointAction = async () => await store.CreateCheckpointAsync(runId, TestData); |
| 179 | + await createCheckpointAction.Should().NotThrowAsync(); |
56 | 180 | } |
57 | 181 | } |
0 commit comments