Skip to content

Commit 711d724

Browse files
committed
pr feedback: use a different approach of allowing requires
1 parent 7c3c25a commit 711d724

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/runtime_adapters.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import type * as os from 'os';
88

99
import { type MongoClientOptions } from './mongo_client';
1010

11+
/**
12+
* @internal
13+
*
14+
* This propery can be set on the global object to allow the driver to require otherwise blocked modules.
15+
* This is used by our test suite to allow tests to access the `os` module without allowing user code to do so.
16+
*/
17+
export const ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME = 'allowedDriverRequire';
18+
1119
/**
1220
* @public
1321
* @experimental
@@ -43,9 +51,14 @@ export interface Runtime {
4351
* not provided by in `options`, and returns a `Runtime`.
4452
*/
4553
export function resolveRuntimeAdapters(options: MongoClientOptions): Runtime {
46-
const correctRequire = (globalThis as any).__driver_require || require;
47-
48-
return {
49-
os: options.runtimeAdapters?.os ?? correctRequire('os')
50-
};
54+
(globalThis as any)[ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME] = true;
55+
try {
56+
const runtime = {
57+
// eslint-disable-next-line @typescript-eslint/no-require-imports
58+
os: options.runtimeAdapters?.os ?? require('os')
59+
};
60+
return runtime;
61+
} finally {
62+
(globalThis as any)[ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME] = false;
63+
}
5164
}

test/tools/runner/vm_context_helper.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { isBuiltin } from 'node:module';
55
import * as path from 'node:path';
66
import * as vm from 'node:vm';
77

8+
import { ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME } from '../../mongodb_all';
9+
810
const allowedModules = new Set([
911
'@aws-sdk/credential-providers',
1012
'@mongodb-js/saslprep',
@@ -20,7 +22,7 @@ const allowedModules = new Set([
2022
]);
2123
const 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
2426
const exposedGlobals = new Set([
2527
'AbortController',
2628
'AbortSignal',
@@ -56,11 +58,12 @@ const exposedGlobals = new Set([
5658
*/
5759
function 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
9494
const sandbox = vm.createContext(context);
9595

96-
// Make global and globalThis point to the sandbox
96+
// Make globalThis point to the sandbox
9797
sandbox.globalThis = sandbox;
9898

9999
/**

0 commit comments

Comments
 (0)