refactor(ses): Colocate logic of exposing FERAL_EVAL inside eval-scope module
MODIFY
packages/ses/src/eval-scope.js
packages/ses/src/eval-scope.js
-import { FERAL_EVAL, create, freeze } from './commons.js';
+import { FERAL_EVAL, create, freeze, defineProperties } from './commons.js';
export const createEvalScope = () => {
const evalScope = create(null);
@@ -15,6 +15,11 @@ export const createEvalScope = () => {
return {
evalScope,
- oneTimeEvalProperties,
+ allowNextEvalToBeUnsafe: () => {
+ // Allow next reference to eval produce the unsafe FERAL_EVAL.
+ // We avoid defineProperty because it consumes an extra stack frame taming
+ // its return value.
+ defineProperties(evalScope, oneTimeEvalProperties);
+ },
};
};

MODIFY
packages/ses/src/make-safe-evaluator.js
packages/ses/src/make-safe-evaluator.js
@@ -39,7 +39,7 @@ export const makeSafeEvaluator = ({
const scopeTerminator = sloppyGlobalsMode
? createSloppyGlobalsScopeTerminator(globalObject)
: strictScopeTerminator;
- const { evalScope, oneTimeEvalProperties } = createEvalScope();
+ const { evalScope, allowNextEvalToBeUnsafe } = createEvalScope();
// Defer creating the actual evaluator to first use.
// Creating a compartment should be possible in no-eval environments
@@ -87,9 +87,7 @@ export const makeSafeEvaluator = ({
]);
// Allow next reference to eval produce the unsafe FERAL_EVAL.
- // We avoid defineProperty because it consumes an extra stack frame taming
- // its return value.
- defineProperties(evalScope, oneTimeEvalProperties);
+ allowNextEvalToBeUnsafe();
let err;
try {

MODIFY
packages/ses/test/test-eval-scope.js
packages/ses/test/test-eval-scope.js
@@ -10,11 +10,11 @@ const FERAL_EVAL = eval;
test('evalScope - is not created with eval exposed', t => {
t.plan(2);
- const { evalScope, oneTimeEvalProperties } = createEvalScope();
+ const { evalScope, allowNextEvalToBeUnsafe } = createEvalScope();
t.is(Reflect.has(evalScope, 'eval'), false);
- Object.defineProperties(evalScope, oneTimeEvalProperties);
+ allowNextEvalToBeUnsafe();
t.is(Reflect.has(evalScope, 'eval'), true);
});
@@ -22,11 +22,11 @@ test('evalScope - is not created with eval exposed', t => {
test('evalScope - getting eval removes it from evalScope', t => {
t.plan(5);
- const { evalScope, oneTimeEvalProperties } = createEvalScope();
+ const { evalScope, allowNextEvalToBeUnsafe } = createEvalScope();
t.is(Reflect.has(evalScope, 'eval'), false);
- Object.defineProperties(evalScope, oneTimeEvalProperties);
+ allowNextEvalToBeUnsafe();
t.is(Reflect.has(evalScope, 'eval'), true);

refactor(ses): Colocate logic of exposing FERAL_EVAL inside eval-scope module