Skip to content

Commit d97cd59

Browse files
[Inproc] Sanitize exception logs (#10462)
1 parent bb017c5 commit d97cd59

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

src/WebJobs.Script/Sanitizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static class Sanitizer
1818

1919
// List of keywords that should not be replaced with [Hidden Credential]
2020
private static readonly string[] AllowedTokens = new string[] { "PublicKeyToken=" };
21-
internal static readonly string[] CredentialTokens = new string[] { "Token=", "DefaultEndpointsProtocol=http", "AccountKey=", "Data Source=", "Server=", "Password=", "pwd=", "&sig=", "&sig=", "?sig=", "SharedAccessKey=" };
21+
internal static readonly string[] CredentialTokens = new string[] { "Token=", "DefaultEndpointsProtocol=http", "AccountKey=", "Data Source=", "Server=", "Password=", "pwd=", "&sig=", "&sig=", "?sig=", "SharedAccessKey=", "&code=", "&code=", "?code=", "key=" };
2222
private static readonly string[] CredentialNameFragments = new[] { "password", "pwd", "key", "secret", "token", "sas" };
2323

2424
// Pattern of format : "<protocol>://<username>:<password>@<address>:<port>"

src/WebJobs.Script/Workers/Rpc/RpcException.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using Microsoft.Azure.WebJobs.Logging;
56

67
namespace Microsoft.Azure.WebJobs.Script.Workers.Rpc
78
{
89
public class RpcException : Exception
910
{
1011
public RpcException(string result, string message, string stack, string typeName = "", bool isUserException = false)
11-
: base($"Result: {result}\nException: {message}\nStack: {stack}")
12+
: base($"Result: {result}\nException: {Sanitizer.Sanitize(message)}\nStack: {stack}")
1213
{
1314
RemoteStackTrace = stack;
14-
RemoteMessage = message;
15+
RemoteMessage = Sanitizer.Sanitize(message);
1516
if (!string.IsNullOrEmpty(typeName))
1617
{
1718
RemoteTypeName = typeName;

test/WebJobs.Script.Tests/SanitizerTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public class SanitizerTests
3434
[InlineData("SharedAccessKey=foo", "[Hidden Credential]")]
3535
[InlineData(@"Hey=AS1$@%#$%W-k2j"";SharedAccessKey=foo,Data Source=barzons,Server=bathouse'testing", @"Hey=AS1$@%#$%W-k2j"";[Hidden Credential]'testing")]
3636
[InlineData("test?sig=", "test[Hidden Credential]")]
37+
[InlineData("test?code=XPAAAAAAAAAAAAAT-ag==", "test[Hidden Credential]")]
38+
[InlineData("test?foo=bar&code=REAAAAAAAAAAAAAT-ag==", "test?foo=bar[Hidden Credential]")]
39+
[InlineData("test&amp;code=MiAAAAAAAAAAAAAAAAT-ag==", "test[Hidden Credential]")]
3740
[InlineData("aaa://aaa:[email protected]:1111", "[Hidden Credential]")]
3841
[InlineData("test,aaa://aaa:[email protected]:1111,test", "test,[Hidden Credential],test")]
3942
[InlineData(@"some text abc://abc:[email protected]:1111 some text abc://abc:[email protected]:1111 text", @"some text [Hidden Credential] some text [Hidden Credential] text")]

test/WebJobs.Script.Tests/Workers/Rpc/GrpcWorkerChannelTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,23 @@ public async Task ReceivesInboundEvent_FunctionLoadResponse()
939939
Assert.True(traces.Any(m => string.Equals(m.FormattedMessage, "Received FunctionLoadResponse for function: 'js1' with functionId: 'TestFunctionId1'.")), "FunctionLoadResponse TestFunctionId1");
940940
}
941941

942+
[Fact]
943+
public async Task ReceivesInboundEvent_Error_FunctionLoadResponse()
944+
{
945+
await CreateDefaultWorkerChannel();
946+
var functionMetadatas = GetTestFunctionsList("node");
947+
_workerChannel.SetupFunctionInvocationBuffers(functionMetadatas);
948+
_testFunctionRpcService.OnMessage(StreamingMessage.ContentOneofCase.FunctionLoadRequest,
949+
_ => _testFunctionRpcService.PublishSystemErrorFunctionLoadResponseEvent("TestFunctionId1", "abc AccountKey== "));
950+
_workerChannel.SendFunctionLoadRequests(null, TimeSpan.FromMinutes(5));
951+
952+
await Task.Delay(500);
953+
var traces = _logger.GetLogMessages();
954+
ShowOutput(traces);
955+
956+
Assert.True(traces.Any(m => m.Exception != null && m.Exception.Message.Contains("abc [Hidden Credential]")));
957+
}
958+
942959
[Fact]
943960
public async Task Receives_Individual_FunctionLoadResponses_Parallel()
944961
{

test/WebJobs.Script.Tests/Workers/Rpc/TestFunctionRpcService.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.Azure.WebJobs.Script.Grpc.Eventing;
1212
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
1313
using Microsoft.Extensions.Logging;
14+
using static Microsoft.Azure.WebJobs.Script.Grpc.Messages.RpcLog.Types;
1415

1516
namespace Microsoft.Azure.WebJobs.Script.Tests.Workers.Rpc
1617
{
@@ -153,6 +154,37 @@ public void PublishFunctionLoadResponseEvent(string functionId)
153154
Write(responseMessage);
154155
}
155156

157+
public void PublishSystemErrorFunctionLoadResponseEvent(string functionId, string exceptionMessage)
158+
{
159+
StatusResult statusResult = new StatusResult()
160+
{
161+
Status = StatusResult.Types.Status.Failure
162+
};
163+
FunctionLoadResponse functionLoadResponse = new FunctionLoadResponse()
164+
{
165+
FunctionId = functionId,
166+
Result = statusResult
167+
};
168+
169+
RpcLog rpcLog = new RpcLog()
170+
{
171+
LogCategory = RpcLogCategory.System,
172+
Level = Level.Error,
173+
Exception = new RpcException()
174+
{
175+
Message = exceptionMessage
176+
}
177+
};
178+
179+
StreamingMessage responseMessage = new StreamingMessage()
180+
{
181+
FunctionLoadResponse = functionLoadResponse,
182+
RpcLog = rpcLog
183+
};
184+
185+
Write(responseMessage);
186+
}
187+
156188
public void PublishFunctionLoadResponsesEvent(List<string> functionIds, StatusResult statusResult)
157189
{
158190
FunctionLoadResponseCollection functionLoadResponseCollection = new FunctionLoadResponseCollection();

0 commit comments

Comments
 (0)