From 1324023fea0657746983a21254ec339c539e7991 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 19 Feb 2022 17:35:46 +0100 Subject: [PATCH] esm: improve `fetch_module` test coverage and remove hack PR-URL: https://github.com/nodejs/node/pull/41947 Reviewed-By: Geoffrey Booth --- lib/internal/modules/esm/fetch_module.js | 9 +---- test/es-module/test-http-imports-cli.mjs | 48 ++++++++++++++++++++++++ test/es-module/test-http-imports.mjs | 11 +++--- 3 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 test/es-module/test-http-imports-cli.mjs diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index 3a273554b6b7af..2cbc6ea72d0934 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -20,7 +20,6 @@ const { } = require('internal/errors').codes; const { URL } = require('internal/url'); const net = require('net'); -const path = require('path'); /** * @typedef CacheEntry @@ -263,15 +262,9 @@ function fetchModule(parsed, { parentURL }) { if (parsed.protocol === 'http:') { return PromisePrototypeThen(isLocalAddress(parsed.hostname), (is) => { if (is !== true) { - let parent = parentURL; - const parentName = path.basename(parent.pathname); - if ( - parentName === '[eval]' || - parentName === '[stdin]' - ) parent = 'command-line'; throw new ERR_NETWORK_IMPORT_DISALLOWED( href, - parent, + parentURL, 'http can only be used to load local resources (use https instead).' ); } diff --git a/test/es-module/test-http-imports-cli.mjs b/test/es-module/test-http-imports-cli.mjs new file mode 100644 index 00000000000000..67cefd69ddd889 --- /dev/null +++ b/test/es-module/test-http-imports-cli.mjs @@ -0,0 +1,48 @@ +import { mustCall } from '../common/index.mjs'; +import { match, notStrictEqual } from 'assert'; +import { spawn } from 'child_process'; +import { execPath } from 'process'; + +{ + const child = spawn(execPath, [ + '--experimental-network-imports', + '--input-type=module', + '-e', + 'import "http://example.com"', + ]); + + let stderr = ''; + child.stderr.setEncoding('utf8'); + child.stderr.on('data', (data) => { + stderr += data; + }); + child.on('close', mustCall((code, _signal) => { + notStrictEqual(code, 0); + + // [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by + // …/[eval1] is not supported: http can only be used to load local + // resources (use https instead). + match(stderr, /[ERR_NETWORK_IMPORT_DISALLOWED]/); + })); +} +{ + const child = spawn(execPath, [ + '--experimental-network-imports', + '--input-type=module', + ]); + child.stdin.end('import "http://example.com"'); + + let stderr = ''; + child.stderr.setEncoding('utf8'); + child.stderr.on('data', (data) => { + stderr += data; + }); + child.on('close', mustCall((code, _signal) => { + notStrictEqual(code, 0); + + // [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by + // …/[stdin] is not supported: http can only be used to load local + // resources (use https instead). + match(stderr, /[ERR_NETWORK_IMPORT_DISALLOWED]/); + })); +} diff --git a/test/es-module/test-http-imports.mjs b/test/es-module/test-http-imports.mjs index dfb05f3cdd12fd..a9a5204ffb7947 100644 --- a/test/es-module/test-http-imports.mjs +++ b/test/es-module/test-http-imports.mjs @@ -114,7 +114,7 @@ for (const { protocol, createServer } of [ })); await assert.rejects( import(crossProtocolRedirect.href), - 'should not be able to redirect across protocols' + { code: 'ERR_NETWORK_IMPORT_DISALLOWED' } ); const deps = new URL(url.href); @@ -135,7 +135,8 @@ for (const { protocol, createServer } of [ export default 1;`); await assert.rejects( import(fileDep.href), - 'should not be able to load file: from http:'); + { code: 'ERR_INVALID_URL_SCHEME' } + ); const builtinDep = new URL(url.href); builtinDep.searchParams.set('body', ` @@ -144,7 +145,7 @@ for (const { protocol, createServer } of [ `); await assert.rejects( import(builtinDep.href), - 'should not be able to load node: from http:' + { code: 'ERR_INVALID_URL_SCHEME' } ); const unprefixedBuiltinDep = new URL(url.href); @@ -154,7 +155,7 @@ for (const { protocol, createServer } of [ `); await assert.rejects( import(unprefixedBuiltinDep.href), - 'should not be able to load unprefixed builtins from http:' + { code: 'ERR_INVALID_URL_SCHEME' } ); const unsupportedMIME = new URL(url.href); @@ -162,7 +163,7 @@ for (const { protocol, createServer } of [ unsupportedMIME.searchParams.set('body', ''); await assert.rejects( import(unsupportedMIME.href), - 'should not be able to load unsupported MIMEs from http:' + { code: 'ERR_UNKNOWN_MODULE_FORMAT' } ); server.close();