Skip to content

Commit 1fac391

Browse files
fix(@embark/blockchain_process): ignore socket disconnect bytes
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`.
1 parent 647f0ea commit 1fac391

File tree

1 file changed

+29
-11
lines changed
  • src/lib/modules/blockchain_process

1 file changed

+29
-11
lines changed

src/lib/modules/blockchain_process/proxy.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global __ exports require */
1+
/* global Buffer __ exports require */
22

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

16+
const hex = (n) => {
17+
let _n = n.toString(16);
18+
return _n.length === 1 ? '0' + _n : _n;
19+
};
20+
1621
const parseJsonMaybe = (string) => {
1722
let object;
18-
try {
19-
if (string && typeof string === 'string') object = JSON.parse(string);
20-
} catch (e) {
21-
console.error('Error parsing string as JSON', string);
22-
} finally {
23-
// eslint-disable-next-line no-unsafe-finally
24-
return object;
23+
if (typeof string === 'string') {
24+
if (string) {
25+
try {
26+
object = JSON.parse(string);
27+
} catch(e) {
28+
if (Array.from(Buffer.from(string)).map(hex).join(':') !==
29+
'03:ef:bf:bd') {
30+
console.error(`Proxy: Error parsing string as JSON '${string}'`);
31+
}
32+
}
33+
} else {
34+
console.error('Proxy: Expected a non-empty string');
35+
}
36+
} else {
37+
console.error(`Proxy: Expected a string but got type '${typeof string}'`);
2538
}
39+
return object;
2640
};
2741

2842
exports.serve = async (ipc, host, port, ws, origin) => {
@@ -46,7 +60,9 @@ exports.serve = async (ipc, host, port, ws, origin) => {
4660
}
4761
}
4862
} catch (e) {
49-
console.error('Error tracking request message', JSON.stringify(req));
63+
console.error(
64+
`Proxy: Error tracking request message '${JSON.stringify(req)}'`,
65+
);
5066
}
5167
};
5268

@@ -80,7 +96,9 @@ exports.serve = async (ipc, host, port, ws, origin) => {
8096
delete receipts[res.id];
8197
}
8298
} catch (e) {
83-
console.error('Error tracking response message', JSON.stringify(res));
99+
console.error(
100+
`Proxy: Error tracking response message '${JSON.stringify(res)}'`
101+
);
84102
}
85103
};
86104

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

112130
onError(err, _req, _res) {
113131
console.error(
114-
__('Error forwarding requests to blockchain/simulator'),
132+
__('Proxy: Error forwarding requests to blockchain/simulator'),
115133
err.message
116134
);
117135
},

0 commit comments

Comments
 (0)