Skip to content

Commit 1e4332b

Browse files
committed
Remove/fix nonsense report input validation, mark SerializedPhotoLevel as nullable
1 parent f682a58 commit 1e4332b

5 files changed

Lines changed: 143 additions & 21 deletions

File tree

Refresh.Database/GameDatabaseContext.Photos.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public GamePhoto UploadPhoto(IPhotoUpload photo, IEnumerable<IPhotoUploadSubject
3939

4040
Publisher = publisher,
4141
Level = level,
42-
LevelType = photo.LevelType,
43-
OriginalLevelId = photo.LevelId,
44-
OriginalLevelName = photo.LevelTitle,
42+
LevelType = photo.LevelType ?? "",
43+
OriginalLevelId = photo.LevelId ?? 0,
44+
OriginalLevelName = photo.LevelTitle ?? "",
4545

4646
TakenAt = DateTimeOffset.FromUnixTimeSeconds(Math.Clamp(photo.Timestamp, this._time.EarliestDate, this._time.TimestampSeconds)),
4747
PublishedAt = this._time.Now,

Refresh.Database/Models/Photos/SerializedPhoto.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,16 @@ public class SerializedPhoto : IPhotoUpload
2222
[XmlElement("medium")] public string MediumHash { get; set; }
2323
[XmlElement("large")] public string LargeHash { get; set; }
2424
[XmlElement("plan")] public string PlanHash { get; set; }
25+
26+
#nullable restore
2527

26-
[XmlElement("slot")] public SerializedPhotoLevel Level { get; set; }
27-
[XmlIgnore] public int LevelId => this.Level.LevelId;
28-
[XmlIgnore] public string LevelType => this.Level.Type;
29-
[XmlIgnore] public string LevelTitle => this.Level.Title;
28+
/// <remarks>
29+
/// Should never be null, but marked as nullable anyway in order to prevent another bad implementation from breaking
30+
/// </remarks>
31+
[XmlElement("slot")] public SerializedPhotoLevel? Level { get; set; }
32+
[XmlIgnore] public int? LevelId => this.Level?.LevelId;
33+
[XmlIgnore] public string? LevelType => this.Level?.Type;
34+
[XmlIgnore] public string? LevelTitle => this.Level?.Title;
3035

3136
[XmlArray("subjects")] public List<SerializedPhotoSubject> PhotoSubjects { get; set; } = [];
3237
}

Refresh.Database/Query/IPhotoUpload.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ namespace Refresh.Database.Query;
22

33
public interface IPhotoUpload
44
{
5-
int LevelId { get; }
6-
string LevelType { get; }
7-
string LevelTitle { get; }
5+
int? LevelId { get; }
6+
string? LevelType { get; }
7+
string? LevelTitle { get; }
88
string SmallHash { get; }
99
string MediumHash { get;}
1010
string LargeHash { get; }

Refresh.Interfaces.Game/Endpoints/ReportingEndpoints.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ public Response UploadReport(RequestContext context, GameDatabaseContext databas
5353
}
5454

5555
string jpegHash = body.JpegHash;
56-
57-
//If the level is specified but its invalid, set it to 0, to indicate the level is unknown
58-
//This case is hit when someone makes a grief report from a non-existent level, which we allow
59-
if (body.LevelId != 0 && level == null)
60-
body.LevelId = 0;
61-
56+
6257
//Basic validation
6358
if (body.Players is { Length: > 4 } || body.ScreenElements is { Player.Length: > 4 })
6459
// Return OK on PSP, since if we dont, it will error when trying to access the community moon and soft-lock the save file
@@ -84,11 +79,11 @@ public Response UploadReport(RequestContext context, GameDatabaseContext databas
8479
MediumHash = jpegHash,
8580
LargeHash = jpegHash,
8681
PlanHash = "0",
87-
Level = body.LevelId == 0 || level == null ? null : new SerializedPhotoLevel
82+
Level = new SerializedPhotoLevel
8883
{
89-
LevelId = level.LevelId,
90-
Title = level.Title,
91-
Type = level.SlotType.ToGameType()
84+
LevelId = body.LevelId,
85+
Title = level?.Title ?? "",
86+
Type = body.LevelType
9287
},
9388
PhotoSubjects = subjects,
9489
}, subjects, user, level);

RefreshTests.GameServer/Tests/Reporting/ReportingEndpointsTests.cs

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ public void CantUploadReportWithBadScreenElementPlayerCount(bool psp)
240240
[TestCase(TokenGame.LittleBigPlanet3)]
241241
[TestCase(TokenGame.LittleBigPlanetVita)]
242242
[TestCase(TokenGame.LittleBigPlanetPSP)]
243+
[TestCase(TokenGame.BetaBuild)]
243244
public void RedirectGriefReportToPhoto(TokenGame game)
244245
{
245246
using TestContext context = this.GetServer();
@@ -271,6 +272,7 @@ public void RedirectGriefReportToPhoto(TokenGame game)
271272
},
272273
},
273274
LevelId = level.LevelId,
275+
LevelType = "user",
274276
JpegHash = TEST_ASSET_HASH,
275277
}.AsXML())).Result;
276278
Assert.That(response.StatusCode, Is.EqualTo(OK));
@@ -282,13 +284,19 @@ public void RedirectGriefReportToPhoto(TokenGame game)
282284

283285
GamePhoto photo = photos.Items.First();
284286
Assert.That(photo.Publisher.UserId, Is.EqualTo(user.UserId));
287+
288+
Assert.That(photo.OriginalLevelId, Is.EqualTo(level.LevelId));
289+
Assert.That(photo.LevelId, Is.EqualTo(level.LevelId));
290+
Assert.That(photo.LevelType, Is.EqualTo("user"));
291+
Assert.That(photo.OriginalLevelName, Is.EqualTo(level.Title));
285292
}
286293

287294
[TestCase(TokenGame.LittleBigPlanet1)]
288295
[TestCase(TokenGame.LittleBigPlanet2)]
289296
[TestCase(TokenGame.LittleBigPlanet3)]
290297
[TestCase(TokenGame.LittleBigPlanetVita)]
291298
[TestCase(TokenGame.LittleBigPlanetPSP)]
299+
[TestCase(TokenGame.BetaBuild)]
292300
public void RedirectGriefReportToPhotoDeveloperLevel(TokenGame game)
293301
{
294302
using TestContext context = this.GetServer();
@@ -318,7 +326,62 @@ public void RedirectGriefReportToPhotoDeveloperLevel(TokenGame game)
318326
},
319327
},
320328
},
321-
LevelId = level.LevelId,
329+
LevelId = level.StoryId,
330+
LevelType = "developer",
331+
JpegHash = TEST_ASSET_HASH,
332+
}.AsXML())).Result;
333+
Assert.That(response.StatusCode, Is.EqualTo(OK));
334+
335+
context.Database.Refresh();
336+
337+
DatabaseList<GamePhoto> photos = context.Database.GetPhotosByUser(user, 10, 0);
338+
Assert.That(photos.TotalItems, Is.EqualTo(1));
339+
340+
GamePhoto photo = photos.Items.First();
341+
Assert.That(photo.Publisher.UserId, Is.EqualTo(user.UserId));
342+
343+
Assert.That(photo.OriginalLevelId, Is.EqualTo(level.StoryId));
344+
Assert.That(photo.LevelId, Is.EqualTo(level.LevelId));
345+
Assert.That(photo.LevelType, Is.EqualTo("developer"));
346+
Assert.That(photo.OriginalLevelName, Is.EqualTo(level.Title));
347+
}
348+
349+
[TestCase(TokenGame.LittleBigPlanet1)]
350+
[TestCase(TokenGame.LittleBigPlanet2)]
351+
[TestCase(TokenGame.LittleBigPlanet3)]
352+
[TestCase(TokenGame.LittleBigPlanetVita)]
353+
[TestCase(TokenGame.LittleBigPlanetPSP)]
354+
[TestCase(TokenGame.BetaBuild)]
355+
public void RedirectGriefReportToPhotoWhenNotUserOrDeveloperSlot(TokenGame game)
356+
{
357+
using TestContext context = this.GetServer();
358+
GameUser user = context.CreateUser();
359+
user.RedirectGriefReportsToPhotos = true;
360+
361+
using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, game, TokenPlatform.PS3, user);
362+
363+
//Upload our """photo"""
364+
HttpResponseMessage message = client.PostAsync($"/lbp/upload/{TEST_ASSET_HASH}", new ReadOnlyMemoryContent(TestAsset)).Result;
365+
Assert.That(message.StatusCode, Is.EqualTo(OK));
366+
367+
HttpResponseMessage response = client.PostAsync("/lbp/grief", new StringContent(new GameReport
368+
{
369+
Players = new Player[]
370+
{
371+
new()
372+
{
373+
Username = user.Username,
374+
Rectangle = new Rect
375+
{
376+
Top = 10,
377+
Left = 10,
378+
Bottom = 20,
379+
Right = 20,
380+
},
381+
},
382+
},
383+
LevelId = 0,
384+
LevelType = "pod",
322385
JpegHash = TEST_ASSET_HASH,
323386
}.AsXML())).Result;
324387
Assert.That(response.StatusCode, Is.EqualTo(OK));
@@ -330,6 +393,65 @@ public void RedirectGriefReportToPhotoDeveloperLevel(TokenGame game)
330393

331394
GamePhoto photo = photos.Items.First();
332395
Assert.That(photo.Publisher.UserId, Is.EqualTo(user.UserId));
396+
397+
Assert.That(photo.OriginalLevelId, Is.EqualTo(0));
398+
Assert.That(photo.LevelId, Is.Null);
399+
Assert.That(photo.LevelType, Is.EqualTo("pod"));
400+
Assert.That(photo.OriginalLevelName, Is.Empty);
401+
}
402+
403+
[TestCase(TokenGame.LittleBigPlanet1)]
404+
[TestCase(TokenGame.LittleBigPlanet2)]
405+
[TestCase(TokenGame.LittleBigPlanet3)]
406+
[TestCase(TokenGame.LittleBigPlanetVita)]
407+
[TestCase(TokenGame.LittleBigPlanetPSP)]
408+
[TestCase(TokenGame.BetaBuild)]
409+
public void RedirectGriefReportToPhotoWhenMissingLevel(TokenGame game)
410+
{
411+
using TestContext context = this.GetServer();
412+
GameUser user = context.CreateUser();
413+
user.RedirectGriefReportsToPhotos = true;
414+
415+
using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, game, TokenPlatform.PS3, user);
416+
417+
//Upload our """photo"""
418+
HttpResponseMessage message = client.PostAsync($"/lbp/upload/{TEST_ASSET_HASH}", new ReadOnlyMemoryContent(TestAsset)).Result;
419+
Assert.That(message.StatusCode, Is.EqualTo(OK));
420+
421+
HttpResponseMessage response = client.PostAsync("/lbp/grief", new StringContent(new GameReport
422+
{
423+
Players = new Player[]
424+
{
425+
new()
426+
{
427+
Username = user.Username,
428+
Rectangle = new Rect
429+
{
430+
Top = 10,
431+
Left = 10,
432+
Bottom = 20,
433+
Right = 20,
434+
},
435+
},
436+
},
437+
LevelId = 1,
438+
LevelType = "user",
439+
JpegHash = TEST_ASSET_HASH,
440+
}.AsXML())).Result;
441+
Assert.That(response.StatusCode, Is.EqualTo(OK));
442+
443+
context.Database.Refresh();
444+
445+
DatabaseList<GamePhoto> photos = context.Database.GetPhotosByUser(user, 10, 0);
446+
Assert.That(photos.TotalItems, Is.EqualTo(1));
447+
448+
GamePhoto photo = photos.Items.First();
449+
Assert.That(photo.Publisher.UserId, Is.EqualTo(user.UserId));
450+
451+
Assert.That(photo.OriginalLevelId, Is.EqualTo(1));
452+
Assert.That(photo.LevelId, Is.Null);
453+
Assert.That(photo.LevelType, Is.EqualTo("user"));
454+
Assert.That(photo.OriginalLevelName, Is.Empty);
333455
}
334456

335457
[Test]

0 commit comments

Comments
 (0)