Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checks for resource strings #930

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Files;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
Expand Down Expand Up @@ -42,6 +43,13 @@ public async Task<IActionResult> UploadPhoto()
GamePhoto? photo = await this.DeserializeBody<GamePhoto>();
if (photo == null) return this.BadRequest();

string[] photoHashes =
{
photo.LargeHash, photo.MediumHash, photo.SmallHash, photo.PlanHash,
};

if (photoHashes.Any(hash => !FileHelper.ResourceExists(hash))) return this.BadRequest();

foreach (PhotoEntity p in this.database.Photos.Where(p => p.CreatorId == token.UserId))
{
if (p.LargeHash == photo.LargeHash) return this.Ok(); // photo already uploaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public IActionResult GetResource(string hash)
return this.NotFound();
}

// TODO: check if this is a valid hash
[HttpPost("upload/{hash}/unattributed")]
[HttpPost("upload/{hash}")]
public async Task<IActionResult> UploadResource(string hash)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,9 @@ public async Task<IActionResult> UpdateUser()
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (string? resource in new[]{update.IconHash, update.YayHash, update.MehHash, update.BooHash, update.PlanetHash,})
{
if (resource == "0") continue;
if (string.IsNullOrWhiteSpace(resource)) continue;

if (resource != null && !resource.StartsWith('g') && !FileHelper.ResourceExists(resource))
{
return this.BadRequest();
}
if (!FileHelper.ResourceExists(resource)) return this.BadRequest();
}

if (update.IconHash != null) user.IconHash = update.IconHash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using LBPUnion.ProjectLighthouse.Types.Users;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using IOFile = System.IO.File;

namespace LBPUnion.ProjectLighthouse.Servers.Website.Controllers.Admin;

Expand Down Expand Up @@ -68,7 +67,7 @@ public async Task<IActionResult> WipePlanets([FromRoute] int id) {
// And finally, attempt to remove the resource from the filesystem. We don't want that taking up space.
try
{
IOFile.Delete(FileHelper.GetResourcePath(hash));
FileHelper.DeleteResource(hash);
Logger.Success($"Deleted planet resource {hash}",
LogArea.Admin);
}
Expand Down
33 changes: 33 additions & 0 deletions ProjectLighthouse.Tests/Unit/ResourceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@ namespace LBPUnion.ProjectLighthouse.Tests.Unit;
[Trait("Category", "Unit")]
public class ResourceTests
{

[Fact]
public void IsResourceValid_ReturnsTrue_ForValidResource()
{
string[] resources = {
"g123456", "g123", "98f54143ab4e86b28c3afee0f50f2f51cfb2ed38", "0ebe53fc820a544798000188d39bfda94f53fe37"
};
Assert.Multiple(() =>
{
foreach (string resource in resources)
{
Assert.True(FileHelper.IsResourceValid(resource));
}
});

}

[Fact]
public void IsResourceValid_ReturnsFalse_ForInvalidResource()
{
string[] resources =
{
"G0234", "g123456789012334567", "b28c3afee0f50f2f51cfb2ed38", "../Test",
};
Assert.Multiple(() =>
{
foreach (string resource in resources)
{
Assert.False(FileHelper.IsResourceValid(resource));
}
});
}

[Fact]
public void ShouldNotDeleteResourceFolder()
{
Expand Down
10 changes: 8 additions & 2 deletions ProjectLighthouse/Files/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Types.Resources;

namespace LBPUnion.ProjectLighthouse.Files;

public static partial class FileHelper
{
[GeneratedRegex("^(g[0-9]{3,16}|[a-z0-9]{40})$")]
private static partial Regex ResourceRegex();

public static readonly string ResourcePath = Path.Combine(Environment.CurrentDirectory, "r");

public static readonly string FullResourcePath = Path.GetFullPath(ResourcePath);
Expand All @@ -21,6 +25,8 @@ public static partial class FileHelper

public static string GetImagePath(string hash) => Path.Combine(ImagePath, hash);

public static bool IsResourceValid(string hash) => ResourceRegex().IsMatch(hash);

public static bool IsFileSafe(LbpFile file)
{
if (!ServerConfiguration.Instance.CheckForUnsafeFiles) return true;
Expand Down Expand Up @@ -52,8 +58,8 @@ public static bool IsFileSafe(LbpFile file)
};
}

public static bool ResourceExists(string hash) => File.Exists(GetResourcePath(hash));
public static bool ImageExists(string hash) => File.Exists(GetImagePath(hash));
public static bool ResourceExists(string hash) => ResourceRegex().IsMatch(hash) && File.Exists(GetResourcePath(hash));
public static bool ImageExists(string hash) => ResourceRegex().IsMatch(hash) && File.Exists(GetImagePath(hash));

public static void DeleteResource(string hash)
{
Expand Down
3 changes: 2 additions & 1 deletion ProjectLighthouse/Types/Resources/LbpFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public LbpFile(byte[] data)

public static LbpFile? FromHash(string? hash)
{
if (hash == null) return null;
if (hash == null || !FileHelper.IsResourceValid(hash)) return null;

string path = FileHelper.GetResourcePath(hash);
if (!File.Exists(path)) return null;

Expand Down
Loading