Skip to content

Commit 2379c9d

Browse files
committed
Merge pull request #497 from RaphHaddad/issue-378
filtering passwords at logging level issue #378
2 parents 03de6d3 + fe3df90 commit 2379c9d

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<Compile Include="Helpers\Constants.cs" />
100100
<Compile Include="InformationalVersionBuilderTests.cs" />
101101
<Compile Include="JsonVersionBuilderTests.cs" />
102+
<Compile Include="LoggerTest.cs" />
102103
<Compile Include="Mocks\MockBranch.cs" />
103104
<Compile Include="Mocks\MockBranchCollection.cs" />
104105
<Compile Include="Mocks\MockCommit.cs" />
@@ -145,9 +146,7 @@
145146
<Content Include="FodyWeavers.xml" />
146147
<Content Include="JsonVersionBuilderTests.Json.approved.txt" />
147148
</ItemGroup>
148-
<ItemGroup>
149-
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
150-
</ItemGroup>
149+
<ItemGroup />
151150
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
152151
<PropertyGroup>
153152
<PostBuildEvent>

GitVersionCore.Tests/LoggerTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using NUnit.Framework;
2+
using System;
3+
using GitVersion;
4+
namespace GitVersionCore.Tests
5+
{
6+
[TestFixture]
7+
public class LoggerTest
8+
{
9+
[Test]
10+
[TestCase("http")]
11+
[TestCase("https")]
12+
public void LoggerObscuresPassword(string protocol)
13+
{
14+
const string username = "username%40domain.com";
15+
const string password = "password";
16+
var s = string.Empty;
17+
Action<string> action = info => { s = info; };
18+
Logger.SetLoggers(action, action, action);
19+
20+
Logger.WriteInfo(string.Format("{0}://{1}:{2}@workspace.visualstudio.com/DefaultCollection/_git/CAS",protocol,username,password));
21+
22+
Assert.IsFalse(s.Contains(password));
23+
}
24+
25+
[Test]
26+
public void UsernameWithoutPassword()
27+
{
28+
var s = string.Empty;
29+
Action<string> action = info => { s = info; };
30+
Logger.SetLoggers(action, action, action);
31+
32+
const string repoUrl = "http://[email protected]/DefaultCollection/_git/CAS";
33+
Logger.WriteInfo(repoUrl);
34+
35+
Assert.IsTrue(s.Contains(repoUrl));
36+
}
37+
38+
}
39+
}

GitVersionCore/Logger.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace GitVersion
22
{
33
using System;
44
using System.Globalization;
5+
using System.Text.RegularExpressions;
56

67
public static class Logger
78
{
@@ -28,11 +29,22 @@ public static IDisposable IndentLog(string operationDescription)
2829
});
2930
}
3031

32+
static Action<string> ObscurePassword(Action<string> info)
33+
{
34+
Action<string> logAction = s =>
35+
{
36+
var rgx = new Regex("(https?://)(.+)(:.+@)");
37+
s = rgx.Replace(s, "$1$2:*******@");
38+
info(s);
39+
};
40+
return logAction;
41+
}
42+
3143
public static void SetLoggers(Action<string> info, Action<string> warn, Action<string> error)
3244
{
33-
WriteInfo = LogMessage(info, "INFO");
34-
WriteWarning = LogMessage(warn, "WARN");
35-
WriteError = LogMessage(error, "ERROR");
45+
WriteInfo = LogMessage(ObscurePassword(info), "INFO");
46+
WriteWarning = LogMessage(ObscurePassword(warn), "WARN");
47+
WriteError = LogMessage(ObscurePassword(error), "ERROR");
3648
}
3749

3850
static Action<string> LogMessage(Action<string> logAction, string level)

0 commit comments

Comments
 (0)