From 18748412bc7f752dfd8c0c67ce8b1b7a3d44075e Mon Sep 17 00:00:00 2001 From: Appurva Murawat Date: Tue, 24 Sep 2024 15:45:39 +0530 Subject: [PATCH] Truncate large response body in legacy sandbox (#1034) --- CHANGELOG.yaml | 4 +++ lib/sandbox/postman-legacy-interface.js | 14 ++++++-- test/unit/sandbox-libraries/legacy.test.js | 42 ++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 59bf98d8..2263e97c 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -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: diff --git a/lib/sandbox/postman-legacy-interface.js b/lib/sandbox/postman-legacy-interface.js index f709a6ba..cf8f3d64 100644 --- a/lib/sandbox/postman-legacy-interface.js +++ b/lib/sandbox/postman-legacy-interface.js @@ -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'), @@ -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 diff --git a/test/unit/sandbox-libraries/legacy.test.js b/test/unit/sandbox-libraries/legacy.test.js index 6482fc2b..3b3c5b3f 100644 --- a/test/unit/sandbox-libraries/legacy.test.js +++ b/test/unit/sandbox-libraries/legacy.test.js @@ -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); + }); });