@@ -5,6 +5,8 @@ import { isBuiltin } from 'node:module';
55import * as path from 'node:path' ;
66import * as vm from 'node:vm' ;
77
8+ import { ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME } from '../../mongodb_all' ;
9+
810const allowedModules = new Set ( [
911 '@aws-sdk/credential-providers' ,
1012 '@mongodb-js/saslprep' ,
@@ -20,7 +22,7 @@ const allowedModules = new Set([
2022] ) ;
2123const blockedModules = new Set ( [ 'os' ] ) ;
2224
23- // TODO: NODE-7460 - Remove Error and other unnecessary exports
25+ // TODO: NODE-7460 - Remove Error, Map, Math, Promise, and other unnecessary exports
2426const exposedGlobals = new Set ( [
2527 'AbortController' ,
2628 'AbortSignal' ,
@@ -56,11 +58,12 @@ const exposedGlobals = new Set([
5658 */
5759function createRestrictedRequire ( ) {
5860 return function restrictedRequire ( moduleName : string ) {
61+ const isAllowedBySymbol = ! ! sandbox [ ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME ] ;
5962 const isModuleBuiltin = isBuiltin ( moduleName ) ;
6063 const isModuleAllowed = allowedModules . has ( moduleName ) ;
6164 const isModuleBlocked = blockedModules . has ( moduleName ) ;
6265 const shouldAllow = isModuleAllowed || isModuleBuiltin ;
63- const shouldBlock = isModuleBlocked || ! shouldAllow ;
66+ const shouldBlock = ( isModuleBlocked || ! shouldAllow ) && ! isAllowedBySymbol ;
6467
6568 if ( shouldBlock ) {
6669 throw new Error ( `Access to core module '${ moduleName } ' is restricted in this context` ) ;
@@ -75,9 +78,6 @@ const context = {
7578 // Custom require that blocks core modules
7679 require : createRestrictedRequire ( ) ,
7780
78- // Driver require
79- __driver_require : require ,
80-
8181 // Needed for some modules
8282 global : undefined as any ,
8383 globalThis : undefined as any
@@ -93,7 +93,7 @@ for (const globalName of exposedGlobals) {
9393// Create a sandbox context with necessary globals
9494const sandbox = vm . createContext ( context ) ;
9595
96- // Make global and globalThis point to the sandbox
96+ // Make globalThis point to the sandbox
9797sandbox . globalThis = sandbox ;
9898
9999/**
0 commit comments