Skip to content

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

Fix issue with vault upgrade that used the wrong migration key (#981)

Leendert de Borst299d1f607573
diff --git a/apps/browser-extension/src/utils/dist/shared/vault-sql/index.js b/apps/browser-extension/src/utils/dist/shared/vault-sql/index.js
index 81cf779c2..2a38bcee7 100644
--- a/apps/browser-extension/src/utils/dist/shared/vault-sql/index.js
+++ b/apps/browser-extension/src/utils/dist/shared/vault-sql/index.js
@@ -593,7 +593,8 @@ var VaultSqlGenerator = class {
);
const sqlCommands = [];
for (const migration of migrationsToApply) {
- const migrationSql = MIGRATION_SCRIPTS[migration.revision];
+ const migrationKey = migration.revision - 1;
+ const migrationSql = MIGRATION_SCRIPTS[migrationKey];
if (migrationSql) {
sqlCommands.push(migrationSql);
}
diff --git a/apps/browser-extension/src/utils/dist/shared/vault-sql/index.mjs b/apps/browser-extension/src/utils/dist/shared/vault-sql/index.mjs
index bf56300b5..264c4abf6 100644
--- a/apps/browser-extension/src/utils/dist/shared/vault-sql/index.mjs
+++ b/apps/browser-extension/src/utils/dist/shared/vault-sql/index.mjs
@@ -564,7 +564,8 @@ var VaultSqlGenerator = class {
);
const sqlCommands = [];
for (const migration of migrationsToApply) {
- const migrationSql = MIGRATION_SCRIPTS[migration.revision];
+ const migrationKey = migration.revision - 1;
+ const migrationSql = MIGRATION_SCRIPTS[migrationKey];
if (migrationSql) {
sqlCommands.push(migrationSql);
}
diff --git a/apps/mobile-app/utils/dist/shared/vault-sql/index.js b/apps/mobile-app/utils/dist/shared/vault-sql/index.js
index 81cf779c2..2a38bcee7 100644
--- a/apps/mobile-app/utils/dist/shared/vault-sql/index.js
+++ b/apps/mobile-app/utils/dist/shared/vault-sql/index.js
@@ -593,7 +593,8 @@ var VaultSqlGenerator = class {
);
const sqlCommands = [];
for (const migration of migrationsToApply) {
- const migrationSql = MIGRATION_SCRIPTS[migration.revision];
+ const migrationKey = migration.revision - 1;
+ const migrationSql = MIGRATION_SCRIPTS[migrationKey];
if (migrationSql) {
sqlCommands.push(migrationSql);
}
diff --git a/apps/mobile-app/utils/dist/shared/vault-sql/index.mjs b/apps/mobile-app/utils/dist/shared/vault-sql/index.mjs
index bf56300b5..264c4abf6 100644
--- a/apps/mobile-app/utils/dist/shared/vault-sql/index.mjs
+++ b/apps/mobile-app/utils/dist/shared/vault-sql/index.mjs
@@ -564,7 +564,8 @@ var VaultSqlGenerator = class {
);
const sqlCommands = [];
for (const migration of migrationsToApply) {
- const migrationSql = MIGRATION_SCRIPTS[migration.revision];
+ const migrationKey = migration.revision - 1;
+ const migrationSql = MIGRATION_SCRIPTS[migrationKey];
if (migrationSql) {
sqlCommands.push(migrationSql);
}
diff --git a/apps/server/AliasVault.Client/wwwroot/js/dist/shared/vault-sql/index.js b/apps/server/AliasVault.Client/wwwroot/js/dist/shared/vault-sql/index.js
index 81cf779c2..2a38bcee7 100644
--- a/apps/server/AliasVault.Client/wwwroot/js/dist/shared/vault-sql/index.js
+++ b/apps/server/AliasVault.Client/wwwroot/js/dist/shared/vault-sql/index.js
@@ -593,7 +593,8 @@ var VaultSqlGenerator = class {
);
const sqlCommands = [];
for (const migration of migrationsToApply) {
- const migrationSql = MIGRATION_SCRIPTS[migration.revision];
+ const migrationKey = migration.revision - 1;
+ const migrationSql = MIGRATION_SCRIPTS[migrationKey];
if (migrationSql) {
sqlCommands.push(migrationSql);
}
diff --git a/apps/server/AliasVault.Client/wwwroot/js/dist/shared/vault-sql/index.mjs b/apps/server/AliasVault.Client/wwwroot/js/dist/shared/vault-sql/index.mjs
index bf56300b5..264c4abf6 100644
--- a/apps/server/AliasVault.Client/wwwroot/js/dist/shared/vault-sql/index.mjs
+++ b/apps/server/AliasVault.Client/wwwroot/js/dist/shared/vault-sql/index.mjs
@@ -564,7 +564,8 @@ var VaultSqlGenerator = class {
);
const sqlCommands = [];
for (const migration of migrationsToApply) {
- const migrationSql = MIGRATION_SCRIPTS[migration.revision];
+ const migrationKey = migration.revision - 1;
+ const migrationSql = MIGRATION_SCRIPTS[migrationKey];
if (migrationSql) {
sqlCommands.push(migrationSql);
}
diff --git a/shared/vault-sql/build.sh b/shared/vault-sql/build.sh
index 10b89820b..76cae9df0 100755
--- a/shared/vault-sql/build.sh
+++ b/shared/vault-sql/build.sh
@@ -15,7 +15,7 @@ package_name="vault-sql"
package_path="."
echo "📦 Building $package_name..."
-npm install && npm run lint && npm run build
+npm install && npm run lint && npm run test && npm run build
dist_path="dist"
diff --git a/shared/vault-sql/src/__tests__/VaultSqlGenerator.test.ts b/shared/vault-sql/src/__tests__/VaultSqlGenerator.test.ts
index a9dddce38..1352767e5 100644
--- a/shared/vault-sql/src/__tests__/VaultSqlGenerator.test.ts
+++ b/shared/vault-sql/src/__tests__/VaultSqlGenerator.test.ts
@@ -13,10 +13,10 @@ describe('VaultSqlGenerator', () => {
expect(result.version).toBe(generator.getLatestVersion().version);
expect(result.migrationNumber).toBe(generator.getLatestVersion().revision);
- // Should include PRAGMA and schema creation
- expect(result.sqlCommands[0]).toBe('PRAGMA foreign_keys = ON;');
+ // Should include schema creation
+ expect(result.sqlCommands[0]).toContain('CREATE TABLE');
expect(result.sqlCommands.some(cmd => cmd.includes('CREATE TABLE "Aliases"'))).toBe(true);
- expect(result.sqlCommands.some(cmd => cmd.includes('vault_version'))).toBe(true);
+ expect(result.sqlCommands.some(cmd => cmd.includes('__EFMigrationsHistory'))).toBe(true);
});
});
@@ -36,9 +36,9 @@ describe('VaultSqlGenerator', () => {
expect(result.success).toBe(true);
expect(result.sqlCommands.length).toBeGreaterThan(0);
- // Should include PRAGMA and version updates
- expect(result.sqlCommands[0]).toBe('PRAGMA foreign_keys = ON;');
- expect(result.sqlCommands.some(cmd => cmd.includes('vault_version'))).toBe(true);
+ // Should include migration SQL
+ expect(result.sqlCommands[0]).toContain('BEGIN TRANSACTION');
+ expect(result.sqlCommands.some(cmd => cmd.includes('__EFMigrationsHistory'))).toBe(true);
});
it('should handle invalid target migration number', () => {
@@ -47,6 +47,21 @@ describe('VaultSqlGenerator', () => {
expect(result.success).toBe(false);
expect(result.error).toContain('not found');
});
+
+ it('should handle upgrade from revision 9 to 10 without returning null sqlCommands', () => {
+ const result = generator.getUpgradeVaultSql(9, 10);
+
+ expect(result.success).toBe(true);
+ expect(result.sqlCommands).not.toBeNull();
+ expect(Array.isArray(result.sqlCommands)).toBe(true);
+
+ expect(result.sqlCommands.length).toBeGreaterThan(0);
+ expect(result.sqlCommands[0]).toContain('BEGIN TRANSACTION');
+ expect(result.sqlCommands.some(cmd => cmd.includes('__EFMigrationsHistory'))).toBe(true);
+
+ expect(result.version).toBeDefined();
+ expect(result.migrationNumber).toBe(10);
+ });
});
describe('getUpgradeToVersionSql', () => {
@@ -127,12 +142,19 @@ describe('VaultSqlGenerator', () => {
expect(isValid).toBe(false);
});
- it('should handle case-insensitive table names', () => {
- const tables = ['aliases', 'services', 'credentials', 'passwords'];
+ it('should handle case-sensitive table names', () => {
+ const tables = ['Aliases', 'Services', 'Credentials', 'Passwords', 'Settings'];
const isValid = generator.validateVaultStructure(tables);
expect(isValid).toBe(true);
});
+
+ it('should reject case-mismatched table names', () => {
+ const tables = ['aliases', 'services', 'credentials', 'passwords', 'settings'];
+ const isValid = generator.validateVaultStructure(tables);
+
+ expect(isValid).toBe(false);
+ });
});
describe('utility methods', () => {
@@ -153,7 +175,8 @@ describe('VaultSqlGenerator', () => {
const sql = generator.getMigrationSql(1);
expect(sql).toBeDefined();
- expect(sql).toContain('CREATE TABLE');
+ expect(sql).toContain('BEGIN TRANSACTION');
+ expect(sql).toContain('__EFMigrationsHistory');
});
it('should return complete schema SQL', () => {
diff --git a/shared/vault-sql/src/sql/VaultSqlGenerator.ts b/shared/vault-sql/src/sql/VaultSqlGenerator.ts
index 2d5c80751..ad813b887 100644
--- a/shared/vault-sql/src/sql/VaultSqlGenerator.ts
+++ b/shared/vault-sql/src/sql/VaultSqlGenerator.ts
@@ -84,7 +84,11 @@ export class VaultSqlGenerator {
};
}
- // Get migrations to apply
+ /*
+ * Get migrations to apply.
+ * Note: when migrating from 9 to 10, we need to execute migration 9 (not 10)
+ * because the migration key represents the migration that gets you FROM the source to the next version.
+ */
const migrationsToApply = VAULT_VERSIONS.filter(
v => v.revision > currentMigrationNumber &&
v.revision <= targetMigration
@@ -94,7 +98,9 @@ export class VaultSqlGenerator {
// Add migration SQL commands
for (const migration of migrationsToApply) {
- const migrationSql = MIGRATION_SCRIPTS[migration.revision];
+ // Use the previous migration number as the key
+ const migrationKey = migration.revision - 1;
+ const migrationSql = MIGRATION_SCRIPTS[migrationKey];
if (migrationSql) {
sqlCommands.push(migrationSql);
}