2026-05-18 00:16:46 +00:00
|
|
|
using Elsa.Diagnostics.ConsoleLogs.Services;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Diagnostics.ConsoleLogs.UnitTests.Redaction;
|
|
|
|
|
|
|
|
|
|
public class ConsoleLogRedactorTests
|
|
|
|
|
{
|
|
|
|
|
private readonly ConsoleLogRedactor _redactor = new(Microsoft.Extensions.Options.Options.Create(new ConsoleLogsOptions()));
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Redact_MasksSensitiveLineText()
|
|
|
|
|
{
|
|
|
|
|
var line = CreateLine(string.Concat("Authorization: ", "Bearer ", "sample-token"));
|
|
|
|
|
|
|
|
|
|
var redacted = _redactor.Redact(line);
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Authorization: [Redacted]", redacted.Text);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 09:49:51 +00:00
|
|
|
[Fact]
|
|
|
|
|
public void Redact_MasksSensitiveLineTextSplitByAnsi()
|
|
|
|
|
{
|
|
|
|
|
var line = CreateLine("to\u001b[31mken=sample-token");
|
|
|
|
|
|
|
|
|
|
var redacted = _redactor.Redact(line);
|
|
|
|
|
|
|
|
|
|
Assert.Equal("[Redacted]", redacted.Text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Redact_PreservesAnsiWhenLineTextIsNotSensitive()
|
|
|
|
|
{
|
|
|
|
|
var line = CreateLine("\u001b[31mhello\u001b[0m");
|
|
|
|
|
|
|
|
|
|
var redacted = _redactor.Redact(line);
|
|
|
|
|
|
|
|
|
|
Assert.Equal("\u001b[31mhello\u001b[0m", redacted.Text);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-18 00:16:46 +00:00
|
|
|
[Fact]
|
|
|
|
|
public void Redact_MasksSensitiveSourceMetadata()
|
|
|
|
|
{
|
|
|
|
|
var line = CreateLine("hello") with
|
|
|
|
|
{
|
|
|
|
|
Source = CreateSource() with
|
|
|
|
|
{
|
|
|
|
|
Metadata = new Dictionary<string, string?> { ["apiKey"] = "secret-value" }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var redacted = _redactor.Redact(line);
|
|
|
|
|
|
|
|
|
|
Assert.Equal("[Redacted]", redacted.Source.Metadata["apiKey"]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ConsoleLogLine CreateLine(string text) => new()
|
|
|
|
|
{
|
|
|
|
|
Text = text,
|
|
|
|
|
Source = CreateSource()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static ConsoleLogSource CreateSource() => new()
|
|
|
|
|
{
|
|
|
|
|
Id = "source",
|
|
|
|
|
DisplayName = "source",
|
|
|
|
|
MachineName = "machine"
|
|
|
|
|
};
|
|
|
|
|
}
|