-
Notifications
You must be signed in to change notification settings - Fork 11
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 #31 from microsoft/sanitize_logs
Sanitize sas tokens in logs
- Loading branch information
Showing
6 changed files
with
104 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using PackageUploader.ClientApi.Client.Ingestion.Sanitizers; | ||
|
||
namespace PackageUploader.ClientApi.Test; | ||
|
||
[TestClass] | ||
public class LogSanitizerTest | ||
{ | ||
|
||
[TestMethod] | ||
public void SanitizeGamePackageResponse() | ||
{ | ||
const string response = | ||
"{\"resourceType\":\"GamePackage\",\"packageType\":\"\",\"fileName\":\"Microsoft.GamePackage_1.1.1.0_x64_abcdefghij\",\"uploadInfo\":{\"fileName\":\"Microsoft.GamePackage_1.1.1.0_x64_abcdefghij\",\"xfusId\":\"12345678-1234-1234-1234-1234567890ab\",\"fileSasUri\":\"https://upload.xboxlive.com/upload/Asset/0123456789012345678?token=%3fsv%3d2021-01-01%26sr%3dc%26si%3d12345678-1234-1234-1234-1234567890ab2022-4-5-00-04-32%26sig%3d000000000000000000000000000000000000000000000000%26se%3d2022-04-10T00%253A37%253A32Z%26t%3dZZZ\",\"token\":\"?sv=2021-01-01%26sr%3dc%26si%3d12345678-1234-1234-1234-1234567890ab2022-4-5-00-04-32%26sig%3d000000000000000000000000000000000000000000000000%26se%3d2022-04-10T00%253A37%253A32Z%26t%3dZZZ\",\"uploadDomain\":\"https://upload.xboxlive.com\",\"xfusTenant\":\"ZZZ\"},\"state\":\"PendingUpload\",\"@odata.etag\":\"\"12345678-1234-1234-1234-1234567890ab\"\",\"id\":\"12345678-1234-1234-1234-1234567890ab\"}"; | ||
const string expectedSanitizedResponse = | ||
"{\"resourceType\":\"GamePackage\",\"packageType\":\"\",\"fileName\":\"Microsoft.GamePackage_1.1.1.0_x64_abcdefghij\",\"uploadInfo\":{\"fileName\":\"Microsoft.GamePackage_1.1.1.0_x64_abcdefghij\",\"xfusId\":\"12345678-1234-1234-1234-1234567890ab\",\"fileSasUri\":\"https://upload.xboxlive.com/upload/Asset/0123456789012345678?REDACTED\",\"token\":\"REDACTED\",\"uploadDomain\":\"https://upload.xboxlive.com\",\"xfusTenant\":\"ZZZ\"},\"state\":\"PendingUpload\",\"@odata.etag\":\"\"12345678-1234-1234-1234-1234567890ab\"\",\"id\":\"12345678-1234-1234-1234-1234567890ab\"}"; | ||
|
||
var sanitizedResponse = LogSanitizer.SanitizeJsonResponse(response); | ||
|
||
Assert.AreEqual(expectedSanitizedResponse, sanitizedResponse); | ||
} | ||
|
||
[TestMethod] | ||
public void SanitizePackageAssetResponse() | ||
{ | ||
const string response = | ||
"{\"resourceType\":\"PackageAsset\",\"type\":\"EraSymbolFile\",\"name\":\"Microsoft.GamePackage_1.1.1.0_x64_abcdefghij_erasymbol.zip\",\"isCommitted\":true,\"packageId\":\"12345678-1234-1234-1234-1234567890ab\",\"packageType\":\"\",\"createdDate\":\"0001-01-01T00:00:00Z\",\"binarySizeInBytes\":229689131,\"uploadInfo\":{\"fileName\":\"Microsoft.GamePackage_1.1.1.0_x64_abcdefghij_erasymbol.zip\",\"xfusId\":\"12345678-1234-1234-1234-1234567890ab\",\"fileSasUri\":\"https://bloburl.blob.core.windows.net/12345678-1234-1234-1234-1234567890ab/Microsoft.GamePackage_1.1.1.0_x64_abcdefghij_erasymbol.zip?sv=2021-01-01%26sr%3dc%26si%3d12345678-1234-1234-1234-1234567890ab2022-4-5-00-04-32%26sig%3d000000000000000000000000000000000000000000000000%26se%3d2022-04-10T00%253A37%253A32Z%26t%3dZZZ\",\"xfusTenant\":\"ZZZ\"},\"id\":\"EraSymbolFile\"}"; | ||
const string expectedSanitizedResponse = | ||
"{\"resourceType\":\"PackageAsset\",\"type\":\"EraSymbolFile\",\"name\":\"Microsoft.GamePackage_1.1.1.0_x64_abcdefghij_erasymbol.zip\",\"isCommitted\":true,\"packageId\":\"12345678-1234-1234-1234-1234567890ab\",\"packageType\":\"\",\"createdDate\":\"0001-01-01T00:00:00Z\",\"binarySizeInBytes\":229689131,\"uploadInfo\":{\"fileName\":\"Microsoft.GamePackage_1.1.1.0_x64_abcdefghij_erasymbol.zip\",\"xfusId\":\"12345678-1234-1234-1234-1234567890ab\",\"fileSasUri\":\"https://bloburl.blob.core.windows.net/12345678-1234-1234-1234-1234567890ab/Microsoft.GamePackage_1.1.1.0_x64_abcdefghij_erasymbol.zip?REDACTED\",\"xfusTenant\":\"ZZZ\"},\"id\":\"EraSymbolFile\"}"; | ||
|
||
var sanitizedResponse = LogSanitizer.SanitizeJsonResponse(response); | ||
|
||
Assert.AreEqual(expectedSanitizedResponse, sanitizedResponse); | ||
} | ||
|
||
} |
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
26 changes: 26 additions & 0 deletions
26
src/PackageUploader.ClientApi/Client/Ingestion/IngestionSdkVersion.cs
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,26 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Reflection; | ||
|
||
namespace PackageUploader.ClientApi.Client.Ingestion; | ||
|
||
internal class IngestionSdkVersion | ||
{ | ||
public string SdkVersion { get; } | ||
|
||
public IngestionSdkVersion() | ||
{ | ||
SdkVersion = GetSdkVersion(); | ||
} | ||
|
||
private static string GetSdkVersion() | ||
{ | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
var assemblyVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>(); | ||
|
||
return assemblyVersionAttribute is null ? | ||
$"SDK-V{assembly.GetName().Version?.ToString() ?? "1.0.0"}" : | ||
$"SDK-V{assemblyVersionAttribute.InformationalVersion}"; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/PackageUploader.ClientApi/Client/Ingestion/Sanitizers/LogSanitizer.cs
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,25 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Text.RegularExpressions; | ||
|
||
namespace PackageUploader.ClientApi.Client.Ingestion.Sanitizers; | ||
|
||
public static class LogSanitizer | ||
{ | ||
public static string SanitizeJsonResponse(string jsonResponse) | ||
{ | ||
if (string.IsNullOrWhiteSpace(jsonResponse)) | ||
return jsonResponse; | ||
|
||
// Sanitizing token from responses | ||
var responseBody = Regex.Replace(jsonResponse, "\"token\":\\s*\"[^\"]+?([^\\/\"]+)\"", "\"token\":\"REDACTED\""); | ||
|
||
// Sanitizing File Sas Uri from responses | ||
var sasPropertyMatch = Regex.Match(responseBody, "\"fileSasUri\":\\s*\"[^\"]+?([^\\/\"]+)\""); | ||
if (sasPropertyMatch.Success) | ||
responseBody = responseBody.Replace(sasPropertyMatch.Groups[0].Value, sasPropertyMatch.Groups[0].Value.Split('?')[0] + "?REDACTED\""); | ||
|
||
return responseBody; | ||
} | ||
} |