mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
Fix cipher upload (#346)
* Upload correct data array * Require BufferArray Encryption for upload to server The CipherArrayBuffer tiny type is only created by CryptoService and required by all upload methods * Add test for attachment upload encryption
This commit is contained in:
61
spec/common/services/cipher.service.spec.ts
Normal file
61
spec/common/services/cipher.service.spec.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { Arg, Substitute, SubstituteOf } from '@fluffy-spoon/substitute';
|
||||
|
||||
import { ApiService } from '../../../src/abstractions/api.service';
|
||||
import { CryptoService } from '../../../src/abstractions/crypto.service';
|
||||
import { FileUploadService } from '../../../src/abstractions/fileUpload.service';
|
||||
import { I18nService } from '../../../src/abstractions/i18n.service';
|
||||
import { SearchService } from '../../../src/abstractions/search.service';
|
||||
import { SettingsService } from '../../../src/abstractions/settings.service';
|
||||
import { StorageService } from '../../../src/abstractions/storage.service';
|
||||
import { UserService } from '../../../src/abstractions/user.service';
|
||||
import { Utils } from '../../../src/misc/utils';
|
||||
import { Cipher } from '../../../src/models/domain/cipher';
|
||||
import { CipherArrayBuffer } from '../../../src/models/domain/cipherArrayBuffer';
|
||||
import { CipherString } from '../../../src/models/domain/cipherString';
|
||||
import { SymmetricCryptoKey } from '../../../src/models/domain/symmetricCryptoKey';
|
||||
|
||||
import { CipherService } from '../../../src/services/cipher.service';
|
||||
|
||||
const ENCRYPTED_TEXT = 'This data has been encrypted';
|
||||
const ENCRYPTED_BYTES = new CipherArrayBuffer(Utils.fromUtf8ToArray(ENCRYPTED_TEXT).buffer);
|
||||
|
||||
describe('Cipher Service', () => {
|
||||
let cryptoService: SubstituteOf<CryptoService>;
|
||||
let userService: SubstituteOf<UserService>;
|
||||
let settingsService: SubstituteOf<SettingsService>;
|
||||
let apiService: SubstituteOf<ApiService>;
|
||||
let fileUploadService: SubstituteOf<FileUploadService>;
|
||||
let storageService: SubstituteOf<StorageService>;
|
||||
let i18nService: SubstituteOf<I18nService>;
|
||||
let searchService: SubstituteOf<SearchService>;
|
||||
|
||||
let cipherService: CipherService;
|
||||
|
||||
beforeEach(() => {
|
||||
cryptoService = Substitute.for<CryptoService>();
|
||||
userService = Substitute.for<UserService>();
|
||||
settingsService = Substitute.for<SettingsService>();
|
||||
apiService = Substitute.for<ApiService>();
|
||||
fileUploadService = Substitute.for<FileUploadService>();
|
||||
storageService = Substitute.for<StorageService>();
|
||||
i18nService = Substitute.for<I18nService>();
|
||||
searchService = Substitute.for<SearchService>();
|
||||
|
||||
cryptoService.encryptToBytes(Arg.any(), Arg.any()).resolves(ENCRYPTED_BYTES);
|
||||
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(new CipherString(ENCRYPTED_TEXT));
|
||||
|
||||
cipherService = new CipherService(cryptoService, userService, settingsService, apiService, fileUploadService,
|
||||
storageService, i18nService, () => searchService);
|
||||
});
|
||||
|
||||
it('attachments upload encrypted file contents', async () => {
|
||||
const key = new SymmetricCryptoKey(new Uint8Array(32).buffer);
|
||||
const fileName = 'filename';
|
||||
const fileData = new Uint8Array(10).buffer;
|
||||
cryptoService.getOrgKey(Arg.any()).resolves(new SymmetricCryptoKey(new Uint8Array(32).buffer));
|
||||
|
||||
await cipherService.saveAttachmentRawWithServer(new Cipher(), fileName, fileData);
|
||||
|
||||
fileUploadService.received(1).uploadCipherAttachment(Arg.any(), Arg.any(), fileName, ENCRYPTED_BYTES);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user