Skip to content

Commit

Permalink
esm: improve fetch_module test coverage and remove hack
Browse files Browse the repository at this point in the history
PR-URL: #41947
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
  • Loading branch information
aduh95 authored and bengl committed Feb 22, 2022
1 parent d900891 commit c0f4289
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
9 changes: 1 addition & 8 deletions lib/internal/modules/esm/fetch_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const {
} = require('internal/errors').codes;
const { URL } = require('internal/url');
const net = require('net');
const path = require('path');

/**
* @typedef CacheEntry
Expand Down Expand Up @@ -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).'
);
}
Expand Down
48 changes: 48 additions & 0 deletions test/es-module/test-http-imports-cli.mjs
Original file line number Diff line number Diff line change
@@ -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]/);
}));
}
11 changes: 6 additions & 5 deletions test/es-module/test-http-imports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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', `
Expand All @@ -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);
Expand All @@ -154,15 +155,15 @@ 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);
unsupportedMIME.searchParams.set('mime', 'application/node');
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();
Expand Down

0 comments on commit c0f4289

Please sign in to comment.