-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request from GHSA-h2wr-3c7f-7h79
* Enforce SHA1 hash regex upon all asset-related API endpoints * Add unit tests for asset uploading & retrieval
- Loading branch information
Showing
13 changed files
with
127 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Refresh.GameServer.Verification; | ||
|
||
public static partial class CommonPatterns | ||
{ | ||
[GeneratedRegex("^[a-f0-9]{40}$")] | ||
public static partial Regex Sha1Regex(); | ||
|
||
[GeneratedRegex("^[a-f0-9]{128}$")] | ||
public static partial Regex Sha512Regex(); | ||
|
||
[GeneratedRegex("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+[.][a-zA-Z]{2,}$")] | ||
public static partial Regex EmailAddressRegex(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,5 @@ | |
<PackageReference Include="NotEnoughLogs" Version="1.1.0" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Security.Cryptography; | ||
using Refresh.GameServer.Authentication; | ||
using Refresh.GameServer.Services; | ||
using Refresh.GameServer.Types.UserData; | ||
|
||
namespace RefreshTests.GameServer.Tests.Assets; | ||
|
||
public class AssetUploadTests : GameServerTest | ||
{ | ||
[Test] | ||
public void CanUploadAsset() | ||
{ | ||
using TestContext context = this.GetServer(); | ||
context.Server.Value.Server.AddService<ImportService>(); | ||
GameUser user = context.CreateUser(); | ||
using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); | ||
|
||
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.IsSuccessStatusCode, Is.True); | ||
Assert.That(response.StatusCode, Is.EqualTo(OK)); | ||
} | ||
|
||
[Test] | ||
public void CanRetrieveAsset() | ||
{ | ||
using TestContext context = this.GetServer(); | ||
context.Server.Value.Server.AddService<ImportService>(); | ||
GameUser user = context.CreateUser(); | ||
using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); | ||
|
||
ReadOnlySpan<byte> data = "TEX a"u8; | ||
string hash = BitConverter.ToString(SHA1.HashData(data)) | ||
.Replace("-", "") | ||
.ToLower(); | ||
|
||
client.PostAsync("/lbp/upload/" + hash, new ByteArrayContent(data.ToArray())).Wait(); | ||
HttpResponseMessage response = client.GetAsync("/lbp/r/" + hash).Result; | ||
Assert.That(response.IsSuccessStatusCode, Is.True); | ||
byte[] returnedData = response.Content.ReadAsByteArrayAsync().Result; | ||
|
||
Assert.That(data.SequenceEqual(returnedData), Is.True); | ||
} | ||
|
||
[Test] | ||
public void InvalidHashFails() | ||
{ | ||
using TestContext context = this.GetServer(); | ||
context.Server.Value.Server.AddService<ImportService>(); | ||
GameUser user = context.CreateUser(); | ||
using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user); | ||
|
||
HttpResponseMessage response = client.GetAsync("/lbp/r/asdf").Result; | ||
Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); | ||
|
||
response = client.GetAsync("/lbp/r/..%2Frpc.json").Result; | ||
Assert.That(response.StatusCode, Is.EqualTo(BadRequest)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters