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 global function get polluted close #52 #63

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions src/repair/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* a copy has been made, and funtions can only be created by syntax (using eval)
* or by invoking a previously saved reference to the originals.
*/

// todo: this file should be moved out to a separate repo and npm module.
const globalEval = eval;
export function repairFunctions() {
Expand Down Expand Up @@ -46,11 +45,21 @@ export function repairFunctions() {
throw e;
}
const FunctionPrototype = getPrototypeOf(FunctionInstance);
const oldFunctionConstructor = FunctionPrototype.constructor;

function isRunningInRealms() {
const e = new Error().stack;
if (!e) return true;
return e.indexOf('eval') !== -1;
}
// Prevents the evaluation of source when calling constructor on the
// prototype of functions.
const TamedFunction = function() {
throw new TypeError('Not available');
if (isRunningInRealms()) {
Copy link
Member

Choose a reason for hiding this comment

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

Switching anything semantically significant on the caller is hugely hazardous, and likely is a form of dynamic scoping, which we have taken pains to avoid in the rest of the language. Also, the stack property of Errors is not yet standard, and is likely to change.

Please explain what concrete compatibility problems you are running into, and what you need them to do. Let's see how we can accommodate those cases, or how you can on top of the realms-shim, with minimal damage to the realms-shim itself. Thanks.

throw new TypeError('Not available');
} else {
return oldFunctionConstructor.apply(this, arguments);
}
};
defineProperties(TamedFunction, { name: { value: name } });

Expand Down
101 changes: 91 additions & 10 deletions test/module/functions.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,70 @@
import test from 'tape';
import { repairFunctions } from '../../src/repair/functions';
import Realm from '../../src/realm';

test('repairFunctions', specs => {
repairFunctions();

specs.test('Function.prototype.constructor', t => {
t.plan(4);
t.plan(7);

// eslint-disable-next-line no-new-func
t.doesNotThrow(() => Function(''));

// eslint-disable-next-line no-proto
t.throws(() => Error.__proto__.constructor(''), TypeError);
t.throws(() => Function.prototype.constructor(''), TypeError);
t.doesNotThrow(() => Error.__proto__.constructor(''));
t.doesNotThrow(() => Function.prototype.constructor(''));

const proto = Object.getPrototypeOf((0, eval)('(function() {})'));
t.throws(() => proto.constructor(''), TypeError);
t.doesNotThrow(() => proto.constructor(''));

const realms = Realm.makeCompartment();

// eslint-disable-next-line no-proto
t.throws(
() => realms.evaluate("Error.__proto__.constructor('')"),
TypeError
);
t.throws(
() => realms.evaluate("Function.prototype.constructor('')"),
TypeError
);

t.throws(
() =>
realms.evaluate(`
const proto = Object.getPrototypeOf((0, eval)('(function() {})'));
proto.constructor('')`),
TypeError
);
});

specs.test('AsyncFunction.constructor', t => {
t.plan(1);
t.plan(2);

try {
const proto = Object.getPrototypeOf((0, eval)('(async function() {})'));
t.throws(() => proto.constructor(''), TypeError);
t.doesNotThrow(() => proto.constructor(''));
} catch (e) {
if (
e instanceof SyntaxError &&
e.message.startsWith('Unexpected token')
) {
t.pass('not supported');
} else {
throw e;
}
}

try {
const realms = Realm.makeCompartment();
t.throws(
() =>
realms.evaluate(`
const proto = Object.getPrototypeOf((0, eval)('(async function() {})'));
proto.constructor('')`),
TypeError
);
} catch (e) {
if (
e instanceof SyntaxError &&
Expand All @@ -37,11 +78,31 @@ test('repairFunctions', specs => {
});

specs.test('GeneratorFunction.constructor', t => {
t.plan(1);
t.plan(2);

try {
const proto = Object.getPrototypeOf((0, eval)('(function* () {})'));
t.throws(() => proto.constructor(''), TypeError);
t.doesNotThrow(() => proto.constructor(''));
} catch (e) {
if (
e instanceof SyntaxError &&
e.message.startsWith('Unexpected token')
) {
t.pass('not supported');
} else {
throw e;
}
}
try {
const realm = Realm.makeCompartment();
t.throws(() =>
realm.evaluate(
`
const proto = Object.getPrototypeOf((0, eval)('(function* () {})'));
proto.constructor('');`,
TypeError
)
);
} catch (e) {
if (
e instanceof SyntaxError &&
Expand All @@ -55,11 +116,31 @@ test('repairFunctions', specs => {
});

specs.test('AsyncGeneratorFunction.constructor', t => {
t.plan(1);
t.plan(2);

try {
const proto = Object.getPrototypeOf((0, eval)('(async function* () {})'));
t.throws(() => proto.constructor(''), TypeError);
t.doesNotThrow(() => proto.constructor(''));
} catch (e) {
if (
e instanceof SyntaxError &&
e.message.startsWith('Unexpected token')
) {
t.pass('not supported');
} else {
throw e;
}
}
try {
const realm = Realm.makeCompartment();
t.throws(
() =>
realm.evaluate(`
const proto = Object.getPrototypeOf((0, eval)('(async function* () {})'));
proto.constructor('');
`),
TypeError
);
} catch (e) {
if (
e instanceof SyntaxError &&
Expand Down