Skip to content

Commit

Permalink
Fix crash streaming process when receive invalid json (mastodon#14859)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfmfuyu authored and thenameisnigel-old committed Sep 28, 2020
1 parent 03cbc59 commit 7109d93
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions streaming/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ const redisUrlToClient = (defaultConfig, redisUrl) => {

const numWorkers = +process.env.STREAMING_CLUSTER_NUM || (env === 'development' ? 1 : Math.max(os.cpus().length - 1, 1));

/**
* @param {string} json
* @return {Object.<string, any>|null}
*/
const parseJSON = (json) => {
try {
return JSON.parse(json);
} catch (err) {
log.error(err);
return null;
}
};

const startMaster = () => {
if (!process.env.SOCKET && process.env.PORT && isNaN(+process.env.PORT)) {
log.warn('UNIX domain socket is now supported by using SOCKET. Please migrate from PORT hack.');
Expand Down Expand Up @@ -522,7 +535,9 @@ const startWorker = (workerId) => {
log.verbose(req.requestId, `Starting stream from ${ids.join(', ')} for ${accountId}${streamType}`);

const listener = message => {
const { event, payload, queued_at } = JSON.parse(message);
const json = parseJSON(message);
if (!json) return;
const { event, payload, queued_at } = json;

const transmit = () => {
const now = new Date().getTime();
Expand Down Expand Up @@ -932,7 +947,9 @@ const startWorker = (workerId) => {
ws.on('error', onEnd);

ws.on('message', data => {
const { type, stream, ...params } = JSON.parse(data);
const json = parseJSON(data);
if (!json) return;
const { type, stream, ...params } = json;

if (type === 'subscribe') {
subscribeWebsocketToChannel(session, firstParam(stream), params);
Expand Down

0 comments on commit 7109d93

Please sign in to comment.