Implement file hashing on upload

Closes #25
This commit is contained in:
jvyden 2021-11-18 19:30:37 -05:00
parent 4542bd3313
commit 09c72eb9c2
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 26 additions and 12 deletions

View file

@ -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<HttpResponseMessage> 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<HttpResponseMessage> UploadFileRequest(string endpoint, string filePath)
=> await this.Client.PostAsync(endpoint, new StringContent(await File.ReadAllTextAsync(filePath)));

View file

@ -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);
}
}

View file

@ -54,7 +54,6 @@ namespace LBPUnion.ProjectLighthouse.Controllers
[AllowSynchronousIo]
public async Task<IActionResult> 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();

View file

@ -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);