From ed8dd33493152d95bc7a379a3a3aea8ae368e13e Mon Sep 17 00:00:00 2001 From: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> Date: Sat, 23 Sep 2023 07:41:26 +0200 Subject: [PATCH] esm: identify parent importing a url with invalid host PR-URL: https://github.com/nodejs/node/pull/49736 Backport-PR-URL: https://github.com/nodejs/node/pull/50669 Reviewed-By: Antoine du Hamel Reviewed-By: Geoffrey Booth --- lib/internal/modules/esm/resolve.js | 11 ++++++++++- .../es-module/test-esm-loader-default-resolver.mjs | 14 ++++++++++++++ test/fixtures/es-modules/invalid-posix-host.mjs | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/es-modules/invalid-posix-host.mjs diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 2f7e4fca52bcf6..7973865504b853 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -255,7 +255,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) { resolved.pathname, 'must not include encoded "/" or "\\" characters', fileURLToPath(base)); - let path = fileURLToPath(resolved); + let path; + try { + path = fileURLToPath(resolved); + } catch (err) { + const { setOwnProperty } = require('internal/util'); + setOwnProperty(err, 'input', `${resolved}`); + setOwnProperty(err, 'module', `${base}`); + throw err; + } + if (getOptionValue('--experimental-specifier-resolution') === 'node') { let file = resolveExtensionsWithTryExactName(resolved); diff --git a/test/es-module/test-esm-loader-default-resolver.mjs b/test/es-module/test-esm-loader-default-resolver.mjs index 27320fcfcfe862..2a69010e05047f 100644 --- a/test/es-module/test-esm-loader-default-resolver.mjs +++ b/test/es-module/test-esm-loader-default-resolver.mjs @@ -49,4 +49,18 @@ describe('default resolver', () => { assert.strictEqual(stdout.trim(), 'index.byoe!'); assert.strictEqual(stderr, ''); }); + + it('should identify the parent module of an invalid URL host in import specifier', async () => { + if (process.platform === 'win32') return; + + const { code, stderr } = await spawnPromisified(execPath, [ + '--no-warnings', + fixtures.path('es-modules', 'invalid-posix-host.mjs'), + ]); + + assert.match(stderr, /ERR_INVALID_FILE_URL_HOST/); + assert.match(stderr, /file:\/\/hmm\.js/); + assert.match(stderr, /invalid-posix-host\.mjs/); + assert.strictEqual(code, 1); + }); }); diff --git a/test/fixtures/es-modules/invalid-posix-host.mjs b/test/fixtures/es-modules/invalid-posix-host.mjs new file mode 100644 index 00000000000000..65ebb2c0496c15 --- /dev/null +++ b/test/fixtures/es-modules/invalid-posix-host.mjs @@ -0,0 +1 @@ +import "file://hmm.js";