Skip to content

Commit

Permalink
THRIFT-5769: fix invalid size error on large messages
Browse files Browse the repository at this point in the history
Client: nodejs
  • Loading branch information
tuomojokimies-sc authored and Jens-G committed Mar 18, 2024
1 parent 0e72363 commit b60b8fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/nodejs/lib/thrift/framed_transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,27 @@ function TFramedTransport(buffer, callback) {
Object.setPrototypeOf(TFramedTransport.prototype, THeaderTransport.prototype);

TFramedTransport.receiver = function(callback, seqid) {
var residual = [];
var residual = new Buffer(0);

return function(data) {
// push received data to residual
for(var i = 0; i < data.length; ++i) {
residual.push(data[i])
}
residual = Buffer.concat([residual, Buffer.from(data)]);

while (residual.length > 0) {
if (residual.length < 4) {
// Not enough bytes to continue, save and resume on next packet
return;
}
// get single package sieze
var frameSize = binary.readI32(Buffer.from(residual.slice(0, 4)), 0);
// Get single package size
var frameSize = binary.readI32(residual, 0);
// Not enough bytes to continue, save and resume on next packet
if (residual.length < 4 + frameSize) {
return;
}

// splice first 4 bytes
residual.splice(0, 4)
// get package data
var frame = Buffer.from(residual.splice(0, frameSize));
// Get package data
var frame = residual.subarray(4, 4 + frameSize);
// Remove processed data from residual
residual = residual.subarray(4 + frameSize);
callback(new TFramedTransport(frame), seqid);
}
};
Expand Down
12 changes: 12 additions & 0 deletions lib/nodejs/test/header.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ const cases = {
assert.equals(headers.otherfoo, undefined);
assert.equals(otherHeaders.foo, undefined);
assert.equals(otherHeaders.otherfoo, "baz");
assert.end();
},
"Should handle large messages without crashing": function(assert) {
const callback = function() {};
const onData = TFramedTransport.receiver(callback);

const largeChunkSize = 2 * 100 * 1024 * 1024;
const largeChunk = Buffer.alloc(largeChunkSize, "A");
const sizeBuffer = new Buffer(4);
sizeBuffer.writeInt32BE(largeChunkSize + 4, 0);
onData(Buffer.concat([sizeBuffer, largeChunk]));

assert.end();
}
};
Expand Down
1 change: 1 addition & 0 deletions lib/nodejs/test/testAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ fi
# unit tests

node ${DIR}/binary.test.js || TESTOK=1
node ${DIR}/header.test.js || TESTOK=1
node ${DIR}/int64.test.js || TESTOK=1
node ${DIR}/deep-constructor.test.js || TESTOK=1

Expand Down

0 comments on commit b60b8fe

Please sign in to comment.