Skip to content

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

Add AvexConstants.cs (#773)

Leendert de Borst456dcd261213
diff --git a/apps/server/AliasVault.Client/Services/Crypto/AvexCryptoService.cs b/apps/server/AliasVault.Client/Services/Crypto/AvexCryptoService.cs
index 7e69dd737..d263461f8 100644
--- a/apps/server/AliasVault.Client/Services/Crypto/AvexCryptoService.cs
+++ b/apps/server/AliasVault.Client/Services/Crypto/AvexCryptoService.cs
@@ -10,7 +10,9 @@ namespace AliasVault.Client.Services.Crypto;
using System.Text;
using System.Text.Json;
using AliasVault.Client.Services.JsInterop;
+using AliasVault.ImportExport.Constants;
using AliasVault.ImportExport.Models.Exports;
+using AliasVault.Shared.Core;
/// <summary>
/// Provides cryptographic operations for .avex encrypted vault export format.
@@ -18,7 +20,6 @@ using AliasVault.ImportExport.Models.Exports;
/// </summary>
public class AvexCryptoService
{
- private const string HeaderDelimiter = "\n--- ENCRYPTED PAYLOAD FOLLOWS ---\n";
private readonly JsInteropService jsInteropService;
/// <summary>
@@ -57,8 +58,8 @@ public class AvexCryptoService
// 4. Create header
var header = new AvexHeader
{
- Format = "avex",
- Version = "1.0.0",
+ Format = AvexConstants.FormatIdentifier,
+ Version = AvexConstants.FormatVersion,
Kdf = new KdfParams
{
Type = AliasVault.Cryptography.Client.Defaults.EncryptionType, // Argon2Id
@@ -79,6 +80,7 @@ public class AvexCryptoService
{
ExportedAt = DateTime.UtcNow,
ExportedBy = username,
+ AppVersion = AppInfo.GetFullVersion(),
},
};
@@ -91,7 +93,7 @@ public class AvexCryptoService
var headerJson = JsonSerializer.Serialize(header, jsonOptions);
var headerBytes = Encoding.UTF8.GetBytes(headerJson);
- var delimiterBytes = Encoding.UTF8.GetBytes(HeaderDelimiter);
+ var delimiterBytes = Encoding.UTF8.GetBytes(AvexConstants.HeaderDelimiter);
// 6. Calculate offset
header.Encryption.EncryptedDataOffset = headerBytes.Length + delimiterBytes.Length;
@@ -121,9 +123,9 @@ public class AvexCryptoService
var (header, payloadOffset) = ParseAvexHeader(avexBytes);
// 2. Validate version
- if (header.Version != "1.0.0")
+ if (header.Version != AvexConstants.FormatVersion)
{
- throw new InvalidOperationException($"Unsupported .avex version: {header.Version}");
+ throw new InvalidOperationException($"Unsupported .avex version: {header.Version}. Expected {AvexConstants.FormatVersion}.");
}
// 3. Extract encrypted payload
@@ -163,7 +165,7 @@ public class AvexCryptoService
/// </summary>
private static (AvexHeader Header, long PayloadOffset) ParseAvexHeader(byte[] avexBytes)
{
- var delimiterBytes = Encoding.UTF8.GetBytes(HeaderDelimiter);
+ var delimiterBytes = Encoding.UTF8.GetBytes(AvexConstants.HeaderDelimiter);
var delimiterIndex = IndexOf(avexBytes, delimiterBytes);
if (delimiterIndex == -1)
@@ -182,9 +184,9 @@ public class AvexCryptoService
var header = JsonSerializer.Deserialize<AvexHeader>(headerJson, jsonOptions);
- if (header == null || header.Format != "avex")
+ if (header == null || header.Format != AvexConstants.FormatIdentifier)
{
- throw new InvalidOperationException("Invalid .avex file: malformed header");
+ throw new InvalidOperationException($"Invalid .avex file: expected format '{AvexConstants.FormatIdentifier}', got '{header?.Format ?? "null"}'");
}
var payloadOffset = delimiterIndex + delimiterBytes.Length;
diff --git a/apps/server/Utilities/AliasVault.ImportExport/Constants/AvexConstants.cs b/apps/server/Utilities/AliasVault.ImportExport/Constants/AvexConstants.cs
new file mode 100644
index 000000000..61c742267
--- /dev/null
+++ b/apps/server/Utilities/AliasVault.ImportExport/Constants/AvexConstants.cs
@@ -0,0 +1,36 @@
+//-----------------------------------------------------------------------
+// <copyright file="AvexConstants.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.ImportExport.Constants;
+
+/// <summary>
+/// Constants for the .avex (AliasVault Encrypted eXport) file format.
+/// </summary>
+public static class AvexConstants
+{
+ /// <summary>
+ /// The delimiter that separates the JSON header from the encrypted payload in .avex files.
+ /// Follows a similar pattern to PEM-encoded certificates for industry familiarity.
+ /// </summary>
+ public const string HeaderDelimiter = "\n-----BEGIN ENCRYPTED DATA-----\n";
+
+ /// <summary>
+ /// The .avex file format identifier.
+ /// </summary>
+ public const string FormatIdentifier = "avex";
+
+ /// <summary>
+ /// The .avex format version.
+ /// This represents the structure version of the .avex container format itself,
+ /// independent of the AliasVault application version that created the export.
+ /// Note: when changing the format version, also update the import logic to support
+ /// both the old and new format versions, as all current logic checks for format version 1.0.0 explicitly.
+ /// Version history:
+ /// - 1.0.0: Initial .avex format with Argon2id KDF + AES-256-GCM encryption.
+ /// </summary>
+ public const string FormatVersion = "1.0.0";
+}
diff --git a/apps/server/Utilities/AliasVault.ImportExport/Models/Exports/AvexHeader.cs b/apps/server/Utilities/AliasVault.ImportExport/Models/Exports/AvexHeader.cs
index 0724617c5..ceeb727a5 100644
--- a/apps/server/Utilities/AliasVault.ImportExport/Models/Exports/AvexHeader.cs
+++ b/apps/server/Utilities/AliasVault.ImportExport/Models/Exports/AvexHeader.cs
@@ -7,6 +7,8 @@
namespace AliasVault.ImportExport.Models.Exports;
+using AliasVault.ImportExport.Constants;
+
/// <summary>
/// Represents the header for an .avex (encrypted export) file.
/// </summary>
@@ -15,12 +17,12 @@ public class AvexHeader
/// <summary>
/// Gets or sets the file format identifier.
/// </summary>
- public string Format { get; set; } = "avex";
+ public string Format { get; set; } = AvexConstants.FormatIdentifier;
/// <summary>
- /// Gets or sets the format version.
+ /// Gets or sets the .avex container format version (not the app version).
/// </summary>
- public string Version { get; set; } = "1.0.0";
+ public string Version { get; set; } = AvexConstants.FormatVersion;
/// <summary>
/// Gets or sets the key derivation function parameters.
@@ -89,4 +91,10 @@ public class AvexMetadata
/// Gets or sets the username who created the export.
/// </summary>
public string ExportedBy { get; set; } = string.Empty;
+
+ /// <summary>
+ /// Gets or sets the AliasVault application version that created this export.
+ /// This helps with troubleshooting and understanding which version generated the export.
+ /// </summary>
+ public string? AppVersion { get; set; }
}
diff --git a/apps/server/Utilities/AliasVault.ImportExport/VaultEncryptedExportService.cs b/apps/server/Utilities/AliasVault.ImportExport/VaultEncryptedExportService.cs
index 1ad71cbe3..bf1913514 100644
--- a/apps/server/Utilities/AliasVault.ImportExport/VaultEncryptedExportService.cs
+++ b/apps/server/Utilities/AliasVault.ImportExport/VaultEncryptedExportService.cs
@@ -11,14 +11,15 @@ using System.Text;
using System.Text.Json;
using AliasVault.Cryptography.Client;
using AliasVault.Cryptography.Server;
+using AliasVault.ImportExport.Constants;
using AliasVault.ImportExport.Models.Exports;
+using AliasVault.Shared.Core;
/// <summary>
/// Service for creating encrypted .avex export files.
/// </summary>
public static class VaultEncryptedExportService
{
- private const string HeaderDelimiter = "\n--- ENCRYPTED PAYLOAD FOLLOWS ---\n";
/// <summary>
/// Exports vault data to .avex (AliasVault Encrypted eXport) format.
@@ -60,8 +61,8 @@ public static class VaultEncryptedExportService
// 4. Create the header
var header = new AvexHeader
{
- Format = "avex",
- Version = "1.0.0",
+ Format = AvexConstants.FormatIdentifier,
+ Version = AvexConstants.FormatVersion,
Kdf = new KdfParams
{
Type = Defaults.EncryptionType,
@@ -82,6 +83,7 @@ public static class VaultEncryptedExportService
{
ExportedAt = DateTime.UtcNow,
ExportedBy = username,
+ AppVersion = AppInfo.GetFullVersion(),
},
};
@@ -94,7 +96,7 @@ public static class VaultEncryptedExportService
var headerJson = JsonSerializer.Serialize(header, jsonOptions);
var headerBytes = Encoding.UTF8.GetBytes(headerJson);
- var delimiterBytes = Encoding.UTF8.GetBytes(HeaderDelimiter);
+ var delimiterBytes = Encoding.UTF8.GetBytes(AvexConstants.HeaderDelimiter);
// 6. Calculate the offset where encrypted data begins
header.Encryption.EncryptedDataOffset = headerBytes.Length + delimiterBytes.Length;
diff --git a/apps/server/Utilities/AliasVault.ImportExport/VaultEncryptedImportService.cs b/apps/server/Utilities/AliasVault.ImportExport/VaultEncryptedImportService.cs
index 464c2a404..cde8c9c83 100644
--- a/apps/server/Utilities/AliasVault.ImportExport/VaultEncryptedImportService.cs
+++ b/apps/server/Utilities/AliasVault.ImportExport/VaultEncryptedImportService.cs
@@ -12,6 +12,7 @@ using System.Text;
using System.Text.Json;
using AliasVault.Cryptography.Client;
using AliasVault.Cryptography.Server;
+using AliasVault.ImportExport.Constants;
using AliasVault.ImportExport.Models.Exports;
/// <summary>
@@ -19,7 +20,6 @@ using AliasVault.ImportExport.Models.Exports;
/// </summary>
public static class VaultEncryptedImportService
{
- private const string HeaderDelimiter = "\n--- ENCRYPTED PAYLOAD FOLLOWS ---\n";
/// <summary>
/// Checks if the provided file bytes represent an .avex encrypted export.
@@ -39,8 +39,8 @@ public static class VaultEncryptedImportService
var headerLength = Math.Min(500, fileBytes.Length);
var headerText = Encoding.UTF8.GetString(fileBytes, 0, headerLength);
- return headerText.Contains("\"format\": \"avex\"") ||
- headerText.Contains("\"format\":\"avex\"");
+ return headerText.Contains($"\"format\": \"{AvexConstants.FormatIdentifier}\"") ||
+ headerText.Contains($"\"format\":\"{AvexConstants.FormatIdentifier}\"");
}
catch
{
@@ -70,9 +70,9 @@ public static class VaultEncryptedImportService
var (header, payloadOffset) = ParseAvexHeader(avexBytes);
// 2. Validate version
- if (header.Version != "1.0.0")
+ if (header.Version != AvexConstants.FormatVersion)
{
- throw new InvalidOperationException($"Unsupported .avex version: {header.Version}. Expected 1.0.0.");
+ throw new InvalidOperationException($"Unsupported .avex version: {header.Version}. Expected {AvexConstants.FormatVersion}.");
}
// 3. Validate encryption algorithm
@@ -121,7 +121,7 @@ public static class VaultEncryptedImportService
private static (AvexHeader Header, long PayloadOffset) ParseAvexHeader(byte[] avexBytes)
{
// Find the delimiter
- var delimiterBytes = Encoding.UTF8.GetBytes(HeaderDelimiter);
+ var delimiterBytes = Encoding.UTF8.GetBytes(AvexConstants.HeaderDelimiter);
var delimiterIndex = IndexOf(avexBytes, delimiterBytes);
if (delimiterIndex == -1)
@@ -147,9 +147,9 @@ public static class VaultEncryptedImportService
throw new InvalidOperationException("Invalid .avex file: failed to parse header JSON");
}
- if (header.Format != "avex")
+ if (header.Format != AvexConstants.FormatIdentifier)
{
- throw new InvalidOperationException($"Invalid .avex file: expected format 'avex', got '{header.Format}'");
+ throw new InvalidOperationException($"Invalid .avex file: expected format '{AvexConstants.FormatIdentifier}', got '{header.Format}'");
}
// Calculate payload offset