Skip to content
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

module: disable package self resolution when name contains a colon #37290

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/api/packages.md
Expand Up @@ -613,7 +613,8 @@ Then any module _in that package_ can reference an export in the package itself:
import { something } from 'a-package'; // Imports "something" from ./main.mjs.
```

Self-referencing is available only if `package.json` has [`"exports"`][], and
Self-referencing is available only if `package.json` has a [`"name"`][] that
does not contain the character `:`, and a [`"exports"`][] field, and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
does not contain the character `:`, and a [`"exports"`][] field, and
does not contain the character `:`, and an [`"exports"`][] field, and

will allow importing only what that [`"exports"`][] (in the `package.json`)
allows. So the code below, given the previous package, will generate a runtime
error:
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/modules/cjs/loader.js
Expand Up @@ -54,6 +54,7 @@ const {
StringPrototypeCharCodeAt,
StringPrototypeEndsWith,
StringPrototypeLastIndexOf,
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeMatch,
StringPrototypeSlice,
Expand Down Expand Up @@ -444,6 +445,11 @@ function trySelf(parentPath, request) {
return false;
}

if (StringPrototypeIncludes(pkg.name, ':')) {
throw new ERR_INVALID_MODULE_SPECIFIER(
request, 'is not a valid package name', parentPath);
}

try {
return finalizeEsmResolution(packageExportsResolve(
pathToFileURL(pkgPath + '/package.json'), expansion, pkg,
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/self_ref_module_with_colon/bin.js
@@ -0,0 +1,11 @@
#!/usr/bin/env node

'use strict';

try {
console.log(require('file:package'));
} catch (e) {
console.log(e);
}
import('file:package').then(console.log, console.log);
import('file%3Apackage').then(console.log, console.log);
4 changes: 4 additions & 0 deletions test/fixtures/self_ref_module_with_colon/index.js
@@ -0,0 +1,4 @@
'use strict'

module.exports = 'Self resolution working';

4 changes: 4 additions & 0 deletions test/fixtures/self_ref_module_with_colon/package.json
@@ -0,0 +1,4 @@
{
"name": "file:package",
"exports": "./index.js"
}
11 changes: 11 additions & 0 deletions test/message/test-self-referential-package.js
@@ -0,0 +1,11 @@
'use strict';

require('../common');
const { spawn } = require('child_process');

const fixtures = require('../common/fixtures');
const selfRefModule = fixtures.path('self_ref_module_with_colon');

spawn(process.argv0, [`${selfRefModule}/bin.js`], {
stdio: 'inherit'
});
39 changes: 39 additions & 0 deletions test/message/test-self-referential-package.out
@@ -0,0 +1,39 @@
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "file:package" is not a valid package name imported from *
at new NodeError (node:internal/errors:*)
at trySelf (node:internal/modules/cjs/loader:*)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:*)
at Function.Module._load (node:internal/modules/cjs/loader:*)
at Module.require (node:internal/modules/cjs/loader:*)
at require (node:internal/modules/cjs/helpers:*)
at Object.<anonymous> (*)
at Module._compile (node:internal/modules/cjs/loader:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*)
at Module.load (node:internal/modules/cjs/loader:*) {
code: 'ERR_INVALID_MODULE_SPECIFIER'
}
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/package' imported from *
at new NodeError (node:internal/errors:*)
at finalizeResolution (node:internal/modules/esm/resolve:*)
at moduleResolve (node:internal/modules/esm/resolve:*)
at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:*)
at Loader.resolve (node:internal/modules/esm/loader:*)
at Loader.getModuleJob (node:internal/modules/esm/loader:*)
at Loader.import (node:internal/modules/esm/loader:*)
at importModuleDynamically (node:internal/modules/cjs/loader:*)
at importModuleDynamicallyWrapper (node:internal/vm/module:*)
at importModuleDynamically (node:vm:*) {
code: 'ERR_MODULE_NOT_FOUND'
}
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "file%%3Apackage" is not a valid package name imported from *
at new NodeError (node:internal/errors:*)
at parsePackageName (node:internal/modules/esm/resolve:*)
at packageResolve (node:internal/modules/esm/resolve:*)
at moduleResolve (node:internal/modules/esm/resolve:*)
at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:*)
at Loader.resolve (node:internal/modules/esm/loader:*)
at Loader.getModuleJob (node:internal/modules/esm/loader:*)
at Loader.import (node:internal/modules/esm/loader:*)
at importModuleDynamically (node:internal/modules/cjs/loader:*)
at importModuleDynamicallyWrapper (node:internal/vm/module:*) {
code: 'ERR_INVALID_MODULE_SPECIFIER'
}