Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
stream.on('end', async () => {
try {
expect(output).to.have.lengthOf(1);
expect(output[0]).to.be.an.instanceOf(type);
const outputBytes = await castData(output[0], { type: Uint8Array });
expect(outputBytes).to.deep.equal(bytes);
if (global.Blob && output[0] instanceof global.Blob) {
// $FlowExpectedError
expect(output[0].type).to.equal(options.mime);
}
if (global.File && output[0] instanceof global.File) {
// $FlowExpectedError
expect(output[0].name).to.equal(options.name);
}
resolve();
} catch (e) {
reject(e);
}
});
});
const progressHandler = new ProgressHandler(progressOptions).start(clearSize);
const resourceId = encryption.extractResourceId(castEncryptedData);
const key = await this._resourceManager.findKeyFromResourceId(resourceId);
let clearData;
try {
// $FlowIKnow Already checked we are using a simple encryption
clearData = encryption.decrypt(key, encryption.unserialize(castEncryptedData));
} catch (error) {
const b64ResourceId = utils.toBase64(resourceId);
throw new DecryptionFailed({ error, b64ResourceId });
}
const castClearData = await castData(clearData, outputOptions);
progressHandler.report(clearSize);
return castClearData;
}
async _pushLastChunk() {
// Always push last chunk even if zero bytes
const uint8array = this._buffer.consume(this._buffer.byteSize());
const lastChunk = await castData(uint8array, this._options);
this.push(lastChunk);
}
}
async _simpleDecryptData(encryptedData: Data, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
const castEncryptedData = await castData(encryptedData, { type: Uint8Array });
const encryption = extractEncryptionFormat(castEncryptedData);
const encryptedSize = getDataLength(castEncryptedData);
// $FlowIKnow Already checked we are using a simple encryption
const clearSize = encryption.getClearSize(encryptedSize);
const progressHandler = new ProgressHandler(progressOptions).start(clearSize);
const resourceId = encryption.extractResourceId(castEncryptedData);
const key = await this._resourceManager.findKeyFromResourceId(resourceId);
let clearData;
try {
// $FlowIKnow Already checked we are using a simple encryption
clearData = encryption.decrypt(key, encryption.unserialize(castEncryptedData));
} catch (error) {
async _simpleDecryptData(encryptedData: Data, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
const castEncryptedData = await castData(encryptedData, { type: Uint8Array });
const encryption = extractEncryptionFormat(castEncryptedData);
const encryptedSize = getDataLength(castEncryptedData);
// $FlowIKnow Already checked we are using a simple encryption
const clearSize = encryption.getClearSize(encryptedSize);
const progressHandler = new ProgressHandler(progressOptions).start(clearSize);
const resourceId = encryption.extractResourceId(castEncryptedData);
const key = await this._resourceManager.findKeyFromResourceId(resourceId);
let clearData;
try {
// $FlowIKnow Already checked we are using a simple encryption
clearData = encryption.decrypt(key, encryption.unserialize(castEncryptedData));
} catch (error) {
const b64ResourceId = utils.toBase64(resourceId);
throw new DecryptionFailed({ error, b64ResourceId });
}
async upload(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions): Promise {
const encryptor = await this._dataProtector.makeEncryptorStream(sharingOptions);
const { resourceId } = encryptor;
const totalClearSize = getDataLength(clearData);
const totalEncryptedSize = encryptor.getEncryptedSize(totalClearSize);
const { type, ...fileMetadata } = outputOptions;
// clearContentLength shouldn't be used since we may not have that
// information. We leave it here only for compatibility with SDKs up to 2.2.1
const metadata = { ...fileMetadata, clearContentLength: totalClearSize, encryptionFormat: getStreamEncryptionFormatDescription() };
const encryptedMetadata = await this._encryptAndShareMetadata(metadata, resourceId);
const {
urls,
headers,
service,
recommended_chunk_size: recommendedChunkSize
} = await this._client.send('get file upload url', {
resource_id: resourceId,
metadata: encryptedMetadata,
constructor(options: { source: ExternalSource, outputSize?: number }) {
// $FlowIKnow Use of Object.prototype
if (!options || typeof options !== 'object' || Object.getPrototypeOf(options) !== Object.prototype)
throw new InvalidArgument('options', 'object', options);
const { source, outputSize } = options;
assertDataType(source, 'options.source');
if (outputSize && typeof outputSize !== 'number')
throw new InvalidArgument('options.outputSize', 'number', outputSize);
super({
// buffering a single output chunk
objectMode: true,
highWaterMark: 1,
});
this._outputSize = outputSize || 5 * 1024 * 1024; // 5MB
if (source instanceof ArrayBuffer || source instanceof Uint8Array) { // also catches Buffer
this._initBinaryMode(source);
} else {
this._initFileMode(source);
constructor(options: { type: Class, mime?: string, name?: string, lastModified?: number }) {
// Note: can't use Infinity as it will be forwarded to the writableHighWaterMark option
super(Number.MAX_SAFE_INTEGER);
// $FlowIKnow Use of Object.prototype
if (!options || typeof options !== 'object' || Object.getPrototypeOf(options) !== Object.prototype)
throw new InvalidArgument('options', 'object', options);
const { type, mime, name, lastModified } = options;
assertDataTypeClass(type, 'options.type');
if (mime && typeof mime !== 'string')
throw new InvalidArgument('options.mime', 'string', mime);
if (name && typeof name !== 'string')
throw new InvalidArgument('options.name', 'string', name);
if (lastModified && typeof lastModified !== 'number')
throw new InvalidArgument('options.lastModified', 'number', lastModified);
this._options = options;
}
it(`can encrypt and decrypt a ${size} ${getConstructorName(type)}`, async () => {
const onProgress = sinon.spy();
const encrypted = await aliceLaptop.encryptData(clear, { onProgress });
expectSameType(encrypted, clear);
expectProgressReport(onProgress, getDataLength(encrypted));
onProgress.resetHistory();
const decrypted = await aliceLaptop.decryptData(encrypted, { onProgress });
expectSameType(decrypted, clear);
expectDeepEqual(decrypted, clear);
expectProgressReport(onProgress, getDataLength(decrypted), encryptionV4.defaultMaxEncryptedChunkSize - encryptionV4.overhead);
});
});
args.resources[size].forEach(({ type: transientType }) => {
it(`can encrypt a ${size} ${getConstructorName(originalType)} into a ${getConstructorName(transientType)} and decrypt back a ${getConstructorName(originalType)}`, async () => {
const encrypted = await aliceLaptop.encryptData(clear, { type: transientType });
expectType(encrypted, transientType);
const outputOptions = {};
outputOptions.type = originalType;
if (global.Blob && outputOptions.type === Blob) {
outputOptions.mime = clear.type;
}
if (global.File && outputOptions.type === File) {
outputOptions.mime = clear.type;
outputOptions.name = clear.name;
outputOptions.lastModified = clear.lastModified;
}
const decrypted = await aliceLaptop.decryptData(encrypted, outputOptions);