52 lines
2.5 KiB
C#
52 lines
2.5 KiB
C#
using Heimdall.Application.Cameras.Commands.CreateCamera;
|
|
using Heimdall.Application.Cameras.Commands.UpdateCamera;
|
|
using Heimdall.Api.Filters;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using Microsoft.AspNetCore.Routing;
|
|
using Microsoft.Extensions.Logging;
|
|
using Shouldly;
|
|
|
|
namespace Heimdall.Web.Tests.Security;
|
|
|
|
public sealed class SensitiveAuditTests
|
|
{
|
|
[Fact]
|
|
public async Task Camera_creation_and_update_audits_do_not_persist_stream_credentials()
|
|
{
|
|
using var f = new AccessFixture();
|
|
const string original = "rtsp://user:ORIGINAL_SECRET@camera/stream?token=PRIVATE_TOKEN";
|
|
const string replacement = "rtsp://user:REPLACEMENT_SECRET@camera/stream";
|
|
var created = await new CreateCameraCommandHandler(f.Db).Handle(new(f.A.Id, "camera", original, null, null), default);
|
|
created.IsSuccess.ShouldBeTrue();
|
|
var updated = await new UpdateCameraCommandHandler(f.Db).Handle(new(created.Data!.Id, "renamed", replacement, null), default);
|
|
updated.IsSuccess.ShouldBeTrue();
|
|
var audit = string.Join("\n", f.Db.AuditLogs.Select(a => (a.OldValue ?? "") + (a.NewValue ?? "")));
|
|
audit.ShouldNotContain("SECRET"); audit.ShouldNotContain("PRIVATE_TOKEN"); audit.ShouldNotContain("rtsp://");
|
|
audit.ShouldContain("renamed");
|
|
}
|
|
|
|
[Fact]
|
|
public void Http_exception_logs_keep_trace_without_sensitive_exception_content()
|
|
{
|
|
var logger = new CaptureLogger<ExceptionFilter>();
|
|
var context = new ExceptionContext(new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor()), [])
|
|
{ Exception = new InvalidOperationException("smtp password=SERVER_SECRET") };
|
|
new ExceptionFilter(logger).OnException(context);
|
|
string.Join("\n", logger.Messages).ShouldNotContain("SERVER_SECRET");
|
|
string.Join("\n", logger.Messages).ShouldContain(nameof(InvalidOperationException));
|
|
context.ExceptionHandled.ShouldBeTrue();
|
|
}
|
|
}
|
|
|
|
public sealed class CaptureLogger<T> : ILogger<T>
|
|
{
|
|
public List<string> Messages { get; } = [];
|
|
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
|
public bool IsEnabled(LogLevel logLevel) => true;
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
|
=> Messages.Add(formatter(state, exception) + exception?.ToString());
|
|
}
|