Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash unencrypted message with crypto module #349

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -2354,9 +2354,7 @@
}
catch (e) {
decryptedPayload = null;
if (console && console.log) {
console.log('decryption error', e.message);
}
announce.error = "Error while decrypting message content: ".concat(e.message);
}
if (decryptedPayload !== null) {
msgPayload = decryptedPayload;
Expand Down Expand Up @@ -2401,10 +2399,7 @@
}
catch (e) {
decryptedPayload = null;
// eslint-disable-next-line
if (console && console.log) {
console.log('decryption error', e.message); //eslint-disable-line
}
announce.error = "Error while decrypting message content: ".concat(e.message);
}
if (decryptedPayload != null) {
announce.message = decryptedPayload;
Expand Down Expand Up @@ -6239,18 +6234,24 @@

/* */
function __processMessage$1(modules, message) {
if (!modules.cryptoModule)
return message;
var result = {};
if (!modules.cryptoModule) {
result.payload = message;
return result;
}
try {
var decryptedData = modules.cryptoModule.decrypt(message);
var decryptedPayload = decryptedData instanceof ArrayBuffer ? JSON.parse(new TextDecoder().decode(decryptedData)) : decryptedData;
return decryptedPayload;
result.payload = decryptedPayload;
return result;
}
catch (e) {
if (console && console.log)
if (modules.config.logVerbosity && console && console.log)
console.log('decryption error', e.message);
return message;
result.payload = message;
result.error = "Error while decrypting message content: ".concat(e.message);
}
return result;
}
function getOperation$5() {
return OPERATIONS.PNHistoryOperation;
Expand Down Expand Up @@ -6301,13 +6302,16 @@
};
if (Array.isArray(serverResponse[0])) {
serverResponse[0].forEach(function (serverHistoryItem) {
var processedMessgeResult = __processMessage$1(modules, serverHistoryItem.message);
var item = {
timetoken: serverHistoryItem.timetoken,
entry: __processMessage$1(modules, serverHistoryItem.message),
entry: processedMessgeResult.payload,
};
if (serverHistoryItem.meta) {
item.meta = serverHistoryItem.meta;
}
if (processedMessgeResult.error)
item.error = processedMessgeResult.error;
response.messages.push(item);
});
}
Expand Down Expand Up @@ -6438,18 +6442,24 @@

/* */
function __processMessage(modules, message) {
if (!modules.cryptoModule)
return message;
var result = {};
if (!modules.cryptoModule) {
result.payload = message;
return result;
}
try {
var decryptedData = modules.cryptoModule.decrypt(message);
var decryptedPayload = decryptedData instanceof ArrayBuffer ? JSON.parse(new TextDecoder().decode(decryptedData)) : decryptedData;
return decryptedPayload;
result.payload = decryptedPayload;
return result;
}
catch (e) {
if (console && console.log)
if (modules.config.logVerbosity && console && console.log)
console.log('decryption error', e.message);
return message;
result.payload = message;
result.error = "Error while decrypting message content: ".concat(e.message);
}
return result;
}
function getOperation$2() {
return OPERATIONS.PNFetchMessagesOperation;
Expand Down Expand Up @@ -6511,9 +6521,10 @@
response.channels[channelName] = [];
(serverResponse.channels[channelName] || []).forEach(function (messageEnvelope) {
var announce = {};
var processedMessgeResult = __processMessage(modules, messageEnvelope.message);
announce.channel = channelName;
announce.timetoken = messageEnvelope.timetoken;
announce.message = __processMessage(modules, messageEnvelope.message);
announce.message = processedMessgeResult.payload;
announce.messageType = messageEnvelope.message_type;
announce.uuid = messageEnvelope.uuid;
if (messageEnvelope.actions) {
Expand All @@ -6524,6 +6535,8 @@
if (messageEnvelope.meta) {
announce.meta = messageEnvelope.meta;
}
if (processedMessgeResult.error)
announce.error = processedMessgeResult.error;
response.channels[channelName].push(announce);
});
});
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions lib/core/components/subscription_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,7 @@ var default_1 = /** @class */ (function () {
}
catch (e) {
decryptedPayload = null;
if (console && console.log) {
console.log('decryption error', e.message);
}
announce.error = "Error while decrypting message content: ".concat(e.message);
}
if (decryptedPayload !== null) {
msgPayload = decryptedPayload;
Expand Down Expand Up @@ -581,10 +579,7 @@ var default_1 = /** @class */ (function () {
}
catch (e) {
decryptedPayload = null;
// eslint-disable-next-line
if (console && console.log) {
console.log('decryption error', e.message); //eslint-disable-line
}
announce.error = "Error while decrypting message content: ".concat(e.message);
}
if (decryptedPayload != null) {
announce.message = decryptedPayload;
Expand Down
21 changes: 15 additions & 6 deletions lib/core/endpoints/fetch_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ exports.handleResponse = exports.prepareParams = exports.isAuthSupported = expor
var operations_1 = __importDefault(require("../constants/operations"));
var utils_1 = __importDefault(require("../utils"));
function __processMessage(modules, message) {
if (!modules.cryptoModule)
return message;
var result = {};
if (!modules.cryptoModule) {
result.payload = message;
return result;
}
try {
var decryptedData = modules.cryptoModule.decrypt(message);
var decryptedPayload = decryptedData instanceof ArrayBuffer ? JSON.parse(new TextDecoder().decode(decryptedData)) : decryptedData;
return decryptedPayload;
result.payload = decryptedPayload;
return result;
}
catch (e) {
if (console && console.log)
if (modules.config.logVerbosity && console && console.log)
console.log('decryption error', e.message);
return message;
result.payload = message;
result.error = "Error while decrypting message content: ".concat(e.message);
}
return result;
}
function getOperation() {
return operations_1.default.PNFetchMessagesOperation;
Expand Down Expand Up @@ -87,9 +93,10 @@ function handleResponse(modules, serverResponse) {
response.channels[channelName] = [];
(serverResponse.channels[channelName] || []).forEach(function (messageEnvelope) {
var announce = {};
var processedMessgeResult = __processMessage(modules, messageEnvelope.message);
announce.channel = channelName;
announce.timetoken = messageEnvelope.timetoken;
announce.message = __processMessage(modules, messageEnvelope.message);
announce.message = processedMessgeResult.payload;
announce.messageType = messageEnvelope.message_type;
announce.uuid = messageEnvelope.uuid;
if (messageEnvelope.actions) {
Expand All @@ -100,6 +107,8 @@ function handleResponse(modules, serverResponse) {
if (messageEnvelope.meta) {
announce.meta = messageEnvelope.meta;
}
if (processedMessgeResult.error)
announce.error = processedMessgeResult.error;
response.channels[channelName].push(announce);
});
});
Expand Down
21 changes: 15 additions & 6 deletions lib/core/endpoints/history/get_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@ exports.handleResponse = exports.prepareParams = exports.isAuthSupported = expor
var operations_1 = __importDefault(require("../../constants/operations"));
var utils_1 = __importDefault(require("../../utils"));
function __processMessage(modules, message) {
if (!modules.cryptoModule)
return message;
var result = {};
if (!modules.cryptoModule) {
result.payload = message;
return result;
}
try {
var decryptedData = modules.cryptoModule.decrypt(message);
var decryptedPayload = decryptedData instanceof ArrayBuffer ? JSON.parse(new TextDecoder().decode(decryptedData)) : decryptedData;
return decryptedPayload;
result.payload = decryptedPayload;
return result;
}
catch (e) {
if (console && console.log)
if (modules.config.logVerbosity && console && console.log)
console.log('decryption error', e.message);
return message;
result.payload = message;
result.error = "Error while decrypting message content: ".concat(e.message);
}
return result;
}
function getOperation() {
return operations_1.default.PNHistoryOperation;
Expand Down Expand Up @@ -76,13 +82,16 @@ function handleResponse(modules, serverResponse) {
};
if (Array.isArray(serverResponse[0])) {
serverResponse[0].forEach(function (serverHistoryItem) {
var processedMessgeResult = __processMessage(modules, serverHistoryItem.message);
var item = {
timetoken: serverHistoryItem.timetoken,
entry: __processMessage(modules, serverHistoryItem.message),
entry: processedMessgeResult.payload,
};
if (serverHistoryItem.meta) {
item.meta = serverHistoryItem.meta;
}
if (processedMessgeResult.error)
item.error = processedMessgeResult.error;
response.messages.push(item);
});
}
Expand Down
9 changes: 2 additions & 7 deletions src/core/components/subscription_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,9 +699,7 @@ export default class {
decryptedData instanceof ArrayBuffer ? JSON.parse(this._decoder.decode(decryptedData)) : decryptedData;
} catch (e) {
decryptedPayload = null;
if (console && console.log) {
console.log('decryption error', e.message);
}
announce.error = `Error while decrypting message content: ${e.message}`;
}
if (decryptedPayload !== null) {
msgPayload = decryptedPayload;
Expand Down Expand Up @@ -752,10 +750,7 @@ export default class {
decryptedData instanceof ArrayBuffer ? JSON.parse(this._decoder.decode(decryptedData)) : decryptedData;
} catch (e) {
decryptedPayload = null;
// eslint-disable-next-line
if (console && console.log) {
console.log('decryption error', e.message); //eslint-disable-line
}
announce.error = `Error while decrypting message content: ${e.message}`;
}
if (decryptedPayload != null) {
announce.message = decryptedPayload;
Expand Down
19 changes: 14 additions & 5 deletions src/core/endpoints/fetch_messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ import operationConstants from '../constants/operations';
import utils from '../utils';

function __processMessage(modules, message) {
if (!modules.cryptoModule) return message;
const result = {};
if (!modules.cryptoModule) {
result.payload = message;
return result;
}
try {
const decryptedData = modules.cryptoModule.decrypt(message);
const decryptedPayload =
decryptedData instanceof ArrayBuffer ? JSON.parse(new TextDecoder().decode(decryptedData)) : decryptedData;
return decryptedPayload;
result.payload = decryptedPayload;
return result;
} catch (e) {
if (console && console.log) console.log('decryption error', e.message);
return message;
if (modules.config.logVerbosity && console && console.log) console.log('decryption error', e.message);
result.payload = message;
result.error = `Error while decrypting message content: ${e.message}`;
}
return result;
}

export function getOperation() {
Expand Down Expand Up @@ -99,9 +106,10 @@ export function handleResponse(modules, serverResponse) {

(serverResponse.channels[channelName] || []).forEach((messageEnvelope) => {
const announce = {};
const processedMessgeResult = __processMessage(modules, messageEnvelope.message);
announce.channel = channelName;
announce.timetoken = messageEnvelope.timetoken;
announce.message = __processMessage(modules, messageEnvelope.message);
announce.message = processedMessgeResult.payload;
announce.messageType = messageEnvelope.message_type;
announce.uuid = messageEnvelope.uuid;

Expand All @@ -114,6 +122,7 @@ export function handleResponse(modules, serverResponse) {
if (messageEnvelope.meta) {
announce.meta = messageEnvelope.meta;
}
if (processedMessgeResult.error) announce.error = processedMessgeResult.error;

response.channels[channelName].push(announce);
});
Expand Down
19 changes: 14 additions & 5 deletions src/core/endpoints/history/get_history.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import operationConstants from '../../constants/operations';
import utils from '../../utils';

function __processMessage(modules, message) {
if (!modules.cryptoModule) return message;
const result = {};
if (!modules.cryptoModule) {
result.payload = message;
return result;
}
try {
const decryptedData = modules.cryptoModule.decrypt(message);
const decryptedPayload =
decryptedData instanceof ArrayBuffer ? JSON.parse(new TextDecoder().decode(decryptedData)) : decryptedData;
return decryptedPayload;
result.payload = decryptedPayload;
return result;
} catch (e) {
if (console && console.log) console.log('decryption error', e.message);
return message;
if (modules.config.logVerbosity && console && console.log) console.log('decryption error', e.message);
result.payload = message;
result.error = `Error while decrypting message content: ${e.message}`;
}
return result;
}

export function getOperation() {
Expand Down Expand Up @@ -68,14 +75,16 @@ export function handleResponse(modules, serverResponse) {

if (Array.isArray(serverResponse[0])) {
serverResponse[0].forEach((serverHistoryItem) => {
const processedMessgeResult = __processMessage(modules, serverHistoryItem.message);
const item = {
timetoken: serverHistoryItem.timetoken,
entry: __processMessage(modules, serverHistoryItem.message),
entry: processedMessgeResult.payload,
};

if (serverHistoryItem.meta) {
item.meta = serverHistoryItem.meta;
}
if (processedMessgeResult.error) item.error = processedMessgeResult.error;

response.messages.push(item);
});
Expand Down
Loading
Loading