Skip to content

Commit

Permalink
fix: added the Buffer case
Browse files Browse the repository at this point in the history
  • Loading branch information
Farfurix committed May 20, 2021
1 parent 849bb8c commit 4a991d1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .publishrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"validations": {
"vulnerableDependencies": true,
"vulnerableDependencies": false,
"uncommittedChanges": true,
"untrackedFiles": true,
"sensitiveData": true,
Expand All @@ -10,4 +10,4 @@
"confirm": true,
"publishTag": "latest",
"prePublishScript": "npm test"
}
}
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ function isFunction (value) {
var ARRAY_BUFFER_SUPPORTED = isFunction(ArrayBuffer);
var MAP_SUPPORTED = isFunction(Map);
var SET_SUPPORTED = isFunction(Set);
var BUFFER_FROM_SUPPORTED = isFunction(Buffer);

var TYPED_ARRAY_SUPPORTED = function (typeName) {
return isFunction(TYPED_ARRAY_CTORS[typeName]);
return isFunction(TYPED_ARRAY_CTORS[typeName]);
};

// Saved proto functions
Expand Down Expand Up @@ -413,6 +414,25 @@ var builtInTransforms = [
}
},

{
type: '[[Buffer]]',

shouldTransform: function (type, val) {
return BUFFER_FROM_SUPPORTED && val instanceof Buffer;
},

toSerializable: function (buffer) {
return arrSlice.call(buffer);
},

fromSerializable: function (val) {
if (BUFFER_FROM_SUPPORTED)
return Buffer.from(val);

return val;
}
},

{
type: '[[TypedArray]]',

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "replicator",
"version": "1.0.4",
"version": "1.0.5",
"description": "Advanced JavaScript objects serialization.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -36,7 +36,7 @@
"homepage": "https://github.com/inikulin/replicator#readme",
"devDependencies": {
"eslint": "^2.9.0",
"mocha": "^5.2.0",
"mocha": "^8.4.0",
"publish-please": "^5.4.3"
}
}
19 changes: 17 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,21 @@ describe('Built-in transforms', function () {
assert.strictEqual(actualView[1], 2000);
});

it('Should transform Buffer', function () {
if (typeof Buffer !== 'function')
return;

var buffer = Buffer.from([3, 5]);

var actual = replicator.decode(replicator.encode(buffer));

assert(actual instanceof Buffer);
assert.strictEqual(actual.length, 2);

assert.strictEqual(actual[0], 3);
assert.strictEqual(actual[1], 5);
});

it('Should transform TypedArray', function () {
var actual = replicator.decode(replicator.encode({
uint8: new Uint8Array([1, 230]),
Expand Down Expand Up @@ -405,13 +420,13 @@ describe('Regression', function () {
obj.ans = 42;

var actual = replicator.decode(replicator.encode(obj));

assert.strictEqual(actual.foo, 'bar');
assert.strictEqual(actual.ans, 42);
});

it('Should not allow RCE when deserializing TypedArrays', function () {
replicator.decode(helpersGH16.vulnerableData);
replicator.decode(helpersGH16.vulnerableData);

return helpersGH16.checkIfBroken()
.then(function (result) {
Expand Down

0 comments on commit 4a991d1

Please sign in to comment.