Skip to content

Commit

Permalink
fix(@embark/blockchain_process): ignore socket disconnect bytes
Browse files Browse the repository at this point in the history
Ignore the sequence of bytes `03:ef:bf:bd` that are sent between
Chrome/Firefox (others?) and the node process when a browser connected via
websocket to the blockchain proxy is closed/reloaded. The theory is that
sequence is part of a socket control frame that is leaking to `parseJsonMaybe`
from `http-proxy-middleware`.
  • Loading branch information
michaelsbradleyjr committed Dec 3, 2018
1 parent 647f0ea commit 1fac391
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/lib/modules/blockchain_process/proxy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global __ exports require */
/* global Buffer __ exports require */

const Asm = require('stream-json/Assembler');
const {canonicalHost, defaultHost} = require('../../utils/host');
Expand All @@ -13,16 +13,30 @@ const utils = require('../../utils/utils');
const WebSocket = require('ws');
const WsParser = require('simples/lib/parsers/ws');

const hex = (n) => {
let _n = n.toString(16);
return _n.length === 1 ? '0' + _n : _n;
};

const parseJsonMaybe = (string) => {
let object;
try {
if (string && typeof string === 'string') object = JSON.parse(string);
} catch (e) {
console.error('Error parsing string as JSON', string);
} finally {
// eslint-disable-next-line no-unsafe-finally
return object;
if (typeof string === 'string') {
if (string) {
try {
object = JSON.parse(string);
} catch(e) {
if (Array.from(Buffer.from(string)).map(hex).join(':') !==
'03:ef:bf:bd') {
console.error(`Proxy: Error parsing string as JSON '${string}'`);
}
}
} else {
console.error('Proxy: Expected a non-empty string');
}
} else {
console.error(`Proxy: Expected a string but got type '${typeof string}'`);
}
return object;
};

exports.serve = async (ipc, host, port, ws, origin) => {
Expand All @@ -46,7 +60,9 @@ exports.serve = async (ipc, host, port, ws, origin) => {
}
}
} catch (e) {
console.error('Error tracking request message', JSON.stringify(req));
console.error(
`Proxy: Error tracking request message '${JSON.stringify(req)}'`,
);
}
};

Expand Down Expand Up @@ -80,7 +96,9 @@ exports.serve = async (ipc, host, port, ws, origin) => {
delete receipts[res.id];
}
} catch (e) {
console.error('Error tracking response message', JSON.stringify(res));
console.error(
`Proxy: Error tracking response message '${JSON.stringify(res)}'`
);
}
};

Expand Down Expand Up @@ -111,7 +129,7 @@ exports.serve = async (ipc, host, port, ws, origin) => {

onError(err, _req, _res) {
console.error(
__('Error forwarding requests to blockchain/simulator'),
__('Proxy: Error forwarding requests to blockchain/simulator'),
err.message
);
},
Expand Down

0 comments on commit 1fac391

Please sign in to comment.