The vulnerability GHSA-9538-79j2-h62h (a duplicate of GHSA-f8gf-w286-fmq2) in vm2 versions prior to 3.11.8 allowed sandboxed code to bypass the allowAsync: false option and execute asynchronously, even when a timeout was configured. This was due to an incomplete enforcement of the asynchronous execution restriction.
While Promise.prototype.then was correctly patched to throw an AsyncErrorHandler, other mechanisms for scheduling asynchronous tasks via thenable assimilation remained open. Specifically, native Promise resolution, triggered by various Promise static methods and the Promise constructor itself, would perform PromiseResolveThenableJob and invoke an attacker-controlled then method in a microtask. This execution occurred after VM.run() or NodeVM.run() had returned, outside the configured timeout, effectively breaking the sandbox's synchronous boundary.
My analysis focused on the fix introduced in commit 1d1aa437a5a39b00b3de0deb3e31f751d4884f5f (part of the v3.11.8 release). This commit modifies lib/setup-sandbox.js to address these bypasses when allowAsync: false is enabled. The patch introduces several key changes:
- Guarding the
localPromise constructor: The resolve argument of the localPromise constructor's executor is now wrapped with makeAsyncGuardedResolve. This function checks if the value being resolved is an object or function (a thenable) and, if so, throws an AsyncErrorHandler. This prevents thenable assimilation through new Promise(r => r(thenable)) and other internal promise capability creations.
- Replacing Promise static methods: The static methods
localPromise.resolve, localPromise.all, localPromise.race, localPromise.any, localPromise.allSettled, and localPromise.try are explicitly replaced with AsyncErrorHandler. This ensures that any attempt to use these methods with thenables when allowAsync: false will synchronously throw an error.