Skip to content

Commit 363a988

Browse files
committed
Restrict access to pm API in external packages
1 parent a36bff5 commit 363a988

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-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: 14 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+
getPackageType = (name) => { return PACKAGE_TYPE_REGEX.test(name) ? 'external' : 'internal'; };
814

915
/**
1016
* Cache of all files that are available to be required.
@@ -135,6 +141,7 @@ function createPostmanRequire (fileCache, scope) {
135141

136142
/* eslint-disable-next-line one-var */
137143
const file = store.getFileData(path),
144+
isExternalPackage = getPackageType(name) === 'external',
138145
moduleObj = {
139146
id: path,
140147
exports: {}
@@ -160,7 +167,12 @@ function createPostmanRequire (fileCache, scope) {
160167
//
161168
// Why `async` = true?
162169
// - We want to allow execution of async code like setTimeout etc.
163-
scope.exec(wrappedModule, { async: true, block: LEGACY_GLOBS }, (err) => {
170+
scope.exec(wrappedModule, {
171+
async: true,
172+
block: isExternalPackage ?
173+
['pm', ...LEGACY_GLOBS] :
174+
LEGACY_GLOBS
175+
}, (err) => {
164176
// Bubble up the error to be caught as execution error
165177
if (err) {
166178
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)