From ae2ae2fef119dc9c9f594f628b5efb38766cfd31 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 11 Nov 2024 17:29:16 +0100 Subject: [PATCH] esm: fix import.meta.resolve crash PR-URL: https://github.com/nodejs/node/pull/55777 Fixes: https://github.com/nodejs/node/issues/55518 Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel --- src/node_modules.cc | 5 ++++- test/es-module/test-esm-import-meta-resolve.mjs | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/node_modules.cc b/src/node_modules.cc index 16a9f923148835..94ed9bc4b3c157 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -412,7 +412,10 @@ void BindingData::GetPackageScopeConfig( } auto file_url = url::FileURLToPath(realm->env(), *package_json_url); - CHECK(file_url); + if (!file_url) { + url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt); + return; + } error_context.specifier = resolved.ToString(); auto package_json = GetPackageJSON(realm, *file_url, &error_context); if (package_json != nullptr) { diff --git a/test/es-module/test-esm-import-meta-resolve.mjs b/test/es-module/test-esm-import-meta-resolve.mjs index 49b6d1ff906ec3..504d8968995bf1 100644 --- a/test/es-module/test-esm-import-meta-resolve.mjs +++ b/test/es-module/test-esm-import-meta-resolve.mjs @@ -106,3 +106,15 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res stdout: 'http://example.com/\n', }); } + +{ + const result = await spawnPromisified(execPath, [ + '--no-warnings', + '--experimental-import-meta-resolve', + '--eval', + 'import.meta.resolve("foo", "http://example.com/bar.js")', + ]); + assert.match(result.stderr, /ERR_INVALID_URL/); + assert.strictEqual(result.stdout, ''); + assert.strictEqual(result.code, 1); +}