Feature flag is no longer required, keep code that is relevant to the feature flag being on. Revoke the rest (#24645)

This commit is contained in:
Jared
2026-11-12 14:30:54 -05:00
committed by GitHub
parent 484911b262
commit 4d76bbfb69
7 changed files with 176 additions and 427 deletions

View File

@@ -1044,7 +1044,6 @@ export default class MainBackground {
this.collectionEncryptionService = new DefaultCollectionEncryptionService(
this.sdkService,
this.logService,
this.configService,
);
this.collectionService = new DefaultCollectionService(

View File

@@ -787,7 +787,6 @@ export class ServiceContainer {
this.collectionEncryptionService = new DefaultCollectionEncryptionService(
this.sdkService,
this.logService,
this.configService,
);
this.collectionService = new DefaultCollectionService(

View File

@@ -21,11 +21,10 @@ export abstract class CollectionEncryptionService {
/**
* Decrypts many collections using the SDK for the given userId.
*
* A collection that fails to decrypt never aborts the rest of the batch. What happens to it
* depends on the `CollectionsDecryptListFailures` feature flag: while the flag is off the
* failure is logged and the collection is dropped from the result; once it is on the collection
* is included with `decryptionFailure` set and its `name` replaced by the decrypt-error
* placeholder, so the item is still shown to the user rather than silently disappearing.
* A collection that fails to decrypt never aborts the rest of the batch. Instead, the failure is
* logged and the collection is included with `decryptionFailure` set and its `name` replaced by
* the decrypt-error placeholder, so the item is still shown to the user rather than silently
* disappearing.
*
* @param collections The encrypted collection objects
* @param userId The user ID whose keys will be used for decryption

View File

@@ -5,8 +5,6 @@ import {
CollectionTypes,
} from "@bitwarden/common/admin-console/models/collections/collection";
import { CollectionView } from "@bitwarden/common/admin-console/models/collections/collection.view";
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { SdkService } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
import { CollectionId, OrganizationId, UserId } from "@bitwarden/common/types/guid";
@@ -72,10 +70,6 @@ describe("DefaultCollectionEncryptionService", () => {
mark: jest.fn(),
} as unknown as LogService;
const sdkService = { userClient$: jest.fn() } as unknown as SdkService;
const configService = {
getFeatureFlag: jest.fn(),
getFeatureFlag$: jest.fn(),
} as unknown as ConfigService;
let mockDecrypt: jest.Mock;
let mockDecryptListWithFailures: jest.Mock;
@@ -105,347 +99,210 @@ describe("DefaultCollectionEncryptionService", () => {
const mockSdk = { take: jest.fn().mockReturnValue(mockRef) };
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(mockSdk));
service = new DefaultCollectionEncryptionService(sdkService, logService, configService);
service = new DefaultCollectionEncryptionService(sdkService, logService);
});
describe("when CollectionsDecryptListFailures is enabled", () => {
beforeEach(() => {
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(true));
describe("decryptMany", () => {
it("returns an empty array without calling the SDK for empty input", async () => {
const result = await firstValueFrom(service.decryptMany([], userId));
expect(result).toEqual([]);
expect(mockDecryptListWithFailures).not.toHaveBeenCalled();
});
describe("decryptMany", () => {
it("checks the CollectionsDecryptListFailures feature flag", async () => {
mockDecryptListWithFailures.mockReturnValue({
successes: [makeSdkCollectionView()],
failures: [],
});
it("decrypts all collections via a single batch call and returns views", async () => {
const collection1 = makeCollection();
const collection2 = makeCollection({ id: collectionId2 });
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
await firstValueFrom(service.decryptMany([makeCollection()], userId));
expect(configService.getFeatureFlag$).toHaveBeenCalledWith(
FeatureFlag.CollectionsDecryptListFailures,
);
mockDecryptListWithFailures.mockReturnValue({
successes: [
makeSdkCollectionView({ id: collectionId as any, name: "Collection 1" }),
makeSdkCollectionView({ id: collectionId2 as any, name: "Collection 2" }),
],
failures: [],
});
it("returns an empty array without calling the SDK for empty input", async () => {
const result = await firstValueFrom(service.decryptMany([], userId));
expect(result).toEqual([]);
expect(mockDecryptListWithFailures).not.toHaveBeenCalled();
});
const result = await firstValueFrom(service.decryptMany([collection1, collection2], userId));
it("decrypts all collections via a single batch call and returns views", async () => {
const collection1 = makeCollection();
const collection2 = makeCollection({ id: collectionId2 });
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockReturnValue({
successes: [
makeSdkCollectionView({ id: collectionId as any, name: "Collection 1" }),
makeSdkCollectionView({ id: collectionId2 as any, name: "Collection 2" }),
],
failures: [],
});
const result = await firstValueFrom(
service.decryptMany([collection1, collection2], userId),
);
expect(result).toHaveLength(2);
expect(result[0].name).toBe("Collection 1");
expect(result[1].name).toBe("Collection 2");
});
it("logs and still returns a placeholder view for any collection the SDK returns as a failure", async () => {
const collection1 = makeCollection();
const collection2 = makeCollection({ id: collectionId2 });
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
// SDK reports collection1 as a failure and returns only collection2.
mockDecryptListWithFailures.mockReturnValue({
successes: [makeSdkCollectionView({ id: collectionId2 as any, name: "Collection 2" })],
failures: [{ id: collectionId as any }],
});
const result = await firstValueFrom(
service.decryptMany([collection1, collection2], userId),
);
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining(`Failed to decrypt collection ${collection1.id}`),
);
expect(result).toHaveLength(2);
const failedView = result.find((v) => v.id === collectionId)!;
const successView = result.find((v) => v.id === collectionId2)!;
expect(failedView.decryptionFailure).toBe(true);
expect(failedView.name).toBe(DECRYPT_ERROR);
expect(successView.decryptionFailure).toBe(false);
expect(successView.name).toBe("Collection 2");
});
it("rejects when the batch SDK call throws", async () => {
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockImplementation(() => {
throw new Error("batch failure");
});
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
});
it("preserves defaultUserCollectionEmail from the source collection", async () => {
const email = "offboarded@example.com";
const collection = makeCollection({
defaultUserCollectionEmail: email,
type: CollectionTypes.DefaultUserCollection,
});
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockReturnValue({
successes: [makeSdkCollectionView({ type: CollectionTypes.DefaultUserCollection })],
failures: [],
});
const [result] = await firstValueFrom(service.decryptMany([collection], userId));
expect(result.defaultUserCollectionEmail).toBe(email);
});
it("matches each decrypted view to its source collection by ID", async () => {
const email = "offboarded@example.com";
const collection1 = makeCollection({
defaultUserCollectionEmail: email,
type: CollectionTypes.DefaultUserCollection,
});
const collection2 = makeCollection({
id: collectionId2,
type: CollectionTypes.SharedCollection,
});
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
// Return views in reverse order to confirm ID-based matching, not index-based.
mockDecryptListWithFailures.mockReturnValue({
successes: [
makeSdkCollectionView({
id: collectionId2 as any,
type: CollectionTypes.SharedCollection,
}),
makeSdkCollectionView({
id: collectionId as any,
type: CollectionTypes.DefaultUserCollection,
}),
],
failures: [],
});
const results = await firstValueFrom(
service.decryptMany([collection1, collection2], userId),
);
const view1 = results.find((v) => v.id === collectionId);
const view2 = results.find((v) => v.id === collectionId2);
expect(view1?.defaultUserCollectionEmail).toBe(email);
expect(view2?.defaultUserCollectionEmail).toBeUndefined();
});
// Pairing is by ID, so a view whose ID does not round-trip cannot be mapped back to its
// source and drops out of the list - the one disappearance this path cannot represent.
it("logs a decrypted view whose ID matches no source collection", async () => {
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockReturnValue({
successes: [makeSdkCollectionView({ id: collectionId2 as any })],
failures: [],
});
const results = await firstValueFrom(service.decryptMany([collection], userId));
expect(results).toEqual([]);
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining(`Decrypted collection ${collectionId2} did not match`),
);
});
it("logs the error and rejects when the SDK client is unavailable", async () => {
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
});
expect(result).toHaveLength(2);
expect(result[0].name).toBe("Collection 1");
expect(result[1].name).toBe("Collection 2");
});
describe("decrypt", () => {
it("decrypts a single collection via the batch SDK call and maps the result", async () => {
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
it("logs and still returns a placeholder view for any collection the SDK returns as a failure", async () => {
const collection1 = makeCollection();
const collection2 = makeCollection({ id: collectionId2 });
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
const sdkView = makeSdkCollectionView({ name: "Decrypted Name" });
mockDecryptListWithFailures.mockReturnValue({ successes: [sdkView], failures: [] });
const result = await firstValueFrom(service.decrypt(collection, userId));
expect(mockDecryptListWithFailures).toHaveBeenCalledWith([stubSdkCollection]);
expect(mockDecrypt).not.toHaveBeenCalled();
expect(result).toBeInstanceOf(CollectionView);
expect(result.name).toBe("Decrypted Name");
// SDK reports collection1 as a failure and returns only collection2.
mockDecryptListWithFailures.mockReturnValue({
successes: [makeSdkCollectionView({ id: collectionId2 as any, name: "Collection 2" })],
failures: [{ id: collectionId as any }],
});
it("rejects when the batch call throws", async () => {
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockImplementation(() => {
throw new Error("batch failure");
});
const result = await firstValueFrom(service.decryptMany([collection1, collection2], userId));
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining(`Failed to decrypt collection ${collection1.id}`),
);
expect(result).toHaveLength(2);
const failedView = result.find((v) => v.id === collectionId)!;
const successView = result.find((v) => v.id === collectionId2)!;
expect(failedView.decryptionFailure).toBe(true);
expect(failedView.name).toBe(DECRYPT_ERROR);
expect(successView.decryptionFailure).toBe(false);
expect(successView.name).toBe("Collection 2");
});
it("rejects when the batch SDK call throws", async () => {
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockImplementation(() => {
throw new Error("batch failure");
});
it("logs the error and rejects when the SDK client is unavailable", async () => {
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
});
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
it("preserves defaultUserCollectionEmail from the source collection", async () => {
const email = "offboarded@example.com";
const collection = makeCollection({
defaultUserCollectionEmail: email,
type: CollectionTypes.DefaultUserCollection,
});
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockReturnValue({
successes: [makeSdkCollectionView({ type: CollectionTypes.DefaultUserCollection })],
failures: [],
});
const [result] = await firstValueFrom(service.decryptMany([collection], userId));
expect(result.defaultUserCollectionEmail).toBe(email);
});
it("matches each decrypted view to its source collection by ID", async () => {
const email = "offboarded@example.com";
const collection1 = makeCollection({
defaultUserCollectionEmail: email,
type: CollectionTypes.DefaultUserCollection,
});
const collection2 = makeCollection({
id: collectionId2,
type: CollectionTypes.SharedCollection,
});
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
// Return views in reverse order to confirm ID-based matching, not index-based.
mockDecryptListWithFailures.mockReturnValue({
successes: [
makeSdkCollectionView({
id: collectionId2 as any,
type: CollectionTypes.SharedCollection,
}),
makeSdkCollectionView({
id: collectionId as any,
type: CollectionTypes.DefaultUserCollection,
}),
],
failures: [],
});
const results = await firstValueFrom(service.decryptMany([collection1, collection2], userId));
const view1 = results.find((v) => v.id === collectionId);
const view2 = results.find((v) => v.id === collectionId2);
expect(view1?.defaultUserCollectionEmail).toBe(email);
expect(view2?.defaultUserCollectionEmail).toBeUndefined();
});
// Pairing is by ID, so a view whose ID does not round-trip cannot be mapped back to its
// source and drops out of the list - the one disappearance this path cannot represent.
it("logs a decrypted view whose ID matches no source collection", async () => {
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockReturnValue({
successes: [makeSdkCollectionView({ id: collectionId2 as any })],
failures: [],
});
const results = await firstValueFrom(service.decryptMany([collection], userId));
expect(results).toEqual([]);
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining(`Decrypted collection ${collectionId2} did not match`),
);
});
it("logs the error and rejects when the SDK client is unavailable", async () => {
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
});
});
describe("when CollectionsDecryptListFailures is disabled", () => {
beforeEach(() => {
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(false));
describe("decrypt", () => {
it("decrypts a single collection via the batch SDK call and maps the result", async () => {
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
const sdkView = makeSdkCollectionView({ name: "Decrypted Name" });
mockDecryptListWithFailures.mockReturnValue({ successes: [sdkView], failures: [] });
const result = await firstValueFrom(service.decrypt(collection, userId));
expect(mockDecryptListWithFailures).toHaveBeenCalledWith([stubSdkCollection]);
expect(mockDecrypt).not.toHaveBeenCalled();
expect(result).toBeInstanceOf(CollectionView);
expect(result.name).toBe("Decrypted Name");
});
describe("decryptMany", () => {
it("returns an empty array without calling the SDK for empty input", async () => {
const result = await firstValueFrom(service.decryptMany([], userId));
expect(result).toEqual([]);
expect(mockDecrypt).not.toHaveBeenCalled();
it("rejects when the batch call throws", async () => {
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockImplementation(() => {
throw new Error("batch failure");
});
it("decrypts all collections one at a time and returns views", async () => {
const collection1 = makeCollection();
const collection2 = makeCollection({ id: collectionId2 });
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecrypt
.mockReturnValueOnce(makeSdkCollectionView({ name: "Collection 1" }))
.mockReturnValueOnce(makeSdkCollectionView({ name: "Collection 2" }));
const result = await firstValueFrom(
service.decryptMany([collection1, collection2], userId),
);
expect(mockDecryptListWithFailures).not.toHaveBeenCalled();
expect(result).toHaveLength(2);
expect(result[0].name).toBe("Collection 1");
expect(result[1].name).toBe("Collection 2");
});
it("logs and drops items that fail to decrypt without aborting the rest", async () => {
const collection1 = makeCollection();
const collection2 = makeCollection({ id: collectionId2 });
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecrypt
.mockImplementationOnce(() => {
throw new Error("key not found");
})
.mockReturnValueOnce(makeSdkCollectionView({ name: "Collection 2" }));
const result = await firstValueFrom(
service.decryptMany([collection1, collection2], userId),
);
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining(`Failed to decrypt collection ${collection1.id}`),
);
expect(result).toHaveLength(1);
expect(result[0].name).toBe("Collection 2");
});
it("preserves defaultUserCollectionEmail from the source collection", async () => {
const email = "offboarded@example.com";
const collection = makeCollection({
defaultUserCollectionEmail: email,
type: CollectionTypes.DefaultUserCollection,
});
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecrypt.mockReturnValue(
makeSdkCollectionView({ type: CollectionTypes.DefaultUserCollection }),
);
const [result] = await firstValueFrom(service.decryptMany([collection], userId));
expect(result.defaultUserCollectionEmail).toBe(email);
});
it("logs the error and rejects when the SDK client is unavailable", async () => {
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
await expect(firstValueFrom(service.decryptMany([collection], userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
});
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
});
describe("decrypt", () => {
it("decrypts a single collection using the original per-item SDK call", async () => {
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecrypt.mockReturnValue(makeSdkCollectionView({ name: "Decrypted Name" }));
it("logs the error and rejects when the SDK client is unavailable", async () => {
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
const result = await firstValueFrom(service.decrypt(collection, userId));
expect(mockDecrypt).toHaveBeenCalledWith(stubSdkCollection);
expect(mockDecryptListWithFailures).not.toHaveBeenCalled();
expect(result).toBeInstanceOf(CollectionView);
expect(result.name).toBe("Decrypted Name");
});
it("logs the error and rejects when the SDK client is unavailable", async () => {
(sdkService.userClient$ as jest.Mock).mockReturnValue(of(null));
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
});
await expect(firstValueFrom(service.decrypt(collection, userId))).rejects.toThrow();
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining("Failed to decrypt collections in batch"),
);
});
});
/**
* The SDK bindings for these two calls are currently typed as synchronous, while the equivalent
* cipher bindings return promises. Both call sites are awaited so the service behaves correctly
* either way, and these specs pin that down: they hand the mocks a promise, which only produces
* the right result if the `await` is present. Dropping either `await` makes the spec below fail
* instead of silently returning an empty list.
* The SDK binding for `decrypt_list_with_failures` is currently typed as synchronous, while the
* equivalent cipher bindings return promises. The call site is awaited so the service behaves
* correctly either way, and these specs pin that down: they hand the mock a promise, which only
* produces the right result if the `await` is present. Dropping the `await` makes the spec below
* fail instead of silently returning an empty list.
*/
describe("when the SDK bindings return promises", () => {
it("awaits decrypt_list_with_failures and still separates successes from failures", async () => {
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(true));
const collection1 = makeCollection();
const collection2 = makeCollection({ id: collectionId2 });
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
@@ -463,43 +320,7 @@ describe("DefaultCollectionEncryptionService", () => {
expect(result.find((c) => c.id === collectionId)?.decryptionFailure).toBe(true);
});
it("awaits the per-item decrypt on the V1 path", async () => {
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(false));
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecrypt.mockResolvedValue(makeSdkCollectionView({ name: "Decrypted Name" }));
const [result] = await firstValueFrom(service.decryptMany([collection], userId));
expect(result).toBeInstanceOf(CollectionView);
expect(result.name).toBe("Decrypted Name");
});
it("logs and drops an item whose per-item decrypt rejects, without aborting the rest", async () => {
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(false));
const collection1 = makeCollection();
const collection2 = makeCollection({ id: collectionId2 });
jest.spyOn(collection1, "toSdkCollection").mockReturnValue(stubSdkCollection);
jest.spyOn(collection2, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecrypt
.mockRejectedValueOnce(new Error("key not found"))
.mockResolvedValueOnce(makeSdkCollectionView({ name: "Collection 2" }));
const result = await firstValueFrom(service.decryptMany([collection1, collection2], userId));
expect(logService.error).toHaveBeenCalledWith(
expect.stringContaining(`Failed to decrypt collection ${collection1.id}`),
);
expect(result).toHaveLength(1);
expect(result[0].name).toBe("Collection 2");
});
it("rejects when a promise-returning decrypt_list_with_failures rejects", async () => {
(configService.getFeatureFlag$ as jest.Mock).mockReturnValue(of(true));
const collection = makeCollection();
jest.spyOn(collection, "toSdkCollection").mockReturnValue(stubSdkCollection);
mockDecryptListWithFailures.mockRejectedValue(new Error("batch failure"));

View File

@@ -1,9 +1,7 @@
import { Observable, catchError, concatMap, distinctUntilChanged, map, of, switchMap } from "rxjs";
import { Observable, catchError, concatMap, map, of } from "rxjs";
import { Collection } from "@bitwarden/common/admin-console/models/collections/collection";
import { CollectionView } from "@bitwarden/common/admin-console/models/collections/collection.view";
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { SdkService, uuidAsString } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
import { CollectionId, UserId } from "@bitwarden/common/types/guid";
@@ -15,7 +13,6 @@ export class DefaultCollectionEncryptionService implements CollectionEncryptionS
constructor(
private sdkService: SdkService,
private logService: LogService,
private configService: ConfigService,
) {}
decrypt(collection: Collection, userId: UserId): Observable<CollectionView> {
@@ -36,70 +33,6 @@ export class DefaultCollectionEncryptionService implements CollectionEncryptionS
return of([]);
}
return this.configService.getFeatureFlag$(FeatureFlag.CollectionsDecryptListFailures).pipe(
distinctUntilChanged(),
switchMap((bulkDecryptEnabled) =>
bulkDecryptEnabled
? this.decryptManyV2(collections, userId)
: this.decryptManyV1(collections, userId),
),
);
}
/**
* V1 implementation: decrypts each collection individually via the SDK, one at a time.
* A collection that fails to decrypt is logged and dropped rather than aborting the rest
* of the batch.
*/
private decryptManyV1(collections: Collection[], userId: UserId): Observable<CollectionView[]> {
return this.sdkService.userClient$(userId).pipe(
concatMap(async (sdk) => {
const startTime = performance.now();
using ref = sdk.take();
const success: CollectionView[] = [];
for (const collection of collections) {
try {
const sdkView = await ref.value
.vault()
.collections()
.decrypt(collection.toSdkCollection());
success.push(CollectionView.fromSdkCollectionView(sdkView, collection));
} catch (error: unknown) {
this.logService.error(`Failed to decrypt collection ${collection.id}: ${error}`);
}
}
this.logService.measure(
startTime,
"Admin Console",
"DefaultCollectionEncryptionService",
"decryptMany (v1, one at a time)",
[
["Items", collections.length],
["Successes", success.length],
],
);
return success;
}),
catchError((error: unknown) => {
this.logService.error(`Failed to decrypt collections in batch: ${error}`);
throw error;
}),
);
}
/**
* V2 implementation using the SDK's `decrypt_list_with_failures` for batch performance with
* per-item failure tolerance. The SDK natively separates successes from failures. Collections
* that fail to decrypt are still returned to the caller as a placeholder view (`name` replaced
* by the decrypt-error placeholder, `decryptionFailure` set) rather than being dropped, so the
* item remains visible instead of silently disappearing from the vault. Gated behind
* {@link FeatureFlag.CollectionsDecryptListFailures} until the SDK bindings have rolled out
* everywhere this service is used.
*/
private decryptManyV2(collections: Collection[], userId: UserId): Observable<CollectionView[]> {
return this.sdkService.userClient$(userId).pipe(
concatMap(async (sdk) => {
const startTime = performance.now();
@@ -144,7 +77,7 @@ export class DefaultCollectionEncryptionService implements CollectionEncryptionS
startTime,
"Admin Console",
"DefaultCollectionEncryptionService",
"decryptMany (v2, decrypt_list_with_failures)",
"decryptMany",
[
["Items", collections.length],
["Successes", views.filter((v) => !v.decryptionFailure).length],

View File

@@ -840,7 +840,7 @@ const safeProviders: SafeProvider[] = [
safeProvider({
provide: CollectionEncryptionService,
useClass: DefaultCollectionEncryptionService,
deps: [SdkService, LogService, ConfigService],
deps: [SdkService, LogService],
}),
safeProvider({
provide: CollectionService,

View File

@@ -11,7 +11,6 @@ import { ServerConfig } from "../platform/abstractions/config/server-config";
// eslint-disable-next-line @bitwarden/platform/no-enums
export enum FeatureFlag {
/* Admin Console Team */
CollectionsDecryptListFailures = "pm-43375-collections-decrypt-list-failures",
InviteLinkAutoConfirm = "pm-34429-invite-link-auto-confirm",
GroupsComponentRefactor = "pm-43419-groups-table-v2",
MembersTableV2 = "pm-37463-members-table-v2",
@@ -125,7 +124,6 @@ const FALSE = false as boolean;
*/
export const DefaultFeatureFlagValue = {
/* Admin Console Team */
[FeatureFlag.CollectionsDecryptListFailures]: FALSE,
[FeatureFlag.InviteLinkAutoConfirm]: FALSE,
[FeatureFlag.GroupsComponentRefactor]: FALSE,
[FeatureFlag.MembersTableV2]: FALSE,