Skip to content

Commit 05ff370

Browse files
authored
Restrict access to pm API in external packages (#1087)
1 parent 8917e62 commit 05ff370

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGELOG.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
unreleased:
2+
fixed bugs:
3+
- GH-1087 Restrict access to `pm` API in external packages
4+
- GH-1086 Revert deprecation warnings for legacy packages
5+
16
6.1.0:
27
date: 2025-03-27
38
new features:

lib/sandbox/pm-require.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ const { LEGACY_GLOBS } = require('./postman-legacy-interface'),
44
MODULE_WRAPPER = [
55
'((exports, module) => {\n',
66
`\n})(${MODULE_KEY}.exports, ${MODULE_KEY});`
7-
];
7+
],
8+
PACKAGE_TYPE_REGEX = /^[^:]+:[^:]+$/,
9+
10+
// This is used to determine if the package is external or internal.
11+
// External packages are those that are not part of the Postman's
12+
// Package Library and follow the format `registry:package`.
13+
isExternalPackage = (name) => { return PACKAGE_TYPE_REGEX.test(name); };
814

915
/**
1016
* Cache of all files that are available to be required.
@@ -160,7 +166,12 @@ function createPostmanRequire (fileCache, scope) {
160166
//
161167
// Why `async` = true?
162168
// - We want to allow execution of async code like setTimeout etc.
163-
scope.exec(wrappedModule, { async: true, block: LEGACY_GLOBS }, (err) => {
169+
scope.exec(wrappedModule, {
170+
async: true,
171+
block: isExternalPackage(name) ?
172+
LEGACY_GLOBS.concat('pm') :
173+
LEGACY_GLOBS
174+
}, (err) => {
164175
// Bubble up the error to be caught as execution error
165176
if (err) {
166177
throw new Error(`Error in package '${name}': ${err.message ? err.message : err}`);

test/unit/sandbox-libraries/pm-require.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,4 +595,19 @@ describe('sandbox library - pm.require api', function () {
595595
}
596596
}, done);
597597
});
598+
599+
it('should not have access to pm object for external packages', function (done) {
600+
context.execute('const pkg = pm.require(\'npm:pkg1\');', {
601+
context: sampleContextData,
602+
resolvedPackages: {
603+
'npm:pkg1': { data: `
604+
const assert = require('assert');
605+
assert.strictEqual(pm, undefined);
606+
` }
607+
}
608+
}, function (err) {
609+
if (err) { return done(err); }
610+
done();
611+
});
612+
});
598613
});

0 commit comments

Comments
 (0)