Skip to content

Commit

Permalink
Add option to block asset uploads through the config
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Mar 23, 2024
1 parent 8d4c2a9 commit 422934f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Refresh.GameServer/Configuration/GameServerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Refresh.GameServer.Configuration;
[SuppressMessage("ReSharper", "RedundantDefaultMemberInitializer")]
public class GameServerConfig : Config
{
public override int CurrentConfigVersion => 10;
public override int CurrentConfigVersion => 11;
public override int Version { get; set; } = 0;

protected override void Migrate(int oldVer, dynamic oldConfig) {}
Expand All @@ -26,4 +26,5 @@ protected override void Migrate(int oldVer, dynamic oldConfig) {}
public bool TrackRequestStatistics { get; set; } = false;
public string WebExternalUrl { get; set; } = "https://refresh.example.com";
public bool AllowInvalidTextureGuids { get; set; } = false;
public bool BlockAssetUploads { get; set; } = false;
}
4 changes: 4 additions & 0 deletions Refresh.GameServer/Endpoints/Game/ResourceEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class ResourceEndpoints : EndpointGroup
public Response UploadAsset(RequestContext context, string hash, string type, byte[] body, IDataStore dataStore,
GameDatabaseContext database, GameUser user, AssetImporter importer, GameServerConfig config, IDateTimeProvider timeProvider, Token token)
{
//If we block asset uploads, return unauthorized, unless the user is an admin
if (config.BlockAssetUploads && user.Role != GameUserRole.Admin)
return Unauthorized;

if (!CommonPatterns.Sha1Regex().IsMatch(hash)) return BadRequest;

bool isPSP = context.IsPSP();
Expand Down
4 changes: 3 additions & 1 deletion RefreshTests.GameServer/GameServer/TestRefreshGameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public class TestRefreshGameServer : RefreshGameServer

protected override void SetupConfiguration()
{
this.Server.AddConfig(new GameServerConfig());
this.Server.AddConfig(this._config = new GameServerConfig());
this.Server.AddConfig(new RichPresenceConfig());
this.Server.AddConfig(new IntegrationConfig());
}

public GameServerConfig GameServerConfig => this._config!;

public override void Start()
{
this.Server.Start(0);
Expand Down
24 changes: 24 additions & 0 deletions RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ public void CanUploadAsset(bool psp)
Assert.That(response.StatusCode, Is.EqualTo(OK));
}

[TestCase(false)]
[TestCase(true)]
public void CannotUploadAssetWhenBlocked(bool psp)
{
using TestContext context = this.GetServer();

context.Server.Value.GameServerConfig.BlockAssetUploads = true;

context.Server.Value.Server.AddService<ImportService>();
GameUser user = context.CreateUser();
using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);
if(psp)
client.DefaultRequestHeaders.UserAgent.TryParseAdd("LBPPSP CLIENT");

ReadOnlySpan<byte> data = "TEX a"u8;

string hash = BitConverter.ToString(SHA1.HashData(data))
.Replace("-", "")
.ToLower();

HttpResponseMessage response = client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Result;
Assert.That(response.StatusCode, Is.EqualTo(Unauthorized));
}

[Test]
public void CantUploadAssetWithInvalidHash()
{
Expand Down

0 comments on commit 422934f

Please sign in to comment.