CVE-2023-38704: Insufficient Information (NVD-CWE-noinfo)
value: import-in-the-middle is a module loading interceptor specifically for ESM modules. The import-in-the-middle loader works by generating a wrapper module on the fly. The wrapper uses the module specifier to load the original module and add some wrapping code. Prior to version 1.4.2, it allows for remote code execution in cases where an application passes user-supplied input directly to the `import()` function. This vulnerability has been patched in import-in-the-middle version 1.4.2.

Some workarounds are available. Do not pass any user-supplied input to `import()`. Instead, verify it against a set of allowed values. If using import-in-the-middle, directly or indirectly, and support for EcmaScript Modules is not needed, ensure that no options are set, either via command-line or the `NODE_OPTIONS` environment variable, that would enable loader hooks.
CWE description: There is insufficient information about the issue to classify it; details are unkown or unspecified.
CVSS v3 base score: 9.8
Published: 2023-08-07T20:15Z
Repository: https://github.com/DataDog/import-in-the-middle
Language: Other
Files changed: hook.js, test/low-level/sanitized-url.mjs
Commit date: 2023-08-03 22:22:32 -0400
Commit message: sanitize URLs

Diff: diff --git a/hook.js b/hook.js
index 884ee3a..3639fbf 100644
--- a/hook.js
+++ b/hook.js
@@ -120,21 +120,21 @@ function createHook (meta) {
       const realUrl = deleteIitm(url)
       const exportNames = await getExports(realUrl, context, parentGetSource)
       return {
         source: `
 import { register } from '${iitmURL}'
-import * as namespace from '${url}'
+import * as namespace from ${JSON.stringify(url)}
 const set = {}
 ${exportNames.map((n) => `
 let $${n} = namespace.${n}
 export { $${n} as ${n} }
 set.${n} = (v) => {
   $${n} = v
   return true
 }
 `).join('\n')}
-register('${realUrl}', namespace, set, '${specifiers.get(realUrl)}')
+register(${JSON.stringify(realUrl)}, namespace, set, ${JSON.stringify(specifiers.get(realUrl))})
 `
       }
     }
 
     return parentGetSource(url, context, parentGetSource)
diff --git a/test/low-level/sanitized-url.mjs b/test/low-level/sanitized-url.mjs
new file mode 100644
index 0000000..6fe5e81
--- /dev/null
+++ b/test/low-level/sanitized-url.mjs
@@ -0,0 +1,11 @@
+// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
+//
+// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
+
+import { addHook } from '../../index.js'
+
+addHook(() => {})
+
+;(async () => {
+  await import("../fixtures/something.mjs#*/'/*';eval('process.exit\x281\x29\x0A')")
+})()

Vulnerable code: import * as namespace from '${url}'
register('${realUrl}', namespace, set, '${specifiers.get(realUrl)}')
Fixed code: import * as namespace from ${JSON.stringify(url)}
register(${JSON.stringify(realUrl)}, namespace, set, ${JSON.stringify(specifiers.get(realUrl))})
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import { addHook } from '../../index.js'

addHook(() => {})

;(async () => {
  await import("../fixtures/something.mjs#*/'/*';eval('process.exit\x281\x29\x0A')")
})()