513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
129 lines
4.5 KiB
C#
129 lines
4.5 KiB
C#
using ZB.MOM.WW.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.AuditLog.Redaction;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Redaction;
|
|
|
|
/// <summary>
|
|
/// Tests for
|
|
/// <see cref="SafeDefaultAuditRedactor"/> — the minimal always-safe
|
|
/// <see cref="ZB.MOM.WW.Audit.IAuditRedactor"/> fallback.
|
|
/// Header-only scrub of the always-sensitive default headers inside
|
|
/// <c>DetailsJson</c>'s RequestSummary / ResponseSummary; never throws, never
|
|
/// performs body / SQL / truncation work.
|
|
/// </summary>
|
|
public class SafeDefaultAuditRedactorTests
|
|
{
|
|
private static AuditEvent EventWith(string? request = null, string? response = null)
|
|
{
|
|
var details = new AuditDetails
|
|
{
|
|
RequestSummary = request,
|
|
ResponseSummary = response,
|
|
};
|
|
return new AuditEvent
|
|
{
|
|
EventId = Guid.NewGuid(),
|
|
OccurredAtUtc = DateTimeOffset.UtcNow,
|
|
Actor = "tester",
|
|
Action = "ApiOutbound.ApiCall",
|
|
Outcome = AuditOutcome.Success,
|
|
DetailsJson = AuditDetailsCodec.Serialize(details),
|
|
};
|
|
}
|
|
|
|
private static AuditDetails Details(AuditEvent evt) =>
|
|
AuditDetailsCodec.Deserialize(evt.DetailsJson);
|
|
|
|
/// <summary>Verifies that the default sensitive headers are redacted in the request summary.</summary>
|
|
[Fact]
|
|
public void Redacts_DefaultSensitiveHeaders_InRequestSummary()
|
|
{
|
|
var evt = EventWith(request: "Authorization: Bearer secret-token\nContent-Type: application/json");
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
var d = Details(result);
|
|
Assert.Contains("Authorization: <redacted>", d.RequestSummary!);
|
|
Assert.DoesNotContain("secret-token", d.RequestSummary!);
|
|
Assert.Contains("Content-Type: application/json", d.RequestSummary!);
|
|
}
|
|
|
|
/// <summary>Verifies that the default sensitive headers are redacted in the response summary.</summary>
|
|
[Fact]
|
|
public void Redacts_DefaultSensitiveHeaders_InResponseSummary()
|
|
{
|
|
var evt = EventWith(response: "Set-Cookie: sessionid=abc123\nX-Other: ok");
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
var d = Details(result);
|
|
Assert.Contains("Set-Cookie: <redacted>", d.ResponseSummary!);
|
|
Assert.DoesNotContain("abc123", d.ResponseSummary!);
|
|
Assert.Contains("X-Other: ok", d.ResponseSummary!);
|
|
}
|
|
|
|
/// <summary>Verifies that header names are matched case-insensitively when redacting.</summary>
|
|
[Fact]
|
|
public void CaseInsensitive_HeaderName_Redacted()
|
|
{
|
|
var evt = EventWith(request: "authorization: Bearer x-y-z");
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
Assert.Contains("<redacted>", Details(result).RequestSummary!);
|
|
Assert.DoesNotContain("x-y-z", Details(result).RequestSummary!);
|
|
}
|
|
|
|
/// <summary>Verifies that a non-sensitive header is preserved unchanged.</summary>
|
|
[Fact]
|
|
public void NonSensitiveHeader_Preserved()
|
|
{
|
|
var evt = EventWith(request: "X-Request-Id: abc-123\nAccept: application/json");
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
var d = Details(result);
|
|
Assert.Contains("X-Request-Id: abc-123", d.RequestSummary!);
|
|
Assert.Contains("Accept: application/json", d.RequestSummary!);
|
|
}
|
|
|
|
/// <summary>Verifies that an event with null details takes the fast path and returns the same instance.</summary>
|
|
[Fact]
|
|
public void NullDetails_FastPath_ReturnsSameInstance()
|
|
{
|
|
var evt = new AuditEvent
|
|
{
|
|
EventId = Guid.NewGuid(),
|
|
OccurredAtUtc = DateTimeOffset.UtcNow,
|
|
Actor = "tester",
|
|
Action = "ApiOutbound.ApiCall",
|
|
Outcome = AuditOutcome.Success,
|
|
DetailsJson = null,
|
|
};
|
|
|
|
var result = SafeDefaultAuditRedactor.Instance.Apply(evt);
|
|
|
|
Assert.Same(evt, result);
|
|
}
|
|
|
|
/// <summary>Verifies that malformed details JSON never causes the redactor to throw.</summary>
|
|
[Fact]
|
|
public void MalformedDetailsJson_NeverThrows()
|
|
{
|
|
var evt = new AuditEvent
|
|
{
|
|
EventId = Guid.NewGuid(),
|
|
OccurredAtUtc = DateTimeOffset.UtcNow,
|
|
Actor = "tester",
|
|
Action = "ApiOutbound.ApiCall",
|
|
Outcome = AuditOutcome.Success,
|
|
DetailsJson = "{not valid json]",
|
|
};
|
|
|
|
var ex = Record.Exception(() => SafeDefaultAuditRedactor.Instance.Apply(evt));
|
|
|
|
Assert.Null(ex);
|
|
}
|
|
}
|