Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate large response body in legacy sandbox #1034

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading