Skip to content

Commit

Permalink
Truncate large response body in legacy sandbox (#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 authored Sep 24, 2024
1 parent 91bbb44 commit 1874841
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
unreleased:
fixed bugs:
- GH-1034 Fixed an issue where sandbox crashes for large response body

5.1.2:
date: 2024-09-04
fixed bugs:
Expand Down
14 changes: 12 additions & 2 deletions lib/sandbox/postman-legacy-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ const _ = require('lodash'),
URLENCODED: 'urlencoded',
FORMDATA: 'formdata',
FILE: 'file'
};
},

MAX_RESPONSE_SIZE = 50 * 1024 * 1024; // 50MB

function getRequestBody (request) {
var mode = _.get(request, 'body.mode'),
Expand Down Expand Up @@ -416,7 +418,15 @@ module.exports = {
* @memberOf SandboxGlobals
* @type {String}
*/
globalvars.responseBody = execution.response ? execution.response.text() : undefined;
globalvars.responseBody = (() => {
// Truncating response body if it is too large to avoid negatively affecting
// the performance since this get calculated for every execution by default
if (!execution.response || execution.response.responseSize > MAX_RESPONSE_SIZE) {
return;
}

return execution.response.text();
})();
}

// 5. add the iteration information
Expand Down
42 changes: 42 additions & 0 deletions test/unit/sandbox-libraries/legacy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,46 @@ describe('sandbox library - legacy', function () {
done();
});
});

it('should support "responseBody" with size upto 50MB', function (done) {
context.execute({
listen: 'test',
script: `
const assert = require('assert');
assert.strictEqual(
responseBody,
Buffer.alloc(50 * 1024 * 1024, 'a').toString(),
'responseBody <= 50MB should be available'
);
`
}, {
context: {
response: {
stream: {
type: 'Base64',
data: Buffer.alloc(50 * 1024 * 1024, 'a').toString('base64')
}
}
}
}, done);
});

it('should truncate "responseBody" with size > 50MB', function (done) {
context.execute({
listen: 'test',
script: `
const assert = require('assert');
assert.strictEqual(typeof responseBody, 'undefined', 'responseBody > 50MB should not be available');
`
}, {
context: {
response: {
stream: {
type: 'Base64',
data: Buffer.alloc(51 * 1024 * 1024, 'a').toString('base64')
}
}
}
}, done);
});
});

0 comments on commit 1874841

Please sign in to comment.