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

lib: make primordials Promise static methods safe #38843

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
17 changes: 7 additions & 10 deletions lib/internal/inspector/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,15 @@ const {
ObjectKeys,
ObjectValues,
Promise,
PromiseAll,
PromisePrototypeCatch,
PromisePrototypeThen,
PromiseResolve,
ReflectGetOwnPropertyDescriptor,
ReflectOwnKeys,
RegExpPrototypeSymbolMatch,
RegExpPrototypeSymbolReplace,
SafeArrayIterator,
SafeMap,
SafePromiseAll,
String,
StringFromCharCode,
StringPrototypeEndsWith,
Expand Down Expand Up @@ -493,8 +492,8 @@ function createRepl(inspector) {
}

loadScopes() {
return PromiseAll(
new SafeArrayIterator(ArrayPrototypeMap(
return SafePromiseAll(
ArrayPrototypeMap(
ArrayPrototypeFilter(
this.scopeChain,
(scope) => scope.type !== 'global'
Expand All @@ -507,7 +506,6 @@ function createRepl(inspector) {
});
return new ScopeSnapshot(scope, result);
})
)
);
}

Expand Down Expand Up @@ -635,8 +633,8 @@ function createRepl(inspector) {
(error) => `<${error.message}>`);
const lastIndex = watchedExpressions.length - 1;

const values = await PromiseAll(new SafeArrayIterator(
ArrayPrototypeMap(watchedExpressions, inspectValue)));
const values = await SafePromiseAll(ArrayPrototypeMap(watchedExpressions,
inspectValue));
const lines = ArrayPrototypeMap(watchedExpressions, (expr, idx) => {
const prefix = `${leftPad(idx, ' ', lastIndex)}: ${expr} =`;
const value = inspect(values[idx], { colors: true });
Expand Down Expand Up @@ -839,7 +837,7 @@ function createRepl(inspector) {
location.lineNumber + 1));
if (!newBreakpoints.length) return PromiseResolve();
return PromisePrototypeThen(
PromiseAll(new SafeArrayIterator(newBreakpoints)),
SafePromiseAll(newBreakpoints),
(results) => {
print(`${results.length} breakpoints restored.`);
});
Expand Down Expand Up @@ -875,8 +873,7 @@ function createRepl(inspector) {

inspector.suspendReplWhile(() =>
PromisePrototypeThen(
PromiseAll(new SafeArrayIterator(
[formatWatchers(true), selectedFrame.list(2)])),
SafePromiseAll([formatWatchers(true), selectedFrame.list(2)]),
({ 0: watcherList, 1: context }) => {
const breakContext = watcherList ?
`${watcherList}\n${inspect(context)}` :
Expand Down
11 changes: 5 additions & 6 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ const {
ArrayPrototypeSome,
FunctionPrototype,
ObjectSetPrototypeOf,
PromiseAll,
PromiseResolve,
PromisePrototypeCatch,
ReflectApply,
RegExpPrototypeTest,
SafeArrayIterator,
SafePromiseAll,
SafeSet,
StringPrototypeIncludes,
StringPrototypeMatch,
Expand Down Expand Up @@ -77,9 +76,9 @@ class ModuleJob {
});

if (promises !== undefined)
await PromiseAll(new SafeArrayIterator(promises));
await SafePromiseAll(promises);

return PromiseAll(new SafeArrayIterator(dependencyJobs));
return SafePromiseAll(dependencyJobs);
};
// Promise for the list of all dependencyJobs.
this.linked = link();
Expand Down Expand Up @@ -107,8 +106,8 @@ class ModuleJob {
}
jobsInGraph.add(moduleJob);
const dependencyJobs = await moduleJob.linked;
return PromiseAll(new SafeArrayIterator(
ArrayPrototypeMap(dependencyJobs, addJobsToDependencyGraph)));
return SafePromiseAll(
ArrayPrototypeMap(dependencyJobs, addJobsToDependencyGraph));
};
await addJobsToDependencyGraph(this);

Expand Down
55 changes: 55 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ function copyPrototype(src, dest, prefix) {

const {
ArrayPrototypeForEach,
ArrayPrototypeMap,
FinalizationRegistry,
FunctionPrototypeCall,
Map,
Expand Down Expand Up @@ -415,5 +416,59 @@ primordials.SafePromisePrototypeFinally = (thisPromise, onFinally) =>
.then(a, b)
);

const arrayToSafePromiseIterable = (promises) =>
new primordials.SafeArrayIterator(
ArrayPrototypeMap(
promises,
(promise) =>
new SafePromise((a, b) => PromisePrototypeThen(promise, a, b))
)
);

/**
* @param {Promise<any>[]} promises
* @returns {Promise<any[]>}
*/
primordials.SafePromiseAll = (promises) =>
// Wrapping on a new Promise is necessary to not expose the SafePromise
// prototype to user-land.
new Promise((a, b) =>
SafePromise.all(arrayToSafePromiseIterable(promises)).then(a, b)
);

/**
* @param {Promise<any>[]} promises
* @returns {Promise<object>}
*/
primordials.SafePromiseAllSettled = (promises) =>
// Wrapping on a new Promise is necessary to not expose the SafePromise
// prototype to user-land.
new Promise((a, b) =>
SafePromise.allSettled(arrayToSafePromiseIterable(promises)).then(a, b)
);

/**
* @param {Promise<any>[]} promises
* @returns {Promise<any>}
*/
primordials.SafePromiseAny = (promises) =>
// Wrapping on a new Promise is necessary to not expose the SafePromise
// prototype to user-land.
new Promise((a, b) =>
SafePromise.any(arrayToSafePromiseIterable(promises)).then(a, b)
);

/**
* @param {Promise<any>[]} promises
* @returns {Promise<any>}
*/
primordials.SafePromiseRace = (promises) =>
// Wrapping on a new Promise is necessary to not expose the SafePromise
// prototype to user-land.
new Promise((a, b) =>
SafePromise.race(arrayToSafePromiseIterable(promises)).then(a, b)
);


ObjectSetPrototypeOf(primordials, null);
ObjectFreeze(primordials);
4 changes: 2 additions & 2 deletions lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const {
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
PromiseAll,
ReflectApply,
SafePromiseAll,
SafeWeakMap,
Symbol,
SymbolToStringTag,
Expand Down Expand Up @@ -329,7 +329,7 @@ class SourceTextModule extends Module {

try {
if (promises !== undefined) {
await PromiseAll(promises);
await SafePromiseAll(promises);
}
} catch (e) {
this.#error = e;
Expand Down
4 changes: 2 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ const {
ObjectKeys,
ObjectSetPrototypeOf,
Promise,
PromiseRace,
ReflectApply,
RegExp,
RegExpPrototypeExec,
RegExpPrototypeTest,
SafePromiseRace,
SafeSet,
SafeWeakSet,
StringPrototypeCharAt,
Expand Down Expand Up @@ -561,7 +561,7 @@ function REPLServer(prompt,
};
prioritizedSigintQueue.add(sigintListener);
});
promise = PromiseRace([promise, interrupt]);
promise = SafePromiseRace([promise, interrupt]);
}

(async () => {
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-primordials-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@ const assert = require('assert');
const {
PromisePrototypeCatch,
PromisePrototypeThen,
SafePromiseAll,
SafePromiseAllSettled,
SafePromiseAny,
SafePromisePrototypeFinally,
SafePromiseRace,
} = require('internal/test/binding').primordials;

Array.prototype[Symbol.iterator] = common.mustNotCall();
Promise.all = common.mustNotCall();
Promise.allSettled = common.mustNotCall();
Promise.any = common.mustNotCall();
Promise.race = common.mustNotCall();
Promise.prototype.catch = common.mustNotCall();
Promise.prototype.finally = common.mustNotCall();
Promise.prototype.then = common.mustNotCall();
Expand All @@ -18,6 +27,11 @@ assertIsPromise(PromisePrototypeCatch(Promise.reject(), common.mustCall()));
assertIsPromise(PromisePrototypeThen(test(), common.mustCall()));
assertIsPromise(SafePromisePrototypeFinally(test(), common.mustCall()));

assertIsPromise(SafePromiseAll([test()]));
assertIsPromise(SafePromiseAllSettled([test()]));
assertIsPromise(SafePromiseAny([test()]));
assertIsPromise(SafePromiseRace([test()]));

async function test() {
const catchFn = common.mustCall();
const finallyFn = common.mustCall();
Expand Down