Skip to content

Commit

Permalink
fix(runtime/js/timers): Use (0, eval) instead of eval() (denoland#10103)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Apr 11, 2021
1 parent 06b5959 commit 8b49d94
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions cli/tests/unit/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
unreachable,
} from "../../../test_util/std/testing/asserts.ts";
export { deferred } from "../../../test_util/std/async/deferred.ts";
export type { Deferred } from "../../../test_util/std/async/deferred.ts";
export { readLines } from "../../../test_util/std/io/bufio.ts";
export { parse as parseArgs } from "../../../test_util/std/flags/mod.ts";

Expand Down
22 changes: 22 additions & 0 deletions cli/tests/unit/timers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
assert,
assertEquals,
assertNotEquals,
Deferred,
deferred,
unitTest,
} from "./test_util.ts";
Expand Down Expand Up @@ -64,6 +65,27 @@ unitTest(async function timeoutSuccess(): Promise<void> {
assertEquals(count, 1);
});

unitTest(async function timeoutEvalNoScopeLeak(): Promise<void> {
// eval can only access global scope
const global = globalThis as unknown as {
globalPromise: Deferred<Error>;
};
global.globalPromise = deferred();
setTimeout(
`
try {
console.log(core);
globalThis.globalPromise.reject(new Error("Didn't throw."));
} catch (error) {
globalThis.globalPromise.resolve(error);
}` as unknown as () => void,
0,
);
const error = await global.globalPromise;
assertEquals(error.name, "ReferenceError");
Reflect.deleteProperty(global, "globalPromise");
});

unitTest(async function timeoutArgs(): Promise<void> {
const promise = deferred();
const arg = 1;
Expand Down
2 changes: 1 addition & 1 deletion runtime/js/11_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@
if ("function" === typeof callback) {
callback();
} else {
eval(callback);
(0, eval)(callback);
}
}

Expand Down

0 comments on commit 8b49d94

Please sign in to comment.