Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json;
using AWS.Lambda.Powertools.Common;
using AWS.Lambda.Powertools.Logging.Internal.Converters;
Expand Down Expand Up @@ -355,6 +356,9 @@ private JsonSerializerOptions BuildJsonSerializerOptions()
jsonOptions.Converters.Add(new ExceptionConverter());
jsonOptions.Converters.Add(new MemoryStreamConverter());
jsonOptions.Converters.Add(new ConstantClassConverter());

jsonOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;

return jsonOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using AWS.Lambda.Powertools.Common;
using AWS.Lambda.Powertools.Logging.Internal;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -1175,5 +1176,49 @@ public void Log_WhenMemoryStream_LogsBase64String()
)
), Times.Once);
}

[Fact]
public void Log_WhenMemoryStream_LogsBase64String_UnsafeRelaxedJsonEscaping()
{
// Arrange
var loggerName = Guid.NewGuid().ToString();
var service = Guid.NewGuid().ToString();

// This will produce the encoded string dW5zYWZlIHN0cmluZyB+IHRlc3Q= (which has a plus sign to test unsafe escaping)
var bytes = Encoding.UTF8.GetBytes("unsafe string ~ test");

var memoryStream = new MemoryStream(bytes)
{
Position = 0
};
var logLevel = LogLevel.Information;
var randomSampleRate = 0.5;

var configurations = new Mock<IPowertoolsConfigurations>();
configurations.Setup(c => c.Service).Returns(service);
configurations.Setup(c => c.LogLevel).Returns(logLevel.ToString);

var systemWrapper = new Mock<ISystemWrapper>();
systemWrapper.Setup(c => c.GetRandom()).Returns(randomSampleRate);

var logger = new PowertoolsLogger(loggerName, configurations.Object, systemWrapper.Object, () =>
new LoggerConfiguration
{
Service = null,
MinimumLevel = null
});

// Act
logger.LogInformation(new { Name = "Test Object", Stream = memoryStream });

// Assert
systemWrapper.Verify(v =>
v.LogLine(
It.Is<string>
(s =>
s.Contains("\"stream\":\"" + Convert.ToBase64String(bytes) + "\"")
)
), Times.Once);
}
}
}