Skip to content

Commit e138fa1

Browse files
authored
Merge pull request #46 from bdach/better-error-on-too-large-request
Show better error message on exceeding absolute request body cap
2 parents 5b23ca8 + 9e33bd4 commit e138fa1

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

osu.Server.BeatmapSubmission.Tests/BeatmapSubmissionControllerTest.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,8 +1028,10 @@ public async Task TestUploadFullPackage_PathTraversalAttackFails(string suspicio
10281028
Assert.False(response.IsSuccessStatusCode);
10291029
}
10301030

1031-
[Fact]
1032-
public async Task TestUploadFullPackage_FailsIfSizeTooLarge()
1031+
[Theory]
1032+
[InlineData(50 * 1024 * 1024)]
1033+
[InlineData(200000001)]
1034+
public async Task TestUploadFullPackage_FailsIfSizeTooLarge(long size)
10331035
{
10341036
using var db = await DatabaseAccess.GetConnectionAsync();
10351037
await db.ExecuteAsync("INSERT INTO `phpbb_users` (`user_id`, `username`, `username_clean`, `country_acronym`, `user_permissions`, `user_sig`, `user_occ`, `user_interests`) VALUES (1000, 'test', 'test', 'JP', '', '', '', '')");
@@ -1042,7 +1044,7 @@ public async Task TestUploadFullPackage_FailsIfSizeTooLarge()
10421044
var request = new HttpRequestMessage(HttpMethod.Put, "/beatmapsets/241526");
10431045

10441046
using var content = new MultipartFormDataContent($"{Guid.NewGuid()}----");
1045-
byte[] data = new byte[50 * 1024 * 1024];
1047+
byte[] data = new byte[size];
10461048
new Random(1337).NextBytes(data);
10471049
var stream = new MemoryStream();
10481050

@@ -1059,7 +1061,7 @@ public async Task TestUploadFullPackage_FailsIfSizeTooLarge()
10591061

10601062
var response = await Client.SendAsync(request);
10611063
Assert.False(response.IsSuccessStatusCode);
1062-
Assert.Contains("The beatmap package is too large.", (await response.Content.ReadFromJsonAsync<ErrorResponse>())!.Error);
1064+
Assert.Contains("too large", (await response.Content.ReadFromJsonAsync<ErrorResponse>())!.Error);
10631065
}
10641066

10651067
[Fact]

osu.Server.BeatmapSubmission/BeatmapSubmissionController.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -493,22 +493,8 @@ private void checkPackageSize(long packageSizeBytes, BeatmapPackageParseResult p
493493
if (packageSizeBytes > allowableSizeBytes)
494494
{
495495
throw new InvariantException($"The beatmap package is too large. "
496-
+ $"The size of the package with the requested changes applied is {humaniseSize(packageSizeBytes)}. "
497-
+ $"The maximum allowable size is {humaniseSize(allowableSizeBytes)}.");
498-
}
499-
500-
static string humaniseSize(double sizeBytes)
501-
{
502-
string humanisedSize;
503-
504-
if (sizeBytes < 1024)
505-
humanisedSize = $@"{sizeBytes}B";
506-
else if (sizeBytes < 1024 * 1024)
507-
humanisedSize = $@"{sizeBytes / 1024:#.0}kB";
508-
else
509-
humanisedSize = $@"{sizeBytes / 1024 / 1024:#.0}MB";
510-
511-
return humanisedSize;
496+
+ $"The size of the package with the requested changes applied is {FormatUtils.HumaniseSize(packageSizeBytes)}. "
497+
+ $"The maximum allowable size is {FormatUtils.HumaniseSize(allowableSizeBytes)}.");
512498
}
513499
}
514500

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
namespace osu.Server.BeatmapSubmission
5+
{
6+
public class FormatUtils
7+
{
8+
public static string HumaniseSize(double sizeBytes)
9+
{
10+
string humanisedSize;
11+
12+
if (sizeBytes < 1024)
13+
humanisedSize = $@"{sizeBytes}B";
14+
else if (sizeBytes < 1024 * 1024)
15+
humanisedSize = $@"{sizeBytes / 1024:#.0}kB";
16+
else
17+
humanisedSize = $@"{sizeBytes / 1024 / 1024:#.0}MB";
18+
19+
return humanisedSize;
20+
}
21+
}
22+
}

osu.Server.BeatmapSubmission/ModelStateValidationFilter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.AspNetCore.Mvc;
55
using Microsoft.AspNetCore.Mvc.Filters;
6+
using osu.Server.BeatmapSubmission.Models.API.Responses;
67

78
namespace osu.Server.BeatmapSubmission
89
{
@@ -28,7 +29,15 @@ public void OnActionExecuting(ActionExecutingContext context)
2829
continue;
2930

3031
foreach (var error in value.Errors)
32+
{
33+
if (string.IsNullOrEmpty(key) && (error.ErrorMessage.Contains("Request body too large") || error.ErrorMessage.Contains("Multipart body length limit")))
34+
{
35+
context.Result = new ErrorResponse($"Request body too large. Size must be lower than {FormatUtils.HumaniseSize(Program.ABSOLUTE_REQUEST_SIZE_LIMIT_BYTES)}.").ToActionResult();
36+
return;
37+
}
38+
3139
errorList.Add($"{{ field: \"{key}\", message: \"{error.ErrorMessage}\", exception: \"{error.Exception}\" }}");
40+
}
3241
}
3342

3443
logger.LogError($"""

0 commit comments

Comments
 (0)