Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix eval(<non‑string>) and eval.length #306

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/safeEval.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ function buildSafeEval(unsafeRec, safeEvalOperation) {

const { defineProperties } = Object;

// We use the the concise method syntax to create an eval without a
// [[Construct]] behavior (such that the invocation "new eval()" throws
// TypeError: eval is not a constructor"), but which still accepts a
// 'this' binding.
const safeEval = {
eval() {
return callAndWrapError(safeEvalOperation, arguments);
}
}.eval;
// We use the the arrow function syntax to create an eval without
// a [[Construct]] internal method (such that the invocation "new eval()"
// throws "TypeError: eval is not a constructor"), since the 'this' binding
// is ignored. This also prevents rejection by the overly eager
// rejectDangerousSources.
const safeEval = x =>
// https://tc39.es/ecma262/#sec-performeval
// 2. If Type(x) is not String, return x.
typeof x !== 'string' ? x : callAndWrapError(safeEvalOperation, [x]);

// safeEval's prototype RootRealm's value and instanceof Function
// is true inside the realm. It doesn't point at the primal realm
// value, and there is no defense against leaking primal realm
// intrinsics.

defineProperties(safeEval, {
// `eval.length` is already initialized correctly.
// Ensure that `eval.name` is also correct:
name: { value: 'eval' },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth making explicit that this property is non-writable, non-enumerable, and non-configurable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property has the defaults of { writeable: false, enumerable: false, configurable: true }, as it’s defined by ECMAScript:

toString: {
// We break up the following literal string so that an
// apparent direct eval syntax does not appear in this
Expand Down
30 changes: 29 additions & 1 deletion test/realm/function-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ test('degenerate-pattern-match-argument', t => {
t.end();
});

test('eval-own-properties', t => {
const r = Realm.makeRootRealm();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn’t exist any more. This might be a hard rebase.


const evalFunc = r.global.eval;
t.equal(evalFunc.name, 'eval', 'eval.name === "eval"');
t.equal(evalFunc.length, 1, 'eval.length === 1');
t.equal(
Object.getOwnPropertyDescriptor(evalFunc, 'prototype'),
undefined,
'eval.prototype === undefined'
);
t.equal(
evalFunc.toString(),
'function eval() { [shim code] }',
'eval.toString()'
);

t.end();
});

test('frozen-eval', t => {
const r = Realm.makeRootRealm();

Expand All @@ -174,7 +194,15 @@ test('frozen-eval', t => {
desc.configurable = false;
Object.defineProperty(r.global, 'eval', desc);

t.equal(r.evaluate('(0,eval)(1)'), 1);
t.equal(r.evaluate('(0,eval)("1")'), 1);
t.equal(
r.evaluate(`\
const sym = Symbol("foo");
(0,eval)(sym) === sym;
`),
true,
'eval(<non-string>) returns <non-string>'
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So much time has passed, we will probably want a test that verifies the immutability of the eval inside a Compartment.


t.end();
});