Skip to content

Commit ed8dd33

Browse files
JakobJingleheimertargos
authored andcommitted
esm: identify parent importing a url with invalid host
PR-URL: #49736 Backport-PR-URL: #50669 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent 73a7e00 commit ed8dd33

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

‎lib/internal/modules/esm/resolve.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,16 @@ function finalizeResolution(resolved, base, preserveSymlinks){
255255
resolved.pathname,'must not include encoded "/" or "\\" characters',
256256
fileURLToPath(base));
257257

258-
letpath=fileURLToPath(resolved);
258+
letpath;
259+
try{
260+
path=fileURLToPath(resolved);
261+
}catch(err){
262+
const{ setOwnProperty }=require('internal/util');
263+
setOwnProperty(err,'input',`${resolved}`);
264+
setOwnProperty(err,'module',`${base}`);
265+
throwerr;
266+
}
267+
259268
if(getOptionValue('--experimental-specifier-resolution')==='node'){
260269
letfile=resolveExtensionsWithTryExactName(resolved);
261270

‎test/es-module/test-esm-loader-default-resolver.mjs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ describe('default resolver', () =>{
4949
assert.strictEqual(stdout.trim(),'index.byoe!');
5050
assert.strictEqual(stderr,'');
5151
});
52+
53+
it('should identify the parent module of an invalid URL host in import specifier',async()=>{
54+
if(process.platform==='win32')return;
55+
56+
const{ code, stderr }=awaitspawnPromisified(execPath,[
57+
'--no-warnings',
58+
fixtures.path('es-modules','invalid-posix-host.mjs'),
59+
]);
60+
61+
assert.match(stderr,/ERR_INVALID_FILE_URL_HOST/);
62+
assert.match(stderr,/file:\/\/hmm\.js/);
63+
assert.match(stderr,/invalid-posix-host\.mjs/);
64+
assert.strictEqual(code,1);
65+
});
5266
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import"file://hmm.js";

0 commit comments

Comments
(0)