@@ -7,7 +7,7 @@ import { readFileSync } from 'node:fs';
77import { dirname , isAbsolute } from 'node:path' ;
88import { pathToFileURL , fileURLToPath } from 'node:url' ;
99import { SourceTextModule , SyntheticModule , createContext , runInContext } from 'node:vm' ;
10- import { Scope } from '../components/Scope .ts' ;
10+ import { ApplicationScope } from '../components/ApplicationScope .ts' ;
1111import logger from '../utility/logging/harper_logger.js' ;
1212import { createRequire } from 'node:module' ;
1313import * as env from '../utility/environment/environmentManager' ;
@@ -25,7 +25,7 @@ let lockedDown = false;
2525 * @param moduleUrl
2626 * @param scope
2727 */
28- export async function scopedImport ( filePath : string | URL , scope ?: Scope ) {
28+ export async function scopedImport ( filePath : string | URL , scope ?: ApplicationScope ) {
2929 if ( ! lockedDown && APPLICATIONS_LOCKDOWN && APPLICATIONS_LOCKDOWN !== 'none' ) {
3030 lockedDown = true ;
3131 if ( APPLICATIONS_LOCKDOWN === 'ses' ) {
@@ -76,7 +76,7 @@ export async function scopedImport(filePath: string | URL, scope?: Scope) {
7676 }
7777 const moduleUrl = ( filePath instanceof URL ? filePath : pathToFileURL ( filePath ) ) . toString ( ) ;
7878 try {
79- const containmentMode = scope ?. applicationContainment . mode ;
79+ const containmentMode = scope ?. mode ;
8080 if ( scope && containmentMode !== 'none' ) {
8181 if ( containmentMode === 'compartment' ) {
8282 // use SES Compartments
@@ -110,7 +110,7 @@ export async function scopedImport(filePath: string | URL, scope?: Scope) {
110110/**
111111 * Load a module using Node's vm.Module API with (not really secure) sandboxing
112112 */
113- async function loadModuleWithVM ( moduleUrl : string , scope : Scope ) {
113+ async function loadModuleWithVM ( moduleUrl : string , scope : ApplicationScope ) {
114114 const moduleCache = new Map < string , Promise < SourceTextModule | SyntheticModule > > ( ) ;
115115
116116 // Create a secure context with limited globals
@@ -167,7 +167,7 @@ async function loadModuleWithVM(moduleUrl: string, scope: Scope) {
167167 filename : url ,
168168 async importModuleDynamically ( specifier : string , script ) {
169169 const resolvedUrl = resolveModule ( specifier , script . sourceURL ) ;
170- const useContainment = specifier . startsWith ( '.' ) || scope . applicationContainment . dependencyContainment ;
170+ const useContainment = specifier . startsWith ( '.' ) || scope . dependencyContainment ;
171171 const dynamicModule = await loadModuleWithCache ( resolvedUrl , useContainment ) ;
172172 return dynamicModule ;
173173 } ,
@@ -213,7 +213,7 @@ async function loadModuleWithVM(moduleUrl: string, scope: Scope) {
213213 return moduleCache . get ( resolvedUrl ) ! ;
214214 }
215215
216- const useContainment = specifier . startsWith ( '.' ) || scope . applicationContainment . dependencyContainment ;
216+ const useContainment = specifier . startsWith ( '.' ) || scope . dependencyContainment ;
217217 // Load the module
218218 return await loadModuleWithCache ( resolvedUrl , useContainment ) ;
219219 }
@@ -246,7 +246,7 @@ async function loadModuleWithVM(moduleUrl: string, scope: Scope) {
246246 { identifier : url , context }
247247 ) ;
248248 } else if ( usePrivateGlobal && url . startsWith ( 'file://' ) ) {
249- checkAllowedModulePath ( url , scope . applicationContainment . verifyPath ) ;
249+ checkAllowedModulePath ( url , scope . verifyPath ) ;
250250 // Load source text from file
251251 const source = await readFile ( new URL ( url ) , { encoding : 'utf-8' } ) ;
252252
@@ -286,7 +286,7 @@ async function loadModuleWithVM(moduleUrl: string, scope: Scope) {
286286 }
287287 }
288288 } else {
289- checkAllowedModulePath ( url , scope . applicationContainment . verifyPath ) ;
289+ checkAllowedModulePath ( url , scope . verifyPath ) ;
290290 // For Node.js built-in modules (node:) and npm packages, use dynamic import
291291 const importedModule = await import ( url ) ;
292292 const exportNames = Object . keys ( importedModule ) ;
@@ -318,7 +318,7 @@ async function loadModuleWithVM(moduleUrl: string, scope: Scope) {
318318 return entryModule . namespace ;
319319}
320320
321- async function getCompartment ( scope : Scope , globals ) {
321+ async function getCompartment ( scope : ApplicationScope , globals ) {
322322 const { StaticModuleRecord } = await import ( '@endo/static-module-record' ) ;
323323 require ( 'ses' ) ;
324324 const compartment : CompartmentOptions = new ( Compartment as typeof CompartmentOptions ) (
@@ -351,7 +351,7 @@ async function getCompartment(scope: Scope, globals) {
351351 const moduleText = await readFile ( new URL ( moduleSpecifier ) , { encoding : 'utf-8' } ) ;
352352 return new StaticModuleRecord ( moduleText , moduleSpecifier ) ;
353353 } else {
354- checkAllowedModulePath ( moduleSpecifier , scope . applicationContainment . verifyPath ) ;
354+ checkAllowedModulePath ( moduleSpecifier , scope . verifyPath ) ;
355355 const moduleExports = await import ( moduleSpecifier ) ;
356356 return {
357357 imports : [ ] ,
@@ -398,7 +398,7 @@ function getDefaultJSGlobalNames() {
398398/**
399399 * Get the set of global variables that should be available to modules that run in scoped compartments/contexts.
400400 */
401- function getGlobalObject ( scope : Scope ) {
401+ function getGlobalObject ( scope : ApplicationScope ) {
402402 const appGlobal = { } ;
403403 // create the new global object, assigning all the global variables from this global
404404 // except those that will be natural intrinsics of the new VM
@@ -411,20 +411,20 @@ function getGlobalObject(scope: Scope) {
411411 server : scope . server ?? server ,
412412 logger : scope . logger ?? logger ,
413413 resources : scope . resources ,
414- config : scope . options . getRoot ( ) ?? { } ,
414+ config : scope . config ?? { } ,
415415 fetch : APPLICATIONS_LOCKDOWN === 'ses' ? secureOnlyFetch : fetch ,
416416 console,
417417 global : appGlobal ,
418418 harper : getHarperExports ( scope ) ,
419419 } ) ;
420420 return appGlobal ;
421421}
422- function getHarperExports ( scope : Scope ) {
422+ function getHarperExports ( scope : ApplicationScope ) {
423423 return {
424424 server : scope . server ?? server ,
425425 logger : scope . logger ?? logger ,
426426 resources : scope . resources ,
427- config : scope . options . getRoot ( ) ?? { } ,
427+ config : scope . config ?? { } ,
428428 Resource,
429429 tables,
430430 databases,
@@ -465,10 +465,10 @@ const ALLOWED_NODE_BUILTIN_MODULES = new Set([
465465function checkAllowedModulePath ( moduleUrl : string , containingFolder : string ) : boolean {
466466 if ( moduleUrl . startsWith ( 'file:' ) ) {
467467 const path = moduleUrl . slice ( 7 ) ;
468- if ( path . startsWith ( containingFolder ) ) {
468+ if ( ! containingFolder || path . startsWith ( containingFolder ) ) {
469469 return true ;
470470 }
471- throw new Error ( `Can not load module outside of application folder` ) ;
471+ throw new Error ( `Can not load module outside of application folder ${ containingFolder } ` ) ;
472472 }
473473 let simpleName = moduleUrl . startsWith ( 'node:' ) ? moduleUrl . slice ( 5 ) : moduleUrl ;
474474 simpleName = simpleName . split ( '/' ) [ 0 ] ;
0 commit comments