Skip to content

Commit

Permalink
Deprecate atob, btoa, backbone, crypto-js and tv4 libs.
Browse files Browse the repository at this point in the history
  • Loading branch information
appurva21 committed Sep 24, 2024
1 parent 0dcb698 commit f7e8378
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ unreleased:
- GH-1032 Enhanced performance when operating on buffers in Node environment
- GH-1035 Added missing buffer APIs to expose a uniform interface across environments
- GH-1049 Replaced shims for atob and btoa with native implementations
- GH-1050 Deprecated `atob`, `btoa`, `backbone`, 'crypto-js' and `tv4` libraries

5.1.1:
date: 2024-08-01
Expand Down
23 changes: 21 additions & 2 deletions lib/sandbox/execute-context.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
const { isNonLegacySandbox } = require('./non-legacy-codemarkers');
const _ = require('lodash'),
legacy = require('./postman-legacy-interface');
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
};

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 @@ -42,7 +50,18 @@ 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],
alt = DEPRECATED_LIBS[key];

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) {
Expand Down
57 changes: 28 additions & 29 deletions lib/sandbox/postman-legacy-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,33 @@ const _ = require('lodash'),
],

LEGACY_GLOBS_ALTERNATIVES = {
tests: 'pm.test()',
globals: 'pm.globals',
environment: 'pm.environment',
data: 'pm.iterationData',
request: 'pm.request',
responseCookies: 'pm.cookies',
responseHeaders: 'pm.response.headers',
responseTime: 'pm.response.responseTime',
responseCode: 'pm.response.code',
responseBody: 'pm.response.text()',
iteration: 'pm.info.iteration',
_: 'require(\'lodash\')',
CryptoJS: 'require(\'crypto-js\')',
tv4: 'require(\'ajv\')',
xml2Json: 'require(\'xml2js\')',
Backbone: 'require(\'backbone\')',
cheerio: 'require(\'cheerio\')',
'postman.setNextRequest': 'pm.execution.setNextRequest()',
'postman.setEnvironmentVariable': 'pm.environment.set()',
'postman.getEnvironmentVariable': 'pm.environment.get()',
'postman.clearEnvironmentVariables': 'pm.environment.clear()',
'postman.clearEnvironmentVariable': 'pm.environment.unset()',
'postman.setGlobalVariable': 'pm.globals.set()',
'postman.getGlobalVariable': 'pm.globals.get()',
'postman.clearGlobalVariables': 'pm.globals.clear()',
'postman.clearGlobalVariable': 'pm.globals.unset()',
'postman.getResponseCookie': 'pm.cookies.get()',
'postman.getResponseHeader': 'pm.response.headers.get()'
tests: '"pm.test()"',
globals: '"pm.globals"',
environment: '"pm.environment"',
data: '"pm.iterationData"',
request: '"pm.request"',
responseCookies: '"pm.cookies"',
responseHeaders: '"pm.response.headers"',
responseTime: '"pm.response.responseTime"',
responseCode: '"pm.response.code"',
responseBody: '"pm.response.text()"',
iteration: '"pm.info.iteration"',
_: '"require(\'lodash\')"',
CryptoJS: 'global "crypto" object',
tv4: '"require(\'ajv\')"',
xml2Json: '"require(\'xml2js\')"',
cheerio: '"require(\'cheerio\')"',
'postman.setNextRequest': '"pm.execution.setNextRequest()"',
'postman.setEnvironmentVariable': '"pm.environment.set()"',
'postman.getEnvironmentVariable': '"pm.environment.get()"',
'postman.clearEnvironmentVariables': '"pm.environment.clear()"',
'postman.clearEnvironmentVariable': '"pm.environment.unset()"',
'postman.setGlobalVariable': '"pm.globals.set()"',
'postman.getGlobalVariable': '"pm.globals.get()"',
'postman.clearGlobalVariables': '"pm.globals.clear()"',
'postman.clearGlobalVariable': '"pm.globals.unset()"',
'postman.getResponseCookie': '"pm.cookies.get()"',
'postman.getResponseHeader': '"pm.response.headers.get()"'
},

E = '',
Expand Down Expand Up @@ -190,7 +189,7 @@ function logDeprecationWarning (key, legacyUsageSet, console) {
legacyUsageSet.add(key);

if (LEGACY_GLOBS_ALTERNATIVES[key]) {
console.warn(`Using "${key}" is deprecated. Use "${LEGACY_GLOBS_ALTERNATIVES[key]}" instead.`);
console.warn(`Using "${key}" is deprecated. Use ${LEGACY_GLOBS_ALTERNATIVES[key]} instead.`);
}
else if (LEGACY_GLOBS.includes(key)) {
console.warn(`Using "${key}" is deprecated.`);
Expand Down
8 changes: 4 additions & 4 deletions npm/utils/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ declare var iteration;
declare var _;
/**
* @deprecated Use require('crypto-js') instead
* @deprecated Use global "crypto" object instead
*/
declare var CryptoJS;
/**
* @deprecated Use require('atob') instead
* @deprecated Use global "atob" function instead
*/
declare var atob;
/**
* @deprecated Use require('btoa') instead
* @deprecated Use global "btoa" function instead
*/
declare var btoa;
Expand All @@ -118,7 +118,7 @@ declare var tv4;
declare var xml2Json;
/**
* @deprecated Use require('backbone') instead
* @deprecated
*/
declare var Backbone;
Expand Down
164 changes: 164 additions & 0 deletions test/unit/sandbox-libraries/deprecation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
const env = require('../../../lib/environment'),
DEPRECATED_LIBS = ['atob', 'btoa', 'crypto-js', 'tv4', 'backbone'];

describe('sandbox library - deprecation', function () {
this.timeout(1000 * 60);
var Sandbox = require('../../../'),
context;

beforeEach(function (done) {
Sandbox.createContext({}, function (err, ctx) {
context = ctx;
done(err);
});
});

afterEach(function () {
context.dispose();
context = null;
});

it('should show deprecation warning for "tv4"', function (done) {
const consoleSpy = sinon.spy();

context.on('console', consoleSpy);
context.execute(`
const requiredTv4 = require('tv4');
tv4.validate;
`, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.be.calledTwice;

expect(consoleSpy.firstCall.args[1]).to.equal('warn');
expect(consoleSpy.firstCall.args[2])
.to.equal('Using "tv4" library is deprecated. Use "ajv" library instead.');

expect(consoleSpy.secondCall.args[1]).to.equal('warn');
expect(consoleSpy.secondCall.args[2])
.to.equal('Using "tv4" is deprecated. Use "require(\'ajv\')" instead.');

done();
});
});

it('should show deprecation warning for "crypto-js"', function (done) {
const consoleSpy = sinon.spy();

context.on('console', consoleSpy);
context.execute(`
const requiredCryptoJS = require('crypto-js');
CryptoJS.lib;
`, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.be.calledTwice;

expect(consoleSpy.firstCall.args[1]).to.equal('warn');
expect(consoleSpy.firstCall.args[2])
.to.equal('Using "crypto-js" library is deprecated. Use global "crypto" object instead.');

expect(consoleSpy.secondCall.args[1]).to.equal('warn');
expect(consoleSpy.secondCall.args[2])
.to.equal('Using "CryptoJS" is deprecated. Use global "crypto" object instead.');

done();
});
});


it('should show deprecation warning for "backbone', function (done) {
const consoleSpy = sinon.spy();

context.on('console', consoleSpy);
context.execute(`
const requiredBackbone = require('backbone');
Backbone.Backbone;
`, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.be.calledTwice;

expect(consoleSpy.firstCall.args[1]).to.equal('warn');
expect(consoleSpy.firstCall.args[2])
.to.equal('Using "backbone" library is deprecated.');

expect(consoleSpy.secondCall.args[1]).to.equal('warn');
expect(consoleSpy.secondCall.args[2])
.to.equal('Using "Backbone" is deprecated.');

done();
});
});

it('should show deprecation warning for "atob', function (done) {
const consoleSpy = sinon.spy();

context.on('console', consoleSpy);
context.execute(`
const requiredAtob = require('atob');
atob('YQ==');
`, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.be.calledOnce;

expect(consoleSpy.firstCall.args[1]).to.equal('warn');
expect(consoleSpy.firstCall.args[2])
.to.equal('Using "atob" library is deprecated. Use global "atob" function instead.');

done();
});
});

it('should show deprecation warning for "btoa', function (done) {
const consoleSpy = sinon.spy();

context.on('console', consoleSpy);
context.execute(`
const requiredBtoa = require('btoa');
btoa('a');
`, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.be.calledOnce;

expect(consoleSpy.firstCall.args[1]).to.equal('warn');
expect(consoleSpy.firstCall.args[2])
.to.equal('Using "btoa" library is deprecated. Use global "btoa" function instead.');

done();
});
});

it('should not show warning for non-deprecated libraries', function (done) {
const consoleSpy = sinon.spy(),
libs = Object.entries(env.require).map(([key, value]) => {
return value.expose ?? key;
}),
code = libs.map((lib) => {
return !DEPRECATED_LIBS.includes(lib) ? `require('${lib}');` : '';
}).join('\n');


context.on('console', consoleSpy);
context.execute(code, function (err) {
if (err) {
return done(err);
}

expect(consoleSpy).to.not.be.called;
done();
});
});
});
4 changes: 2 additions & 2 deletions test/unit/sandbox-libraries/legacy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('sandbox library - legacy', function () {

context.on('console', consoleSpy);
context.execute(`
CryptoJS.AES.encrypt('my message', 'secret key 123')
cheerio.load('foo');
`, function (err) {
if (err) {
return done(err);
Expand All @@ -64,7 +64,7 @@ describe('sandbox library - legacy', function () {
expect(consoleSpy).to.be.calledOnce;
expect(consoleSpy.firstCall.args[1]).to.equal('warn');
expect(consoleSpy.firstCall.args[2])
.to.equal('Using "CryptoJS" is deprecated. Use "require(\'crypto-js\')" instead.');
.to.equal('Using "cheerio" is deprecated. Use "require(\'cheerio\')" instead.');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for postman-sandbox 5.0.0
// Type definitions for postman-sandbox 5.1.1
// Project: https://github.com/postmanlabs/postman-sandbox
// Definitions by: PostmanLabs
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
Expand Down
10 changes: 5 additions & 5 deletions types/sandbox/prerequest.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for postman-sandbox 5.0.0
// Type definitions for postman-sandbox 5.1.1
// Project: https://github.com/postmanlabs/postman-sandbox
// Definitions by: PostmanLabs
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
Expand Down Expand Up @@ -55,17 +55,17 @@ declare var iteration;
declare var _;

/**
* @deprecated Use require('crypto-js') instead
* @deprecated Use global "crypto" object instead
*/
declare var CryptoJS;

/**
* @deprecated Use require('atob') instead
* @deprecated Use global "atob" function instead
*/
declare var atob;

/**
* @deprecated Use require('btoa') instead
* @deprecated Use global "btoa" function instead
*/
declare var btoa;

Expand All @@ -80,7 +80,7 @@ declare var tv4;
declare var xml2Json;

/**
* @deprecated Use require('backbone') instead
* @deprecated
*/
declare var Backbone;

Expand Down
Loading

0 comments on commit f7e8378

Please sign in to comment.