Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit 8219344

Browse files
Merge pull request #9 from Unity-Technologies/bugfix/protocol-version-size-check
Protocol version size check
2 parents b86d679 + 7903aba commit 8219344

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

lib/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const constants = {
22
VERSION: "5.4.0",
33
PROTOCOL_VERSION: 254,
4+
PROTOCOL_VERSION_MIN_SIZE: 2,
45
UINT32_SIZE: 8, // hex
56
UINT64_SIZE: 16, // hex
67
HASH_SIZE: 16, // bin

lib/server.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class CacheServer {
7373
// Get the version as the first thing
7474
var idx = 0;
7575
if (!socket.protocolVersion) {
76+
if (data.length < consts.PROTOCOL_VERSION_MIN_SIZE)
77+
{
78+
// We need more data
79+
socket.pendingData = data;
80+
return false;
81+
}
82+
7683
socket.protocolVersion = helpers.readUInt32(data);
7784
let buf = Buffer.allocUnsafe(consts.UINT32_SIZE);
7885
if (socket.protocolVersion == consts.PROTOCOL_VERSION) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"scripts": {
1313
"test": "mocha test",
14+
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha; open coverage/lcov-report/index.html",
1415
"start": "node main.js",
1516
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
1617
},

test/server.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ describe("CacheServer protocol", function() {
122122

123123
client.write(helpers.encodeInt32(consts.PROTOCOL_VERSION + 1));
124124
});
125+
126+
it("should recognize a 2 byte version sent 1 byte at a time", function (done) {
127+
this.slow(250);
128+
129+
client.on('data', function(data) {
130+
var ver = helpers.readUInt32(data);
131+
assert(ver == consts.PROTOCOL_VERSION, "Expected " + consts.PROTOCOL_VERSION + " Received " + ver);
132+
done();
133+
});
134+
135+
var ver = "fe";
136+
client.write(ver[0]);
137+
sleep(50).then(() => { client.write(ver[1]); });
138+
});
125139
});
126140

127141
describe("Transactions", function () {

0 commit comments

Comments
 (0)