Skip to content

Commit

Permalink
Fix/blob file encryption (#346)
Browse files Browse the repository at this point in the history
* fix: Blob file support for encryption in Web environment

* dist/lib

* PubNub SDK v7.4.3 release.

---------

Co-authored-by: PubNub Release Bot <[email protected]>
  • Loading branch information
mohitpubnub and pubnub-release-bot authored Nov 8, 2023
1 parent 513113f commit 291b212
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 36 deletions.
11 changes: 8 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
---
changelog:
- date: 2023-11-08
version: v7.4.3
changes:
- type: bug
text: "Fixes issue of not able to encrypt Blob file content in web."
- date: 2023-10-30
version: v7.4.2
changes:
Expand Down Expand Up @@ -912,7 +917,7 @@ supported-platforms:
- 'Ubuntu 14.04 and up'
- 'Windows 7 and up'
version: 'Pubnub Javascript for Node'
version: '7.4.2'
version: '7.4.3'
sdks:
- full-name: PubNub Javascript SDK
short-name: Javascript
Expand All @@ -928,7 +933,7 @@ sdks:
- distribution-type: source
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/archive/refs/tags/v7.4.2.zip
location: https://github.com/pubnub/javascript/archive/refs/tags/v7.4.3.zip
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down Expand Up @@ -1599,7 +1604,7 @@ sdks:
- distribution-type: library
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/releases/download/v7.4.2/pubnub.7.4.2.js
location: https://github.com/pubnub/javascript/releases/download/v7.4.3/pubnub.7.4.3.js
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v7.4.3
November 08 2023

#### Fixed
- Fixes issue of not able to encrypt Blob file content in web.

## v7.4.2
October 30 2023

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
npm install pubnub
```
* or download one of our builds from our CDN:
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.4.2.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.4.2.min.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.4.3.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.7.4.3.min.js

2. Configure your keys:

Expand Down
42 changes: 30 additions & 12 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@
return this;
};
default_1.prototype.getVersion = function () {
return '7.4.2';
return '7.4.3';
};
default_1.prototype._addPnsdkSuffix = function (name, suffix) {
this._PNSDKSuffix[name] = suffix;
Expand Down Expand Up @@ -13136,9 +13136,11 @@
case 0:
if (this.defaultCryptor.identifier === CryptorHeader.LEGACY_IDENTIFIER)
return [2 /*return*/, this.defaultCryptor.encryptFile(file, File)];
fileData = this.getFileData(file.data);
return [4 /*yield*/, this.defaultCryptor.encryptFileData(fileData)];
return [4 /*yield*/, this.getFileData(file.data)];
case 1:
fileData = _a.sent();
return [4 /*yield*/, this.defaultCryptor.encryptFileData(fileData)];
case 2:
encrypted = _a.sent();
return [2 /*return*/, File.create({
name: file.name,
Expand All @@ -13163,7 +13165,9 @@
if ((cryptor === null || cryptor === void 0 ? void 0 : cryptor.identifier) === CryptoModule.LEGACY_IDENTIFIER) {
return [2 /*return*/, cryptor.decryptFile(file, File)];
}
fileData = this.getFileData(data);
return [4 /*yield*/, this.getFileData(data)];
case 2:
fileData = _d.sent();
metadata = fileData.slice(header.length - header.metadataLength, header.length);
_b = (_a = File).create;
_c = {
Expand All @@ -13173,7 +13177,7 @@
data: data.slice(header.length),
metadata: metadata,
})];
case 2: return [2 /*return*/, _b.apply(_a, [(_c.data = _d.sent(),
case 3: return [2 /*return*/, _b.apply(_a, [(_c.data = _d.sent(),
_c)])];
}
});
Expand Down Expand Up @@ -13215,13 +13219,27 @@
return headerData.buffer;
};
CryptoModule.prototype.getFileData = function (input) {
if (input instanceof ArrayBuffer) {
return input;
}
if (typeof input === 'string') {
return CryptoModule.encoder.encode(input);
}
throw new Error('Cannot decrypt/encrypt file. In browsers file decryption supports only string or ArrayBuffer');
return __awaiter(this, void 0, void 0, function () {
var fileData;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(input instanceof Blob)) return [3 /*break*/, 2];
return [4 /*yield*/, input.arrayBuffer()];
case 1:
fileData = _a.sent();
return [2 /*return*/, fileData];
case 2:
if (input instanceof ArrayBuffer) {
return [2 /*return*/, input];
}
if (typeof input === 'string') {
return [2 /*return*/, CryptoModule.encoder.encode(input)];
}
throw new Error('Cannot decrypt/encrypt file. In browsers file encrypt/decrypt supported for string, ArrayBuffer or Blob');
}
});
});
};
CryptoModule.LEGACY_IDENTIFIER = '';
CryptoModule.encoder = new TextEncoder();
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/core/components/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ var default_1 = /** @class */ (function () {
return this;
};
default_1.prototype.getVersion = function () {
return '7.4.2';
return '7.4.3';
};
default_1.prototype._addPnsdkSuffix = function (name, suffix) {
this._PNSDKSuffix[name] = suffix;
Expand Down
40 changes: 29 additions & 11 deletions lib/crypto/modules/WebCryptoModule/webCryptoModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ var CryptoModule = /** @class */ (function () {
case 0:
if (this.defaultCryptor.identifier === CryptorHeader.LEGACY_IDENTIFIER)
return [2 /*return*/, this.defaultCryptor.encryptFile(file, File)];
fileData = this.getFileData(file.data);
return [4 /*yield*/, this.defaultCryptor.encryptFileData(fileData)];
return [4 /*yield*/, this.getFileData(file.data)];
case 1:
fileData = _a.sent();
return [4 /*yield*/, this.defaultCryptor.encryptFileData(fileData)];
case 2:
encrypted = _a.sent();
return [2 /*return*/, File.create({
name: file.name,
Expand All @@ -162,7 +164,9 @@ var CryptoModule = /** @class */ (function () {
if ((cryptor === null || cryptor === void 0 ? void 0 : cryptor.identifier) === CryptoModule.LEGACY_IDENTIFIER) {
return [2 /*return*/, cryptor.decryptFile(file, File)];
}
fileData = this.getFileData(data);
return [4 /*yield*/, this.getFileData(data)];
case 2:
fileData = _d.sent();
metadata = fileData.slice(header.length - header.metadataLength, header.length);
_b = (_a = File).create;
_c = {
Expand All @@ -172,7 +176,7 @@ var CryptoModule = /** @class */ (function () {
data: data.slice(header.length),
metadata: metadata,
})];
case 2: return [2 /*return*/, _b.apply(_a, [(_c.data = _d.sent(),
case 3: return [2 /*return*/, _b.apply(_a, [(_c.data = _d.sent(),
_c)])];
}
});
Expand Down Expand Up @@ -214,13 +218,27 @@ var CryptoModule = /** @class */ (function () {
return headerData.buffer;
};
CryptoModule.prototype.getFileData = function (input) {
if (input instanceof ArrayBuffer) {
return input;
}
if (typeof input === 'string') {
return CryptoModule.encoder.encode(input);
}
throw new Error('Cannot decrypt/encrypt file. In browsers file decryption supports only string or ArrayBuffer');
return __awaiter(this, void 0, void 0, function () {
var fileData;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(input instanceof Blob)) return [3 /*break*/, 2];
return [4 /*yield*/, input.arrayBuffer()];
case 1:
fileData = _a.sent();
return [2 /*return*/, fileData];
case 2:
if (input instanceof ArrayBuffer) {
return [2 /*return*/, input];
}
if (typeof input === 'string') {
return [2 /*return*/, CryptoModule.encoder.encode(input)];
}
throw new Error('Cannot decrypt/encrypt file. In browsers file encrypt/decrypt supported for string, ArrayBuffer or Blob');
}
});
});
};
CryptoModule.LEGACY_IDENTIFIER = '';
CryptoModule.encoder = new TextEncoder();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pubnub",
"version": "7.4.2",
"version": "7.4.3",
"author": "PubNub <[email protected]>",
"description": "Publish & Subscribe Real-time Messaging with PubNub",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ export default class {
}

getVersion() {
return '7.4.2';
return '7.4.3';
}

_addPnsdkSuffix(name, suffix) {
Expand Down
14 changes: 10 additions & 4 deletions src/crypto/modules/WebCryptoModule/webCryptoModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class CryptoModule {
async encryptFile(file: PubNubFileType, File: PubNubFileType) {
if (this.defaultCryptor.identifier === CryptorHeader.LEGACY_IDENTIFIER)
return (this.defaultCryptor as ILegacyCryptor<PubNubFileType>).encryptFile(file, File);
const fileData = this.getFileData(file.data);
const fileData = await this.getFileData(file.data);
const encrypted = await (this.defaultCryptor as ICryptor).encryptFileData(fileData);
return File.create({
name: file.name,
Expand All @@ -98,7 +98,7 @@ export class CryptoModule {
if (cryptor?.identifier === CryptoModule.LEGACY_IDENTIFIER) {
return (cryptor as ILegacyCryptor<PubNubFileType>).decryptFile(file, File);
}
const fileData = this.getFileData(data);
const fileData = await this.getFileData(data);
const metadata = fileData.slice(header.length - (header as CryptorHeaderV1).metadataLength, header.length);
return File.create({
name: file.name,
Expand Down Expand Up @@ -147,14 +147,20 @@ export class CryptoModule {
return headerData.buffer;
}

private getFileData(input: any) {
private async getFileData(input: any) {
if (input instanceof Blob) {
const fileData = await input.arrayBuffer();
return fileData;
}
if (input instanceof ArrayBuffer) {
return input;
}
if (typeof input === 'string') {
return CryptoModule.encoder.encode(input);
}
throw new Error('Cannot decrypt/encrypt file. In browsers file decryption supports only string or ArrayBuffer');
throw new Error(
'Cannot decrypt/encrypt file. In browsers file encrypt/decrypt supported for string, ArrayBuffer or Blob',
);
}
}

Expand Down

0 comments on commit 291b212

Please sign in to comment.