Skip to content

Commit ac8465f

Browse files
committed
expanding sanitizer; adding test
1 parent 7e6f825 commit ac8465f

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/WebJobs.Script.WebHost/Diagnostics/Sanitizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static class Sanitizer
1313
{
1414
private const string SecretReplacement = "[Hidden Credential]";
1515
private static readonly char[] ValueTerminators = new char[] { '<', '"' };
16-
private static readonly string[] CredentialTokens = new string[] { "Token=", "DefaultEndpointsProtocol=http", "AccountKey=", "Data Source=", "Server=", "Password=", "pwd=", "&amp;sig=" };
16+
private static readonly string[] CredentialTokens = new string[] { "Token=", "DefaultEndpointsProtocol=http", "AccountKey=", "Data Source=", "Server=", "Password=", "pwd=", "&amp;sig=", "SharedAccessKey=" };
1717

1818
/// <summary>
1919
/// Removes well-known credential strings from strings.

test/WebJobs.Script.Tests/ScriptHostTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,80 @@ public void Initialize_AppliesLoggerConfig()
10511051
}
10521052
}
10531053

1054+
[Fact]
1055+
public void Initialize_Sanitizes_HostJsonLog()
1056+
{
1057+
TestTraceWriter traceWriter = new TestTraceWriter(TraceLevel.Info);
1058+
TestLoggerProvider loggerProvider = null;
1059+
var loggerFactoryHookMock = new Mock<ILoggerFactoryBuilder>(MockBehavior.Strict);
1060+
loggerFactoryHookMock
1061+
.Setup(m => m.AddLoggerProviders(It.IsAny<ILoggerFactory>(), It.IsAny<ScriptHostConfiguration>(), It.IsAny<ScriptSettingsManager>()))
1062+
.Callback<ILoggerFactory, ScriptHostConfiguration, ScriptSettingsManager>((factory, scriptConfig, settings) =>
1063+
{
1064+
loggerProvider = new TestLoggerProvider(scriptConfig.LogFilter.Filter);
1065+
factory.AddProvider(loggerProvider);
1066+
});
1067+
1068+
string rootPath = Path.Combine(Environment.CurrentDirectory, "ScriptHostTests");
1069+
if (!Directory.Exists(rootPath))
1070+
{
1071+
Directory.CreateDirectory(rootPath);
1072+
}
1073+
1074+
// Turn off all logging. We shouldn't see any output.
1075+
string hostJsonContent = @"
1076+
{
1077+
'functionTimeout': '00:05:00',
1078+
'functions': [ 'FunctionA', 'FunctionB' ],
1079+
'logger': {
1080+
'categoryFilter': {
1081+
'defaultLevel': 'Information'
1082+
}
1083+
},
1084+
'Values': {
1085+
'MyCustomValue': 'abc'
1086+
}
1087+
}";
1088+
1089+
File.WriteAllText(Path.Combine(rootPath, "host.json"), hostJsonContent);
1090+
1091+
ScriptHostConfiguration config = new ScriptHostConfiguration()
1092+
{
1093+
RootScriptPath = rootPath,
1094+
TraceWriter = traceWriter
1095+
};
1096+
1097+
config.LoggerFactoryBuilder = loggerFactoryHookMock.Object;
1098+
1099+
config.HostConfig.HostId = ID;
1100+
var environment = new Mock<IScriptHostEnvironment>();
1101+
var eventManager = new Mock<IScriptEventManager>();
1102+
1103+
ScriptHost.Create(environment.Object, eventManager.Object, config, _settingsManager);
1104+
1105+
string hostJsonSanitized = @"
1106+
{
1107+
'functionTimeout': '00:05:00',
1108+
'functions': [ 'FunctionA', 'FunctionB' ],
1109+
'logger': {
1110+
'categoryFilter': {
1111+
'defaultLevel': 'Information'
1112+
}
1113+
}
1114+
}";
1115+
1116+
// for formatting
1117+
var hostJson = JObject.Parse(hostJsonSanitized);
1118+
string expectedMessgae = $"Host configuration file read:{Environment.NewLine}{hostJson}";
1119+
1120+
var logger = loggerProvider.CreatedLoggers.Single(l => l.Category == LogCategories.Startup);
1121+
var logMessage = logger.LogMessages.Single(l => l.FormattedMessage.StartsWith("Host configuration ")).FormattedMessage;
1122+
Assert.Equal(expectedMessgae, logMessage);
1123+
1124+
var traceMessage = traceWriter.Traces.Single(t => t.Message.StartsWith("Host configuration ")).Message;
1125+
Assert.Equal(expectedMessgae, traceMessage);
1126+
}
1127+
10541128
[Fact]
10551129
public void Initialize_LogsException_IfParseError()
10561130
{

0 commit comments

Comments
 (0)