Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add vault version check to webapi to prevent conflicts (#541)
Leendert de Borste62dcd532786
diff --git a/src/AliasVault.Api/Controllers/VaultController.cs b/src/AliasVault.Api/Controllers/VaultController.csindex c6309f69e..3345aafcf 100644--- a/src/AliasVault.Api/Controllers/VaultController.cs+++ b/src/AliasVault.Api/Controllers/VaultController.cs@@ -214,7 +214,13 @@ public class VaultController(ILogger<VaultController> logger, IAliasServerDbCont}// Retrieve latest vault of user which contains the current encryption settings.- var latestVault = user.Vaults.OrderByDescending(x => x.RevisionNumber).Select(x => new { x.Salt, x.Verifier, x.EncryptionType, x.EncryptionSettings, x.RevisionNumber }).First();+ var latestVault = user.Vaults.OrderByDescending(x => x.RevisionNumber).Select(x => new { x.Salt, x.Verifier, x.EncryptionType, x.EncryptionSettings, x.RevisionNumber, x.Version }).First();++ // Reject vaults with a version that is lower than the last vault version.+ if (VersionHelper.IsVersionOlder(model.Version, latestVault.Version))+ {+ return BadRequest("The uploaded vault version is lower than the last vault version. Please update and/or refresh your client.");+ }// Calculate the new revision number for the vault.var newRevisionNumber = model.CurrentRevisionNumber + 1;diff --git a/src/AliasVault.Api/Helpers/VersionHelper.cs b/src/AliasVault.Api/Helpers/VersionHelper.csnew file mode 100644index 000000000..b692bd470--- /dev/null+++ b/src/AliasVault.Api/Helpers/VersionHelper.cs@@ -0,0 +1,38 @@+//-----------------------------------------------------------------------+// <copyright file="VersionHelper.cs" company="lanedirt">+// Copyright (c) lanedirt. All rights reserved.+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.Api.Helpers;++/// <summary>+/// VersionHelper class which contains helper methods for version comparison.+/// </summary>+public static class VersionHelper+{+ /// <summary>+ /// Checks if version1 is older than version2.+ /// </summary>+ /// <param name="version1">The first version.</param>+ /// <param name="version2">The second version.</param>+ /// <returns>True if version1 is older than version2, false otherwise.</returns>+ public static bool IsVersionOlder(string version1, string version2)+ {+ if (string.IsNullOrEmpty(version1) || string.IsNullOrEmpty(version2))+ {+ return false;+ }++ // Try parsing both versions+ if (!Version.TryParse(version1, out Version? v1) || !Version.TryParse(version2, out Version? v2))+ {+ // If one of the versions is not a valid version string, throw an exception.+ throw new ArgumentException("Invalid version string.");+ }++ // Compare the versions+ return v1 < v2;+ }+}diff --git a/src/Tests/AliasVault.UnitTests/Utilities/ConversionUtilityTest.cs b/src/Tests/AliasVault.UnitTests/Utilities/ConversionUtilityTest.csindex 06141dd35..2008239f4 100644--- a/src/Tests/AliasVault.UnitTests/Utilities/ConversionUtilityTest.cs+++ b/src/Tests/AliasVault.UnitTests/Utilities/ConversionUtilityTest.cs@@ -13,7 +13,7 @@ using AliasVault.Shared.Utilities;/// <summary>/// Tests for the AliasVault.CsvImportExport class./// </summary>-public partial class ConversionUtilityTest+public class ConversionUtilityTest{/// <summary>/// Tests the conversion of a simple anchor tag to open in a new tab.diff --git a/src/Tests/AliasVault.UnitTests/Vault/VersionTests.cs b/src/Tests/AliasVault.UnitTests/Vault/VersionTests.csnew file mode 100644index 000000000..5607a552b--- /dev/null+++ b/src/Tests/AliasVault.UnitTests/Vault/VersionTests.cs@@ -0,0 +1,49 @@+//-----------------------------------------------------------------------+// <copyright file="VersionTests.cs" company="lanedirt">+// Copyright (c) lanedirt. All rights reserved.+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.Tests.Vault;++using AliasVault.Api.Helpers;++/// <summary>+/// Tests for the Vault version comparison methods.+/// </summary>+public class VersionTests+{+ /// <summary>+ /// Test the version comparison for lower versions.+ /// </summary>+ [Test]+ public void VersionLowerComparisonTrueTest()+ {+ var version1 = "1.0.0";+ var version2 = "1.0.1";+ Assert.That(VersionHelper.IsVersionOlder(version1, version2), Is.True);+ }++ /// <summary>+ /// Test the version comparison for higher versions.+ /// </summary>+ [Test]+ public void VersionLowerComparisonFalseTest()+ {+ var version1 = "1.2.0";+ var version2 = "1.0.1";+ Assert.That(VersionHelper.IsVersionOlder(version1, version2), Is.False);+ }++ /// <summary>+ /// Test the version comparison throws an exception for illegal version strings.+ /// </summary>+ [Test]+ public void VersionLowerComparisonExceptionTest()+ {+ var version1 = "1.2.0.5.1";+ var version2 = "1.0.1";+ Assert.Throws<ArgumentException>(() => VersionHelper.IsVersionOlder(version1, version2));+ }+}