diff --git a/lib/crypto/modules/NodeCryptoModule/NodeCryptoModule.js b/lib/crypto/modules/NodeCryptoModule/NodeCryptoModule.js index 1cb80e7cc..7b43de9db 100644 --- a/lib/crypto/modules/NodeCryptoModule/NodeCryptoModule.js +++ b/lib/crypto/modules/NodeCryptoModule/NodeCryptoModule.js @@ -74,7 +74,8 @@ var CryptoModule = /** @class */ (function () { this.defaultCryptor = cryptoModuleConfiguration.default; this.cryptors = (_a = cryptoModuleConfiguration.cryptors) !== null && _a !== void 0 ? _a : []; } - //@ts-ignore: type detection issue with old Config type assignment + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore: type detection issue with old Config type assignment CryptoModule.legacyCryptoModule = function (_a) { var config = _a.config; return new this({ @@ -160,10 +161,12 @@ var CryptoModule = /** @class */ (function () { }; return [4 /*yield*/, this.encrypt(file.data)]; case 1: return [2 /*return*/, _b.apply(_a, [( + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field _c.data = _d.sent(), _c)])]; case 2: + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field if (file.data instanceof stream_1.Readable) { encryptedStream = this.defaultCryptor.encryptStream(file.data); @@ -205,6 +208,7 @@ var CryptoModule = /** @class */ (function () { * If It's legacyone then redirect it. * (as long as we support legacy need to check on instance type) */ + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: cryptor will be there or unreachable due to exception if ((cryptor === null || cryptor === void 0 ? void 0 : cryptor.identifier) === CryptoModule.LEGACY_IDENTIFIER) return [2 /*return*/, cryptor.decryptFile(file, File)]; @@ -263,6 +267,7 @@ var CryptoModule = /** @class */ (function () { return __generator(this, function (_a) { cryptor = this.getLegacyCryptor(); if (cryptor) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: cryptor will be there or unreachable due to exception return [2 /*return*/, cryptor.decryptFile(File.create({ name: file.name, @@ -434,12 +439,12 @@ var CryptorHeaderV1 = /** @class */ (function () { if (this.identifier) header.set(Buffer.from(this.identifier), pos); pos += CryptorHeaderV1.IDENTIFIER_LENGTH; - var metadata_size = this.metadataLength; - if (metadata_size < 255) { - header[pos] = metadata_size; + var metadataSize = this.metadataLength; + if (metadataSize < 255) { + header[pos] = metadataSize; } else { - header.set([255, metadata_size >> 8, metadata_size & 0xff], pos); + header.set([255, metadataSize >> 8, metadataSize & 0xff], pos); } return header; }, diff --git a/lib/crypto/modules/NodeCryptoModule/legacyCryptor.js b/lib/crypto/modules/NodeCryptoModule/legacyCryptor.js index 9938ff928..86163e55c 100644 --- a/lib/crypto/modules/NodeCryptoModule/legacyCryptor.js +++ b/lib/crypto/modules/NodeCryptoModule/legacyCryptor.js @@ -67,7 +67,6 @@ var LegacyCryptor = /** @class */ (function () { LegacyCryptor.prototype.decrypt = function (encryptedData) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { - // Legacy cryptor only works on base64 string return [2 /*return*/, this.cryptor.decrypt(encryptedData.data.toString())]; }); }); diff --git a/lib/crypto/modules/WebCryptoModule/legacyCryptor.js b/lib/crypto/modules/WebCryptoModule/legacyCryptor.js index 9a685954f..0666c878d 100644 --- a/lib/crypto/modules/WebCryptoModule/legacyCryptor.js +++ b/lib/crypto/modules/WebCryptoModule/legacyCryptor.js @@ -75,6 +75,7 @@ var LegacyCryptor = /** @class */ (function () { var _a; return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_b) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not detect cipherKey from old Config return [2 /*return*/, this.fileCryptor.encryptFile((_a = this.config) === null || _a === void 0 ? void 0 : _a.cipherKey, file, File)]; }); @@ -83,6 +84,7 @@ var LegacyCryptor = /** @class */ (function () { LegacyCryptor.prototype.decryptFile = function (file, File) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not detect cipherKey from old Config return [2 /*return*/, this.fileCryptor.decryptFile(this.config.cipherKey, file, File)]; }); diff --git a/lib/crypto/modules/WebCryptoModule/webCryptoModule.js b/lib/crypto/modules/WebCryptoModule/webCryptoModule.js index 02daea5a5..9adcd3555 100644 --- a/lib/crypto/modules/WebCryptoModule/webCryptoModule.js +++ b/lib/crypto/modules/WebCryptoModule/webCryptoModule.js @@ -73,6 +73,7 @@ var CryptoModule = /** @class */ (function () { this.defaultCryptor = cryptoModuleConfiguration.default; this.cryptors = (_a = cryptoModuleConfiguration.cryptors) !== null && _a !== void 0 ? _a : []; } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: type detection issue with old Config type assignment CryptoModule.legacyCryptoModule = function (_a) { var config = _a.config; @@ -126,6 +127,7 @@ var CryptoModule = /** @class */ (function () { metadata = header.length > 0 ? encryptedData.slice(header.length - header.metadataLength, header.length) : null; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: cryptor will be there or unreachable due to exception return [2 /*return*/, cryptor.decrypt({ data: encryptedData.slice(header.length), @@ -145,6 +147,7 @@ var CryptoModule = /** @class */ (function () { * Files handled differently in case of Legacy cryptor. * (as long as we support legacy need to check on default cryptor) */ + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: default cryptor will be there. As long as legacy cryptor supported. if (this.defaultCryptor.identifier === '') return [2 /*return*/, this.defaultCryptor.encryptFile(file, File)]; @@ -156,6 +159,7 @@ var CryptoModule = /** @class */ (function () { }; return [4 /*yield*/, this.encrypt(file.data)]; case 1: return [2 /*return*/, _b.apply(_a, [( + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field _c.data = _d.sent(), _c)])]; @@ -178,6 +182,7 @@ var CryptoModule = /** @class */ (function () { * If It's legacyone then redirect it. * (as long as we support legacy need to check on instance type) */ + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: cryptor will be there or unreachable due to exception if (cryptor.identifier === CryptoModule.LEGACY_IDENTIFIER) return [2 /*return*/, cryptor.decryptFile(file, File)]; @@ -187,6 +192,7 @@ var CryptoModule = /** @class */ (function () { }; return [4 /*yield*/, this.decrypt(file.data)]; case 1: return [2 /*return*/, _b.apply(_a, [( + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field _c.data = _d.sent(), _c)])]; @@ -329,12 +335,12 @@ var CryptorHeaderV1 = /** @class */ (function () { if (this.identifier) header.set(encoder.encode(this.identifier), pos); pos += CryptorHeaderV1.IDENTIFIER_LENGTH; - var metadata_size = this.metadataLength; - if (metadata_size < 255) { - header[pos] = metadata_size; + var metadataSize = this.metadataLength; + if (metadataSize < 255) { + header[pos] = metadataSize; } else { - header.set([255, metadata_size >> 8, metadata_size & 0xff], pos); + header.set([255, metadataSize >> 8, metadataSize & 0xff], pos); } return header; }, diff --git a/src/crypto/modules/NodeCryptoModule/nodeCryptoModule.ts b/src/crypto/modules/NodeCryptoModule/nodeCryptoModule.ts index 31d60d87d..742235f13 100644 --- a/src/crypto/modules/NodeCryptoModule/nodeCryptoModule.ts +++ b/src/crypto/modules/NodeCryptoModule/nodeCryptoModule.ts @@ -23,7 +23,8 @@ export default class CryptoModule { this.cryptors = cryptoModuleConfiguration.cryptors ?? []; } - //@ts-ignore: type detection issue with old Config type assignment + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore: type detection issue with old Config type assignment static legacyCryptoModule({ config }) { return new this({ default: new LegacyCryptor({ config }), @@ -51,12 +52,12 @@ export default class CryptoModule { } async encrypt(data: Buffer) { - let encrypted = await this.defaultCryptor.encrypt(data); + const encrypted = await this.defaultCryptor.encrypt(data); if (!encrypted.metadata) return encrypted.data; - let header = CryptorHeader.from(this.defaultCryptor.identifier, encrypted.metadata); + const header = CryptorHeader.from(this.defaultCryptor.identifier, encrypted.metadata); - let headerData = new Uint8Array(header!.length); + const headerData = new Uint8Array(header!.length); let pos = 0; headerData.set(header!.data, pos); pos += header!.length; @@ -71,9 +72,9 @@ export default class CryptoModule { let encryptedData = null; encryptedData = Buffer.from(data); - let header = CryptorHeader.tryParse(encryptedData); - let cryptor = this.getCryptor(header); - let metadata = + const header = CryptorHeader.tryParse(encryptedData); + const cryptor = this.getCryptor(header); + const metadata = header.length > 0 ? encryptedData.slice(header.length - (header as CryptorHeaderV1).metadataLength, header.length) : null; @@ -89,22 +90,26 @@ export default class CryptoModule { * (as long as we support legacy need to check on intsance type) */ if (this.defaultCryptor instanceof LegacyCryptor) return this.defaultCryptor.encryptFile(file, File); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field if (file.data instanceof Buffer) { return File.create({ name: file.name, mimeType: 'application/octet-stream', + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field data: await this.encrypt(file.data!), }); } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field if (file.data instanceof Readable) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: default cryptor will be there. As long as legacy cryptor supported. - let encryptedStream = this.defaultCryptor.encryptStream(file.data); - let header = CryptorHeader.from(this.defaultCryptor.identifier, encryptedStream.metadata); + const encryptedStream = this.defaultCryptor.encryptStream(file.data); + const header = CryptorHeader.from(this.defaultCryptor.identifier, encryptedStream.metadata); - let payload = new Uint8Array(header!.length); + const payload = new Uint8Array(header!.length); let pos = 0; payload.set(header!.data, pos); pos += header!.length; @@ -126,12 +131,13 @@ export default class CryptoModule { async decryptFile(file: any, File: PubnubFile) { if (file?.data instanceof Buffer) { - let header = CryptorHeader.tryParse(file.data); - let cryptor = this.getCryptor(header); + const header = CryptorHeader.tryParse(file.data); + const cryptor = this.getCryptor(header); /** * If It's legacyone then redirect it. * (as long as we support legacy need to check on instance type) */ + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: cryptor will be there or unreachable due to exception if (cryptor?.identifier === CryptoModule.LEGACY_IDENTIFIER) return cryptor.decryptFile(file, File); return File.create({ @@ -151,16 +157,16 @@ export default class CryptoModule { async onStreamReadable(stream: NodeJS.ReadableStream, file: PubnubFile, File: PubnubFile) { stream.removeAllListeners('readable'); - let magicBytes = stream.read(4); + const magicBytes = stream.read(4); if (!CryptorHeader.isSentinel(magicBytes as Buffer)) { stream.unshift(magicBytes); return this.decryptLegacyFileStream(stream, file, File); } - let versionByte = stream.read(1); + const versionByte = stream.read(1); CryptorHeader.validateVersion(versionByte[0] as number); - let identifier = stream.read(4); - let cryptor = this.getCryptorFromId(CryptorHeader.tryGetIdentifier(identifier as Buffer)); - let headerSize = CryptorHeader.tryGetMetadataSizeFromStream(stream); + const identifier = stream.read(4); + const cryptor = this.getCryptorFromId(CryptorHeader.tryGetIdentifier(identifier as Buffer)); + const headerSize = CryptorHeader.tryGetMetadataSizeFromStream(stream); return File.create({ name: file.name, @@ -172,6 +178,7 @@ export default class CryptoModule { async decryptLegacyFileStream(stream: NodeJS.ReadableStream, file: PubnubFile, File: PubnubFile) { const cryptor = this.getLegacyCryptor(); if (cryptor) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: cryptor will be there or unreachable due to exception return cryptor.decryptFile( File.create({ @@ -336,11 +343,11 @@ class CryptorHeaderV1 { pos++; if (this.identifier) header.set(Buffer.from(this.identifier), pos); pos += CryptorHeaderV1.IDENTIFIER_LENGTH; - let metadata_size = this.metadataLength; - if (metadata_size < 255) { - header[pos] = metadata_size; + const metadataSize = this.metadataLength; + if (metadataSize < 255) { + header[pos] = metadataSize; } else { - header.set([255, metadata_size >> 8, metadata_size & 0xff], pos); + header.set([255, metadataSize >> 8, metadataSize & 0xff], pos); } return header; } diff --git a/src/crypto/modules/WebCryptoModule/legacyCryptor.ts b/src/crypto/modules/WebCryptoModule/legacyCryptor.ts index 062398eec..b4c689faa 100644 --- a/src/crypto/modules/WebCryptoModule/legacyCryptor.ts +++ b/src/crypto/modules/WebCryptoModule/legacyCryptor.ts @@ -33,11 +33,13 @@ export default class LegacyCryptor implements ILegacyCryptor { } async encryptFile(file: PubnubFile, File: PubnubFile) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not detect cipherKey from old Config return this.fileCryptor.encryptFile(this.config?.cipherKey, file, File); } async decryptFile(file: PubnubFile, File: PubnubFile) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not detect cipherKey from old Config return this.fileCryptor.decryptFile(this.config.cipherKey, file, File); } diff --git a/src/crypto/modules/WebCryptoModule/webCryptoModule.ts b/src/crypto/modules/WebCryptoModule/webCryptoModule.ts index 3bba4dd37..ec7f11386 100644 --- a/src/crypto/modules/WebCryptoModule/webCryptoModule.ts +++ b/src/crypto/modules/WebCryptoModule/webCryptoModule.ts @@ -21,6 +21,7 @@ export default class CryptoModule { this.cryptors = cryptoModuleConfiguration.cryptors ?? []; } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: type detection issue with old Config type assignment static legacyCryptoModule({ config }) { return new this({ @@ -45,12 +46,12 @@ export default class CryptoModule { } async encrypt(data: ArrayBuffer) { - let encrypted = await this.defaultCryptor.encrypt(data); + const encrypted = await this.defaultCryptor.encrypt(data); if (!encrypted.metadata) return encrypted.data; - let header = CryptorHeader.from(this.defaultCryptor.identifier, encrypted.metadata); + const header = CryptorHeader.from(this.defaultCryptor.identifier, encrypted.metadata); - let headerData = new Uint8Array((header as CryptorHeaderV1).length); + const headerData = new Uint8Array((header as CryptorHeaderV1).length); let pos = 0; headerData.set((header as CryptorHeaderV1).data, pos); pos += (header as CryptorHeaderV1).length; @@ -62,12 +63,13 @@ export default class CryptoModule { } async decrypt(encryptedData: ArrayBuffer) { - let header = CryptorHeader.tryParse(encryptedData); - let cryptor = this.getCryptor(header); - let metadata = + const header = CryptorHeader.tryParse(encryptedData); + const cryptor = this.getCryptor(header); + const metadata = header.length > 0 ? encryptedData.slice(header.length - (header as CryptorHeaderV1).metadataLength, header.length) : null; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: cryptor will be there or unreachable due to exception return cryptor.decrypt({ data: encryptedData.slice(header.length), @@ -80,13 +82,16 @@ export default class CryptoModule { * Files handled differently in case of Legacy cryptor. * (as long as we support legacy need to check on default cryptor) */ + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: default cryptor will be there. As long as legacy cryptor supported. if (this.defaultCryptor.identifier === '') return this.defaultCryptor.encryptFile(file, File); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field if (file.data instanceof Buffer) { return File.create({ name: file.name, mimeType: 'application/octet-stream', + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field data: await this.encrypt(file.data), }); @@ -94,19 +99,23 @@ export default class CryptoModule { } async decryptFile(file: PubnubFile, File: PubnubFile) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field if (file.data instanceof Buffer) { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field - let header = CryptorHeader.tryParse(file.data); - let cryptor = this.getCryptor(header); + const header = CryptorHeader.tryParse(file.data); + const cryptor = this.getCryptor(header); /** * If It's legacyone then redirect it. * (as long as we support legacy need to check on instance type) */ + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: cryptor will be there or unreachable due to exception if (cryptor.identifier === CryptoModule.LEGACY_IDENTIFIER) return cryptor.decryptFile(file, File); return File.create({ name: file.name, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore: can not infer that PubNubFile has data field data: await this.decrypt(file.data), }); @@ -243,11 +252,11 @@ class CryptorHeaderV1 { pos++; if (this.identifier) header.set(encoder.encode(this.identifier), pos); pos += CryptorHeaderV1.IDENTIFIER_LENGTH; - let metadata_size = this.metadataLength; - if (metadata_size < 255) { - header[pos] = metadata_size; + const metadataSize = this.metadataLength; + if (metadataSize < 255) { + header[pos] = metadataSize; } else { - header.set([255, metadata_size >> 8, metadata_size & 0xff], pos); + header.set([255, metadataSize >> 8, metadataSize & 0xff], pos); } return header; } diff --git a/src/crypto/modules/web.js b/src/crypto/modules/web.js index 44e419f4d..31cabdd2b 100644 --- a/src/crypto/modules/web.js +++ b/src/crypto/modules/web.js @@ -98,9 +98,8 @@ export default class WebCryptography { async decryptArrayBuffer(key, ciphertext) { const abIv = ciphertext.slice(0, 16); - - const data = await crypto.subtle.decrypt({ name: 'AES-CBC', iv: abIv }, key, ciphertext.slice(16)); - return data; + const data = await crypto.subtle.decrypt({ name: 'AES-CBC', iv: abIv }, key, ciphertext.slice(16)); + return data; } async encryptString(key, plaintext) {