-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple sanitization in strings used in CLI before logging (#1155)
- Loading branch information
Showing
4 changed files
with
67 additions
and
5 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
32 changes: 32 additions & 0 deletions
32
src/Microsoft.ComponentDetection.Common/Utilities/StringUtilities.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,32 @@ | ||
namespace Microsoft.ComponentDetection.Common; | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
|
||
public static class StringUtilities | ||
{ | ||
private static readonly Regex SensitiveInfoRegex = new Regex(@"(?<=https://)(.+)(?=@)", RegexOptions.Compiled, TimeSpan.FromSeconds(5)); | ||
|
||
/// <summary> | ||
/// Utility method to remove sensitive information from a string, currently focused on removing on the credentials placed within URL which can be part of CLI commands. | ||
/// </summary> | ||
/// <param name="inputString">String with possible credentials.</param> | ||
/// <returns>New string identical to original string, except credentials in URL are replaced with placeholders.</returns> | ||
public static string RemoveSensitiveInformation(this string inputString) | ||
{ | ||
if (string.IsNullOrWhiteSpace(inputString)) | ||
{ | ||
return inputString; | ||
} | ||
|
||
try | ||
{ | ||
return SensitiveInfoRegex.Replace(inputString, "******"); | ||
} | ||
catch (Exception) | ||
{ | ||
// No matter the exception, we should not break flow due to regex failure/timeout. | ||
return inputString; | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
test/Microsoft.ComponentDetection.Common.Tests/StringUtilitiesTests.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,28 @@ | ||
namespace Microsoft.ComponentDetection.Common.Tests; | ||
|
||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
[TestClass] | ||
[TestCategory("Governance/All")] | ||
[TestCategory("Governance/ComponentDetection")] | ||
[TestCategory("Governance/Utilities")] | ||
public class StringUtilitiesTests | ||
{ | ||
[TestMethod] | ||
[DataRow("", "")] | ||
[DataRow(null, null)] | ||
[DataRow(" ", " ")] | ||
[DataRow(" https:// ", " https:// ")] | ||
[DataRow("https://username:password@domain.me", "https://******@domain.me")] | ||
[DataRow("https://domain.me", "https://domain.me")] | ||
[DataRow("https://@domain.me", "https://@domain.me")] | ||
[DataRow( | ||
"install -r requirements.txt --dry-run --ignore-installed --quiet --report file.zvn --index-url https://user:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@someregistry.localhost.com", | ||
"install -r requirements.txt --dry-run --ignore-installed --quiet --report file.zvn --index-url https://******@someregistry.localhost.com")] | ||
public void RemoveSensitiveInformation_ReturnsAsExpected(string input, string expected) | ||
{ | ||
var actual = StringUtilities.RemoveSensitiveInformation(input); | ||
actual.Should().Be(expected); | ||
} | ||
} |