| | | 1 | | using System.Buffers; |
| | | 2 | | using System.IO.Compression; |
| | | 3 | | using Elsa.Common; |
| | | 4 | | using Elsa.Http.Options; |
| | | 5 | | using FluentStorage.Blobs; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Http.Services; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides a helper service for zipping downloadable content. |
| | | 13 | | /// </summary> |
| | | 14 | | internal class ZipManager |
| | | 15 | | { |
| | | 16 | | private const int MaxDownloadCorrelationIdLength = 128; |
| | 1 | 17 | | private static readonly SearchValues<char> DownloadCorrelationIdCharacters = SearchValues.Create("abcdefghijklmnopqr |
| | | 18 | | private readonly ISystemClock _clock; |
| | | 19 | | private readonly IFileCacheStorageProvider _fileCacheStorageProvider; |
| | | 20 | | private readonly IOptions<HttpFileCacheOptions> _fileCacheOptions; |
| | | 21 | | private readonly ILogger<ZipManager> _logger; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="ZipManager"/> class. |
| | | 25 | | /// </summary> |
| | 15 | 26 | | public ZipManager(ISystemClock clock, IFileCacheStorageProvider fileCacheStorageProvider, IOptions<HttpFileCacheOpti |
| | | 27 | | { |
| | 15 | 28 | | _clock = clock; |
| | 15 | 29 | | _fileCacheStorageProvider = fileCacheStorageProvider; |
| | 15 | 30 | | _fileCacheOptions = fileCacheOptions; |
| | 15 | 31 | | _logger = logger; |
| | 15 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task<(Blob, Stream, Action)> CreateAsync( |
| | | 35 | | ICollection<Func<ValueTask<Downloadable>>> downloadables, |
| | | 36 | | bool cache, |
| | | 37 | | string? downloadCorrelationId, |
| | | 38 | | string? downloadAsFilename = default, |
| | | 39 | | string? contentType = default, |
| | | 40 | | CancellationToken cancellationToken = default) |
| | | 41 | | { |
| | | 42 | | // Create a temporary file. |
| | 5 | 43 | | var tempFilePath = GetTempFilePath(); |
| | | 44 | | |
| | | 45 | | // Create a zip archive from the downloadables. |
| | 5 | 46 | | await CreateZipArchiveAsync(tempFilePath, downloadables, cancellationToken); |
| | | 47 | | |
| | | 48 | | // Create a blob with metadata for resuming the download. |
| | 5 | 49 | | var zipBlob = CreateBlob(tempFilePath, downloadAsFilename, contentType); |
| | | 50 | | |
| | | 51 | | // If resumable downloads are enabled, cache the file. |
| | 5 | 52 | | if (cache && !string.IsNullOrWhiteSpace(downloadCorrelationId)) |
| | 5 | 53 | | await CreateCachedZipBlobAsync(tempFilePath, downloadCorrelationId, downloadAsFilename, contentType, cancell |
| | | 54 | | |
| | 5 | 55 | | var zipStream = File.OpenRead(tempFilePath); |
| | 10 | 56 | | return (zipBlob, zipStream, () => Cleanup(tempFilePath)); |
| | 5 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Loads a cached zip blob for the specified download correlation ID. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="downloadCorrelationId">The download correlation ID.</param> |
| | | 63 | | /// <param name="cancellationToken">An optional cancellation token.</param> |
| | | 64 | | /// <returns>A tuple containing the blob and the stream.</returns> |
| | | 65 | | public async Task<(Blob, Stream)?> LoadAsync(string downloadCorrelationId, CancellationToken cancellationToken = def |
| | | 66 | | { |
| | 10 | 67 | | if (!TryGetCacheFilename(downloadCorrelationId, out var fileCacheFilename)) |
| | | 68 | | { |
| | 6 | 69 | | _logger.LogDebug("Rejected invalid zip download correlation ID"); |
| | 6 | 70 | | return null; |
| | | 71 | | } |
| | | 72 | | |
| | 4 | 73 | | var fileCacheStorage = _fileCacheStorageProvider.GetStorage(); |
| | 4 | 74 | | var blob = await fileCacheStorage.GetBlobAsync(fileCacheFilename, cancellationToken); |
| | | 75 | | |
| | 4 | 76 | | if (blob == null) |
| | 0 | 77 | | return null; |
| | | 78 | | |
| | 4 | 79 | | if (!TryGetSafeBlobPath(blob.FullPath, fileCacheFilename, out var safeBlobPath)) |
| | | 80 | | { |
| | 1 | 81 | | _logger.LogWarning("Rejected unsafe cached zip blob path {FullPath}", blob.FullPath); |
| | 1 | 82 | | return null; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | // Check if the blob has expired. |
| | 3 | 86 | | var expiresAt = DateTimeOffset.Parse(blob.Metadata["ExpiresAt"]); |
| | | 87 | | |
| | 3 | 88 | | if (_clock.UtcNow > expiresAt) |
| | | 89 | | { |
| | | 90 | | // File expired. Try to delete it. |
| | | 91 | | try |
| | | 92 | | { |
| | 0 | 93 | | await fileCacheStorage.DeleteAsync(safeBlobPath, cancellationToken); |
| | 0 | 94 | | } |
| | 0 | 95 | | catch (Exception e) |
| | | 96 | | { |
| | 0 | 97 | | _logger.LogWarning(e, "Failed to delete expired file {FullPath}", blob.FullPath); |
| | 0 | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | return null; |
| | | 101 | | } |
| | | 102 | | |
| | 3 | 103 | | var stream = await fileCacheStorage.OpenReadAsync(safeBlobPath, cancellationToken); |
| | 3 | 104 | | return (blob, stream); |
| | 10 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Creates a zip archive from the specified <see cref="Downloadable"/> instances. |
| | | 109 | | /// </summary> |
| | | 110 | | private async Task CreateZipArchiveAsync(string filePath, IEnumerable<Func<ValueTask<Downloadable>>> downloadables, |
| | | 111 | | { |
| | 5 | 112 | | var currentFileIndex = 0; |
| | | 113 | | |
| | | 114 | | // Write the zip archive to the temporary file. |
| | 5 | 115 | | await using var tempFileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read, buf |
| | | 116 | | |
| | 5 | 117 | | using var zipArchive = new ZipArchive(tempFileStream, ZipArchiveMode.Create, true); |
| | 20 | 118 | | foreach (var downloadableFunc in downloadables) |
| | | 119 | | { |
| | 5 | 120 | | var downloadable = await downloadableFunc(); |
| | 5 | 121 | | var entryName = !string.IsNullOrWhiteSpace(downloadable.Filename) ? downloadable.Filename : $"file-{currentF |
| | 5 | 122 | | var entry = zipArchive.CreateEntry(entryName); |
| | 5 | 123 | | var fileStream = downloadable.Stream; |
| | 5 | 124 | | await using var entryStream = entry.Open(); |
| | 5 | 125 | | await fileStream.CopyToAsync(entryStream, cancellationToken); |
| | 5 | 126 | | await entryStream.FlushAsync(cancellationToken); |
| | 5 | 127 | | entryStream.Close(); |
| | 5 | 128 | | currentFileIndex++; |
| | 5 | 129 | | } |
| | 5 | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Creates a cached zip blob for the specified file. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="localPath">The full path of the file to upload.</param> |
| | | 136 | | /// <param name="downloadCorrelationId">The download correlation ID.</param> |
| | | 137 | | /// <param name="downloadAsFilename">The filename to use when downloading the file.</param> |
| | | 138 | | /// <param name="contentType">The content type of the file.</param> |
| | | 139 | | /// <param name="cancellationToken">An optional cancellation token.</param> |
| | | 140 | | private async Task CreateCachedZipBlobAsync(string localPath, string downloadCorrelationId, string? downloadAsFilena |
| | | 141 | | { |
| | 5 | 142 | | if (!TryGetCacheFilename(downloadCorrelationId, out var fileCacheFilename)) |
| | | 143 | | { |
| | 3 | 144 | | _logger.LogDebug("Rejected invalid zip download correlation ID"); |
| | 3 | 145 | | return; |
| | | 146 | | } |
| | | 147 | | |
| | 2 | 148 | | var fileCacheStorage = _fileCacheStorageProvider.GetStorage(); |
| | 2 | 149 | | var expiresAt = _clock.UtcNow.Add(_fileCacheOptions.Value.TimeToLive); |
| | 2 | 150 | | var cachedBlob = CreateBlob(fileCacheFilename, downloadAsFilename, contentType, expiresAt); |
| | 2 | 151 | | await fileCacheStorage.WriteFileAsync(fileCacheFilename, localPath, cancellationToken); |
| | 2 | 152 | | await fileCacheStorage.SetBlobAsync(cachedBlob, cancellationToken: cancellationToken); |
| | 5 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Creates a blob for the specified file. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <param name="fullPath">The full path of the file.</param> |
| | | 159 | | /// <param name="downloadAsFilename">The filename to use when downloading the file.</param> |
| | | 160 | | /// <param name="contentType">The content type of the file.</param> |
| | | 161 | | /// <param name="expiresAt">The date and time at which the file expires.</param> |
| | | 162 | | /// <returns>The blob.</returns> |
| | | 163 | | private Blob CreateBlob(string fullPath, string? downloadAsFilename, string? contentType, DateTimeOffset? expiresAt |
| | | 164 | | { |
| | 7 | 165 | | (downloadAsFilename, contentType) = GetDownloadableMetadata(downloadAsFilename, contentType); |
| | | 166 | | |
| | 7 | 167 | | var now = _clock.UtcNow; |
| | | 168 | | |
| | 7 | 169 | | var blob = new Blob(fullPath) |
| | 7 | 170 | | { |
| | 7 | 171 | | Metadata = |
| | 7 | 172 | | { |
| | 7 | 173 | | ["ContentType"] = contentType, |
| | 7 | 174 | | ["Filename"] = downloadAsFilename |
| | 7 | 175 | | }, |
| | 7 | 176 | | CreatedTime = now, |
| | 7 | 177 | | LastModificationTime = now |
| | 7 | 178 | | }; |
| | | 179 | | |
| | 7 | 180 | | if(expiresAt.HasValue) |
| | 2 | 181 | | blob.Metadata["ExpiresAt"] = expiresAt.Value.ToString("O"); |
| | | 182 | | |
| | 7 | 183 | | return blob; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | private (string downloadAsFilename, string contentType) GetDownloadableMetadata(string? contentType, string? downloa |
| | | 187 | | { |
| | 7 | 188 | | contentType = !string.IsNullOrWhiteSpace(contentType) ? contentType : System.Net.Mime.MediaTypeNames.Application |
| | 7 | 189 | | downloadAsFilename = !string.IsNullOrWhiteSpace(downloadAsFilename) ? downloadAsFilename : "download.zip"; |
| | | 190 | | |
| | 7 | 191 | | return (downloadAsFilename, contentType); |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | private string GetTempFilePath() |
| | | 195 | | { |
| | 5 | 196 | | var tempFileName = Path.GetRandomFileName(); |
| | 5 | 197 | | var tempFilePath = Path.Combine(_fileCacheOptions.Value.LocalCacheDirectory, tempFileName); |
| | 5 | 198 | | return tempFilePath; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | private bool TryGetCacheFilename(string downloadCorrelationId, out string fileCacheFilename) |
| | | 202 | | { |
| | 15 | 203 | | fileCacheFilename = default!; |
| | | 204 | | |
| | 15 | 205 | | if (!IsValidDownloadCorrelationId(downloadCorrelationId)) |
| | 9 | 206 | | return false; |
| | | 207 | | |
| | 6 | 208 | | var candidateFilename = $"{downloadCorrelationId}.tmp"; |
| | | 209 | | |
| | 6 | 210 | | if (!IsCachePathSafe(candidateFilename)) |
| | 0 | 211 | | return false; |
| | | 212 | | |
| | 6 | 213 | | fileCacheFilename = candidateFilename; |
| | 6 | 214 | | return true; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | private static bool IsValidDownloadCorrelationId(string value) |
| | | 218 | | { |
| | 15 | 219 | | if (string.IsNullOrWhiteSpace(value) || value.Length > MaxDownloadCorrelationIdLength) |
| | 2 | 220 | | return false; |
| | | 221 | | |
| | 13 | 222 | | return value.AsSpan().IndexOfAnyExcept(DownloadCorrelationIdCharacters) < 0; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | private bool TryGetSafeBlobPath(string path, string expectedFilename, out string safeBlobPath) |
| | | 226 | | { |
| | 4 | 227 | | safeBlobPath = default!; |
| | | 228 | | |
| | 4 | 229 | | if (string.IsNullOrWhiteSpace(path)) |
| | 0 | 230 | | return false; |
| | | 231 | | |
| | 4 | 232 | | if (TryGetRootedBlobNamespacePath(path, expectedFilename, out safeBlobPath)) |
| | 2 | 233 | | return true; |
| | | 234 | | |
| | 2 | 235 | | if (!Path.IsPathRooted(path)) |
| | | 236 | | { |
| | 0 | 237 | | if (!IsCachePathSafe(path)) |
| | 0 | 238 | | return false; |
| | | 239 | | |
| | 0 | 240 | | safeBlobPath = path; |
| | 0 | 241 | | return true; |
| | | 242 | | } |
| | | 243 | | |
| | 2 | 244 | | var fullPath = Path.GetFullPath(path); |
| | 2 | 245 | | var fullCacheDirectory = GetFullCacheDirectory(); |
| | 2 | 246 | | var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; |
| | 2 | 247 | | if (!fullPath.StartsWith(fullCacheDirectory, comparison)) |
| | 1 | 248 | | return false; |
| | | 249 | | |
| | 1 | 250 | | safeBlobPath = fullPath; |
| | 1 | 251 | | return true; |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | private static bool TryGetRootedBlobNamespacePath(string path, string expectedFilename, out string safeBlobPath) |
| | | 255 | | { |
| | 4 | 256 | | safeBlobPath = default!; |
| | | 257 | | |
| | | 258 | | // FluentStorage directory blobs use "/file" as a blob namespace path. |
| | 4 | 259 | | var blobPath = path.Replace('\\', '/'); |
| | 4 | 260 | | if (blobPath != $"/{expectedFilename}") |
| | 2 | 261 | | return false; |
| | | 262 | | |
| | 2 | 263 | | safeBlobPath = expectedFilename; |
| | 2 | 264 | | return true; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | private bool IsCachePathSafe(string path) |
| | | 268 | | { |
| | 6 | 269 | | if (string.IsNullOrWhiteSpace(path)) |
| | 0 | 270 | | return false; |
| | | 271 | | |
| | 6 | 272 | | if (Path.IsPathRooted(path)) |
| | 0 | 273 | | return false; |
| | | 274 | | |
| | 6 | 275 | | var fullCacheDirectory = GetFullCacheDirectory(); |
| | 6 | 276 | | var fullPath = Path.GetFullPath(Path.Join(fullCacheDirectory, path)); |
| | 6 | 277 | | var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; |
| | 6 | 278 | | return fullPath.StartsWith(fullCacheDirectory, comparison); |
| | | 279 | | } |
| | | 280 | | |
| | | 281 | | private string GetFullCacheDirectory() |
| | | 282 | | { |
| | 8 | 283 | | var cacheDirectory = Path.GetFullPath(_fileCacheOptions.Value.LocalCacheDirectory); |
| | 8 | 284 | | return Path.EndsInDirectorySeparator(cacheDirectory) ? cacheDirectory : cacheDirectory + Path.DirectorySeparator |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | private void Cleanup(string filePath) |
| | | 288 | | { |
| | | 289 | | try |
| | | 290 | | { |
| | 5 | 291 | | File.Delete(filePath); |
| | 5 | 292 | | } |
| | 0 | 293 | | catch (Exception e) |
| | | 294 | | { |
| | 0 | 295 | | _logger.LogWarning(e, "Failed to delete temporary file {TempFilePath}", filePath); |
| | 0 | 296 | | } |
| | 5 | 297 | | } |
| | | 298 | | } |