Skip to content

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

Update queries (#2159)

Leendert de Borsta99383221a71
diff --git a/apps/mobile-app/ios/VaultStoreKit/Database/Queries/PasskeyQueries.swift b/apps/mobile-app/ios/VaultStoreKit/Database/Queries/PasskeyQueries.swift
index 996eb7b3b..e719e41ff 100644
--- a/apps/mobile-app/ios/VaultStoreKit/Database/Queries/PasskeyQueries.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/Database/Queries/PasskeyQueries.swift
@@ -95,40 +95,6 @@ public struct PasskeyQueries {
WHERE Id = ? AND ManifestId = ? AND IsDeleted = 0
"""
- /// Get Items that match an rpId but don't have a passkey yet.
- /// Note: The public API now uses getAllItemsWithoutPasskey + Rust credential matcher for consistent cross-platform matching.
- /// This query is kept for potential fallback scenarios.
- public static let getItemsWithoutPasskeyForRpId = """
- SELECT i.Id, i.ManifestId, i.Name, i.CreatedAt, i.UpdatedAt,
- fv_url.Value as Url,
- fv_username.Value as Username,
- fv_email.Value as Email,
- fv_password.Value as Password
- FROM Items i
- INNER JOIN FieldValues fv_url ON fv_url.ItemId = i.Id AND fv_url.ManifestId = i.ManifestId
- AND fv_url.FieldKey = 'login.url'
- AND fv_url.IsDeleted = 0
- LEFT JOIN FieldValues fv_username ON fv_username.ItemId = i.Id AND fv_username.ManifestId = i.ManifestId
- AND fv_username.FieldKey = 'login.username'
- AND fv_username.IsDeleted = 0
- LEFT JOIN FieldValues fv_email ON fv_email.ItemId = i.Id AND fv_email.ManifestId = i.ManifestId
- AND fv_email.FieldKey = 'login.email'
- AND fv_email.IsDeleted = 0
- LEFT JOIN FieldValues fv_password ON fv_password.ItemId = i.Id AND fv_password.ManifestId = i.ManifestId
- AND fv_password.FieldKey = 'login.password'
- AND fv_password.IsDeleted = 0
- WHERE i.IsDeleted = 0
- AND i.DeletedAt IS NULL
- AND i.ArchivedAt IS NULL
- AND i.ItemType = 'Login'
- AND (LOWER(fv_url.Value) LIKE ? OR LOWER(fv_url.Value) LIKE ?)
- AND NOT EXISTS (
- SELECT 1 FROM Passkeys p
- WHERE p.ItemId = i.Id AND p.ManifestId = i.ManifestId AND p.IsDeleted = 0
- )
- ORDER BY i.UpdatedAt DESC
- """
-
/// Get ALL active Login items that don't have a passkey yet (no URL filtering).
/// Used with Rust credential matcher for intelligent filtering.
/// Returns items with their URLs aggregated using GROUP_CONCAT for multi-URL support.
diff --git a/core/client/src/database/queries/FolderQueries.ts b/core/client/src/database/queries/FolderQueries.ts
index a881aba42..33db98989 100644
--- a/core/client/src/database/queries/FolderQueries.ts
+++ b/core/client/src/database/queries/FolderQueries.ts
@@ -3,6 +3,10 @@ import { BaseQueries } from './BaseQueries';
/**
* SQL query constants for Folder operations.
* Centralizes all folder-related queries to avoid duplication.
+ *
+ * A folder is keyed by `(ManifestId, Id)` like every other manifest-scoped row, so each statement below
+ * takes the folder's manifest as well as its id: two manifests may hold a folder with the same id, and
+ * matching on the id alone would rename or delete the other manifest's folder along with this one.
*/
export class FolderQueries {
/**
@@ -15,12 +19,12 @@ export class FolderQueries {
ORDER BY Weight, Name`;
/**
- * Get folder by ID.
+ * Get one folder by its manifest-qualified key.
*/
public static readonly GET_BY_ID = `
SELECT Id, Name, ParentFolderId, ManifestId
FROM Folders
- WHERE Id = ? AND IsDeleted = 0`;
+ WHERE Id = ? AND ManifestId = ? AND IsDeleted = 0`;
/**
* Insert a new folder, stamped with its parent folder's manifest.
@@ -36,7 +40,7 @@ export class FolderQueries {
UPDATE Folders
SET Name = ?,
UpdatedAt = ?
- WHERE Id = ?`;
+ WHERE Id = ? AND ManifestId = ?`;
/**
* Soft delete folder.
@@ -45,17 +49,18 @@ export class FolderQueries {
UPDATE Folders
SET IsDeleted = 1,
UpdatedAt = ?
- WHERE Id = ?`;
+ WHERE Id = ? AND ManifestId = ?`;
/**
- * Clear folder reference from items (set to NULL).
+ * Clear folder reference from items (set to NULL). The manifest bound to the SET is the one an item
+ * outside every folder joins; the one in the WHERE is the folder's own.
*/
public static readonly CLEAR_ITEMS_FOLDER = `
UPDATE Items
SET FolderId = NULL,
ManifestId = ?,
UpdatedAt = ?
- WHERE FolderId = ?`;
+ WHERE FolderId = ? AND ManifestId = ?`;
/**
* Move items to a different folder.
@@ -65,7 +70,7 @@ export class FolderQueries {
SET FolderId = ?,
ManifestId = ${BaseQueries.MANIFEST_OF_FOLDER},
UpdatedAt = ?
- WHERE FolderId = ?`;
+ WHERE FolderId = ? AND ManifestId = ?`;
/**
* Trash items in folder.
@@ -75,15 +80,15 @@ export class FolderQueries {
SET DeletedAt = ?,
UpdatedAt = ?,
FolderId = NULL
- WHERE FolderId = ? AND IsDeleted = 0 AND DeletedAt IS NULL`;
+ WHERE FolderId = ? AND ManifestId = ? AND IsDeleted = 0 AND DeletedAt IS NULL`;
/**
- * Get all child folder IDs (direct children only).
+ * Get all child folder IDs (direct children only), within the parent's own manifest.
*/
public static readonly GET_CHILD_FOLDER_IDS = `
SELECT Id
FROM Folders
- WHERE ParentFolderId = ? AND IsDeleted = 0`;
+ WHERE ParentFolderId = ? AND ManifestId = ? AND IsDeleted = 0`;
/**
* Update parent folder for child folders.
@@ -92,5 +97,5 @@ export class FolderQueries {
UPDATE Folders
SET ParentFolderId = ?,
UpdatedAt = ?
- WHERE ParentFolderId = ?`;
+ WHERE ParentFolderId = ? AND ManifestId = ?`;
}
diff --git a/core/client/src/database/repositories/FolderRepository.ts b/core/client/src/database/repositories/FolderRepository.ts
index ecfa46da2..0e3a95727 100644
--- a/core/client/src/database/repositories/FolderRepository.ts
+++ b/core/client/src/database/repositories/FolderRepository.ts
@@ -17,6 +17,15 @@ export type Folder = {
ManifestId?: string | null;
}
+/**
+ * A manifest-qualified reference to a folder: folders are keyed by `(ManifestId, Id)`, so an id on its
+ * own does not name one folder.
+ */
+type FolderRef = {
+ Id: string;
+ ManifestId: string;
+}
+
/**
* Repository for Folder CRUD operations.
*/
@@ -69,37 +78,67 @@ export class FolderRepository extends BaseRepository {
/**
* Get a folder by ID.
* @param folderId - The ID of the folder
+ * @param manifestId - The manifest the folder belongs to, when known
* @returns Folder object or null if not found
*/
- public *getById(folderId: string): DbOp<Omit<Folder, 'Weight'> | null> {
- const results = yield* this.query<Omit<Folder, 'Weight'>>(FolderQueries.GET_BY_ID, [folderId]);
+ public *getById(folderId: string, manifestId?: string): DbOp<Omit<Folder, 'Weight'> | null> {
+ const ref = yield* this.resolveFolderRef(folderId, manifestId);
+ if (!ref) {
+ return null;
+ }
+
+ const results = yield* this.query<Omit<Folder, 'Weight'>>(FolderQueries.GET_BY_ID, [ref.Id, ref.ManifestId]);
return results.length > 0 ? results[0] : null;
}
+ /**
+ * Resolve the manifest-qualified reference a folder id names.
+ * @param folderId - The folder id
+ * @param manifestId - The manifest the folder belongs to, when the caller already knows it
+ * @returns The reference, or null when no folder carries this id
+ */
+ private *resolveFolderRef(folderId: string, manifestId?: string): DbOp<FolderRef | null> {
+ if (manifestId) {
+ return { Id: folderId, ManifestId: manifestId };
+ }
+
+ const resolved = yield* this.resolveRowManifestId('Folders', folderId);
+ return resolved ? { Id: folderId, ManifestId: resolved } : null;
+ }
+
/**
* Update a folder's name.
* @param folderId - The ID of the folder to update
* @param name - The new name for the folder
+ * @param manifestId - The manifest the folder belongs to, when known
* @returns The number of rows updated
*/
- public async update(folderId: string, name: string): Promise<number> {
- return this.withTransaction(() => this.run(this.execute(FolderQueries.UPDATE_NAME, [name, this.now(), folderId])));
+ public async update(folderId: string, name: string, manifestId?: string): Promise<number> {
+ return this.withTransaction(async () => {
+ const ref = await this.run(this.resolveFolderRef(folderId, manifestId));
+ if (!ref) {
+ return 0;
+ }
+ return this.run(this.execute(FolderQueries.UPDATE_NAME, [name, this.now(), ref.Id, ref.ManifestId]));
+ });
}
/**
- * Get all child folder IDs recursively.
+ * Get all child folder IDs recursively. A folder tree never crosses a manifest, so every descendant is
+ * looked up inside the root folder's own manifest.
* @param folderId - The parent folder ID
+ * @param manifestId - The manifest the folder tree lives in
* @returns Array of all descendant folder IDs
*/
- private *getAllChildFolderIds(folderId: string): DbOp<string[]> {
- const directChildren = yield* this.query<{ Id: string }>(FolderQueries.GET_CHILD_FOLDER_IDS, [folderId]);
+ private *getAllChildFolderIds(folderId: string, manifestId: string): DbOp<string[]> {
+ const directChildren = yield* this.query<{ Id: string }>(FolderQueries.GET_CHILD_FOLDER_IDS, [folderId, manifestId]);
const allChildIds: string[] = [];
for (const child of directChildren) {
allChildIds.push(child.Id);
// Recursively get all descendants
- allChildIds.push(...(yield* this.getAllChildFolderIds(child.Id)));
+ allChildIds.push(...(yield* this.getAllChildFolderIds(child.Id, manifestId)));
}
return allChildIds;
@@ -112,23 +151,29 @@ export class FolderRepository extends BaseRepository {
* - Items in child folders stay in their respective folders (since child folders are moved to parent)
* - All direct child folders are moved to the parent of the deleted folder
* @param folderId - The ID of the folder to delete
+ * @param manifestId - The manifest the folder belongs to, when known
* @returns The number of rows updated
*/
- public async delete(folderId: string): Promise<number> {
- await this.assertDeletable(folderId);
- return this.withTransaction(() => this.run(this.deleteKeepingContents(folderId)));
+ public async delete(folderId: string, manifestId?: string): Promise<number> {
+ const ref = await this.run(this.resolveFolderRef(folderId, manifestId));
+ if (!ref) {
+ return 0;
+ }
+
+ await this.assertDeletable(ref);
+ return this.withTransaction(() => this.run(this.deleteKeepingContents(ref)));
}
/**
* Soft delete a folder, moving its items and child folders up to its parent.
- * @param folderId - The ID of the folder to delete
+ * @param ref - The folder to delete
* @returns The number of rows updated
*/
- private *deleteKeepingContents(folderId: string): DbOp<number> {
+ private *deleteKeepingContents(ref: FolderRef): DbOp<number> {
const currentDateTime = this.now();
// Get the parent folder of the folder being deleted
- const folder = yield* this.getById(folderId);
+ const folder = yield* this.getById(ref.Id, ref.ManifestId);
const targetParentId = folder?.ParentFolderId || null;
const manifestId = yield* this.writeManifestId();
@@ -141,18 +186,19 @@ export class FolderRepository extends BaseRepository {
targetParentId,
manifestId,
currentDateTime,
- folderId
+ ref.Id,
+ ref.ManifestId
]);
} else {
// No parent: move items to root (NULL); out of every folder means into the default manifest.
- yield* this.execute(FolderQueries.CLEAR_ITEMS_FOLDER, [manifestId, currentDateTime, folderId]);
+ yield* this.execute(FolderQueries.CLEAR_ITEMS_FOLDER, [manifestId, currentDateTime, ref.Id, ref.ManifestId]);
}
// Move direct child folders to the parent of the deleted folder
- yield* this.execute(FolderQueries.UPDATE_PARENT_FOLDER, [targetParentId, currentDateTime, folderId]);
+ yield* this.execute(FolderQueries.UPDATE_PARENT_FOLDER, [targetParentId, currentDateTime, ref.Id, ref.ManifestId]);
// Soft delete the folder
- return yield* this.execute(FolderQueries.SOFT_DELETE, [currentDateTime, folderId]);
+ return yield* this.execute(FolderQueries.SOFT_DELETE, [currentDateTime, ref.Id, ref.ManifestId]);
}
/**
@@ -161,32 +207,38 @@ export class FolderRepository extends BaseRepository {
* - All items in this folder and child folders are moved to "Recently Deleted" (trash)
* - All child folders are also deleted
* @param folderId - The ID of the folder to delete
+ * @param manifestId - The manifest the folder belongs to, when known
* @returns The number of items trashed
*/
- public async deleteWithContents(folderId: string): Promise<number> {
- await this.assertDeletable(folderId);
- return this.withTransaction(() => this.run(this.deleteFolderTree(folderId)));
+ public async deleteWithContents(folderId: string, manifestId?: string): Promise<number> {
+ const ref = await this.run(this.resolveFolderRef(folderId, manifestId));
+ if (!ref) {
+ return 0;
+ }
+
+ await this.assertDeletable(ref);
+ return this.withTransaction(() => this.run(this.deleteFolderTree(ref)));
}
/**
* Soft delete a folder and its child folders, moving every item inside them to the trash.
- * @param folderId - The ID of the folder to delete
+ * @param ref - The folder to delete
* @returns The number of items trashed
*/
- private *deleteFolderTree(folderId: string): DbOp<number> {
+ private *deleteFolderTree(ref: FolderRef): DbOp<number> {
const currentDateTime = this.now();
- const allChildFolderIds = yield* this.getAllChildFolderIds(folderId);
+ const allChildFolderIds = yield* this.getAllChildFolderIds(ref.Id, ref.ManifestId);
let totalItemsDeleted = 0;
// Move all items in this folder and in its child folders to trash
- for (const id of [folderId, ...allChildFolderIds]) {
- totalItemsDeleted += yield* this.execute(FolderQueries.TRASH_ITEMS_IN_FOLDER, [currentDateTime, currentDateTime, id]);
+ for (const id of [ref.Id, ...allChildFolderIds]) {
+ totalItemsDeleted += yield* this.execute(FolderQueries.TRASH_ITEMS_IN_FOLDER, [currentDateTime, currentDateTime, id, ref.ManifestId]);
}
// Soft delete all child folders, then the folder itself
- for (const id of [...allChildFolderIds, folderId]) {
- yield* this.execute(FolderQueries.SOFT_DELETE, [currentDateTime, id]);
+ for (const id of [...allChildFolderIds, ref.Id]) {
+ yield* this.execute(FolderQueries.SOFT_DELETE, [currentDateTime, id, ref.ManifestId]);
}
return totalItemsDeleted;
@@ -194,10 +246,10 @@ export class FolderRepository extends BaseRepository {
/**
* Refuse to delete a folder that a shared manifest is rendered as (see {@link multiManifestRendering}).
- * @param folderId - The folder about to be deleted
+ * @param ref - The folder about to be deleted
*/
- private async assertDeletable(folderId: string): Promise<void> {
- const folder = await this.run(this.getById(folderId));
+ private async assertDeletable(ref: FolderRef): Promise<void> {
+ const folder = await this.run(this.getById(ref.Id, ref.ManifestId));
if (folder && multiManifestRendering.isManifestRoot(folder)) {
throw new Error(await getPlatform().translate(TranslatableMessage.SharedFolderDeleteRefused));
}