diff --git a/ProjectLighthouse.Tests/LighthouseTest.cs b/ProjectLighthouse.Tests/LighthouseTest.cs index df207d74..3efb3684 100644 --- a/ProjectLighthouse.Tests/LighthouseTest.cs +++ b/ProjectLighthouse.Tests/LighthouseTest.cs @@ -1,6 +1,7 @@ using System.Diagnostics.CodeAnalysis; using System.IO; using System.Net.Http; +using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using LBPUnion.ProjectLighthouse.Helpers; @@ -55,6 +56,14 @@ namespace LBPUnion.ProjectLighthouse.Tests return this.Client.SendAsync(requestMessage); } + public async Task UploadFileEndpointRequest(string filePath) + { + byte[] bytes = Encoding.UTF8.GetBytes(await File.ReadAllTextAsync(filePath)); + string hash = HashHelper.Sha1Hash(bytes); + + return await this.Client.PostAsync($"/LITTLEBIGPLANETPS3_XML/upload/{hash}", new ByteArrayContent(bytes)); + } + public async Task UploadFileRequest(string endpoint, string filePath) => await this.Client.PostAsync(endpoint, new StringContent(await File.ReadAllTextAsync(filePath))); diff --git a/ProjectLighthouse.Tests/Tests/UploadTests.cs b/ProjectLighthouse.Tests/Tests/UploadTests.cs index 41612256..fb73be01 100644 --- a/ProjectLighthouse.Tests/Tests/UploadTests.cs +++ b/ProjectLighthouse.Tests/Tests/UploadTests.cs @@ -17,35 +17,35 @@ namespace LBPUnion.ProjectLighthouse.Tests [Fact] public async Task ShouldNotAcceptScript() { - HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/scriptTest", "ExampleFiles/TestScript.ff"); + HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestScript.ff"); Assert.False(response.IsSuccessStatusCode); } [Fact] public async Task ShouldNotAcceptFarc() { - HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/farcTest", "ExampleFiles/TestFarc.farc"); + HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestFarc.farc"); Assert.False(response.IsSuccessStatusCode); } [Fact] public async Task ShouldNotAcceptGarbage() { - HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/garbageTest", "ExampleFiles/TestGarbage.bin"); + HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestGarbage.bin"); Assert.False(response.IsSuccessStatusCode); } [Fact] public async Task ShouldAcceptTexture() { - HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/textureTest", "ExampleFiles/TestTexture.tex"); + HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestTexture.tex"); Assert.True(response.IsSuccessStatusCode); } [Fact] public async Task ShouldAcceptLevel() { - HttpResponseMessage response = await this.UploadFileRequest("/LITTLEBIGPLANETPS3_XML/upload/levelTest", "ExampleFiles/TestLevel.lvl"); + HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestLevel.lvl"); Assert.True(response.IsSuccessStatusCode); } } diff --git a/ProjectLighthouse/Controllers/ResourcesController.cs b/ProjectLighthouse/Controllers/ResourcesController.cs index 3680bbfd..d1a438db 100644 --- a/ProjectLighthouse/Controllers/ResourcesController.cs +++ b/ProjectLighthouse/Controllers/ResourcesController.cs @@ -54,7 +54,6 @@ namespace LBPUnion.ProjectLighthouse.Controllers [AllowSynchronousIo] public async Task UploadResource(string hash) { - string assetsDirectory = FileHelper.ResourcePath; string path = FileHelper.GetResourcePath(hash); @@ -70,6 +69,12 @@ namespace LBPUnion.ProjectLighthouse.Controllers return this.UnprocessableEntity(); } + if (HashHelper.Sha1Hash(file.Data) != hash) + { + Logger.Log($"File hash does not match the uploaded file! (hash: {hash}, type: {file.FileType})", LoggerLevelResources.Instance); + return this.Conflict(); + } + Logger.Log($"File is OK! (hash: {hash}, type: {file.FileType})", LoggerLevelResources.Instance); await IOFile.WriteAllBytesAsync(path, file.Data); return this.Ok(); diff --git a/ProjectLighthouse/Helpers/HashHelper.cs b/ProjectLighthouse/Helpers/HashHelper.cs index ffda6e17..838a22a4 100644 --- a/ProjectLighthouse/Helpers/HashHelper.cs +++ b/ProjectLighthouse/Helpers/HashHelper.cs @@ -11,7 +11,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers [SuppressMessage("ReSharper", "UnusedMember.Global")] public static class HashHelper { -// private static readonly SHA1 sha1 = SHA1.Create(); + private static readonly SHA1 sha1 = SHA1.Create(); private static readonly SHA256 sha256 = SHA256.Create(); private static readonly Random random = new(); @@ -67,11 +67,11 @@ namespace LBPUnion.ProjectLighthouse.Helpers public static string Sha256Hash(string str) => Sha256Hash(Encoding.UTF8.GetBytes(str)); - public static string Sha256Hash(byte[] bytes) - { - byte[] hash = sha256.ComputeHash(bytes); - return Encoding.UTF8.GetString(hash, 0, hash.Length); - } + public static string Sha256Hash(byte[] bytes) => BitConverter.ToString(sha256.ComputeHash(bytes)).Replace("-", ""); + + public static string Sha1Hash(string str) => Sha1Hash(Encoding.UTF8.GetBytes(str)); + + public static string Sha1Hash(byte[] bytes) => BitConverter.ToString(sha1.ComputeHash(bytes)).Replace("-", ""); public static string BCryptHash(string str) => BCrypt.Net.BCrypt.HashPassword(str);