Skip to content

Allow require to be shared across executions #1086

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

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
45 changes: 26 additions & 19 deletions lib/sandbox/execute-context.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
const { isNonLegacySandbox } = require('./non-legacy-codemarkers');
const _ = require('lodash'),
legacy = require('./postman-legacy-interface'),

DEPRECATED_LIBS = {
atob: 'global "atob" function',
btoa: 'global "btoa" function',
'crypto-js': 'global "crypto" object',
tv4: '"ajv" library',
backbone: null
};
legacy = require('./postman-legacy-interface');

module.exports = function (scope, code, execution, console, timers, pmapi, onAssertion, options) {
// if there is no code, then no point bubbling anything up
Expand Down Expand Up @@ -52,21 +44,36 @@ module.exports = function (scope, code, execution, console, timers, pmapi, onAss
setImmediate: timers.setImmediate,
clearTimeout: timers.clearTimeout,
clearInterval: timers.clearInterval,
clearImmediate: timers.clearImmediate,
clearImmediate: timers.clearImmediate
});

require: (...args) => {
const key = args?.[0],
const wrappedCode = `
;((originalRequire) => {
Copy link
Member Author

@appurva21 appurva21 Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This right now will keep on wrapping require with each execution, but if the general direction seems satisfactory, we can move it inside the initialize event and only do it once.

require = (...args) => {
const key = args?.[0],
DEPRECATED_LIBS = {
atob: 'global "atob" function',
btoa: 'global "btoa" function',
'crypto-js': 'global "crypto" object',
tv4: '"ajv" library',
backbone: null
},
alt = DEPRECATED_LIBS[key];

if (alt !== undefined) {
console.warn(`Using "${key}" library is deprecated.${alt !== null ? ` Use ${alt} instead.` : ''}`);
}
if (alt !== undefined) {
console.warn(
\`Using "\${key}" library is deprecated.\${alt !== null ? \` Use \${alt} instead.\` : ''}\`
);
}

return require(...args);
}
});

scope.exec(code, { async: true }, function (err) {
return originalRequire(...args);
}
})(require);
${code}
`;

scope.exec(wrappedCode, { async: true }, function (err) {
// we check if the execution went async by determining the timer queue length at this time
execution.return.async = (timers.queueLength() > 0);

Expand Down
35 changes: 35 additions & 0 deletions test/unit/sandbox-sanity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,41 @@ describe('sandbox', function () {
});
});

it('should persist overridden `require` across executions', function (done) {
const consoleSpy = sinon.spy();

Sandbox.createContext(function (err, ctx) {
if (err) { return done(err); }

ctx.on('error', done);
ctx.on('console', consoleSpy);

ctx.execute(`
require = (...args) => {
if (args[0] === 'utils') {
return () => console.log('utils');
}

return require(...args);
}

require('utils')();
`, function (err) {
if (err) { return done(err); }

ctx.execute('require(\'utils\')();', function (err) {
if (err) { return done(err); }

expect(consoleSpy).to.be.calledTwice;
expect(consoleSpy.firstCall.args[2]).to.equal('utils');
expect(consoleSpy.secondCall.args[2]).to.equal('utils');

done();
});
});
});
});

(IS_NODE ? it : it.skip)('should have missing globals as subset of explicitly ignored globals', function (done) {
Sandbox.createContext(function (err, ctx) {
if (err) { return done(err); }
Expand Down
Loading