Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update logging config

Leendert de Borst4a1874034f50
diff --git a/apps/server/Databases/AliasServerDb/AuthLog.cs b/apps/server/Databases/AliasServerDb/AuthLog.cs
index adc9b787f..081fe0b6c 100644
--- a/apps/server/Databases/AliasServerDb/AuthLog.cs
+++ b/apps/server/Databases/AliasServerDb/AuthLog.cs
@@ -108,6 +108,11 @@ public class AuthLog
/// </summary>
public const int ClientMaxLength = 100;
+ /// <summary>
+ /// Maximum stored length of <see cref="Username"/>.
+ /// </summary>
+ public const int UsernameMaxLength = 255;
+
/// <summary>
/// Gets or sets the unique identifier for the authentication log entry.
/// </summary>
@@ -124,7 +129,7 @@ public class AuthLog
/// Gets or sets the username associated with the authentication event.
/// </summary>
[Required]
- [MaxLength(255)]
+ [MaxLength(UsernameMaxLength)]
public string Username { get; set; } = null!;
/// <summary>
diff --git a/apps/server/Tests/AliasVault.UnitTests/Utilities/ControlCharacterEscapingEnricherTests.cs b/apps/server/Tests/AliasVault.UnitTests/Utilities/ControlCharacterEscapingEnricherTests.cs
new file mode 100644
index 000000000..733d41bdf
--- /dev/null
+++ b/apps/server/Tests/AliasVault.UnitTests/Utilities/ControlCharacterEscapingEnricherTests.cs
@@ -0,0 +1,44 @@
+//-----------------------------------------------------------------------
+// <copyright file="ControlCharacterEscapingEnricherTests.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.UnitTests.Utilities;
+
+using AliasVault.Logging;
+using Serilog.Events;
+using Serilog.Parsing;
+
+/// <summary>
+/// Tests that request-supplied log values cannot forge log lines.
+/// </summary>
+public class ControlCharacterEscapingEnricherTests
+{
+ /// <summary>
+ /// CR/LF and other control characters in a string property are escaped; other values are left as-is.
+ /// </summary>
+ [Test]
+ public void EscapesControlCharactersInStringProperties()
+ {
+ var logEvent = new LogEvent(
+ DateTimeOffset.UtcNow,
+ LogEventLevel.Warning,
+ null,
+ new MessageTemplateParser().Parse("{User} tried to claim invalid email: {Email}"),
+ [
+ new LogEventProperty("User", new ScalarValue("alice")),
+ new LogEventProperty("Email", new ScalarValue("x\r\n[12:00:00 ERR] forged\u0007")),
+ new LogEventProperty("Count", new ScalarValue(3)),
+ ]);
+
+ new ControlCharacterEscapingEnricher().Enrich(logEvent, null!);
+
+ Assert.Multiple(() =>
+ {
+ Assert.That(logEvent.RenderMessage(), Is.EqualTo("\"alice\" tried to claim invalid email: \"x\\r\\n[12:00:00 ERR] forged\\u0007\""));
+ Assert.That(((ScalarValue)logEvent.Properties["Count"]).Value, Is.EqualTo(3));
+ });
+ }
+}
diff --git a/apps/server/Utilities/AliasVault.Auth/AuthLoggingService.cs b/apps/server/Utilities/AliasVault.Auth/AuthLoggingService.cs
index 210efb9d7..50928c0a1 100644
--- a/apps/server/Utilities/AliasVault.Auth/AuthLoggingService.cs
+++ b/apps/server/Utilities/AliasVault.Auth/AuthLoggingService.cs
@@ -41,7 +41,7 @@ public class AuthLoggingService(IServiceProvider serviceProvider, IHttpContextAc
var authAttempt = new AuthLog
{
Timestamp = DateTime.UtcNow,
- Username = username,
+ Username = Truncate(username, AuthLog.UsernameMaxLength)!,
EventType = eventType,
IsSuccess = true,
FailureReason = null,
@@ -87,7 +87,7 @@ public class AuthLoggingService(IServiceProvider serviceProvider, IHttpContextAc
var authAttempt = new AuthLog
{
Timestamp = DateTime.UtcNow,
- Username = username,
+ Username = Truncate(username, AuthLog.UsernameMaxLength)!,
EventType = eventType,
IsSuccess = false,
FailureReason = failureReason,
diff --git a/apps/server/Utilities/AliasVault.Logging/ControlCharacterEscapingEnricher.cs b/apps/server/Utilities/AliasVault.Logging/ControlCharacterEscapingEnricher.cs
new file mode 100644
index 000000000..d6d0735de
--- /dev/null
+++ b/apps/server/Utilities/AliasVault.Logging/ControlCharacterEscapingEnricher.cs
@@ -0,0 +1,57 @@
+//-----------------------------------------------------------------------
+// <copyright file="ControlCharacterEscapingEnricher.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Logging;
+
+using System.Globalization;
+using System.Text;
+using Serilog.Core;
+using Serilog.Events;
+
+/// <summary>
+/// Escapes control characters in string properties.
+/// </summary>
+public sealed class ControlCharacterEscapingEnricher : ILogEventEnricher
+{
+ /// <inheritdoc />
+ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
+ {
+ List<LogEventProperty>? updates = null;
+ foreach (var (name, value) in logEvent.Properties)
+ {
+ if (value is ScalarValue { Value: string text } && text.Any(char.IsControl))
+ {
+ (updates ??= []).Add(new LogEventProperty(name, new ScalarValue(Escape(text))));
+ }
+ }
+
+ updates?.ForEach(logEvent.AddOrUpdateProperty);
+ }
+
+ /// <summary>
+ /// Replaces control characters with their escaped form (<c>\r</c>, <c>\n</c>, <c>\t</c> or <c>\uXXXX</c>).
+ /// </summary>
+ /// <param name="text">The value to escape.</param>
+ /// <returns>The escaped value.</returns>
+ internal static string Escape(string text)
+ {
+ var builder = new StringBuilder(text.Length + 8);
+ foreach (var c in text)
+ {
+ builder.Append(c switch
+ {
+ '\r' => "\\r",
+ '\n' => "\\n",
+ '\t' => "\\t",
+ _ when char.IsControl(c) => "\\u" + ((int)c).ToString("x4", CultureInfo.InvariantCulture),
+ _ => c.ToString(),
+ });
+ }
+
+ return builder.ToString();
+ }
+}
diff --git a/apps/server/Utilities/AliasVault.Logging/LoggingConfiguration.cs b/apps/server/Utilities/AliasVault.Logging/LoggingConfiguration.cs
index 7705e7729..dbb2ca5cc 100644
--- a/apps/server/Utilities/AliasVault.Logging/LoggingConfiguration.cs
+++ b/apps/server/Utilities/AliasVault.Logging/LoggingConfiguration.cs
@@ -70,6 +70,7 @@ public static class LoggingConfiguration
services.AddSerilog(new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.Enrich.FromLogContext()
+ .Enrich.With<ControlCharacterEscapingEnricher>()
.Enrich.WithProperty("Application", applicationName)
.Filter.ByIncludingOnly(GetSourceContextFilter(configuration))