|
1 | 1 | using System.IO.Compression;
|
2 | 2 | using System.Runtime.Versioning;
|
| 3 | +using System.Security.Cryptography; |
3 | 4 | using Microsoft.Extensions.Logging;
|
4 | 5 | using Newtonsoft.Json;
|
5 | 6 | using VRCFaceTracking.Core.Helpers;
|
@@ -52,11 +53,23 @@ private static void MoveDirectory(string source, string dest)
|
52 | 53 | Directory.Delete(source, true);
|
53 | 54 | }
|
54 | 55 |
|
55 |
| - private static async Task DownloadModuleToFile(TrackingModuleMetadata moduleMetadata, string filePath) |
| 56 | + private static async Task DownloadModuleToFile(TrackingModuleMetadata moduleMetadata, string filePath, string md5Hash = null) |
56 | 57 | {
|
57 | 58 | using var client = new HttpClient();
|
58 | 59 | var response = await client.GetAsync(moduleMetadata.DownloadUrl);
|
59 | 60 | var content = await response.Content.ReadAsByteArrayAsync();
|
| 61 | + if (!string.IsNullOrEmpty(md5Hash)) |
| 62 | + { |
| 63 | + using var md5 = MD5.Create(); |
| 64 | + var hash = md5.ComputeHash(content); |
| 65 | + var hashStr = BitConverter.ToString(hash).Replace("-", string.Empty).ToLowerInvariant(); |
| 66 | + |
| 67 | + if (!string.Equals(hashStr, md5Hash.ToLowerInvariant(), StringComparison.OrdinalIgnoreCase)) |
| 68 | + { |
| 69 | + throw new InvalidDataException($"MD5 hash mismatch. Expected {md5Hash}, got {hashStr}"); |
| 70 | + } |
| 71 | + } |
| 72 | + |
60 | 73 | await File.WriteAllBytesAsync(filePath, content);
|
61 | 74 | await Task.CompletedTask;
|
62 | 75 | }
|
@@ -214,7 +227,7 @@ public async Task<string> InstallRemoteModule(TrackingModuleMetadata moduleMetad
|
214 | 227 | try
|
215 | 228 | {
|
216 | 229 | tempZipPath = Path.Combine(tempDirectory, "module.zip");
|
217 |
| - await DownloadModuleToFile(moduleMetadata, tempZipPath); |
| 230 | + await DownloadModuleToFile(moduleMetadata, tempZipPath, moduleMetadata.FileHash); |
218 | 231 | }
|
219 | 232 | catch (Exception e)
|
220 | 233 | {
|
@@ -280,7 +293,15 @@ public async Task<string> InstallRemoteModule(TrackingModuleMetadata moduleMetad
|
280 | 293 | }
|
281 | 294 | Directory.CreateDirectory(moduleDirectory);
|
282 | 295 |
|
283 |
| - await DownloadModuleToFile(moduleMetadata, dllPath); |
| 296 | + try |
| 297 | + { |
| 298 | + await DownloadModuleToFile(moduleMetadata, dllPath, moduleMetadata.FileHash); |
| 299 | + } |
| 300 | + catch (Exception e) |
| 301 | + { |
| 302 | + _logger.LogError($"Failed to download module data for {moduleMetadata.ModuleName}. {e}"); |
| 303 | + return null; |
| 304 | + } |
284 | 305 |
|
285 | 306 | _logger.LogDebug("Downloaded module {module} to {dllPath}", moduleMetadata.ModuleId, dllPath);
|
286 | 307 | }
|
|
0 commit comments