Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update import/export flow for avex/avux to include new params (#2413)
Leendert de Borst79fdce406f2b
diff --git a/apps/web/src/components/settings/importexport/ImportServiceCard.tsx b/apps/web/src/components/settings/importexport/ImportServiceCard.tsxindex 802615da1..e19981d59 100644--- a/apps/web/src/components/settings/importexport/ImportServiceCard.tsx+++ b/apps/web/src/components/settings/importexport/ImportServiceCard.tsx@@ -385,7 +385,7 @@ const ImportServiceCard: React.FC<ImportServiceCardProps> = ({ serviceName, desc};/**- * Fetch favicons for the credentials that do not carry one and have a URL, one fetch per unique domain.+ * Fetch favicons for the credentials that do not carry a logo and have a URL, one fetch per unique domain.*/const extractFaviconsForCredentials = async (): Promise<void> => {setIsExtractingFavicons(true);@@ -395,7 +395,7 @@ const ImportServiceCard: React.FC<ImportServiceCardProps> = ({ serviceName, descfaviconExtractionAbort.current = abort;const targetsBySource = new Map<string, FaviconTarget>();- for (const credential of importedCredentials.filter(c => !c.FaviconBytes)) {+ for (const credential of importedCredentials.filter(c => !c.FaviconBytes && !c.AliasVaultItem?.Logo)) {const target = await selectFaviconTarget(credential.ServiceUrls ?? []);if (target && !targetsBySource.has(target.source)) {targetsBySource.set(target.source, target);diff --git a/apps/web/src/pages/settings/importexport/ImportExport.tsx b/apps/web/src/pages/settings/importexport/ImportExport.tsxindex 9e59346eb..38175fe27 100644--- a/apps/web/src/pages/settings/importexport/ImportExport.tsx+++ b/apps/web/src/pages/settings/importexport/ImportExport.tsx@@ -83,7 +83,7 @@ const ImportExport: React.FC = () => {throw new Error('Vault is not available');}const data = sqliteClient.importExport.getExportData();- return AvuxExportService.exportToAvux(data.items, data.folders, data.tags, data.itemTags, data.fieldDefinitions, data.logos, username ?? '');+ return AvuxExportService.exportToAvux(data.items, data.folders, data.tags, data.itemTags, data.fieldDefinitions, username ?? '');};/**diff --git a/core/client/src/database/__tests__/ExportScoping.test.ts b/core/client/src/database/__tests__/ExportScoping.test.tsindex 170ccdf04..ff104ef4b 100644--- a/core/client/src/database/__tests__/ExportScoping.test.ts+++ b/core/client/src/database/__tests__/ExportScoping.test.ts@@ -129,7 +129,7 @@ describe('vault export scoping', () => {expect(data.tags.map(tag => tag.Name)).toEqual(['personal tag']);expect(data.itemTags).toHaveLength(1);expect(data.fieldDefinitions.map(definition => definition.Label)).toEqual(['personal field']);- expect(data.logos.map(logo => logo.Source)).toEqual(['personal.example']);+ expect(data.items[0].Logo?.Source).toBe('personal.example');});it('writes only the personal manifest to a CSV export, archived items included', async () => {diff --git a/core/client/src/database/repositories/ImportExportRepository.ts b/core/client/src/database/repositories/ImportExportRepository.tsindex becb5cb07..734c97bc3 100644--- a/core/client/src/database/repositories/ImportExportRepository.ts+++ b/core/client/src/database/repositories/ImportExportRepository.ts@@ -1,13 +1,12 @@-import { LogoKinds, type ItemType } from '@aliasvault/models/vault';+import { LogoKinds, type ItemType, type LogoKind } from '@aliasvault/models/vault';import { AliasVaultCsvExportService } from '../../transfer/export/AliasVaultCsvExportService';import { fromStandardFormat, toStandardFormat } from '../../utilities/DateFormatter';import { BaseRepository } from '../BaseRepository';import { scopedKey } from '../ItemRef';-import { AttachmentQueries, FieldDefinitionQueries, FieldValueQueries, ItemQueries, TotpCodeQueries } from '../queries/ItemQueries';-import { PasskeyQueries } from '../queries/PasskeyQueries';+import { AttachmentQueries, FieldDefinitionQueries, FieldHistoryQueries, FieldValueQueries, TotpCodeQueries } from '../queries/ItemQueries';-import type { AttachmentEntity, FieldDefinitionEntity, FieldValueEntity, FolderEntity, ItemEntity, ItemTagEntity, LogoEntity, PasskeyEntity, TagEntity, TotpCodeEntity } from '../../transfer/shared/VaultEntities';+import type { AttachmentEntity, FieldDefinitionEntity, FieldHistoryEntity, FieldValueEntity, FolderEntity, ItemEntity, ItemTagEntity, LogoEntity, PasskeyEntity, TagEntity, TotpCodeEntity } from '../../transfer/shared/VaultEntities';import type { IDatabaseClient } from '../BaseRepository';import type { DbOp } from '../DbOp';import type { LogoRepository } from './LogoRepository';@@ -21,9 +20,26 @@ export type VaultExportData = {tags: TagEntity[];itemTags: ItemTagEntity[];fieldDefinitions: FieldDefinitionEntity[];- logos: LogoEntity[];};+/**+ * Keep track of what the import already wrote (shared and reused parent objects).+ */+export type ImportWriteSession = {+ /** Ids of the custom field definitions already written. */+ writtenDefinitionIds: Set<string>;+ /** Imported tag ids to the ids of the tags they were written as. */+ tagIds: Map<string, string>;+};++/**+ * Start an empty import write session.+ * @returns The session+ */+export function createImportWriteSession(): ImportWriteSession {+ return { writtenDefinitionIds: new Set(), tagIds: new Map() };+}+/** A row of a manifest-scoped child table, with the key of the item it hangs off. */type ChildRow = { ItemId: string; ManifestId: string };@@ -76,8 +92,8 @@ export class ImportExportRepository extends BaseRepository {public *getExportData(): DbOp<VaultExportData> {const manifestId = yield* this.exportManifestId();- const itemRows = yield* this.query<{ Id: string; ManifestId: string; Name: string | null; ItemType: string; FolderId: string | null; LogoId: string | null; CreatedAt: string; UpdatedAt: string }>(- 'SELECT Id, ManifestId, Name, ItemType, FolderId, LogoId, CreatedAt, UpdatedAt FROM Items WHERE IsDeleted = 0 AND DeletedAt IS NULL AND ManifestId = ? ORDER BY CreatedAt',+ const itemRows = yield* this.query<{ Id: string; ManifestId: string; Name: string | null; ItemType: string; FolderId: string | null; LogoId: string | null; ArchivedAt: string | null; CreatedAt: string; UpdatedAt: string }>(+ 'SELECT Id, ManifestId, Name, ItemType, FolderId, LogoId, ArchivedAt, CreatedAt, UpdatedAt FROM Items WHERE IsDeleted = 0 AND DeletedAt IS NULL AND ManifestId = ? ORDER BY CreatedAt',[manifestId]);@@ -118,6 +134,22 @@ export class ImportExportRepository extends BaseRepository {IsDeleted: false,}));+ const fieldHistoryRows = yield* this.query<ChildRow & { Id: string; FieldKey: string | null; FieldDefinitionId: string | null; ValueSnapshot: string; ChangedAt: string; CreatedAt: string; UpdatedAt: string }>(+ 'SELECT fh.Id, fh.ItemId, fh.ManifestId, fh.FieldKey, fh.FieldDefinitionId, fh.ValueSnapshot, fh.ChangedAt, fh.CreatedAt, fh.UpdatedAt FROM FieldHistories fh INNER JOIN Items i ON i.Id = fh.ItemId AND i.ManifestId = fh.ManifestId WHERE fh.IsDeleted = 0 AND i.IsDeleted = 0 AND i.DeletedAt IS NULL AND i.ManifestId = ? ORDER BY fh.ChangedAt',+ [manifestId]+ );+ const fieldHistoriesByItem = ImportExportRepository.groupByItem(fieldHistoryRows, (row): FieldHistoryEntity => ({+ Id: row.Id,+ ItemId: row.ItemId,+ FieldKey: row.FieldKey,+ FieldDefinitionId: row.FieldDefinitionId,+ ValueSnapshot: row.ValueSnapshot,+ ChangedAt: fromStandardFormat(row.ChangedAt),+ CreatedAt: fromStandardFormat(row.CreatedAt),+ UpdatedAt: fromStandardFormat(row.UpdatedAt),+ IsDeleted: false,+ }));+const attachmentRows = yield* this.query<ChildRow & { Id: string; Filename: string; Blob: Uint8Array | null; CreatedAt: string; UpdatedAt: string }>('SELECT a.Id, a.ItemId, a.ManifestId, a.Filename, a.Blob, a.CreatedAt, a.UpdatedAt FROM Attachments a INNER JOIN Items i ON i.Id = a.ItemId AND i.ManifestId = a.ManifestId WHERE a.IsDeleted = 0 AND i.IsDeleted = 0 AND i.DeletedAt IS NULL AND i.ManifestId = ?',[manifestId]@@ -149,12 +181,13 @@ export class ImportExportRepository extends BaseRepository {IsDeleted: false,}));- const passkeyRows = yield* this.query<ChildRow & { Id: string; RpId: string; UserHandle: Uint8Array | null; PublicKey: string; PrivateKey: string; PrfKey: Uint8Array | null; DisplayName: string; CreatedAt: string; UpdatedAt: string }>(- 'SELECT p.Id, p.ItemId, p.ManifestId, p.RpId, p.UserHandle, p.PublicKey, p.PrivateKey, p.PrfKey, p.DisplayName, p.CreatedAt, p.UpdatedAt FROM Passkeys p INNER JOIN Items i ON i.Id = p.ItemId AND i.ManifestId = p.ManifestId WHERE p.IsDeleted = 0 AND i.IsDeleted = 0 AND i.DeletedAt IS NULL AND i.ManifestId = ?',+ const passkeyRows = yield* this.query<ChildRow & { Id: string; CredentialId: Uint8Array | null; RpId: string; UserHandle: Uint8Array | null; PublicKey: string; PrivateKey: string; PrfKey: Uint8Array | null; DisplayName: string; AdditionalData: Uint8Array | null; CreatedAt: string; UpdatedAt: string }>(+ 'SELECT p.Id, p.CredentialId, p.ItemId, p.ManifestId, p.RpId, p.UserHandle, p.PublicKey, p.PrivateKey, p.PrfKey, p.DisplayName, p.AdditionalData, p.CreatedAt, p.UpdatedAt FROM Passkeys p INNER JOIN Items i ON i.Id = p.ItemId AND i.ManifestId = p.ManifestId WHERE p.IsDeleted = 0 AND i.IsDeleted = 0 AND i.DeletedAt IS NULL AND i.ManifestId = ?',[manifestId]);const passkeysByItem = ImportExportRepository.groupByItem(passkeyRows, (row): PasskeyEntity => ({Id: row.Id,+ CredentialId: row.CredentialId ? new Uint8Array(row.CredentialId) : null,ItemId: row.ItemId,RpId: row.RpId,UserHandle: row.UserHandle ? new Uint8Array(row.UserHandle) : null,@@ -162,11 +195,29 @@ export class ImportExportRepository extends BaseRepository {PrivateKey: row.PrivateKey,PrfKey: row.PrfKey ? new Uint8Array(row.PrfKey) : null,DisplayName: row.DisplayName ?? '',+ AdditionalData: row.AdditionalData ? new Uint8Array(row.AdditionalData) : null,CreatedAt: fromStandardFormat(row.CreatedAt),UpdatedAt: fromStandardFormat(row.UpdatedAt),IsDeleted: false,}));+ // Only the logos the exported items point at.+ const logoIds = new Set(itemRows.map(row => row.LogoId).filter((id): id is string => id !== null));+ const logoRows = yield* this.query<{ Id: string; Kind: LogoKind; Source: string; Name: string | null; FileData: Uint8Array | null; MimeType: string | null; UpdatedAt: string }>(+ 'SELECT Id, Kind, Source, Name, FileData, MimeType, UpdatedAt FROM Logos WHERE IsDeleted = 0 AND ManifestId = ?',+ [manifestId]+ );+ const logosById = new Map(logoRows.filter(row => logoIds.has(row.Id)).map((row): [string, LogoEntity] => [row.Id, {+ Id: row.Id,+ Kind: row.Kind,+ Source: row.Source,+ Name: row.Name,+ FileData: row.FileData ? new Uint8Array(row.FileData) : null,+ MimeType: row.MimeType,+ FetchedAt: fromStandardFormat(row.UpdatedAt),+ IsDeleted: false,+ }]));+const items = itemRows.map((row): ItemEntity => {const key = scopedKey(row.ManifestId, row.Id);return {@@ -174,11 +225,13 @@ export class ImportExportRepository extends BaseRepository {Name: row.Name,ItemType: row.ItemType as ItemType,FolderId: row.FolderId,- LogoId: row.LogoId,+ Logo: row.LogoId ? logosById.get(row.LogoId) ?? null : null,+ ArchivedAt: row.ArchivedAt ? fromStandardFormat(row.ArchivedAt) : null,CreatedAt: fromStandardFormat(row.CreatedAt),UpdatedAt: fromStandardFormat(row.UpdatedAt),IsDeleted: false,FieldValues: fieldValuesByItem.get(key) ?? [],+ FieldHistories: fieldHistoriesByItem.get(key) ?? [],Attachments: attachmentsByItem.get(key) ?? [],TotpCodes: totpCodesByItem.get(key) ?? [],Passkeys: passkeysByItem.get(key) ?? [],@@ -216,22 +269,7 @@ export class ImportExportRepository extends BaseRepository {const itemTagRows = yield* this.query<{ ItemId: string; TagId: string }>('SELECT ItemId, TagId FROM ItemTags WHERE IsDeleted = 0 AND ManifestId = ?', [manifestId]);const itemTags = itemTagRows.map((row): ItemTagEntity => ({ ItemId: row.ItemId, TagId: row.TagId, IsDeleted: false }));- // Only the logos the exported items point at.- const logoIds = new Set(items.map(item => item.LogoId).filter((id): id is string => id !== null));- const logoRows = yield* this.query<{ Id: string; Source: string; FileData: Uint8Array | null; MimeType: string | null; UpdatedAt: string }>(- 'SELECT Id, Source, FileData, MimeType, UpdatedAt FROM Logos WHERE IsDeleted = 0 AND ManifestId = ?',- [manifestId]- );- const logos = logoRows.filter(row => logoIds.has(row.Id)).map((row): LogoEntity => ({- Id: row.Id,- Source: row.Source,- FileData: row.FileData ? new Uint8Array(row.FileData) : null,- MimeType: row.MimeType,- FetchedAt: fromStandardFormat(row.UpdatedAt),- IsDeleted: false,- }));-- return { items, folders, tags, itemTags, fieldDefinitions, logos };+ return { items, folders, tags, itemTags, fieldDefinitions };}/**@@ -278,59 +316,67 @@ export class ImportExportRepository extends BaseRepository {}/**- * Insert an imported item with its field values, custom field definitions, TOTP codes, passkeys and- * attachments, keeping the timestamps the source export carried.+ * Resolve the logo an item carried in from an AliasVault export, keeping its kind: a favicon goes through+ * {@link resolveImportLogo}, a built-in logo is recreated from its catalog key and an uploaded one from its bytes.+ * @param logo - The carried logo+ * @returns The logo id, or null when the item gets no logo+ */+ public async resolveCarriedLogo(logo: LogoEntity): Promise<string | null> {+ if (logo.Kind === LogoKinds.Favicon) {+ return this.resolveImportLogo(logo.Source, logo.FileData);+ }++ const scope = await this.run(this.writeManifestId());+ const currentDateTime = this.now();+ const options = { mimeType: logo.MimeType, name: logo.Name };++ if (logo.Kind === LogoKinds.Builtin) {+ return this.logoRepository.getOrCreate(scope, LogoKinds.Builtin, logo.Source, null, currentDateTime, options);+ }++ // An uploaded logo is keyed by the hash of its bytes, so it is stored the way an upload is rather than trusting the file's key.+ if (logo.FileData && logo.FileData.length > 0) {+ return this.logoRepository.storeUpload(scope, logo.FileData, currentDateTime, options);+ }++ return this.logoRepository.ensureInScope(scope, LogoKinds.Custom, logo.Source, currentDateTime);+ }++ /**+ * Insert an imported item with its field values and history, custom field definitions, tags, TOTP codes, passkeys+ * and attachments, keeping the timestamps the source export carried.* @param item - The item graph to insert+ * @param logoId - The id of the item's logo in this vault (from {@link resolveImportLogo} or {@link resolveCarriedLogo}), or null+ * @param session - What this import already wrote, shared across its items*/- public async insertImportedItem(item: ItemEntity): Promise<void> {- return this.withTransaction(() => this.run(this.insertItemGraph(item)));+ public async insertImportedItem(item: ItemEntity, logoId: string | null, session: ImportWriteSession = createImportWriteSession()): Promise<void> {+ return this.withTransaction(() => this.run(this.insertItemGraph(item, logoId, session)));}/*** Write one item graph.* @param item - The item graph+ * @param logoId - The id of the item's logo in this vault, or null+ * @param session - What this import already wrote*/- private *insertItemGraph(item: ItemEntity): DbOp<void> {+ private *insertItemGraph(item: ItemEntity, logoId: string | null, session: ImportWriteSession): DbOp<void> {const manifestId = yield* this.writeManifestId();- const createdAt = toStandardFormat(item.CreatedAt);- const updatedAt = toStandardFormat(item.UpdatedAt);-- yield* this.execute(ItemQueries.INSERT_ITEM, [- item.Id,- item.Name,- item.ItemType,- item.LogoId,- item.FolderId,- manifestId,- createdAt,- updatedAt,- 0,- ]);- // Custom field definitions come before the values that reference them; each is written once.- const writtenDefinitions = new Set<string>();+ yield* this.execute(+ 'INSERT INTO Items (Id, Name, ItemType, LogoId, FolderId, ManifestId, ArchivedAt, CreatedAt, UpdatedAt, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 0)',+ [item.Id, item.Name, item.ItemType, logoId, item.FolderId, manifestId, item.ArchivedAt ? toStandardFormat(item.ArchivedAt) : null, toStandardFormat(item.CreatedAt), toStandardFormat(item.UpdatedAt)]+ );++ // Custom field definitions come before the values and history that reference them.+ for (const definition of [...item.FieldValues, ...item.FieldHistories].map(row => row.FieldDefinition)) {+ if (definition) {+ yield* this.insertFieldDefinition(definition, manifestId, session);+ }+ }+// Values of one multi-value field are numbered in file order.const valueIndexes = new Map<string, number>();for (const fieldValue of item.FieldValues) {- const definition = fieldValue.FieldDefinition;- if (definition && !writtenDefinitions.has(definition.Id)) {- writtenDefinitions.add(definition.Id);- yield* this.execute(FieldDefinitionQueries.INSERT, [- definition.Id,- manifestId,- definition.FieldType,- definition.Label,- definition.IsMultiValue ? 1 : 0,- definition.IsHidden ? 1 : 0,- definition.EnableHistory ? 1 : 0,- definition.Weight,- definition.ApplicableToTypes,- toStandardFormat(definition.CreatedAt),- toStandardFormat(definition.UpdatedAt),- 0,- ]);- }-const fieldIdentity = fieldValue.FieldDefinitionId ?? fieldValue.FieldKey ?? '';const valueIndex = valueIndexes.get(fieldIdentity) ?? 0;valueIndexes.set(fieldIdentity, valueIndex + 1);@@ -350,6 +396,21 @@ export class ImportExportRepository extends BaseRepository {]);}+ for (const fieldHistory of item.FieldHistories) {+ yield* this.execute(FieldHistoryQueries.INSERT, [+ fieldHistory.Id,+ item.Id,+ manifestId,+ fieldHistory.FieldDefinitionId,+ fieldHistory.FieldKey,+ fieldHistory.ValueSnapshot,+ toStandardFormat(fieldHistory.ChangedAt),+ toStandardFormat(fieldHistory.CreatedAt),+ toStandardFormat(fieldHistory.UpdatedAt),+ 0,+ ]);+ }+for (const totpCode of item.TotpCodes) {yield* this.execute(TotpCodeQueries.INSERT, [totpCode.Id,@@ -367,21 +428,10 @@ export class ImportExportRepository extends BaseRepository {}for (const passkey of item.Passkeys) {- yield* this.execute(PasskeyQueries.INSERT, [- passkey.Id,- item.Id,- manifestId,- passkey.RpId,- passkey.UserHandle,- passkey.PublicKey,- passkey.PrivateKey,- passkey.PrfKey,- passkey.DisplayName,- null,- toStandardFormat(passkey.CreatedAt),- toStandardFormat(passkey.UpdatedAt),- 0,- ]);+ yield* this.execute(+ 'INSERT INTO Passkeys (Id, CredentialId, ItemId, ManifestId, RpId, UserHandle, PublicKey, PrivateKey, PrfKey, DisplayName, AdditionalData, CreatedAt, UpdatedAt, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0)',+ [passkey.Id, passkey.CredentialId, item.Id, manifestId, passkey.RpId, passkey.UserHandle, passkey.PublicKey, passkey.PrivateKey, passkey.PrfKey, passkey.DisplayName, passkey.AdditionalData, toStandardFormat(passkey.CreatedAt), toStandardFormat(passkey.UpdatedAt)]+ );}for (const attachment of item.Attachments) {@@ -396,6 +446,68 @@ export class ImportExportRepository extends BaseRepository {0,]);}++ for (const tag of item.Tags ?? []) {+ const tagId = yield* this.resolveImportTag(tag, manifestId, session);+ yield* this.execute(+ 'INSERT OR IGNORE INTO ItemTags (ManifestId, ItemId, TagId, CreatedAt, UpdatedAt, IsDeleted) VALUES (?, ?, ?, ?, ?, 0)',+ [manifestId, item.Id, tagId, toStandardFormat(item.CreatedAt), toStandardFormat(item.UpdatedAt)]+ );+ }+ }++ /**+ * Write a custom field definition, once per import.+ * @param definition - The definition+ * @param manifestId - The manifest to write into+ * @param session - What this import already wrote+ */+ private *insertFieldDefinition(definition: FieldDefinitionEntity, manifestId: string, session: ImportWriteSession): DbOp<void> {+ if (session.writtenDefinitionIds.has(definition.Id)) {+ return;+ }+ session.writtenDefinitionIds.add(definition.Id);+ yield* this.execute(FieldDefinitionQueries.INSERT, [+ definition.Id,+ manifestId,+ definition.FieldType,+ definition.Label,+ definition.IsMultiValue ? 1 : 0,+ definition.IsHidden ? 1 : 0,+ definition.EnableHistory ? 1 : 0,+ definition.Weight,+ definition.ApplicableToTypes,+ toStandardFormat(definition.CreatedAt),+ toStandardFormat(definition.UpdatedAt),+ 0,+ ]);+ }++ /**+ * The tag an imported tag is written as: the vault's own tag with the same name when there is one, else a new tag.+ * @param tag - The imported tag+ * @param manifestId - The manifest to write into+ * @param session - What this import already wrote+ * @returns The tag id+ */+ private *resolveImportTag(tag: TagEntity, manifestId: string, session: ImportWriteSession): DbOp<string> {+ const known = session.tagIds.get(tag.Id);+ if (known) {+ return known;+ }++ const existing = (yield* this.query<{ Id: string }>('SELECT Id FROM Tags WHERE ManifestId = ? AND IsDeleted = 0 AND Name = ? COLLATE NOCASE LIMIT 1', [manifestId, tag.Name.trim()]))[0];+ let tagId = existing?.Id;+ if (!tagId) {+ tagId = tag.Id;+ yield* this.execute(+ 'INSERT INTO Tags (Id, ManifestId, Name, Color, DisplayOrder, CreatedAt, UpdatedAt, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, 0)',+ [tagId, manifestId, tag.Name.trim(), tag.Color, tag.DisplayOrder, toStandardFormat(tag.CreatedAt), toStandardFormat(tag.UpdatedAt)]+ );+ }++ session.tagIds.set(tag.Id, tagId);+ return tagId;}/**diff --git a/core/client/src/transfer/export/AvuxExportService.ts b/core/client/src/transfer/export/AvuxExportService.tsindex 4c0f0d765..40b395520 100644--- a/core/client/src/transfer/export/AvuxExportService.ts+++ b/core/client/src/transfer/export/AvuxExportService.ts@@ -1,8 +1,8 @@import { bytesToBase64 } from '../../utilities/Base64';import { createZipArchive, textToZipBytes } from '../shared/ZipArchive';-import type { AvuxAttachment, AvuxFieldDefinition, AvuxFieldValue, AvuxFolder, AvuxItem, AvuxItemTag, AvuxLogo, AvuxManifest, AvuxPasskey, AvuxTag, AvuxTotpCode } from './AvuxManifest';-import type { AttachmentEntity, FieldDefinitionEntity, FieldValueEntity, FolderEntity, ItemEntity, ItemTagEntity, LogoEntity, PasskeyEntity, TagEntity, TotpCodeEntity } from '../shared/VaultEntities';+import type { AvuxAttachment, AvuxFieldDefinition, AvuxFieldHistory, AvuxFieldValue, AvuxFolder, AvuxItem, AvuxItemTag, AvuxLogo, AvuxManifest, AvuxPasskey, AvuxTag, AvuxTotpCode } from './AvuxManifest';+import type { AttachmentEntity, FieldDefinitionEntity, FieldHistoryEntity, FieldValueEntity, FolderEntity, ItemEntity, ItemTagEntity, LogoEntity, PasskeyEntity, TagEntity, TotpCodeEntity } from '../shared/VaultEntities';/** The .avux manifest format version this service writes. */export const AVUX_FORMAT_VERSION = '1.0.0';@@ -19,11 +19,11 @@ export class AvuxExportService {* @param tags - The tags to export* @param itemTags - The item-tag associations to export* @param fieldDefinitions - The custom field definitions to export- * @param logos - The logos to export* @param username - The username creating the export* @returns The .avux ZIP file bytes*/- public static exportToAvux(items: ItemEntity[], folders: FolderEntity[], tags: TagEntity[], itemTags: ItemTagEntity[], fieldDefinitions: FieldDefinitionEntity[], logos: LogoEntity[], username: string): Uint8Array {+ public static exportToAvux(items: ItemEntity[], folders: FolderEntity[], tags: TagEntity[], itemTags: ItemTagEntity[], fieldDefinitions: FieldDefinitionEntity[], username: string): Uint8Array {+ const logos = AvuxExportService.itemLogos(items);const manifest = AvuxExportService.createManifest(items, folders, tags, itemTags, fieldDefinitions, logos, username);const attachmentMap = AvuxExportService.extractAttachments(items);const logoMap = AvuxExportService.extractLogos(logos);@@ -52,10 +52,25 @@ export class AvuxExportService {tags: tags.filter(t => !t.IsDeleted).map(AvuxExportService.mapTagToAvux),itemTags: itemTags.filter(it => !it.IsDeleted).map(AvuxExportService.mapItemTagToAvux),fieldDefinitions: fieldDefinitions.filter(fd => !fd.IsDeleted).map(AvuxExportService.mapFieldDefinitionToAvux),- logos: logos.filter(l => !l.IsDeleted).map(AvuxExportService.mapLogoToAvux),+ logos: logos.map(AvuxExportService.mapLogoToAvux),};}+ /**+ * Get all the live logos of the live items.+ * @param items - The items+ * @returns The logos, one per id+ */+ private static itemLogos(items: ItemEntity[]): LogoEntity[] {+ const logosById = new Map<string, LogoEntity>();+ for (const item of items.filter(i => !i.IsDeleted)) {+ if (item.Logo && !item.Logo.IsDeleted) {+ logosById.set(item.Logo.Id, item.Logo);+ }+ }+ return [...logosById.values()];+ }+/*** Map an item.* @param item - The item@@ -69,8 +84,10 @@ export class AvuxExportService {createdAt: item.CreatedAt.toISOString(),updatedAt: item.UpdatedAt.toISOString(),folderId: item.FolderId,- logoId: item.LogoId,+ logoId: item.Logo && !item.Logo.IsDeleted ? item.Logo.Id : null,+ archivedAt: item.ArchivedAt ? item.ArchivedAt.toISOString() : null,fieldValues: item.FieldValues.filter(fv => !fv.IsDeleted).map(AvuxExportService.mapFieldValueToAvux),+ fieldHistories: item.FieldHistories.filter(fh => !fh.IsDeleted).map(AvuxExportService.mapFieldHistoryToAvux),attachments: item.Attachments.filter(a => !a.IsDeleted).map(AvuxExportService.mapAttachmentToAvux),totpCodes: item.TotpCodes.filter(tc => !tc.IsDeleted).map(AvuxExportService.mapTotpCodeToAvux),passkeys: item.Passkeys.filter(p => !p.IsDeleted).map(AvuxExportService.mapPasskeyToAvux),@@ -92,6 +109,21 @@ export class AvuxExportService {};}+ /**+ * Map a field history record.+ * @param fieldHistory - The field history record+ * @returns The manifest field history record+ */+ private static mapFieldHistoryToAvux(fieldHistory: FieldHistoryEntity): AvuxFieldHistory {+ return {+ id: fieldHistory.Id,+ fieldKey: fieldHistory.FieldKey,+ fieldDefinitionId: fieldHistory.FieldDefinitionId,+ valueSnapshot: fieldHistory.ValueSnapshot,+ changedAt: fieldHistory.ChangedAt.toISOString(),+ };+ }+/*** Map an attachment.* @param attachment - The attachment@@ -129,12 +161,14 @@ export class AvuxExportService {private static mapPasskeyToAvux(passkey: PasskeyEntity): AvuxPasskey {return {id: passkey.Id,+ credentialId: passkey.CredentialId ? bytesToBase64(passkey.CredentialId) : null,rpId: passkey.RpId,userHandle: passkey.UserHandle ? bytesToBase64(passkey.UserHandle) : null,publicKey: passkey.PublicKey,privateKey: passkey.PrivateKey,prfKey: passkey.PrfKey ? bytesToBase64(passkey.PrfKey) : null,displayName: passkey.DisplayName,+ additionalData: passkey.AdditionalData ? bytesToBase64(passkey.AdditionalData) : null,};}@@ -209,7 +243,9 @@ export class AvuxExportService {private static mapLogoToAvux(logo: LogoEntity): AvuxLogo {return {id: logo.Id,+ kind: logo.Kind,source: logo.Source,+ name: logo.Name,mimeType: logo.MimeType,fetchedAt: logo.FetchedAt ? logo.FetchedAt.toISOString() : null,relativePath: AvuxExportService.logoPath(logo),diff --git a/core/client/src/transfer/export/AvuxManifest.ts b/core/client/src/transfer/export/AvuxManifest.tsindex ff1a9b9cd..ad4b5b749 100644--- a/core/client/src/transfer/export/AvuxManifest.ts+++ b/core/client/src/transfer/export/AvuxManifest.ts@@ -16,6 +16,19 @@ export type AvuxFieldValue = {weight: number;};+/**+ * A previous value of a field in an item.+ */+export type AvuxFieldHistory = {+ id: string;+ /** The system field key, null for custom fields. */+ fieldKey: string | null;+ /** The custom field definition id, null for system fields. */+ fieldDefinitionId: string | null;+ valueSnapshot: string;+ changedAt: string;+};+/*** An attachment in an item.*/@@ -43,8 +56,10 @@ export type AvuxTotpCode = {* A passkey in an item.*/export type AvuxPasskey = {- /** The passkey id; the WebAuthn credential id is derived from it at runtime. */+ /** The passkey id; the WebAuthn credential id is derived from it at runtime unless credentialId is set. */id: string;+ /** The WebAuthn credential id (base64-encoded), null when it is derived from id. */+ credentialId: string | null;rpId: string;/** The user handle (base64-encoded). */userHandle: string | null;@@ -53,6 +68,7 @@ export type AvuxPasskey = {/** The PRF key (base64-encoded). */prfKey: string | null;displayName: string;+ additionalData: string | null;};/**@@ -67,7 +83,10 @@ export type AvuxItem = {updatedAt: string;folderId: string | null;logoId: string | null;+ /** When the item was archived, null when it is not. */+ archivedAt: string | null;fieldValues: AvuxFieldValue[];+ fieldHistories: AvuxFieldHistory[];attachments: AvuxAttachment[];totpCodes: AvuxTotpCode[];passkeys: AvuxPasskey[];@@ -122,14 +141,19 @@ export type AvuxFieldDefinition = {};/**- * A logo in the .avux export, deduplicated by source domain.+ * A logo in the .avux export.*/export type AvuxLogo = {id: string;+ /** favicon, builtin or custom. */+ kind: string;+ /** The natural key within the kind: a domain, a catalog key or an image hash. */source: string;+ /** The user-facing label of an uploaded logo. */+ name: string | null;mimeType: string | null;fetchedAt: string | null;- /** The relative path to the logo file in the .avux archive. */+ /** The relative path to the logo file in the .avux archive; a builtin logo has no file. */relativePath: string;};diff --git a/core/client/src/transfer/export/__tests__/AvuxExportService.test.ts b/core/client/src/transfer/export/__tests__/AvuxExportService.test.tsindex c5a2a5fa2..069abcf81 100644--- a/core/client/src/transfer/export/__tests__/AvuxExportService.test.ts+++ b/core/client/src/transfer/export/__tests__/AvuxExportService.test.ts@@ -25,7 +25,7 @@ describe('AvuxExportService', () => {const tags: TagEntity[] = [{ Id: crypto.randomUUID(), Name: 'Test Tag', Color: '#FF0000', DisplayOrder: 0, CreatedAt: new Date(), UpdatedAt: new Date(), IsDeleted: false }];const itemTags: ItemTagEntity[] = [{ ItemId: items[0].Id, TagId: tags[0].Id, IsDeleted: false }];- const avuxBytes = AvuxExportService.exportToAvux(items, folders, tags, itemTags, [], [], '[email protected]');+ const avuxBytes = AvuxExportService.exportToAvux(items, folders, tags, itemTags, [], '[email protected]');expect(avuxBytes.length).toBeGreaterThan(0);@@ -114,7 +114,7 @@ describe('AvuxExportService', () => {addCustomFieldValue(item, textFieldDef, 'My first pet');addCustomFieldValue(item, hiddenFieldDef, 'super-secret-recovery');- const imported = AvuxImportService.importFromAvux(exportItems([item], [], [textFieldDef, hiddenFieldDef]));+ const imported = AvuxImportService.importFromAvux(exportItems([item], [textFieldDef, hiddenFieldDef]));expect(imported).toHaveLength(1);const credential = imported[0];@@ -145,23 +145,18 @@ describe('AvuxExportService', () => {});it('re-imports exported logos', () => {- const logoId1 = crypto.randomUUID();- const logoId2 = crypto.randomUUID();+ const githubLogo: LogoEntity = { Id: crypto.randomUUID(), Kind: 'favicon', Source: 'github.com', Name: null, FileData: new Uint8Array([1, 2, 3, 4, 5]), MimeType: 'image/png', FetchedAt: new Date(), IsDeleted: false };+ const googleLogo: LogoEntity = { Id: crypto.randomUUID(), Kind: 'favicon', Source: 'google.com', Name: null, FileData: new Uint8Array([6, 7, 8, 9, 10]), MimeType: 'image/png', FetchedAt: new Date(), IsDeleted: false };const item1 = createTestItem('GitHub', ItemTypes.Login, { [FieldKey.LoginUsername]: 'testuser', [FieldKey.LoginPassword]: 'password123', [FieldKey.LoginUrl]: 'https://github.com' });- item1.LogoId = logoId1;+ item1.Logo = githubLogo;const item2 = createTestItem('Google', ItemTypes.Login, { [FieldKey.LoginUsername]: '[email protected]', [FieldKey.LoginPassword]: 'pass456', [FieldKey.LoginUrl]: 'https://google.com' });- item2.LogoId = logoId2;+ item2.Logo = googleLogo;// Shares the logo of item1 (deduplication).const item3 = createTestItem('GitHub Issue Tracker', ItemTypes.Login, { [FieldKey.LoginUsername]: 'issueuser', [FieldKey.LoginPassword]: 'issuepass' });- item3.LogoId = logoId1;+ item3.Logo = githubLogo;- const logos: LogoEntity[] = [- { Id: logoId1, Source: 'github.com', FileData: new Uint8Array([1, 2, 3, 4, 5]), MimeType: 'image/png', FetchedAt: new Date(), IsDeleted: false },- { Id: logoId2, Source: 'google.com', FileData: new Uint8Array([6, 7, 8, 9, 10]), MimeType: 'image/png', FetchedAt: new Date(), IsDeleted: false },- ];-- const imported = AvuxImportService.importFromAvux(exportItems([item1, item2, item3], logos));+ const imported = AvuxImportService.importFromAvux(exportItems([item1, item2, item3]));expect(imported).toHaveLength(3);diff --git a/core/client/src/transfer/export/__tests__/testHelpers.ts b/core/client/src/transfer/export/__tests__/testHelpers.tsindex 9f056bc74..3e5545d76 100644--- a/core/client/src/transfer/export/__tests__/testHelpers.ts+++ b/core/client/src/transfer/export/__tests__/testHelpers.ts@@ -1,6 +1,6 @@import { AvuxExportService } from '../AvuxExportService';-import type { FieldDefinitionEntity, FieldValueEntity, ItemEntity, LogoEntity, TotpCodeEntity } from '../../shared/VaultEntities';+import type { FieldDefinitionEntity, FieldValueEntity, ItemEntity, TotpCodeEntity } from '../../shared/VaultEntities';import type { ItemType } from '@aliasvault/models/vault';/*@@ -30,11 +30,13 @@ export function createTestItem(name: string, itemType: ItemType, fields: Record<Name: name,ItemType: itemType,FolderId: null,- LogoId: null,+ Logo: null,+ ArchivedAt: null,CreatedAt: now,UpdatedAt: now,IsDeleted: false,FieldValues: [],+ FieldHistories: [],Attachments: [],TotpCodes: [],Passkeys: [],@@ -109,12 +111,11 @@ export function addAttachment(item: ItemEntity, filename: string, content: strin}/**- * Export with no folders, tags or definitions.+ * Export with no folders or tags.* @param items - The items- * @param logos - The logos* @param fieldDefinitions - The custom field definitions* @returns The .avux bytes*/-export function exportItems(items: ItemEntity[], logos: LogoEntity[] = [], fieldDefinitions: FieldDefinitionEntity[] = []): Uint8Array {- return AvuxExportService.exportToAvux(items, [], [], [], fieldDefinitions, logos, '[email protected]');+export function exportItems(items: ItemEntity[], fieldDefinitions: FieldDefinitionEntity[] = []): Uint8Array {+ return AvuxExportService.exportToAvux(items, [], [], [], fieldDefinitions, '[email protected]');}diff --git a/core/client/src/transfer/import/importers/aliasvault/AvuxImportService.ts b/core/client/src/transfer/import/importers/aliasvault/AvuxImportService.tsindex 4961313c3..178974f8e 100644--- a/core/client/src/transfer/import/importers/aliasvault/AvuxImportService.ts+++ b/core/client/src/transfer/import/importers/aliasvault/AvuxImportService.ts@@ -1,4 +1,4 @@-import { FieldTypes, isItemType, ItemTypes, type FieldType } from '@aliasvault/models/vault';+import { FieldTypes, isItemType, ItemTypes, LogoKinds, normalizeTotpAlgorithm, normalizeTotpDigits, normalizeTotpPeriod, type FieldType, type LogoKind } from '@aliasvault/models/vault';import { base64ToBytes } from '../../../../utilities/Base64';import { parseDateTime } from '../../../shared/DateTimeUtils';@@ -9,14 +9,26 @@ import { parseJson } from '../../readers/JsonReader';import { parseAvuxManifest } from './AvuxManifestReader';-import type { AvuxAttachment, AvuxFieldDefinition, AvuxFieldValue, AvuxManifest, AvuxPasskey } from '../../../export/AvuxManifest';+import type { AvuxAttachment, AvuxFieldDefinition, AvuxFieldValue, AvuxItem, AvuxManifest, AvuxPasskey } from '../../../export/AvuxManifest';+import type { AttachmentEntity, FieldDefinitionEntity, ItemEntity, LogoEntity, TagEntity } from '../../../shared/VaultEntities';import type { ImportedAttachment } from '../../models/ImportedAttachment';import type { ImportedCredential } from '../../models/ImportedCredential';import type { ImportedPasskey } from '../../models/ImportedPasskey';+/**+ * The rows an .avux manifest shares between its items, each under a new id for the vault it is imported into.+ */+type SharedRows = {+ definitions: Map<string, FieldDefinitionEntity>;+ tagsByItem: Map<string, TagEntity[]>;+ logos: Map<string, LogoEntity>;+};+/*** Imports vault data from the .avux (AliasVault Unencrypted eXport) format: a ZIP archive with a manifest.json,- * all attachments and all logos. Logos are embedded into the FaviconBytes of each credential.+ * all attachments and all logos.+ *+ * Every credential carries the complete item as AliasVaultItem, which is what gets written as-is.*/export class AvuxImportService {/**@@ -60,6 +72,7 @@ export class AvuxImportService {private static convertManifestToImportedCredentials(manifest: AvuxManifest, attachmentMap: Map<string, Uint8Array>, logoDataById: Map<string, Uint8Array>): ImportedCredential[] {const foldersById = new Map(manifest.folders.map(folder => [folder.id, { Name: folder.name, ParentFolderId: folder.parentFolderId }]));const fieldDefinitionsById = new Map(manifest.fieldDefinitions.map(fd => [fd.id, fd]));+ const sharedRows = AvuxImportService.buildSharedRows(manifest, logoDataById);return manifest.items.map((item): ImportedCredential => {const credential: ImportedCredential = {@@ -96,10 +109,144 @@ export class AvuxImportService {credential.Attachments = item.attachments.map(a => AvuxImportService.mapAvuxAttachmentToImported(a, attachmentMap));}+ credential.AliasVaultItem = AvuxImportService.buildItem(item, credential, sharedRows, attachmentMap);+return credential;});}+ /**+ * Create the custom field definitions, tags and logos the items share, under new ids.+ * @param manifest - The manifest+ * @param logoDataById - Logo ids to image data+ * @returns The shared rows, keyed by their id in the manifest (tags by the id of the item they are on)+ */+ private static buildSharedRows(manifest: AvuxManifest, logoDataById: Map<string, Uint8Array>): SharedRows {+ const now = new Date();++ const definitions = new Map(manifest.fieldDefinitions.map((definition): [string, FieldDefinitionEntity] => [definition.id, {+ Id: crypto.randomUUID(),+ FieldType: AvuxImportService.toFieldType(definition.fieldType),+ Label: definition.label,+ IsMultiValue: definition.isMultiValue,+ IsHidden: definition.isHidden,+ EnableHistory: definition.enableHistory,+ Weight: definition.weight,+ ApplicableToTypes: definition.applicableToTypes,+ CreatedAt: now,+ UpdatedAt: now,+ IsDeleted: false,+ }]));++ const tags = new Map(manifest.tags.filter(tag => tag.name.trim().length > 0).map((tag): [string, TagEntity] => [tag.id, {+ Id: crypto.randomUUID(),+ Name: tag.name,+ Color: tag.color,+ DisplayOrder: tag.displayOrder,+ CreatedAt: parseDateTime(tag.createdAt) ?? now,+ UpdatedAt: parseDateTime(tag.updatedAt) ?? now,+ IsDeleted: false,+ }]));+ const tagsByItem = new Map<string, TagEntity[]>();+ for (const itemTag of manifest.itemTags) {+ const tag = tags.get(itemTag.tagId);+ if (tag) {+ tagsByItem.set(itemTag.itemId, [...tagsByItem.get(itemTag.itemId) ?? [], tag]);+ }+ }++ const logos = new Map<string, LogoEntity>();+ for (const logo of manifest.logos) {+ const kind = AvuxImportService.toLogoKind(logo.kind);+ if (kind && logo.source.length > 0) {+ logos.set(logo.id, { Id: logo.id, Kind: kind, Source: logo.source, Name: logo.name, FileData: logoDataById.get(logo.id) ?? null, MimeType: logo.mimeType, FetchedAt: parseDateTime(logo.fetchedAt), IsDeleted: false });+ }+ }++ return { definitions, tagsByItem, logos };+ }++ /**+ * Build the complete item graph of a manifest item, under new ids. Passkeys keep theirs, since the WebAuthn+ * credential id the relying party knows is derived from it.+ * @param item - The manifest item+ * @param credential - The credential summary of the item, for its dates and item type+ * @param sharedRows - The definitions, tags and logos the items share+ * @param attachmentMap - Attachment paths to file data+ * @returns The item+ */+ private static buildItem(item: AvuxItem, credential: ImportedCredential, sharedRows: SharedRows, attachmentMap: Map<string, Uint8Array>): ItemEntity {+ const now = new Date();+ const itemId = crypto.randomUUID();+ const row = { CreatedAt: credential.CreatedAt ?? now, UpdatedAt: credential.UpdatedAt ?? now, IsDeleted: false };+ /**+ * The new definition of a manifest definition id, or null for a system field or an unknown definition.+ */+ const definitionOf = (definitionId: string | null): FieldDefinitionEntity | null => definitionId ? sharedRows.definitions.get(definitionId) ?? null : null;++ const attachments: AttachmentEntity[] = [];+ for (const attachment of item.attachments) {+ const blob = attachmentMap.get(attachment.relativePath);+ if (blob && blob.length > 0) {+ attachments.push({ Id: crypto.randomUUID(), ItemId: itemId, Filename: attachment.filename, Blob: blob, ...row });+ }+ }++ return {+ Id: itemId,+ Name: item.name,+ ItemType: credential.ItemType ?? ItemTypes.Login,+ FolderId: null,+ ArchivedAt: parseDateTime(item.archivedAt),+ ...row,+ // A value or history record is either a system field or a custom field whose definition the file holds.+ FieldValues: item.fieldValues.filter(fv => fv.fieldKey || definitionOf(fv.fieldDefinitionId)).map(fv => {+ const definition = definitionOf(fv.fieldDefinitionId);+ return { Id: crypto.randomUUID(), ItemId: itemId, FieldKey: definition ? null : fv.fieldKey, FieldDefinitionId: definition?.Id ?? null, FieldDefinition: definition, Value: fv.value, Weight: fv.weight, ...row };+ }),+ FieldHistories: item.fieldHistories.filter(fh => fh.fieldKey || definitionOf(fh.fieldDefinitionId)).map(fh => {+ const definition = definitionOf(fh.fieldDefinitionId);+ const changedAt = parseDateTime(fh.changedAt) ?? row.UpdatedAt;+ return { Id: crypto.randomUUID(), ItemId: itemId, FieldKey: definition ? null : fh.fieldKey, FieldDefinitionId: definition?.Id ?? null, FieldDefinition: definition, ValueSnapshot: fh.valueSnapshot, ChangedAt: changedAt, CreatedAt: changedAt, UpdatedAt: changedAt, IsDeleted: false };+ }),+ Attachments: attachments,+ TotpCodes: item.totpCodes.filter(totp => totp.secretKey.length > 0).map(totp => ({+ Id: crypto.randomUUID(),+ ItemId: itemId,+ Name: totp.name,+ SecretKey: totp.secretKey,+ Algorithm: normalizeTotpAlgorithm(totp.algorithm),+ Digits: normalizeTotpDigits(totp.digits),+ Period: normalizeTotpPeriod(totp.period),+ ...row,+ })),+ Passkeys: item.passkeys.map(passkey => ({+ Id: passkey.id.length > 0 ? passkey.id : crypto.randomUUID(),+ CredentialId: passkey.credentialId ? base64ToBytes(passkey.credentialId) : null,+ ItemId: itemId,+ RpId: passkey.rpId,+ UserHandle: passkey.userHandle ? base64ToBytes(passkey.userHandle) : new Uint8Array(0),+ PublicKey: passkey.publicKey,+ PrivateKey: passkey.privateKey,+ PrfKey: passkey.prfKey ? base64ToBytes(passkey.prfKey) : null,+ DisplayName: passkey.displayName,+ AdditionalData: passkey.additionalData ? base64ToBytes(passkey.additionalData) : null,+ ...row,+ })),+ Logo: item.logoId ? sharedRows.logos.get(item.logoId) ?? null : null,+ Tags: sharedRows.tagsByItem.get(item.id) ?? [],+ };+ }++ /**+ * Map a logo kind string to a known logo kind.+ * @param value - The logo kind text+ * @returns The logo kind, or null when this version does not know it+ */+ private static toLogoKind(value: string): LogoKind | null {+ return (Object.values(LogoKinds) as string[]).includes(value) ? value as LogoKind : null;+ }+/*** Fill the credential from the item's field values: system fields map to properties, custom fields become* custom field values with their definition's metadata.diff --git a/core/client/src/transfer/import/importers/aliasvault/AvuxManifestReader.ts b/core/client/src/transfer/import/importers/aliasvault/AvuxManifestReader.tsindex fc7a46097..e413a675f 100644--- a/core/client/src/transfer/import/importers/aliasvault/AvuxManifestReader.ts+++ b/core/client/src/transfer/import/importers/aliasvault/AvuxManifestReader.ts@@ -1,6 +1,8 @@+import { LogoKinds } from '@aliasvault/models/vault';+import { readBoolean, readNumber, readObjectArray, readString, requireObject, type JsonObject } from '../../readers/JsonReader';-import type { AvuxAttachment, AvuxFieldDefinition, AvuxFieldValue, AvuxFolder, AvuxItem, AvuxItemTag, AvuxLogo, AvuxManifest, AvuxPasskey, AvuxTag, AvuxTotpCode } from '../../../export/AvuxManifest';+import type { AvuxAttachment, AvuxFieldDefinition, AvuxFieldHistory, AvuxFieldValue, AvuxFolder, AvuxItem, AvuxItemTag, AvuxLogo, AvuxManifest, AvuxPasskey, AvuxTag, AvuxTotpCode } from '../../../export/AvuxManifest';/*** Read an .avux manifest.json, matching property names case-insensitively.@@ -37,7 +39,9 @@ function parseAvuxItem(obj: JsonObject): AvuxItem {updatedAt: readString(obj, 'updatedAt') ?? '',folderId: readString(obj, 'folderId'),logoId: readString(obj, 'logoId'),+ archivedAt: readString(obj, 'archivedAt'),fieldValues: readObjectArray(obj, 'fieldValues', parseAvuxFieldValue) ?? [],+ fieldHistories: readObjectArray(obj, 'fieldHistories', parseAvuxFieldHistory) ?? [],attachments: readObjectArray(obj, 'attachments', parseAvuxAttachment) ?? [],totpCodes: readObjectArray(obj, 'totpCodes', parseAvuxTotpCode) ?? [],passkeys: readObjectArray(obj, 'passkeys', parseAvuxPasskey) ?? [],@@ -59,6 +63,21 @@ function parseAvuxFieldValue(obj: JsonObject): AvuxFieldValue {};}+/**+ * Read a field history record.+ * @param obj - The field history JSON+ * @returns The field history record+ */+function parseAvuxFieldHistory(obj: JsonObject): AvuxFieldHistory {+ return {+ id: readString(obj, 'id') ?? '',+ fieldKey: readString(obj, 'fieldKey'),+ fieldDefinitionId: readString(obj, 'fieldDefinitionId'),+ valueSnapshot: readString(obj, 'valueSnapshot') ?? '',+ changedAt: readString(obj, 'changedAt') ?? '',+ };+}+/*** Read an attachment.* @param obj - The attachment JSON@@ -96,12 +115,14 @@ function parseAvuxTotpCode(obj: JsonObject): AvuxTotpCode {function parseAvuxPasskey(obj: JsonObject): AvuxPasskey {return {id: readString(obj, 'id') ?? '',+ credentialId: readString(obj, 'credentialId'),rpId: readString(obj, 'rpId') ?? '',userHandle: readString(obj, 'userHandle'),publicKey: readString(obj, 'publicKey') ?? '',privateKey: readString(obj, 'privateKey') ?? '',prfKey: readString(obj, 'prfKey'),displayName: readString(obj, 'displayName') ?? '',+ additionalData: readString(obj, 'additionalData'),};}@@ -176,7 +197,9 @@ function parseAvuxFieldDefinition(obj: JsonObject): AvuxFieldDefinition {function parseAvuxLogo(obj: JsonObject): AvuxLogo {return {id: readString(obj, 'id') ?? '',+ kind: readString(obj, 'kind') ?? LogoKinds.Favicon,source: readString(obj, 'source') ?? '',+ name: readString(obj, 'name'),mimeType: readString(obj, 'mimeType'),fetchedAt: readString(obj, 'fetchedAt'),relativePath: readString(obj, 'relativePath') ?? '',diff --git a/core/client/src/transfer/import/models/ImportedCredential.ts b/core/client/src/transfer/import/models/ImportedCredential.tsindex 295a45dff..3b6717c43 100644--- a/core/client/src/transfer/import/models/ImportedCredential.ts+++ b/core/client/src/transfer/import/models/ImportedCredential.ts@@ -3,6 +3,7 @@ import type { ImportedAttachment } from './ImportedAttachment';import type { ImportedCreditcard } from './ImportedCreditcard';import type { ImportedCustomField } from './ImportedCustomField';import type { ImportedPasskey } from './ImportedPasskey';+import type { ItemEntity } from '../../shared/VaultEntities';import type { ItemType } from '@aliasvault/models/vault';/**@@ -32,4 +33,10 @@ export type ImportedCredential = {Tags?: string[] | null;Attachments?: ImportedAttachment[] | null;CustomFieldValues?: ImportedCustomField[] | null;+ /**+ * The full item from an AliasVault export. When set, the writer stores this item graph instead of rebuilding one+ * from the fields above, which then only serve the preview and the duplicate check. This is used primarily for the+ * .avux and .avex file formats which contains much more detailed item data compared to e.g. CSV.+ */+ AliasVaultItem?: ItemEntity | null;};diff --git a/core/client/src/transfer/import/writers/ItemConverter.ts b/core/client/src/transfer/import/writers/ItemConverter.tsindex 45329eff7..f6f19b895 100644--- a/core/client/src/transfer/import/writers/ItemConverter.ts+++ b/core/client/src/transfer/import/writers/ItemConverter.ts@@ -25,7 +25,7 @@ export function convertToItems(credentials: ImportedCredential[], folderPathToId* @param folderPathToId - Folder paths to folder ids, or null* @returns The item*/-function convertToItem(credential: ImportedCredential, folderPathToId: Map<string, string> | null): ItemEntity {+export function convertToItem(credential: ImportedCredential, folderPathToId: Map<string, string> | null): ItemEntity {const now = new Date();const itemId = crypto.randomUUID();const itemType = determineItemType(credential);@@ -36,13 +36,16 @@ function convertToItem(credential: ImportedCredential, folderPathToId: Map<strinName: credential.ServiceName ?? '',ItemType: itemType,FolderId: folderPathToId && !isBlank(credential.FolderPath) ? folderPathToId.get(credential.FolderPath) ?? null : null,- LogoId: null,+ Logo: null,+ ArchivedAt: null,...row,FieldValues: [],+ FieldHistories: [],Attachments: (credential.Attachments ?? []).map(attachment => ({ Id: crypto.randomUUID(), ItemId: itemId, Filename: attachment.Filename, Blob: attachment.Blob, ...row })),TotpCodes: [],Passkeys: (credential.Passkeys ?? []).map(passkey => ({Id: passkey.Id ?? crypto.randomUUID(),+ CredentialId: null,ItemId: itemId,RpId: passkey.RpId,UserHandle: passkey.UserHandle ?? new Uint8Array(0),@@ -50,6 +53,7 @@ function convertToItem(credential: ImportedCredential, folderPathToId: Map<strinPrivateKey: passkey.PrivateKey,PrfKey: passkey.PrfKey ?? null,DisplayName: passkey.DisplayName,+ AdditionalData: null,...row,})),};diff --git a/core/client/src/transfer/import/writers/VaultImportWriter.ts b/core/client/src/transfer/import/writers/VaultImportWriter.tsindex 9f6426d19..5f7814540 100644--- a/core/client/src/transfer/import/writers/VaultImportWriter.ts+++ b/core/client/src/transfer/import/writers/VaultImportWriter.ts@@ -1,10 +1,12 @@-import { FieldKey } from '@aliasvault/models/vault';+import { FieldKey, LogoKinds } from '@aliasvault/models/vault';+import { createImportWriteSession } from '../../../database/repositories/ImportExportRepository';import { selectFaviconTarget } from '../../../rust/RustCore';import { buildFolderPath } from '../../shared/FolderPaths';+import { isBlank } from '../../shared/StringUtils';import { ImportException, ImportStage } from '../models/ImportException';-import { convertToItems } from './ItemConverter';+import { convertToItem } from './ItemConverter';import type { SqliteClient } from '../../../database/SqliteClient';import type { ItemEntity } from '../../shared/VaultEntities';@@ -14,13 +16,9 @@ import type { ImportedCredential } from '../models/ImportedCredential';* How an import is written.*/export type VaultImportOptions = {- /** Folder paths to the folder ids they were created as; null when folders are not imported. */folderNameToId?: Map<string, string> | null;- /** Whether attachments are written; false strips them before conversion. */importAttachments: boolean;- /** Favicons fetched for the import, keyed by their Logos.Source (domain). */extractedFavicons: Map<string, Uint8Array>;- /** Progress callback with the number of items saved so far and the total. */onProgress?: (saved: number, total: number) => Promise<void> | void;};@@ -81,63 +79,92 @@ export class VaultImportWriter {* @throws {ImportException} With stage Save when an item cannot be written.*/public static async importCredentialsToVault(sqliteClient: SqliteClient, credentials: ImportedCredential[], options: VaultImportOptions): Promise<number> {- // Stripping the attachments before conversion keeps them out of the resulting items.- if (!options.importAttachments) {- for (const credential of credentials) {- credential.Attachments = null;- }- }+ const session = createImportWriteSession();- const items = convertToItems(credentials, options.folderNameToId);-- // One logo row per source per import session.+ // One logo row per kind and source per import session.const sessionLogoIds = new Map<string, string | null>();- for (let index = 0; index < items.length; index++) {- const item = items[index];+ for (let index = 0; index < credentials.length; index++) {+ const credential = credentials[index];+ const item = VaultImportWriter.toItem(credential, options);try {- await VaultImportWriter.processSingleItem(sqliteClient, item, credentials, options.extractedFavicons, sessionLogoIds);+ const logoId = await VaultImportWriter.resolveLogoId(sqliteClient, item, credential, options.extractedFavicons, sessionLogoIds);+ await sqliteClient.importExport.insertImportedItem(item, logoId, session);} catch (error) {// Re-thrown with item context so the UI can show the credential that failed.- throw new ImportException(ImportStage.Save, `Failed to save item #${index + 1} of ${items.length} ("${item.Name ?? ''}"): ${error instanceof Error ? error.message : String(error)}`, error);+ throw new ImportException(ImportStage.Save, `Failed to save item #${index + 1} of ${credentials.length} ("${item.Name ?? ''}"): ${error instanceof Error ? error.message : String(error)}`, error);}- await options.onProgress?.(index + 1, items.length);+ await options.onProgress?.(index + 1, credentials.length);+ }++ return credentials.length;+ }++ /**+ * The item a credential is written as: the item an AliasVault export carried, placed in the imported folder, or+ * else an item built from the credential's fields.+ * @param credential - The credential+ * @param options - Folder mapping and attachments+ * @returns The item+ */+ private static toItem(credential: ImportedCredential, options: VaultImportOptions): ItemEntity {+ // Stripping the attachments before conversion keeps them out of the resulting item.+ if (!options.importAttachments) {+ credential.Attachments = null;+ }++ const carried = credential.AliasVaultItem;+ if (!carried) {+ return convertToItem(credential, options.folderNameToId ?? null);}- return items.length;+ const folderNameToId = options.folderNameToId;+ return {+ ...carried,+ FolderId: folderNameToId && !isBlank(credential.FolderPath) ? folderNameToId.get(credential.FolderPath) ?? null : null,+ Attachments: options.importAttachments ? carried.Attachments : [],+ };}/**- * Resolve an item's logo and insert it.+ * Resolve the id an item's logo gets in this vault: the logo an AliasVault export carried, with its kind, or else+ * the favicon of its URL.* @param sqliteClient - The vault* @param item - The item- * @param credentials - The credentials the item came from, for embedded logo bytes+ * @param credential - The credential the item came from, for embedded favicon bytes* @param extractedFavicons - Favicons fetched for the import, by source- * @param sessionLogoIds - Logo ids already resolved in this import, by source+ * @param sessionLogoIds - Logo ids already resolved in this import, by kind and source+ * @returns The logo id, or null when the item gets no logo*/- private static async processSingleItem(sqliteClient: SqliteClient, item: ItemEntity, credentials: ImportedCredential[], extractedFavicons: Map<string, Uint8Array>, sessionLogoIds: Map<string, string | null>): Promise<void> {- const importedCredential = credentials.find(c => c.ServiceName === item.Name);- const urls = item.FieldValues.filter(fv => fv.FieldKey === FieldKey.LoginUrl && !!fv.Value).map(fv => fv.Value!);- const target = await selectFaviconTarget(urls);-- if (target) {- // Bytes embedded in the import file take precedence; otherwise what was fetched for this source.- const faviconBytes = importedCredential?.FaviconBytes ?? extractedFavicons.get(target.source) ?? null;- try {- if (sessionLogoIds.has(target.source)) {- item.LogoId = sessionLogoIds.get(target.source) ?? null;- } else {- item.LogoId = await sqliteClient.importExport.resolveImportLogo(target.source, faviconBytes);- sessionLogoIds.set(target.source, item.LogoId);+ private static async resolveLogoId(sqliteClient: SqliteClient, item: ItemEntity, credential: ImportedCredential, extractedFavicons: Map<string, Uint8Array>, sessionLogoIds: Map<string, string | null>): Promise<string | null> {+ try {+ const carriedLogo = item.Logo;+ if (carriedLogo) {+ const key = `${carriedLogo.Kind}:${carriedLogo.Source}`;+ if (!sessionLogoIds.has(key)) {+ sessionLogoIds.set(key, await sqliteClient.importExport.resolveCarriedLogo(carriedLogo));}- } catch (error) {- // A favicon is not critical: log and import the credential without one.- console.warn(`Failed to process favicon for item ${item.Name ?? ''}:`, error);- item.LogoId = null;+ return sessionLogoIds.get(key) ?? null;+ }++ const urls = item.FieldValues.filter(fv => fv.FieldKey === FieldKey.LoginUrl && !!fv.Value).map(fv => fv.Value!);+ const target = await selectFaviconTarget(urls);+ if (!target) {+ return null;}- }- await sqliteClient.importExport.insertImportedItem(item);+ const key = `${LogoKinds.Favicon}:${target.source}`;+ if (!sessionLogoIds.has(key)) {+ // Bytes embedded in the import file take precedence; otherwise what was fetched for this source.+ const faviconBytes = credential.FaviconBytes ?? extractedFavicons.get(target.source) ?? null;+ sessionLogoIds.set(key, await sqliteClient.importExport.resolveImportLogo(target.source, faviconBytes));+ }+ return sessionLogoIds.get(key) ?? null;+ } catch (error) {+ // A logo is not critical: log and import the credential without one.+ console.warn(`Failed to process logo for item ${item.Name ?? ''}:`, error);+ return null;+ }}}diff --git a/core/client/src/transfer/shared/VaultEntities.ts b/core/client/src/transfer/shared/VaultEntities.tsindex 41b577e02..c32b5a04f 100644--- a/core/client/src/transfer/shared/VaultEntities.ts+++ b/core/client/src/transfer/shared/VaultEntities.ts@@ -1,4 +1,4 @@-import type { ItemType } from '@aliasvault/models/vault';+import type { ItemType, LogoKind } from '@aliasvault/models/vault';/*** An item with its child fields, as the export reads it and as the import produces it.@@ -8,14 +8,18 @@ export type ItemEntity = {Name: string | null;ItemType: ItemType;FolderId: string | null;- LogoId: string | null;+ ArchivedAt: Date | null;CreatedAt: Date;UpdatedAt: Date;IsDeleted: boolean;FieldValues: FieldValueEntity[];+ FieldHistories: FieldHistoryEntity[];Attachments: AttachmentEntity[];TotpCodes: TotpCodeEntity[];Passkeys: PasskeyEntity[];+ /** The item's logo, null when it has none. */+ Logo: LogoEntity | null;+ Tags?: TagEntity[];};/**@@ -26,7 +30,6 @@ export type FieldValueEntity = {ItemId: string;FieldKey: string | null;FieldDefinitionId: string | null;- /** The definition of a custom field, carried along so the import can recreate it. */FieldDefinition?: FieldDefinitionEntity | null;Value: string | null;Weight: number;@@ -35,6 +38,22 @@ export type FieldValueEntity = {IsDeleted: boolean;};+/**+ * A previous value of a field: a system field (FieldKey set) or a custom field (FieldDefinitionId set).+ */+export type FieldHistoryEntity = {+ Id: string;+ ItemId: string;+ FieldKey: string | null;+ FieldDefinitionId: string | null;+ FieldDefinition?: FieldDefinitionEntity | null;+ ValueSnapshot: string;+ ChangedAt: Date;+ CreatedAt: Date;+ UpdatedAt: Date;+ IsDeleted: boolean;+};+/*** A custom field definition.*/@@ -73,6 +92,7 @@ export type TotpCodeEntity = {*/export type PasskeyEntity = {Id: string;+ CredentialId: Uint8Array | null;ItemId: string;RpId: string;UserHandle: Uint8Array | null;@@ -80,6 +100,7 @@ export type PasskeyEntity = {PrivateKey: string;PrfKey: Uint8Array | null;DisplayName: string;+ AdditionalData: Uint8Array | null;CreatedAt: Date;UpdatedAt: Date;IsDeleted: boolean;@@ -138,7 +159,9 @@ export type ItemTagEntity = {*/export type LogoEntity = {Id: string;+ Kind: LogoKind;Source: string;+ Name: string | null;FileData: Uint8Array | null;MimeType: string | null;FetchedAt: Date | null;