From fd28b3eeb7b3470b74ccf8b8e4d7de8186e64f77 Mon Sep 17 00:00:00 2001 From: Sergey Chernyshev Date: Thu, 25 Apr 2024 02:40:27 +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 --- graal-nodejs/lib/internal/modules/esm/resolve.js | 11 ++++++++++- .../es-module/test-esm-loader-default-resolver.mjs | 14 ++++++++++++++ .../fixtures/es-modules/invalid-posix-host.mjs | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 graal-nodejs/test/fixtures/es-modules/invalid-posix-host.mjs diff --git a/graal-nodejs/lib/internal/modules/esm/resolve.js b/graal-nodejs/lib/internal/modules/esm/resolve.js index 2f7e4fca52b..7973865504b 100644 --- a/graal-nodejs/lib/internal/modules/esm/resolve.js +++ b/graal-nodejs/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/graal-nodejs/test/es-module/test-esm-loader-default-resolver.mjs b/graal-nodejs/test/es-module/test-esm-loader-default-resolver.mjs index 27320fcfcfe..2a69010e050 100644 --- a/graal-nodejs/test/es-module/test-esm-loader-default-resolver.mjs +++ b/graal-nodejs/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/graal-nodejs/test/fixtures/es-modules/invalid-posix-host.mjs b/graal-nodejs/test/fixtures/es-modules/invalid-posix-host.mjs new file mode 100644 index 00000000000..65ebb2c0496 --- /dev/null +++ b/graal-nodejs/test/fixtures/es-modules/invalid-posix-host.mjs @@ -0,0 +1 @@ +import "file://hmm.js";