Skip to content

Commit 04ef1d3

Browse files
authored
Merge pull request #907 from postmanlabs/feature/fix-prepareStackTrace
Defined custom Error.prepareStackTrace
2 parents 2759dd3 + 65b5afb commit 04ef1d3

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

CHANGELOG.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
unreleased:
2+
fixed bugs:
3+
- GH-907 Defined `Error.prepareStackTrace` to prevent stack trace pollution
4+
15
4.2.4:
26
date: 2023-03-10
37
fixed bugs:

lib/sandbox/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@
5454
// @note this deletes the constructor as well to make sure one can't recreate the same scope
5555
contextObject = Object.getPrototypeOf(contextObject);
5656
} while (contextObject && contextObject.constructor !== Object);
57+
58+
// define custom Error.prepareStackTrace
59+
Object.defineProperty(Error, 'prepareStackTrace', {
60+
value: function (error, structuredStackTrace) {
61+
const errorString = String(error);
62+
63+
if (Array.isArray(structuredStackTrace) && structuredStackTrace.length) {
64+
return `${errorString}\n at ${structuredStackTrace.join('\n at ')}`;
65+
}
66+
67+
return errorString;
68+
},
69+
configurable: false,
70+
enumerable: false,
71+
writable: false
72+
});
5773
}());
5874

5975
// do include json purse

test/unit/sandbox-sanity.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ describe('sandbox', function () {
5757
});
5858
});
5959

60+
it('should not be able to mutate Error.prepareStackTrace', function (done) {
61+
Sandbox.createContext(function (err, ctx) {
62+
if (err) { return done(err); }
63+
ctx.on('error', done);
64+
65+
ctx.execute(`
66+
var assert = require('assert');
67+
var fn = Error.prepareStackTrace;
68+
69+
Error.prepareStackTrace = () => {};
70+
assert.equal(Error.prepareStackTrace, fn);
71+
72+
var err = new Error('Test');
73+
assert.equal(err.stack.split('\\n')[0], 'Error: Test');
74+
`, done);
75+
});
76+
});
77+
6078
it('should not have access to global properties', function (done) {
6179
Sandbox.createContext({ debug: true }, function (err, ctx) {
6280
if (err) { return done(err); }

0 commit comments

Comments
 (0)