Skip to content

Commit c05d42a

Browse files
committed
Make UserFilesizeQuota role-specific
1 parent 9fd4d87 commit c05d42a

7 files changed

Lines changed: 35 additions & 11 deletions

File tree

Refresh.Core/Configuration/GameServerConfig.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,26 @@ public class GameServerConfig : Config
1414
protected override void Migrate(int oldVer, dynamic oldConfig)
1515
{
1616
// In version 27, various (mostly already role-specific) perms, like blocked assets and read-only mode, were moved to dedicated child objects,
17-
// to better split them between certain roles.
17+
// to more cleanly split the perms between certain roles, and to make their enforcement easier.
1818
if (oldVer < 27)
1919
{
2020
this.NormalUserPermissions = new();
2121
this.TrustedUserPermissions = new();
2222

23+
// filesize quota limit was added during version 11, but the version wasn't bumped, so catch error to be safe
24+
if (oldVer >= 11)
25+
{
26+
try
27+
{
28+
this.NormalUserPermissions.UserFilesizeQuota = (int)oldConfig.UserFilesizeQuota;
29+
this.TrustedUserPermissions.UserFilesizeQuota = (int)oldConfig.UserFilesizeQuota;
30+
}
31+
catch (RuntimeBinderException)
32+
{
33+
// do nothing
34+
}
35+
}
36+
2337
if (oldVer >= 18)
2438
{
2539
this.NormalUserPermissions.BlockedAssetFlags.Dangerous = (bool)oldConfig.BlockedAssetFlags.Dangerous;
@@ -138,10 +152,6 @@ protected override void Migrate(int oldVer, dynamic oldConfig)
138152
/// </summary>
139153
public string GameConfigStorageUrl { get; set; } = "https://refresh.example.com/lbp";
140154
public bool AllowInvalidTextureGuids { get; set; } = false;
141-
/// <summary>
142-
/// The amount of data the user is allowed to upload before all resource uploads get blocked, defaults to 100mb.
143-
/// </summary>
144-
public int UserFilesizeQuota { get; set; } = 100 * 1_048_576;
145155

146156
/// <summary>
147157
/// Whether to print the room state whenever a `FindBestRoom` match returns no results

Refresh.Core/Configuration/RolePermissions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ public RolePermissions() {}
1414
TimeSpanHours = 24,
1515
LevelQuota = 10,
1616
};
17+
18+
/// <summary>
19+
/// The amount of data the user is allowed to upload before all resource uploads get blocked, defaults to 100mb.
20+
/// </summary>
21+
public int UserFilesizeQuota { get; set; } = 100 * 1_048_576;
1722
}

Refresh.Interfaces.APIv3/Endpoints/ResourceApiEndpoints.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ IntegrationConfig integration
193193
return new ApiValidationError($"The asset must be under 2MB. Your file was {body.Length:N0} bytes.");
194194
}
195195

196-
if (body.Length + user.FilesizeQuotaUsage > config.UserFilesizeQuota)
196+
RolePermissions rolePerms = user.GetRolePermissionsForUser(config);
197+
if (body.Length + user.FilesizeQuotaUsage > rolePerms.UserFilesizeQuota)
197198
{
198-
context.Logger.LogWarning(BunkumCategory.UserContent, "User {0} has hit the filesize quota ({1} bytes), rejecting.", user.Username, config.UserFilesizeQuota);
199+
context.Logger.LogWarning(BunkumCategory.UserContent, "User {0} has hit the filesize quota ({1} bytes), rejecting.", user.Username, rolePerms.UserFilesizeQuota);
199200
return new ApiValidationError($"You have exceeded your filesize quota.");
200201
}
201202

Refresh.Interfaces.Game/Endpoints/ResourceEndpoints.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ public Response UploadAsset(RequestContext context, string hash, string type, by
4848
if (dataStore.ExistsInStore(assetPath))
4949
return Conflict;
5050

51-
if (body.Length + user.FilesizeQuotaUsage > config.UserFilesizeQuota)
51+
RolePermissions rolePerms = user.GetRolePermissionsForUser(config);
52+
53+
if (body.Length + user.FilesizeQuotaUsage > rolePerms.UserFilesizeQuota)
5254
{
53-
context.Logger.LogWarning(BunkumCategory.UserContent, "User {0} has hit the filesize quota ({1} bytes), rejecting.", user.Username, config.UserFilesizeQuota);
55+
context.Logger.LogWarning(BunkumCategory.UserContent, "User {0} has hit the filesize quota ({1} bytes), rejecting.", user.Username, rolePerms.UserFilesizeQuota);
5456
return RequestEntityTooLarge;
5557
}
5658

@@ -66,7 +68,7 @@ public Response UploadAsset(RequestContext context, string hash, string type, by
6668

6769
gameAsset.UploadDate = DateTimeOffset.FromUnixTimeSeconds(Math.Clamp(gameAsset.UploadDate.ToUnixTimeSeconds(), timeProvider.EarliestDate, timeProvider.TimestampSeconds));
6870

69-
AssetFlags blockedAssetFlags = user.GetRolePermissionsForUser(config).BlockedAssetFlags.ToAssetFlags();
71+
AssetFlags blockedAssetFlags = rolePerms.BlockedAssetFlags.ToAssetFlags();
7072

7173
// Don't block any assets uploaded from PSP, else block any unwanted assets,
7274
// For example, if the "blocked asset flags" has the "Media" bit set, and so does the asset,

RefreshTests.GameServer/GameServer/TestGameServerConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ public void TestMigration()
2424

2525
public int MaximumAssetSafetyLevel { get; set; } = 0;
2626
public int MaximumAssetSafetyLevelForTrustedUsers { get; set; } = 0;
27+
public int UserFilesizeQuota { get; set; } = 141;
2728
}

RefreshTests.GameServer/Tests/Assets/AssetUploadTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public void CannotUploadAssetPastFillingFilesizeQuota(bool psp)
3939
{
4040
using TestContext context = this.GetServer();
4141

42-
context.Server.Value.GameServerConfig.UserFilesizeQuota = 8;
42+
context.Server.Value.GameServerConfig.NormalUserPermissions.UserFilesizeQuota = 8;
43+
context.Server.Value.GameServerConfig.TrustedUserPermissions.UserFilesizeQuota = 8;
4344

4445
context.Server.Value.Server.AddService<ImportService>();
4546
GameUser user = context.CreateUser();

RefreshTests.GameServer/Tests/Configs/GameServerConfigTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public void MigratesRolePermsFromVersion26()
2020
},
2121
BlockedAssetFlags = new(AssetFlags.Dangerous | AssetFlags.Media),
2222
BlockedAssetFlagsForTrustedUsers = new(AssetFlags.Modded),
23+
UserFilesizeQuota = 141,
2324
};
2425

2526
config.TestMigration();
@@ -38,6 +39,9 @@ public void MigratesRolePermsFromVersion26()
3839

3940
Assert.That(config.NormalUserPermissions.BlockedAssetFlags.ToAssetFlags(), Is.EqualTo(AssetFlags.Dangerous | AssetFlags.Media));
4041
Assert.That(config.TrustedUserPermissions.BlockedAssetFlags.ToAssetFlags(), Is.EqualTo(AssetFlags.Modded));
42+
43+
Assert.That(config.NormalUserPermissions.UserFilesizeQuota, Is.EqualTo(141));
44+
Assert.That(config.TrustedUserPermissions.UserFilesizeQuota, Is.EqualTo(141));
4145
}
4246

4347
[Test]

0 commit comments

Comments
 (0)