-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
esm: Ensure custom loader resolved "url" is properly validated #21352
Changes from 7 commits
bc83382
cf1e32f
6594462
1776edf
888d186
233cf2a
d434368
a235f08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,13 @@ | |
|
||
const { | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_INVALID_PROTOCOL, | ||
ERR_INVALID_RETURN_PROPERTY, | ||
ERR_INVALID_RETURN_PROPERTY_VALUE, | ||
ERR_INVALID_RETURN_VALUE, | ||
ERR_MISSING_DYNAMIC_INTSTANTIATE_HOOK, | ||
ERR_UNKNOWN_MODULE_FORMAT | ||
} = require('internal/errors').codes; | ||
const { URL } = require('url'); | ||
const ModuleMap = require('internal/modules/esm/module_map'); | ||
const ModuleJob = require('internal/modules/esm/module_job'); | ||
const defaultResolve = require('internal/modules/esm/default_resolve'); | ||
|
@@ -52,20 +55,42 @@ class Loader { | |
if (!isMain && typeof parentURL !== 'string') | ||
throw new ERR_INVALID_ARG_TYPE('parentURL', 'string', parentURL); | ||
|
||
const { url, format } = | ||
await this._resolve(specifier, parentURL, defaultResolve); | ||
const resolved = await this._resolve(specifier, parentURL, defaultResolve); | ||
|
||
if (typeof resolved !== 'object') | ||
throw new ERR_INVALID_RETURN_VALUE( | ||
'object', 'loader resolve', resolved | ||
); | ||
|
||
const { url, format } = resolved; | ||
|
||
if (typeof url !== 'string') | ||
throw new ERR_INVALID_ARG_TYPE('url', 'string', url); | ||
throw new ERR_INVALID_RETURN_PROPERTY_VALUE( | ||
'string', 'loader resolve', 'url', url | ||
); | ||
|
||
if (typeof format !== 'string') | ||
throw new ERR_INVALID_ARG_TYPE('format', 'string', format); | ||
throw new ERR_INVALID_RETURN_PROPERTY( | ||
'module format', 'loader resolve', 'format', format | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error should likely be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first error checks that it is a string, the second error checks that it is the right type of string. The messages are:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fail to see the second case being related to this error. It is about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, I missed this was specifically the format case, but the same argument applied -
instead of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am still not certain why |
||
|
||
if (format === 'builtin') | ||
return { url: `node:${url}`, format }; | ||
|
||
if (this._resolve !== defaultResolve) { | ||
try { | ||
new URL(url); | ||
} catch (e) { | ||
throw new ERR_INVALID_RETURN_PROPERTY( | ||
'url', 'loader resolve', 'url', url | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm... I'm not sure what the benefit of the try/catch and additional throw here. If the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well caught, yes that's simpler. Updated. |
||
} | ||
|
||
if (format !== 'dynamic' && !url.startsWith('file:')) | ||
throw new ERR_INVALID_PROTOCOL(url, 'file:'); | ||
throw new ERR_INVALID_RETURN_PROPERTY( | ||
'file: url', 'loader resolve', 'url', url | ||
); | ||
|
||
return { url, format }; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,123 @@ | ||
// Flags: --experimental-modules | ||
/* eslint-disable node-core/required-modules */ | ||
import common from './index.js'; | ||
|
||
import assert from 'assert'; | ||
const { | ||
PORT, | ||
isMainThread, | ||
isWindows, | ||
isWOW64, | ||
isAIX, | ||
isLinuxPPCBE, | ||
isSunOS, | ||
isFreeBSD, | ||
isOpenBSD, | ||
isLinux, | ||
isOSX, | ||
isGlibc, | ||
enoughTestMem, | ||
enoughTestCpu, | ||
rootDir, | ||
buildType, | ||
localIPv6Hosts, | ||
opensslCli, | ||
PIPE, | ||
hasIPv6, | ||
childShouldThrowAndAbort, | ||
ddCommand, | ||
spawnPwd, | ||
spawnSyncPwd, | ||
platformTimeout, | ||
allowGlobals, | ||
leakedGlobals, | ||
mustCall, | ||
mustCallAtLeast, | ||
mustCallAsync, | ||
hasMultiLocalhost, | ||
fileExists, | ||
skipIfEslintMissing, | ||
canCreateSymLink, | ||
getCallSite, | ||
mustNotCall, | ||
printSkipMessage, | ||
skip, | ||
ArrayStream, | ||
nodeProcessAborted, | ||
busyLoop, | ||
isAlive, | ||
noWarnCode, | ||
expectWarning, | ||
expectsError, | ||
skipIfInspectorDisabled, | ||
skipIf32Bits, | ||
getArrayBufferViews, | ||
getBufferSources, | ||
crashOnUnhandledRejection, | ||
getTTYfd, | ||
runWithInvalidFD, | ||
hijackStdout, | ||
hijackStderr, | ||
restoreStdout, | ||
restoreStderr, | ||
isCPPSymbolsNotMapped | ||
} = common; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be sufficient to just export There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can only do this once we have named exports support for CommonJS modules. |
||
|
||
let knownGlobals = [ | ||
Buffer, | ||
clearImmediate, | ||
clearInterval, | ||
clearTimeout, | ||
global, | ||
process, | ||
setImmediate, | ||
setInterval, | ||
setTimeout | ||
]; | ||
|
||
if (process.env.NODE_TEST_KNOWN_GLOBALS) { | ||
const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(','); | ||
allowGlobals(...knownFromEnv); | ||
} | ||
|
||
export function allowGlobals(...whitelist) { | ||
knownGlobals = knownGlobals.concat(whitelist); | ||
} | ||
|
||
export function leakedGlobals() { | ||
// Add possible expected globals | ||
if (global.gc) { | ||
knownGlobals.push(global.gc); | ||
} | ||
|
||
if (global.DTRACE_HTTP_SERVER_RESPONSE) { | ||
knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); | ||
knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); | ||
knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); | ||
knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); | ||
knownGlobals.push(DTRACE_NET_STREAM_END); | ||
knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); | ||
} | ||
|
||
if (global.COUNTER_NET_SERVER_CONNECTION) { | ||
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); | ||
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE); | ||
knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST); | ||
knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE); | ||
knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST); | ||
knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE); | ||
} | ||
|
||
const leaked = []; | ||
|
||
for (const val in global) { | ||
if (!knownGlobals.includes(global[val])) { | ||
leaked.push(val); | ||
} | ||
} | ||
|
||
if (global.__coverage__) { | ||
return leaked.filter((varname) => !/^(?:cov_|__cov)/.test(varname)); | ||
} else { | ||
return leaked; | ||
} | ||
} | ||
|
||
process.on('exit', function() { | ||
const leaked = leakedGlobals(); | ||
if (leaked.length > 0) { | ||
assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`); | ||
} | ||
}); | ||
export { | ||
PORT, | ||
isMainThread, | ||
isWindows, | ||
isWOW64, | ||
isAIX, | ||
isLinuxPPCBE, | ||
isSunOS, | ||
isFreeBSD, | ||
isOpenBSD, | ||
isLinux, | ||
isOSX, | ||
isGlibc, | ||
enoughTestMem, | ||
enoughTestCpu, | ||
rootDir, | ||
buildType, | ||
localIPv6Hosts, | ||
opensslCli, | ||
PIPE, | ||
hasIPv6, | ||
childShouldThrowAndAbort, | ||
ddCommand, | ||
spawnPwd, | ||
spawnSyncPwd, | ||
platformTimeout, | ||
allowGlobals, | ||
leakedGlobals, | ||
mustCall, | ||
mustCallAtLeast, | ||
mustCallAsync, | ||
hasMultiLocalhost, | ||
fileExists, | ||
skipIfEslintMissing, | ||
canCreateSymLink, | ||
getCallSite, | ||
mustNotCall, | ||
printSkipMessage, | ||
skip, | ||
ArrayStream, | ||
nodeProcessAborted, | ||
busyLoop, | ||
isAlive, | ||
noWarnCode, | ||
expectWarning, | ||
expectsError, | ||
skipIfInspectorDisabled, | ||
skipIf32Bits, | ||
getArrayBufferViews, | ||
getBufferSources, | ||
crashOnUnhandledRejection, | ||
getTTYfd, | ||
runWithInvalidFD, | ||
hijackStdout, | ||
hijackStderr, | ||
restoreStdout, | ||
restoreStderr, | ||
isCPPSymbolsNotMapped | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-invalid-url.mjs | ||
import { expectsError, mustCall } from '../common'; | ||
import assert from 'assert'; | ||
|
||
import('../fixtures/es-modules/test-esm-ok.mjs') | ||
.then(assert.fail, expectsError({ | ||
code: 'ERR_INVALID_RETURN_PROPERTY', | ||
message: 'Expected a valid url to be returned for the "url" from the ' + | ||
'"loader resolve" function but got ' + | ||
'../fixtures/es-modules/test-esm-ok.mjs.' | ||
})) | ||
.then(mustCall()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export async function resolve(specifier, parentModuleURL, defaultResolve) { | ||
if (parentModuleURL && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { | ||
return { | ||
url: specifier, | ||
format: 'esm' | ||
}; | ||
} | ||
return defaultResolve(specifier, parentModuleURL); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name here might be better as:
ERR_INVALID_RETURN_PROPERTY_TYPE
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, the consistent name here would be
ERR_INVALID_RETURN_PROPERTY_VALUE
actually :)